Commit
·
34f9c66
1
Parent(s):
1c2ade9
Initial commit
Browse files- .gitattributes +3 -0
- app.py +72 -0
- effnet_b2_model.pth +3 -0
- example/2582289.jpg +0 -0
- example/3622237.jpg +0 -0
- example/592799.jpg +0 -0
- model.py +24 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ 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 |
+
effnet_b2_model.pth filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
.pth filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*pth filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Import and class names setup
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
from model import create_effnetb2_model
|
| 8 |
+
from timeit import default_timer as timer
|
| 9 |
+
from typings import Tuple, Dict
|
| 10 |
+
|
| 11 |
+
# Setup class names
|
| 12 |
+
class_names= ['pizza', 'steak', 'sushi']
|
| 13 |
+
|
| 14 |
+
# Model and transforms preparation
|
| 15 |
+
effnetb2_model, effnetb2_transform= create_effnetb2_model()
|
| 16 |
+
# Load state dict
|
| 17 |
+
effnetb2_model.load_state_dict(torch.load(
|
| 18 |
+
f= 'effnet_b2_model.pth',
|
| 19 |
+
map_location= torch.device('cpu')
|
| 20 |
+
)
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Predict function
|
| 24 |
+
|
| 25 |
+
def predict(img)-> Tuple[Dict, float]:
|
| 26 |
+
# start a timer
|
| 27 |
+
start_time= timer()
|
| 28 |
+
|
| 29 |
+
#transform the input image for use with effnet b2
|
| 30 |
+
transform_image= effnetb2_transform(img).unsqueeze(0)
|
| 31 |
+
|
| 32 |
+
#put model into eval mode, make pred
|
| 33 |
+
effnetb2_model.eval()
|
| 34 |
+
with torch.inference_mode():
|
| 35 |
+
pred_logits= effnetb2_model(transform_image)
|
| 36 |
+
pred_prob= torch.softmax(pred_logits, dim=1)
|
| 37 |
+
|
| 38 |
+
# create a pred label and pred prob dict
|
| 39 |
+
pred_label_and_prob= {class_names[i]: float(pred_prob[0][i]) for i in range(len(class_names))}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# calc pred time
|
| 43 |
+
stop_time= timer()
|
| 44 |
+
pred_time= round(stop_time - start_time, 4)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# return pred dict and pred time
|
| 48 |
+
return pred_label_and_prob, pred_time
|
| 49 |
+
|
| 50 |
+
# create example list
|
| 51 |
+
example_list= [['example/'+example] for example in os.listdir('example')]
|
| 52 |
+
|
| 53 |
+
# create gradio app
|
| 54 |
+
title= 'FoodVision Mini 🍕🥩🍣 '
|
| 55 |
+
description= 'An EfficientnetB2 feature extractor Computer vision model to classify image as pizza, steak or sushi'
|
| 56 |
+
article= 'Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/).'
|
| 57 |
+
|
| 58 |
+
# Create the gradio demo
|
| 59 |
+
demo= gr.Interface(fn= predict,
|
| 60 |
+
inputs=gr.Image(type='pil'),
|
| 61 |
+
outputs= [gr.Label(num_top_classes=3, label= 'predictions'),
|
| 62 |
+
gr.Number(label= 'Prediction time (S)')],
|
| 63 |
+
examples= example_list,
|
| 64 |
+
title= title,
|
| 65 |
+
description= description,
|
| 66 |
+
article= article
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Launch the demo
|
| 70 |
+
#demo.launch()
|
| 71 |
+
demo.launch(debug=False, # print errors locally?
|
| 72 |
+
share=True) # generate a publically shareable URL?
|
effnet_b2_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2bcd7636f134dff8f3606ab07122e3dcc393ce42b1531a032311566e4d9da6bd
|
| 3 |
+
size 31277689
|
example/2582289.jpg
ADDED
|
example/3622237.jpg
ADDED
|
example/592799.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch
|
| 3 |
+
import torchvision
|
| 4 |
+
from torch import nn
|
| 5 |
+
|
| 6 |
+
def create_effnetb2_model(num_classes:int=3,
|
| 7 |
+
seed:int=42):
|
| 8 |
+
# Create Effnet pretrained model
|
| 9 |
+
weights= torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 10 |
+
transforms= weights.transforms()
|
| 11 |
+
model= torchvision.models.efficientnet_b2(weights=weights)
|
| 12 |
+
|
| 13 |
+
# Freeze all layers in the base model
|
| 14 |
+
for param in model.parameters():
|
| 15 |
+
param.requires_grad= False
|
| 16 |
+
|
| 17 |
+
# Change the classifier layer
|
| 18 |
+
torch.manual_seed(seed)
|
| 19 |
+
model.classifier= nn.Sequential(
|
| 20 |
+
nn.Dropout(p=0.3, inplace= True),
|
| 21 |
+
nn.Linear(in_features=1408, out_features= num_classes)
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.0.1
|
| 2 |
+
torchvision==0.15.2
|
| 3 |
+
gradio==3.35.2
|