Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from PIL import Image
|
|
| 4 |
import numpy as np
|
| 5 |
from torchvision import transforms, models
|
| 6 |
import torch.nn as nn
|
|
|
|
| 7 |
|
| 8 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
|
|
@@ -13,13 +14,13 @@ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
| 13 |
class FoodIngredientClassifier(nn.Module):
|
| 14 |
def __init__(self, num_classes):
|
| 15 |
super().__init__()
|
| 16 |
-
|
| 17 |
self.backbone = models.vit_b_16(
|
| 18 |
weights=models.ViT_B_16_Weights.DEFAULT
|
| 19 |
)
|
| 20 |
-
|
| 21 |
num_features = self.backbone.heads.head.in_features
|
| 22 |
-
|
| 23 |
self.backbone.heads = nn.Sequential(
|
| 24 |
nn.Dropout(0.5),
|
| 25 |
nn.Linear(num_features, 1024),
|
|
@@ -32,14 +33,16 @@ class FoodIngredientClassifier(nn.Module):
|
|
| 32 |
nn.Dropout(0.3),
|
| 33 |
nn.Linear(512, num_classes)
|
| 34 |
)
|
| 35 |
-
|
| 36 |
def forward(self, x):
|
| 37 |
return self.backbone(x)
|
| 38 |
|
| 39 |
-
|
| 40 |
# -------------------------
|
| 41 |
# LOAD CHECKPOINT
|
| 42 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
| 43 |
checkpoint = torch.load("model.pth", map_location=DEVICE)
|
| 44 |
|
| 45 |
mlb = checkpoint["mlb"]
|
|
@@ -70,27 +73,26 @@ transform = transforms.Compose([
|
|
| 70 |
def predict(image):
|
| 71 |
image = image.convert("RGB")
|
| 72 |
img_tensor = transform(image).unsqueeze(0).to(DEVICE)
|
| 73 |
-
|
| 74 |
with torch.no_grad():
|
| 75 |
output = model(img_tensor)
|
| 76 |
probs = torch.sigmoid(output).cpu().numpy()[0]
|
| 77 |
-
|
| 78 |
pred_indices = np.where(probs > threshold)[0]
|
| 79 |
ingredients = mlb.classes_[pred_indices]
|
| 80 |
confidences = probs[pred_indices]
|
| 81 |
-
|
| 82 |
results = sorted(
|
| 83 |
[(ing, float(conf)) for ing, conf in zip(ingredients, confidences)],
|
| 84 |
key=lambda x: x[1],
|
| 85 |
reverse=True
|
| 86 |
)
|
| 87 |
-
|
| 88 |
if not results:
|
| 89 |
return {"No ingredient detected": 1.0}
|
| 90 |
-
|
| 91 |
return {k: v for k, v in results}
|
| 92 |
|
| 93 |
-
|
| 94 |
# -------------------------
|
| 95 |
# GRADIO INTERFACE
|
| 96 |
# -------------------------
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from torchvision import transforms, models
|
| 6 |
import torch.nn as nn
|
| 7 |
+
from sklearn.preprocessing import MultiLabelBinarizer # Add this import
|
| 8 |
|
| 9 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
|
|
|
|
| 14 |
class FoodIngredientClassifier(nn.Module):
|
| 15 |
def __init__(self, num_classes):
|
| 16 |
super().__init__()
|
| 17 |
+
|
| 18 |
self.backbone = models.vit_b_16(
|
| 19 |
weights=models.ViT_B_16_Weights.DEFAULT
|
| 20 |
)
|
| 21 |
+
|
| 22 |
num_features = self.backbone.heads.head.in_features
|
| 23 |
+
|
| 24 |
self.backbone.heads = nn.Sequential(
|
| 25 |
nn.Dropout(0.5),
|
| 26 |
nn.Linear(num_features, 1024),
|
|
|
|
| 33 |
nn.Dropout(0.3),
|
| 34 |
nn.Linear(512, num_classes)
|
| 35 |
)
|
| 36 |
+
|
| 37 |
def forward(self, x):
|
| 38 |
return self.backbone(x)
|
| 39 |
|
|
|
|
| 40 |
# -------------------------
|
| 41 |
# LOAD CHECKPOINT
|
| 42 |
# -------------------------
|
| 43 |
+
# Add sklearn MultiLabelBinarizer to safe globals before loading
|
| 44 |
+
torch.serialization.add_safe_globals([MultiLabelBinarizer])
|
| 45 |
+
|
| 46 |
checkpoint = torch.load("model.pth", map_location=DEVICE)
|
| 47 |
|
| 48 |
mlb = checkpoint["mlb"]
|
|
|
|
| 73 |
def predict(image):
|
| 74 |
image = image.convert("RGB")
|
| 75 |
img_tensor = transform(image).unsqueeze(0).to(DEVICE)
|
| 76 |
+
|
| 77 |
with torch.no_grad():
|
| 78 |
output = model(img_tensor)
|
| 79 |
probs = torch.sigmoid(output).cpu().numpy()[0]
|
| 80 |
+
|
| 81 |
pred_indices = np.where(probs > threshold)[0]
|
| 82 |
ingredients = mlb.classes_[pred_indices]
|
| 83 |
confidences = probs[pred_indices]
|
| 84 |
+
|
| 85 |
results = sorted(
|
| 86 |
[(ing, float(conf)) for ing, conf in zip(ingredients, confidences)],
|
| 87 |
key=lambda x: x[1],
|
| 88 |
reverse=True
|
| 89 |
)
|
| 90 |
+
|
| 91 |
if not results:
|
| 92 |
return {"No ingredient detected": 1.0}
|
| 93 |
+
|
| 94 |
return {k: v for k, v in results}
|
| 95 |
|
|
|
|
| 96 |
# -------------------------
|
| 97 |
# GRADIO INTERFACE
|
| 98 |
# -------------------------
|