Upload inference.py
Browse files214 unmodified lines215216217214 unmodified linesreturn {str(i + 1): name for i, name in enumerate(self.class_names)}214 unmodified lines215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246214 unmodified linesreturn {str(i + 1): name for i, name in enumerate(self.class_names)}def get_tensor(self, crop: Image.Image):"""Preprocess a crop into a numpy array for batch inference."""if crop.mode != "RGB":crop = crop.convert("RGB")img_tensor = TF.pil_to_tensor(crop)img_tensor = TF.convert_image_dtype(img_tensor, torch.float32)img_tensor = TF.resize(img_tensor, [IMG_SIZE, IMG_SIZE], antialias=False)img_tensor = TF.convert_image_dtype(img_tensor, torch.uint8)return img_tensor.permute(1, 2, 0).numpy().astype("float32") / 255.0def classify_batch(self, batch):"""Run inference on a batch of preprocessed numpy arrays."""tensor = torch.from_numpy(batch).to(self.device)with torch.no_grad():logits = self.model(tensor)probs = F.softmax(logits, dim=1).cpu().numpy()results = []for p in probs:classifications = [[self.class_names[i], float(p[i])]for i in range(len(self.class_names))]results.append(classifications)return results
inference.py+29