Spaces:
Sleeping
Sleeping
initial commit
Browse files- app.py +73 -0
- class_names.txt +101 -0
- effnetb2_feature_extractor_food101_20_percent.pth +3 -0
- examples/sashimi.png +0 -0
- model.py +18 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
"""The main parts are:
|
3 |
+
1. Imports and class names setup
|
4 |
+
2. Model and transforms preparation
|
5 |
+
3. Write a predict function for gradio to use
|
6 |
+
4. Write the Gradio app and the launch command
|
7 |
+
"""
|
8 |
+
import os
|
9 |
+
from typing import Tuple, Dict, List
|
10 |
+
import PIL
|
11 |
+
import torch
|
12 |
+
from torch import nn
|
13 |
+
import torchvision
|
14 |
+
import gradio as gr
|
15 |
+
from timeit import default_timer as timer
|
16 |
+
from model import create_effnetb2_model
|
17 |
+
|
18 |
+
# Setup class names
|
19 |
+
filepath = "./class_names.txt"
|
20 |
+
with open(filepath, "r") as f:
|
21 |
+
class_names = [food_name.strip() for food_name in f.readlines()]
|
22 |
+
|
23 |
+
model, transforms = create_effnetb2_model(num_classes = len(class_names))
|
24 |
+
|
25 |
+
# Load saved weights into the model, and load the model onto the CPU
|
26 |
+
model.load_state_dict(torch.load(f = "effnetb2_feature_extractor_food101_20_percent.pth",
|
27 |
+
map_location = torch.device('cpu')))
|
28 |
+
|
29 |
+
# Write function to run inference on gradio
|
30 |
+
def predict(img: PIL.Image,
|
31 |
+
model: nn.Module = model,
|
32 |
+
transforms: torchvision.transforms = transforms,
|
33 |
+
class_names: List[str] = class_names) -> Tuple[Dict, float]:
|
34 |
+
"""Function to predict image class on gradio
|
35 |
+
|
36 |
+
Args:
|
37 |
+
img (np.array): Image as a numpy array
|
38 |
+
model (nn.Module, optional): Model. Defaults to vit.
|
39 |
+
class_names (List[str], optional): List of class anmes. Defaults to class_names.
|
40 |
+
|
41 |
+
Returns:
|
42 |
+
Tuple[Dict, float]: Tuplefor further processing on gradio
|
43 |
+
"""
|
44 |
+
start_time = timer()
|
45 |
+
img = transforms(img).unsqueeze(0) #add batch dimension
|
46 |
+
model.eval()
|
47 |
+
with torch.inference_mode():
|
48 |
+
pred_probs = torch.softmax(model(img), dim = 1)
|
49 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
50 |
+
end_time = timer()
|
51 |
+
pred_time = round(end_time - start_time, 4)
|
52 |
+
return pred_labels_and_probs, pred_time
|
53 |
+
|
54 |
+
# Create example_list
|
55 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
56 |
+
|
57 |
+
# Create Gradio App
|
58 |
+
title = 'FoodVision 101 🍔🍣🥩🍕🍦😋'
|
59 |
+
description = "Using an [EfficientNet](https://arxiv.org/abs/1905.11946) for Image Classification of 101 different food classes"
|
60 |
+
article = "Created by [Titus Lim](https://github.com/tituslhy)"
|
61 |
+
|
62 |
+
demo = gr.Interface(fn = predict,
|
63 |
+
inputs = gr.Image(type = "pil"),
|
64 |
+
outputs = [gr.Label(num_top_classes = 5, label = "Predictions"),
|
65 |
+
gr.Number(label = "Prediction time (s)")],
|
66 |
+
examples 😋= example_list,
|
67 |
+
title = title,
|
68 |
+
description = description,
|
69 |
+
article = article)
|
70 |
+
|
71 |
+
# Launch demo
|
72 |
+
demo.launch(debug = False, #prints errors locally
|
73 |
+
)
|
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_feature_extractor_food101_20_percent.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8edf12deee22fbca3e7cd777df1130c572017a1cc6677ddb9c6a3d3053df29b5
|
3 |
+
size 31841405
|
examples/sashimi.png
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes: int = 101,
|
7 |
+
seed: int = 42):
|
8 |
+
effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
9 |
+
effnetb2_transforms = effnetb2_weights.transforms()
|
10 |
+
effnetb2 = torchvision.models.efficientnet_b2(weights=effnetb2_weights)
|
11 |
+
for param in effnetb2.parameters():
|
12 |
+
param.requires_grad = False
|
13 |
+
torch.manual_seed(seed)
|
14 |
+
effnetb2.classifier = nn.Sequential(
|
15 |
+
nn.Dropout(p = 0.3, inplace = True),
|
16 |
+
nn.Linear(in_features = 1408, out_features = num_classes)
|
17 |
+
)
|
18 |
+
return effnetb2, effnetb2_transforms
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.41.2
|
2 |
+
Pillow==9.5.0
|
3 |
+
torch==2.0.1
|
4 |
+
torchvision==0.15.2
|