KabeerAmjad
commited on
Commit
•
db29817
1
Parent(s):
998a9ec
Update app.py
Browse files
app.py
CHANGED
@@ -3,20 +3,14 @@ import torch
|
|
3 |
from torch import nn
|
4 |
from torchvision import models, transforms
|
5 |
from PIL import Image
|
6 |
-
from transformers import AutoFeatureExtractor
|
7 |
|
8 |
-
# Load the
|
9 |
-
|
10 |
-
# Load ResNet50 model and adjust the final layer
|
11 |
-
model = models.resnet50(pretrained=True)
|
12 |
model.fc = nn.Linear(model.fc.in_features, 11) # Adjust the output layer to match your number of classes
|
13 |
|
14 |
-
# Load the
|
15 |
-
model.load_state_dict(torch.
|
16 |
-
model.eval()
|
17 |
-
|
18 |
-
# Load the feature extractor
|
19 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
20 |
|
21 |
# Define the same preprocessing used during training
|
22 |
transform = transforms.Compose([
|
@@ -36,7 +30,7 @@ def classify_image(img):
|
|
36 |
probs = torch.softmax(outputs, dim=-1)
|
37 |
|
38 |
# Get the label with the highest probability
|
39 |
-
top_label =
|
40 |
return top_label
|
41 |
|
42 |
# Create the Gradio interface
|
@@ -50,4 +44,3 @@ iface = gr.Interface(
|
|
50 |
|
51 |
# Launch the app
|
52 |
iface.launch()
|
53 |
-
|
|
|
3 |
from torch import nn
|
4 |
from torchvision import models, transforms
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
+
# Load the ResNet50 model
|
8 |
+
model = models.resnet50(pretrained=False) # Don't load pre-trained weights here
|
|
|
|
|
9 |
model.fc = nn.Linear(model.fc.in_features, 11) # Adjust the output layer to match your number of classes
|
10 |
|
11 |
+
# Load the saved model weights (food_classification_model.pth)
|
12 |
+
model.load_state_dict(torch.load('food_classification_model.pth')) # Load from the local file
|
13 |
+
model.eval() # Set the model to evaluation mode
|
|
|
|
|
|
|
14 |
|
15 |
# Define the same preprocessing used during training
|
16 |
transform = transforms.Compose([
|
|
|
30 |
probs = torch.softmax(outputs, dim=-1)
|
31 |
|
32 |
# Get the label with the highest probability
|
33 |
+
top_label = probs.argmax().item() # Get the index of the highest probability
|
34 |
return top_label
|
35 |
|
36 |
# Create the Gradio interface
|
|
|
44 |
|
45 |
# Launch the app
|
46 |
iface.launch()
|
|