Amit Kumar commited on
Commit
41ba2bc
·
1 Parent(s): 162e31c

initial commit

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
+ pretrained_effnetb2_feature_extractor_food101_20_percent.pth filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import torch
4
+ import torchvision
5
+ from typing import Tuple, Dict
6
+ from timeit import default_timer as timer
7
+ from model import create_effnetb2_model
8
+
9
+ with open("class_names.txt", "r") as f:
10
+ class_names = [food_name.strip() for food_name in f.readlines()]
11
+
12
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
13
+ num_classes=101
14
+ )
15
+
16
+ effnetb2.load_state_dict(
17
+ torch.load(f="pretrained_effnetb2_feature_extractor_food101_20_percent.pth",
18
+ map_location=torch.device("cpu")) # Load the model to the CPU
19
+ )
20
+
21
+ def predict(img) -> Tuple[Dict, float]:
22
+
23
+ start_time = timer()
24
+
25
+ transformed_image = effnetb2_transforms(img).unsqueeze(dim=0)
26
+
27
+ effnetb2.eval()
28
+
29
+ with torch.inference_mode():
30
+
31
+ pred_probs = torch.softmax(effnetb2(transformed_image), dim=1)
32
+
33
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
34
+
35
+ end_time = timer()
36
+
37
+ pred_time = round(end_time-start_time, 4)
38
+
39
+ return pred_labels_and_probs, pred_time
40
+
41
+
42
+ title = "Food101 Classification App 🍔"
43
+ description = "An [EfficientNetB2 feature extractor](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2) computer vision model trained on [Food101 dataset](https://pytorch.org/vision/main/generated/torchvision.datasets.Food101.html) which classifies 101 different food categories."
44
+ article = "How to Use: Upload a food image in the upload section above or select an images from the 'Examples' section. " \
45
+ "Click on the 'Submit' button and the model will detect which" \
46
+ "food catagory the image belongs to."
47
+
48
+ example_list = [["examples" / example] for example in os.listdir("examples")]
49
+
50
+ food101_app = gr.Interface(fn=predict,
51
+ inputs=gr.Image(type="pil"),
52
+ outputs=[gr.Label(num_top_classes=5, label="Predictions"),
53
+ gr.Number(label="Prediction Time (s)")],
54
+ examples=example_list,
55
+ title=title,
56
+ description=description,
57
+ article=article)
58
+
59
+ food101_app.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/216054.jpg ADDED
examples/Indian-Samosas-sq.jpg ADDED
model.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torch import nn
4
+
5
+ def create_effnetb2_model(num_classes:int=3,
6
+ seed:int=42):
7
+
8
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
9
+ transforms = weights.transforms()
10
+ model = torchvision.models.efficientnet_b2(weights=weights)
11
+
12
+ for param in model.parameters():
13
+ param.requires_grad = False
14
+
15
+ torch.manual_seed(seed=42)
16
+ model.classifier = nn.Sequential(
17
+ nn.Dropout(p=0.3, inplace=True),
18
+ nn.Linear(in_features=1408, out_features=num_classes)
19
+ )
20
+
21
+ model.name = "effnetb2"
22
+ print(f"Created new model: {model.name}")
23
+ return model, transforms
pretrained_effnetb2_feature_extractor_food101_20_percent.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:11d2f79bfdeb19d7cb1dc41a2a98e120dfc84ea339e03a166c9c5ce9910886fc
3
+ size 31855674
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.3.0
2
+ torchvision==0.18.0
3
+ gradio==4.32.0