urameez commited on
Commit
2ac8add
1 Parent(s): 122aa20

Upload 5 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ examples/01-StreetStyle-Chicken-Biryani.jpg filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+
5
+ from model import create_effnetb2_model
6
+ from timeit import default_timer as timer
7
+ from typing import Tuple, Dict
8
+
9
+ # Setup class names by reading them from the file
10
+ with open(file="class_names.txt", mode='r') as f:
11
+ class_names = [food_name.strip() for food_name in f.readlines()]
12
+
13
+ ### Model and transforms preparation ###
14
+ # Create model and transforms for EfficientNetB2
15
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=101)
16
+
17
+ # Load saved weights into the model
18
+ effnetb2.load_state_dict(
19
+ torch.load(f="effnetb2_feature_extractor_food101_20_percent.pth",
20
+ map_location=torch.device("cpu")) # load model to CPU
21
+ )
22
+
23
+ ### Predict function ###
24
+ def predict(img) -> Tuple[Dict, float]:
25
+ # Start a timer
26
+ start_time = timer()
27
+
28
+ # Transform the input image for use with EfficientNetB2
29
+ img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze to add the batch dimension
30
+
31
+ # Put model into evaluation mode
32
+ effnetb2.eval()
33
+ with torch.inference_mode():
34
+ # Pass transformed image through the model and turn the prediction logits into probabilities
35
+ pred_probs = torch.softmax(input=effnetb2(img), dim=1)
36
+
37
+ # Create a prediction label and prediction probability dictionary
38
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
39
+
40
+ # Calculate prediction time
41
+ end_time = timer()
42
+ pred_time = round(end_time - start_time, 4)
43
+
44
+ # Return prediction dictionary and prediction time
45
+ return pred_labels_and_probs, pred_time
46
+
47
+ ### Gradio app ###
48
+ # Create title, description, and article for the Gradio interface
49
+ title = "Food101_Big_Classification"
50
+ description = "An [EfficientNetB2 feature extractor](https://pytorch.org/vision/stable/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2) computer vision model to classify images [101 classes of food from the Food101 dataset](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/food101_class_names.txt)."
51
+ article = "Created at [https://github.com/MRameezU/Food101_Big_Classification_EffNetB2/blob/e9c502b4f9e5a0f68e102184d18a6b934334b17b/food101-big-classification-effnetb2.ipynb]"
52
+
53
+ # Create a list of example images
54
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
55
+
56
+ # Create the Gradio demo interface
57
+ demo = gr.Interface(fn=predict, # function to map inputs to outputs
58
+ inputs=gr.Image(type="pil"), # input type is an image
59
+ outputs=[gr.Label(num_top_classes=5, label="Predictions"), # output top 5 predictions
60
+ gr.Number(label="Prediction time (s)")], # output prediction time
61
+ examples=example_list, # provide example images
62
+ title=title, # set the title of the interface
63
+ description=description, # set the description
64
+ article=article) # set the article link
65
+
66
+ # Launch the Gradio demo
67
+ demo.launch()
class_names.txt ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ apple_pie
2
+ baby_back_ribs
3
+ baklava
4
+ beef_carpaccio
5
+ beef_tartare
6
+ beet_salad
7
+ beignets
8
+ bibimbap
9
+ bread_pudding
10
+ breakfast_burrito
11
+ bruschetta
12
+ caesar_salad
13
+ cannoli
14
+ caprese_salad
15
+ carrot_cake
16
+ ceviche
17
+ cheese_plate
18
+ cheesecake
19
+ chicken_curry
20
+ chicken_quesadilla
21
+ chicken_wings
22
+ chocolate_cake
23
+ chocolate_mousse
24
+ churros
25
+ clam_chowder
26
+ club_sandwich
27
+ crab_cakes
28
+ creme_brulee
29
+ croque_madame
30
+ cup_cakes
31
+ deviled_eggs
32
+ donuts
33
+ dumplings
34
+ edamame
35
+ eggs_benedict
36
+ escargots
37
+ falafel
38
+ filet_mignon
39
+ fish_and_chips
40
+ foie_gras
41
+ french_fries
42
+ french_onion_soup
43
+ french_toast
44
+ fried_calamari
45
+ fried_rice
46
+ frozen_yogurt
47
+ garlic_bread
48
+ gnocchi
49
+ greek_salad
50
+ grilled_cheese_sandwich
51
+ grilled_salmon
52
+ guacamole
53
+ gyoza
54
+ hamburger
55
+ hot_and_sour_soup
56
+ hot_dog
57
+ huevos_rancheros
58
+ hummus
59
+ ice_cream
60
+ lasagna
61
+ lobster_bisque
62
+ lobster_roll_sandwich
63
+ macaroni_and_cheese
64
+ macarons
65
+ miso_soup
66
+ mussels
67
+ nachos
68
+ omelette
69
+ onion_rings
70
+ oysters
71
+ pad_thai
72
+ paella
73
+ pancakes
74
+ panna_cotta
75
+ peking_duck
76
+ pho
77
+ pizza
78
+ pork_chop
79
+ poutine
80
+ prime_rib
81
+ pulled_pork_sandwich
82
+ ramen
83
+ ravioli
84
+ red_velvet_cake
85
+ risotto
86
+ samosa
87
+ sashimi
88
+ scallops
89
+ seaweed_salad
90
+ shrimp_and_grits
91
+ spaghetti_bolognese
92
+ spaghetti_carbonara
93
+ spring_rolls
94
+ steak
95
+ strawberry_shortcake
96
+ sushi
97
+ tacos
98
+ takoyaki
99
+ tiramisu
100
+ tuna_tartare
101
+ waffles
examples/01-StreetStyle-Chicken-Biryani.jpg ADDED

Git LFS Details

  • SHA256: 92b286c4ff82e6cdd65aa87db43358f202b67db8d20f0bbf79b924cba9d4de86
  • Pointer size: 132 Bytes
  • Size of remote file: 1.63 MB
model.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+ def create_effnetb2_model(num_classes: int = 3, seed: int = 42):
7
+ """
8
+ Creates an EfficientNetB2 model with a custom classifier for the given number of classes.
9
+
10
+ Args:
11
+ num_classes (int): Number of output classes for the classifier.
12
+ seed (int): Random seed for reproducibility.
13
+
14
+ Returns:
15
+ model (torchvision.models): Modified EfficientNetB2 model with custom classifier.
16
+ transforms (torchvision.transforms): Preprocessing transforms for the model.
17
+ """
18
+ # Load pre-trained EfficientNetB2 weights and transforms
19
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
20
+ transforms = weights.transforms()
21
+
22
+ # Initialize the EfficientNetB2 model with pre-trained weights
23
+ model = torchvision.models.efficientnet_b2(weights=weights)
24
+
25
+ # Freeze the base parameters of the model
26
+ for param in model.parameters():
27
+ param.requires_grad = False
28
+
29
+ # Set the random seed for reproducibility
30
+ torch.manual_seed(seed)
31
+
32
+ # Replace the classifier with a custom one for the specified number of classes
33
+ model.classifier = nn.Sequential(
34
+ nn.Dropout(p=0.3, inplace=True), # Add dropout for regularization
35
+ nn.Linear(in_features=1408, out_features=num_classes) # Linear layer for classification
36
+ )
37
+
38
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.12.0
2
+ torchvision==0.13.0
3
+ gradio==3.1.4