Spaces:
Sleeping
Sleeping
init commit
Browse files- .gitattributes +1 -0
- app.py +67 -0
- class_names.txt +101 -0
- effnetv2L_100_percent.pth +3 -0
- examples/apple_pie.jpg +0 -0
- examples/cheese_plate.jpg +0 -0
- examples/hamburger.jpg +0 -0
- model.py +17 -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 |
+
effnetv2L_100_percent.pth 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_efficientnet
|
6 |
+
from timeit import default_timer as timer
|
7 |
+
from typing import Tuple, Dict
|
8 |
+
|
9 |
+
# Setup class names
|
10 |
+
with open("class_names.txt", "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
|
15 |
+
effnet, effnet_transforms = create_efficientnet(output_shape=101)
|
16 |
+
|
17 |
+
# Load saved weights
|
18 |
+
effnet.load_state_dict(
|
19 |
+
torch.load(f="effnetv2L_100_percent.pth",
|
20 |
+
map_location=torch.device("cpu")) # load 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 EffNetB2
|
29 |
+
img = effnet_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index
|
30 |
+
|
31 |
+
# Put model into eval mode, make prediction
|
32 |
+
effnet.eval()
|
33 |
+
with torch.inference_mode():
|
34 |
+
# Pass transformed image through the model and turn the prediction logits into probaiblities
|
35 |
+
pred_probs = torch.softmax(effnet(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 pred time
|
41 |
+
end_time = timer()
|
42 |
+
pred_time = round(end_time - start_time, 4)
|
43 |
+
|
44 |
+
# Return pred dict and pred time
|
45 |
+
return pred_labels_and_probs, pred_time
|
46 |
+
|
47 |
+
### 4. Gradio app ###
|
48 |
+
# Create title, description and article
|
49 |
+
title = "Food101 classifier"
|
50 |
+
description = "An [EfficientNetV2 feature extractor](https://pytorch.org/vision/main/models/efficientnetv2.html) 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 = "Built with [Gradio](https://github.com/gradio-app/gradio) and [PyTorch](https://pytorch.org/)."
|
52 |
+
|
53 |
+
# Create example list
|
54 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
55 |
+
|
56 |
+
# Create the Gradio demo
|
57 |
+
demo = gr.Interface(fn=predict, # maps inputs to outputs
|
58 |
+
inputs=gr.Image(type="pil"),
|
59 |
+
outputs=[gr.Label(num_top_classes=5, label="Predictions"),
|
60 |
+
gr.Number(label="Prediction time (s)")],
|
61 |
+
examples=example_list,
|
62 |
+
title=title,
|
63 |
+
description=description,
|
64 |
+
article=article)
|
65 |
+
|
66 |
+
# Launch the 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
|
effnetv2L_100_percent.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:394f5454053132ee9431edcc96effaadc57402faca556db1033aa8e83b0a5ac3
|
3 |
+
size 472090718
|
examples/apple_pie.jpg
ADDED
examples/cheese_plate.jpg
ADDED
examples/hamburger.jpg
ADDED
model.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
from torch import nn
|
4 |
+
|
5 |
+
def create_efficientnet(output_shape: int):
|
6 |
+
weights = torchvision.models.EfficientNet_V2_L_Weights.IMAGENET1K_V1.DEFAULT
|
7 |
+
model = torchvision.models.efficientnet_v2_l(weights=weights)
|
8 |
+
|
9 |
+
for param in model.parameters():
|
10 |
+
param.requires_grad = False
|
11 |
+
|
12 |
+
model.classifier = nn.Sequential(
|
13 |
+
nn.Dropout(p=0.10, inplace=True),
|
14 |
+
nn.Linear(in_features=1280, out_features=output_shape)
|
15 |
+
)
|
16 |
+
|
17 |
+
return model, weights.transforms()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.1.0
|
2 |
+
torchvision==0.16.0
|
3 |
+
gradio==3.50.2
|