Upload 7 files
Browse files- 09_pretrained_effnetb2_feature_extractor__pizza_steak_sushi_20_percent.pth +3 -0
- 2582289.jpg +0 -0
- 3622237.jpg +0 -0
- 592799.jpg +0 -0
- app.py +43 -0
- model.py +25 -0
- requirements.txt +3 -0
09_pretrained_effnetb2_feature_extractor__pizza_steak_sushi_20_percent.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:77e90b32d59d9cf33e5a15bf57e9c6d786272b7daa0ec937f2802357c1957e4c
|
3 |
+
size 31334283
|
2582289.jpg
ADDED
3622237.jpg
ADDED
592799.jpg
ADDED
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import create_effnetb2_model
|
7 |
+
from timeit import default_timer as timer
|
8 |
+
from typing import Tuple, Dict
|
9 |
+
|
10 |
+
class_names= ['pizza','steak','sushi']
|
11 |
+
|
12 |
+
effnetb2, effnetb2_transforms= create_effnetb2_model(num_classes= 3)
|
13 |
+
|
14 |
+
effnetb2.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor__pizza_steak_sushi_20_percent.pth",map_location=
|
15 |
+
torch.device("cpu")
|
16 |
+
))
|
17 |
+
|
18 |
+
|
19 |
+
def predict(img)-> Tuple[Dict, float]:
|
20 |
+
|
21 |
+
start_time= timer()
|
22 |
+
img= effnetb2_transforms(img).unsqueeze(0)
|
23 |
+
effnetb2.eval()
|
24 |
+
with torch.inference_mode():
|
25 |
+
pred_probs= torch.softmax(effnetb2(img), dim= 1)
|
26 |
+
|
27 |
+
pred_labels_and_probs= {class_names[i]: float(pred_probs[0][i]) for i in range (len(class_names))}
|
28 |
+
pred_time=round(timer()- start_time, 5)
|
29 |
+
|
30 |
+
return pred_labels_and_probs, pred_time
|
31 |
+
|
32 |
+
|
33 |
+
titile= "Foodvision Mini"
|
34 |
+
description="An efficientnetb2 feature extractor computer vision model to classify images of pizza, steak and sushi."
|
35 |
+
article= "Created at [09_Pytorch model deployment] (https://www.learnpytorch.io/09_pytorch_model_deployment/)"
|
36 |
+
example_list= [["examples/"+ example] for example in os.listdir("examples")]
|
37 |
+
|
38 |
+
demo= gr.Interface(fn= predict, inputs= gr.Image(type="pil"), outputs= [gr.Label(num_top_classes= 3, label= "predictions"),
|
39 |
+
gr.Number(label= "prediction time (s)")],
|
40 |
+
example_list= example_list, title= title, description= description, article= article)
|
41 |
+
|
42 |
+
|
43 |
+
demo.launch()
|
model.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
from torch import nn
|
4 |
+
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes:int= 3,
|
7 |
+
seed:int= 40):
|
8 |
+
|
9 |
+
weights= torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
10 |
+
transforms= weights.transforms()
|
11 |
+
model= torchvision.models.efficientnet_b2(weights= weights)
|
12 |
+
|
13 |
+
for param in model.parameters():
|
14 |
+
param.requires_grad= False
|
15 |
+
|
16 |
+
torch.manual_seed(seed)
|
17 |
+
|
18 |
+
model.classifier(nn.Sequential(
|
19 |
+
|
20 |
+
nn.Dropout(0.3, inplace=True),
|
21 |
+
nn.Linear(in_features= 1408, out_features= num_classes),
|
22 |
+
|
23 |
+
)
|
24 |
+
|
25 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch== 1.13.1
|
2 |
+
torchvision== 0.14.1
|
3 |
+
gradio== 3.23.0
|