Spaces:
Sleeping
Sleeping
Mezei Dragos
commited on
Commit
•
72e82be
1
Parent(s):
cc22697
first commit
Browse files- .gitattributes +1 -0
- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pt +3 -0
- app.py +52 -0
- model.py +32 -0
- requirements.txt +4 -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 |
+
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pt filter=lfs diff=lfs merge=lfs -text
|
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:43b4f3a85a9b4b8c4a7eae79f59747344f8824ca043f4585a2451d951b024e18
|
3 |
+
size 31314554
|
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
|
5 |
+
from model import create_effnetb2_model
|
6 |
+
from timeit import default_timer as timer
|
7 |
+
from typing import Tuple, Dict
|
8 |
+
|
9 |
+
class_names = ['pizza', 'steak', 'sushi']
|
10 |
+
|
11 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
12 |
+
num_classes=3
|
13 |
+
)
|
14 |
+
|
15 |
+
effnetb2.load_state_dict(
|
16 |
+
torch.load(
|
17 |
+
f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pt",
|
18 |
+
map_location=torch.device('cpu'),
|
19 |
+
)
|
20 |
+
)
|
21 |
+
|
22 |
+
def predict(img) -> Tuple[Dict, float]:
|
23 |
+
start_time = timer()
|
24 |
+
|
25 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
26 |
+
|
27 |
+
effnetb2.eval()
|
28 |
+
with torch.inference_mode():
|
29 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
30 |
+
|
31 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
32 |
+
|
33 |
+
pred_time = round(timer() - start_time, 5)
|
34 |
+
|
35 |
+
return pred_labels_and_probs, pred_time
|
36 |
+
|
37 |
+
title = "FoodVision Mini"
|
38 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food."
|
39 |
+
article = "Created at pytorch tutorial."
|
40 |
+
|
41 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
42 |
+
|
43 |
+
demo = gr.Interface(fn=predict,
|
44 |
+
inputs=gr.Image(type="pil"),
|
45 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
46 |
+
gr.Number(label="Prediction time (s)")],
|
47 |
+
examples=example_list,
|
48 |
+
title=title,
|
49 |
+
description=description,
|
50 |
+
article=article)
|
51 |
+
|
52 |
+
demo.launch()
|
model.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes:int=3,
|
7 |
+
seed:int=42):
|
8 |
+
"""Creates an efficientnetb2 feature extractor model and transforms.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
num_classes (int, optional): number of classes in the classifier head.
|
12 |
+
Defaults to 3.
|
13 |
+
seed (int, optional): random seed value, default 42
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
17 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
18 |
+
"""
|
19 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
20 |
+
transforms = weights.transforms()
|
21 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
22 |
+
|
23 |
+
for param in model.parameters():
|
24 |
+
param.requires_grad = False
|
25 |
+
|
26 |
+
torch.manual_seed(seed)
|
27 |
+
model.classifier = nn.Sequential(
|
28 |
+
nn.Dropout(p=0.3, inplace=True),
|
29 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
30 |
+
)
|
31 |
+
|
32 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.4.1
|
2 |
+
torchvision==0.19.1
|
3 |
+
gradio==4.44.0
|
4 |
+
httpx==0.24.1
|