Spaces:
Sleeping
Sleeping
DaimonKing
commited on
Commit
•
48b605c
1
Parent(s):
188d6b4
Upload 5 files
Browse filesUploading required files
- EffnetB2_Big_Label_Smoothning_14_Epochs.pt +3 -0
- app.py +43 -0
- class_names.txt +101 -0
- model.py +25 -0
- requirements.txt +3 -0
EffnetB2_Big_Label_Smoothning_14_Epochs.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e488619dcc465fba7722de99a6e559b4876389be7dc696e2e9ed7e2d0d6d2f5a
|
3 |
+
size 31927482
|
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from model import create_effnetb2
|
5 |
+
from PIL import Image
|
6 |
+
from pathlib import Path
|
7 |
+
from timeit import default_timer as timer
|
8 |
+
|
9 |
+
model, transforms = create_effnetb2(101)
|
10 |
+
model = torch.load("EffnetB2_Big_Label_Smoothning_14_Epochs.pt")
|
11 |
+
examples_path = [["examples/" + example] for example in os.listdir("examples")]
|
12 |
+
|
13 |
+
with open("class_names.txt", 'r') as f:
|
14 |
+
food101_class_names_loaded = [food.strip() for food in f.readlines()]
|
15 |
+
f.close()
|
16 |
+
|
17 |
+
def predict(img, model= model, transforms= transforms, class_names= food101_class_names_loaded, device= 'cpu'):
|
18 |
+
pred_labels_and_probs = {}
|
19 |
+
with torch.inference_mode():
|
20 |
+
model.eval()
|
21 |
+
model = model.to(device)
|
22 |
+
start_time = timer()
|
23 |
+
|
24 |
+
transformed_img = transforms(img)
|
25 |
+
y_pred_probs = torch.softmax(model(transformed_img.unsqueeze(dim= 0)), dim= 1)
|
26 |
+
y_pred_class = class_names[torch.argmax(y_pred_probs, dim= 1)]
|
27 |
+
for i in range(len(class_names)):
|
28 |
+
pred_labels_and_probs[class_names[i]] = y_pred_probs[0][i].item()
|
29 |
+
|
30 |
+
end_time = timer()
|
31 |
+
pred_time = round(end_time - start_time, 4)
|
32 |
+
return (pred_labels_and_probs, pred_time)
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
title = "Food Vision Big"
|
37 |
+
description = "An EfficientNetB2 feature extractor to classify food images into 101 classes from the Food101 dataset"
|
38 |
+
demo = gr.Interface(fn= predict, inputs= gr.Image(type= 'pil'),
|
39 |
+
outputs= [gr.Label(num_top_classes= 5, label= "Predictions"),
|
40 |
+
gr.Number(label= "Prediction time (s)")], title= title,
|
41 |
+
description= description, examples= examples_path)
|
42 |
+
|
43 |
+
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
|
model.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
from torchvision import models
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
device = 'cpu'
|
7 |
+
def create_effnetb2(num_output_classes: int):
|
8 |
+
"""
|
9 |
+
Returns an effnetb2 feature extractor model with all layers except classifier layer frozen and the corresponding transforms for data preprocessing
|
10 |
+
|
11 |
+
Args
|
12 |
+
num_output_classes (int) : The number of classes in the classifier head
|
13 |
+
"""
|
14 |
+
effnet_b2_weights = models.EfficientNet_B2_Weights.DEFAULT
|
15 |
+
effnet_b2 = models.efficientnet_b2(weights= effnet_b2_weights).to(device)
|
16 |
+
effnet_transforms = effnet_b2_weights.transforms()
|
17 |
+
for params in effnet_b2.parameters():
|
18 |
+
params.requires_grad = False
|
19 |
+
|
20 |
+
effnet_b2.classifier = nn.Sequential(
|
21 |
+
nn.Dropout(p= 0.3, inplace= True),
|
22 |
+
nn.Linear(in_features= 1408, out_features= num_output_classes, bias= True)
|
23 |
+
).to(device)
|
24 |
+
|
25 |
+
return effnet_b2, effnet_transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.3.0
|
2 |
+
gradio==4.37.0
|
3 |
+
torchvision==0.18.0
|