aayanb09 commited on
Commit
9dc3e41
·
verified ·
1 Parent(s): 2739820

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -8
app.py CHANGED
@@ -2,24 +2,71 @@ import torch
2
  import gradio as gr
3
  from PIL import Image
4
  import numpy as np
5
- from torchvision import transforms
6
-
7
- from model import FoodIngredientClassifier
8
- from utils import load_model_and_mlb
9
 
10
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
 
12
- # Load model + label binarizer
13
- model, mlb, threshold = load_model_and_mlb("model.pth", DEVICE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  model.eval()
15
 
 
 
 
 
 
16
  transform = transforms.Compose([
17
  transforms.Resize((224, 224)),
18
  transforms.ToTensor(),
19
- transforms.Normalize([0.485, 0.456, 0.406],
20
- [0.229, 0.224, 0.225])
 
 
21
  ])
22
 
 
 
 
23
  def predict(image):
24
  image = image.convert("RGB")
25
  img_tensor = transform(image).unsqueeze(0).to(DEVICE)
@@ -43,6 +90,10 @@ def predict(image):
43
 
44
  return {k: v for k, v in results}
45
 
 
 
 
 
46
  iface = gr.Interface(
47
  fn=predict,
48
  inputs=gr.Image(type="pil"),
 
2
  import gradio as gr
3
  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
 
10
+ # -------------------------
11
+ # MODEL DEFINITION
12
+ # -------------------------
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),
26
+ nn.BatchNorm1d(1024),
27
+ nn.ReLU(),
28
+ nn.Dropout(0.4),
29
+ nn.Linear(1024, 512),
30
+ nn.BatchNorm1d(512),
31
+ nn.ReLU(),
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"]
46
+ num_classes = len(mlb.classes_)
47
+
48
+ model = FoodIngredientClassifier(num_classes)
49
+ model.load_state_dict(checkpoint["model_state_dict"])
50
+ model.to(DEVICE)
51
  model.eval()
52
 
53
+ threshold = checkpoint.get("optimal_threshold", 0.5)
54
+
55
+ # -------------------------
56
+ # IMAGE TRANSFORM
57
+ # -------------------------
58
  transform = transforms.Compose([
59
  transforms.Resize((224, 224)),
60
  transforms.ToTensor(),
61
+ transforms.Normalize(
62
+ [0.485, 0.456, 0.406],
63
+ [0.229, 0.224, 0.225]
64
+ )
65
  ])
66
 
67
+ # -------------------------
68
+ # PREDICTION FUNCTION
69
+ # -------------------------
70
  def predict(image):
71
  image = image.convert("RGB")
72
  img_tensor = transform(image).unsqueeze(0).to(DEVICE)
 
90
 
91
  return {k: v for k, v in results}
92
 
93
+
94
+ # -------------------------
95
+ # GRADIO INTERFACE
96
+ # -------------------------
97
  iface = gr.Interface(
98
  fn=predict,
99
  inputs=gr.Image(type="pil"),