Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,19 @@ def predict(img):
|
|
| 24 |
try:
|
| 25 |
print("Starting prediction...", flush=True)
|
| 26 |
img = PILImage.create(img)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 29 |
except Exception as e:
|
| 30 |
tb = traceback.format_exc()
|
|
|
|
| 24 |
try:
|
| 25 |
print("Starting prediction...", flush=True)
|
| 26 |
img = PILImage.create(img)
|
| 27 |
+
|
| 28 |
+
fimg = PILImage.create(img)
|
| 29 |
+
x = Resize(224)(fimg) # -> fastai PILImage
|
| 30 |
+
x = ToTensor()(x) # -> float tensor [0,1], shape [3,224,224]
|
| 31 |
+
x = Normalize.from_stats(*imagenet_stats)(x)
|
| 32 |
+
x = x.unsqueeze(0) # -> [1,3,224,224]
|
| 33 |
+
|
| 34 |
+
# --- Forward pass ---
|
| 35 |
+
learn.model.eval()
|
| 36 |
+
logits = learn.model(x)
|
| 37 |
+
probs = torch.softmax(logits, dim=1)[0].cpu().numpy()
|
| 38 |
+
|
| 39 |
+
# pred, pred_idx, probs = learn.predict(img)
|
| 40 |
return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
| 41 |
except Exception as e:
|
| 42 |
tb = traceback.format_exc()
|