Spaces:
Build error
Build error
repositivator
commited on
Commit
•
eeb0a2f
1
Parent(s):
9679d3d
Upload 8 files
Browse files- ConvNeXt_Tiny_101classes_20_10epochs.pth +3 -0
- app.py +51 -0
- class_names.txt +101 -0
- examples/0.jpg +0 -0
- examples/1.jpg +0 -0
- examples/2.jpg +0 -0
- model.py +23 -0
- requirements.txt +4 -0
ConvNeXt_Tiny_101classes_20_10epochs.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:930ce0c2c48294e4cf8215c27a1ec1799593fc2c42363441a7063adf499462df
|
3 |
+
size 111662445
|
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import create_model
|
7 |
+
from timeit import default_timer as timer
|
8 |
+
|
9 |
+
|
10 |
+
with open("class_names.txt", "r") as f:
|
11 |
+
class_names = [class_name.strip() for class_name in f.readlines()]
|
12 |
+
|
13 |
+
|
14 |
+
model, model_transforms = create_model(num_classes=len(class_names))
|
15 |
+
model.load_state_dict(torch.load("ConvNeXt_Tiny_101classes_20_10epochs.pth")) # load -> load_state_dict
|
16 |
+
model = model.to('cpu') # Place the model on the CPU
|
17 |
+
|
18 |
+
|
19 |
+
def predict(img):
|
20 |
+
|
21 |
+
time_start = timer()
|
22 |
+
|
23 |
+
weights = torchvision.models.ConvNeXt_Tiny_Weights.DEFAULT
|
24 |
+
transform_convnext_tiny = weights.transforms()
|
25 |
+
img_tensor = transform_convnext_tiny(img).unsqueeze(dim=0) # [Channels, Height, Width] -> [Batch_size, Channels, Height, Width]
|
26 |
+
|
27 |
+
model.eval()
|
28 |
+
with torch.inference_mode():
|
29 |
+
predicted_probs = model(img_tensor).softmax(dim=1)
|
30 |
+
|
31 |
+
# Class name & predicted probability for each class (required by Gradio)
|
32 |
+
pred_labels_probs = {}
|
33 |
+
for i in range(len(CLASS_NAMES)):
|
34 |
+
pred_labels_probs[CLASS_NAMES[i]] = float(predicted_probs[0][i])
|
35 |
+
|
36 |
+
return pred_labels_probs, round(timer() - time_start, 5)
|
37 |
+
|
38 |
+
|
39 |
+
app = gr.Interface(fn=predict, # mapping function for [ input -> output ]
|
40 |
+
|
41 |
+
inputs=gr.Image(type="pil"), # Input data
|
42 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), # Output data (fn function's return values)
|
43 |
+
gr.Number(label="Inference time (s)")],
|
44 |
+
|
45 |
+
examples=[["examples/" + example] for example in os.listdir("examples")]
|
46 |
+
|
47 |
+
title='ConvNeXt_Food101',
|
48 |
+
description='A ConvNext CV model to classify 101 foods',
|
49 |
+
article='Model trained on 150 images per class')
|
50 |
+
|
51 |
+
app.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
|
examples/0.jpg
ADDED
examples/1.jpg
ADDED
examples/2.jpg
ADDED
model.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
|
5 |
+
from torch import nn
|
6 |
+
|
7 |
+
|
8 |
+
def create_model(num_classes=101, seed=42):
|
9 |
+
|
10 |
+
weights = torchvision.models.ConvNeXt_Tiny_Weights.DEFAULT # .DEFAULT = best available weights on ImageNet
|
11 |
+
transforms = weights.transforms()
|
12 |
+
model = torchvision.models.convnext_tiny(weights=weights)
|
13 |
+
|
14 |
+
# Sequential (features)
|
15 |
+
for param in model.features.parameters():
|
16 |
+
param.requires_grad = False # "requires" "grad"ient-descent
|
17 |
+
|
18 |
+
# Sequential (classifier)
|
19 |
+
torch.manual_seed(seed)
|
20 |
+
model.classifier[-1] = nn.Linear(in_features=model.classifier[-1].in_features,
|
21 |
+
out_features=num_classes)
|
22 |
+
|
23 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
torch==1.13.0
|
3 |
+
torchvision==0.14.0
|
4 |
+
gradio==3.12.0
|