kdallash commited on
Commit
5718ca3
·
verified ·
1 Parent(s): 80f72fa

Upload 11 files

Browse files
11_model_deployment_effnetb2_full_dataset_food101.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a90a9a1e236b1746bcbfbee2ea0772e3bf453afc50f27d8df82abcf402c3020f
3
+ size 31851995
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1 Imports and class names setup###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+ from model import create_effnetb2_model
6
+ from timeit import default_timer as timer
7
+ from typing import List, Dict,Tuple
8
+
9
+ with open("class_name.txt", "r") as f:
10
+ class_names = [food.strip("\n") for food in f.readlines()]
11
+
12
+ ### 2 model and transform preparation###
13
+ effnetb2_loaded, transforms = create_effnetb2_model(num_classes=len(class_names))
14
+ effnetb2_loaded.load_state_dict(torch.load("11_model_deployment_effnetb2_full_dataset_food101.pth",map_location="cpu"))
15
+
16
+
17
+ ### 3 we need a predict function###
18
+ def predict(img) -> Tuple[Dict,float]:
19
+ #start a timer
20
+ start_time = timer()
21
+
22
+ # transform the image
23
+ transformed_image = effnet_transform(img).unsqueeze(0)
24
+
25
+ # putting the model in eval mode and make the prediction
26
+ effnetb2_loaded.eval()
27
+ with torch.inference_mode():
28
+ logit = effnetb2_loaded(transformed_image)
29
+
30
+ probs = torch.softmax(logit, dim=1)
31
+ # Create a prediction label and prediction probability dictionary
32
+ pred_label_dict ={class_names[i] : probs[0][i].item() for i in range(len(class_names))}
33
+
34
+ # calculate the pred time
35
+ end_time = timer()
36
+ inference_time = round(end_time - start_time, 4)
37
+ # return the label dict and inference time
38
+ return pred_label_dict, inference_time
39
+ ###Grad###
40
+ title = "FoodVision big models trained on Food101 Dataset"
41
+ description = "An EfficientnetB2 feature extraction model is used to classify images of food101 dataset"
42
+ example_list =[["examples/"+example] for example in os.listdir("examples")]
43
+
44
+ # create a gradio demo
45
+ demo = gr.Interface(
46
+ fn=predict,
47
+ inputs=gr.Image(type="pil"),
48
+ outputs=[gr.Label(num_top_classes = 5,label= "prediction"),
49
+ gr.Number(label=" Prediction time in second")],
50
+ examples=example_list,
51
+ title=title,
52
+ description=description,
53
+ cache_examples=False
54
+ )
55
+ demo.launch(share= False)
56
+
class_name.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/2089860.jpg ADDED
examples/2169308.jpg ADDED
examples/2177928.jpg ADDED
examples/298717.jpg ADDED
examples/77054.jpg ADDED
foodvision_big.zip ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:35d653329c5352cd128249887389a7b366dbe2a44c65f8054fb9a9cd0be0c826
3
+ size 29602137
model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetb2_model(num_classes:int=101,
8
+ seed:int=42):
9
+ """Creates an EfficientNetB2 feature extractor model and transforms.
10
+
11
+ Args:
12
+ num_classes (int, optional): number of classes in the classifier head.
13
+ Defaults to 3.
14
+ seed (int, optional): random seed value. Defaults to 42.
15
+
16
+ Returns:
17
+ model (torch.nn.Module): EffNetB2 feature extractor model.
18
+ transforms (torchvision.transforms): EffNetB2 image transforms.
19
+ """
20
+ # Create EffNetB2 pretrained weights, transforms and model
21
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
22
+ transforms = weights.transforms()
23
+ model = torchvision.models.efficientnet_b2(weights=weights)
24
+
25
+ # Freeze all layers in base model
26
+ for param in model.parameters():
27
+ param.requires_grad = False
28
+
29
+ # Change classifier head with random seed for reproducibility
30
+ torch.manual_seed(seed)
31
+ model.classifier = nn.Sequential(
32
+ nn.Dropout(p=0.3, inplace=True),
33
+ nn.Linear(in_features=1408, out_features=num_classes),
34
+ )
35
+
36
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio