VuSamSon commited on
Commit
b9f70c4
1 Parent(s): 4e5e2f8

Upload 5 files

Browse files
Files changed (5) hide show
  1. app.py +42 -0
  2. class_names.txt +101 -0
  3. effnetB2_101.pth +3 -0
  4. model.py +23 -0
  5. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ import random
4
+ import torch
5
+ from model import create_effnetb2
6
+ import gradio as gr
7
+ from typing import Dict, Tuple
8
+ from time import time
9
+
10
+ effnetb2, effnetb2_transforms = create_effnetb2(101)
11
+ effnetb2.load_state_dict(torch.load(f='effnetB2_101.pth', map_location=torch.device('cpu')))
12
+ with open('class_names.txt', 'r') as f:
13
+ class_names = [food.strip() for food in f.readlines()]
14
+
15
+ def predict(image) -> Tuple[Dict, float]:
16
+ start = time()
17
+ transformed_image = effnetb2_transforms(image).unsqueeze(0)
18
+ effnetb2.eval()
19
+ with torch.inference_mode():
20
+ y_logits = effnetb2(transformed_image)
21
+ probs = torch.softmax(y_logits, dim=1).squeeze()
22
+ pred_labels_and_probs = {class_names[i]: float(probs[i].item()) for i in range(len(class_names))}
23
+ end = time()
24
+ return pred_labels_and_probs, round(end - start, 5)
25
+
26
+ images = os.listdir('examples')
27
+ example_list = [[str('examples/' + x)] for x in images]
28
+ # Create title, description and article strings
29
+ title = "FoodVision"
30
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food."
31
+
32
+ # Create the Gradio demo
33
+ demo = gr.Interface(fn=predict, # mapping function from input to output
34
+ inputs=gr.Image(type="pil"), # what are the inputs?
35
+ outputs=[gr.Label(num_top_classes=5, label="Predictions"), # what are the outputs?
36
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
37
+ examples=example_list,
38
+ title=title,
39
+ description=description)
40
+
41
+ # Launch the demo!
42
+ demo.launch(debug=False)
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
effnetB2_101.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e2ac47e15babde6fa9b82e8b646937c3d392c71a203895db5a7bab561b322aa5
3
+ size 31828666
model.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torch import nn
4
+ from torchvision.models._api import WeightsEnum
5
+ from torch.hub import load_state_dict_from_url
6
+ def get_state_dict(self, *args, **kwargs):
7
+ kwargs.pop("check_hash")
8
+ return load_state_dict_from_url(self.url, *args, **kwargs)
9
+ WeightsEnum.get_state_dict = get_state_dict
10
+
11
+ def create_effnetb2(num_classes: int=3):
12
+ effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
13
+ effnetb2_transforms = effnetb2_weights.transforms()
14
+ effnetb2 = torchvision.models.efficientnet_b2(weights="DEFAULT")
15
+ for param in effnetb2.parameters():
16
+ param.requires_grad = False
17
+ torch.manual_seed(42)
18
+ torch.cuda.manual_seed(42)
19
+ effnetb2.classifier = nn.Sequential(
20
+ nn.Dropout(p=0.3, inplace=True),
21
+ nn.Linear(in_features=1408, out_features=num_classes)
22
+ )
23
+ return effnetb2, effnetb2_transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.1.2
2
+ torchvision==0.16.2
3
+ gradio==4.15.0