KabeerAmjad commited on
Commit
fadf2ad
·
verified ·
1 Parent(s): d97b868

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -4
app.py CHANGED
@@ -3,14 +3,21 @@ import torch
3
  from torch import nn
4
  from torchvision import models, transforms
5
  from PIL import Image
 
6
 
7
- # Load the model architecture and weights
8
  model_id = "KabeerAmjad/food_classification_model"
9
- model = models.resnet50(pretrained=False) # Do not load the pretrained weights here
10
- model.fc = nn.Linear(model.fc.in_features, 11) # Adjust the number of classes (replace 11 with your number of classes)
11
- model.load_state_dict(torch.load(model_id)) # Load the model weights you uploaded
 
 
 
12
  model.eval()
13
 
 
 
 
14
  # Define the same preprocessing used during training
15
  transform = transforms.Compose([
16
  transforms.Resize((224, 224)),
@@ -43,3 +50,4 @@ iface = gr.Interface(
43
 
44
  # Launch the app
45
  iface.launch()
 
 
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 model from Hugging Face model hub
9
  model_id = "KabeerAmjad/food_classification_model"
10
+ # Load ResNet50 model and adjust the final layer
11
+ model = models.resnet50(pretrained=False)
12
+ model.fc = nn.Linear(model.fc.in_features, 11) # Adjust the output layer to match your number of classes
13
+
14
+ # Load the weights from the Hugging Face model hub
15
+ model.load_state_dict(torch.hub.load_state_dict_from_url(f"https://huggingface.co/{model_id}/resolve/main/food_classification_model.pth"))
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([
23
  transforms.Resize((224, 224)),
 
50
 
51
  # Launch the app
52
  iface.launch()
53
+