Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- .gitattributes +1 -0
- app.py +58 -0
- class_names.txt +101 -0
- effnetb2_food101_20pct.pth +3 -0
- examples/example.jpg +3 -0
- model.py +21 -0
- requirements.txt +3 -0
.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/example.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Imports
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
from typing import Tuple, Dict
|
5 |
+
from timeit import default_timer as timer
|
6 |
+
import model
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Class names
|
10 |
+
with open("class_names.txt", "r") as f:
|
11 |
+
class_names = [food.strip() for food in f.readlines()]
|
12 |
+
|
13 |
+
# Create instance of model
|
14 |
+
effnetb2, effnetb2_transforms = model.create_effnetb2_model(num_classes=len(class_names))
|
15 |
+
# Load Weights
|
16 |
+
effnetb2.load_state_dict(state_dict=torch.load("effnetb2_food101_20pct.pth",
|
17 |
+
map_location=torch.device("cpu") # hard-coded load to cpu
|
18 |
+
))
|
19 |
+
|
20 |
+
# Predict function
|
21 |
+
def predict(img) -> Tuple[Dict, float]:
|
22 |
+
# Start timer
|
23 |
+
start = timer()
|
24 |
+
# Transform input image for use
|
25 |
+
img = effnetb2_transforms(img).unsqueeze(dim=0)
|
26 |
+
# Put model in eval mode
|
27 |
+
effnetb2.eval()
|
28 |
+
with torch.inference_mode():
|
29 |
+
logits = effnetb2(img)
|
30 |
+
pred_probs = torch.softmax(logits, dim=1).squeeze()
|
31 |
+
prediction = logits.argmax(dim=1).item()
|
32 |
+
prediction_label = class_names[prediction]
|
33 |
+
end = timer()
|
34 |
+
pred_dict = {class_names[i]: pred_probs[i].item() for i in range(len(class_names))}
|
35 |
+
delta_time = round(end-start, 4)
|
36 |
+
return pred_dict, delta_time
|
37 |
+
|
38 |
+
# Gradio app
|
39 |
+
|
40 |
+
# Create example list from within this file
|
41 |
+
example_list = [ ["examples/" + example] for example in os.listdir("examples")]
|
42 |
+
|
43 |
+
title = "FoodVision Big"
|
44 |
+
description = "EfficientNetB2 feature extractor CV model to classify images of 101 types of food from the Food101 dataset."
|
45 |
+
article = "Created for PyTorch ZTM course"
|
46 |
+
|
47 |
+
demo = gr.Interface(fn=predict, # maps inputs to outputs
|
48 |
+
inputs=gr.Image(type="pil"),
|
49 |
+
outputs=[gr.Label(num_top_classes=5, label="Predictions"), # for the prediction dictionary
|
50 |
+
gr.Number(label="Prediction time (s)")
|
51 |
+
],
|
52 |
+
examples=example_list,
|
53 |
+
title=title,
|
54 |
+
description=description,
|
55 |
+
article=article,
|
56 |
+
)
|
57 |
+
|
58 |
+
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
|
effnetb2_food101_20pct.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4481d999993e8ca8e47c0ddb485176713ddf132822d2c213a743fbf6695f4d92
|
3 |
+
size 31833771
|
examples/example.jpg
ADDED
![]() |
Git LFS Details
|
model.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torchvision
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
"""Script to load and create and instance of effnetb2 """
|
7 |
+
|
8 |
+
def create_effnetb2_model(num_classes=101):
|
9 |
+
weights=torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
10 |
+
transforms=weights.transforms()
|
11 |
+
model=torchvision.models.efficientnet_b2(weights=weights)
|
12 |
+
|
13 |
+
for params in model.parameters():
|
14 |
+
params.requires_grad=False
|
15 |
+
|
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 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.0.1
|
2 |
+
torchvision==0.15.2
|
3 |
+
gradio==3.44.4
|