Spaces:
Sleeping
Sleeping
initial commit
Browse files- .gitattributes +2 -0
- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- app.py +66 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +24 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ 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.pth filter=lfs diff=lfs merge=lfs -text
|
37 |
+
.pth filter=lfs diff=lfs merge=lfs -text
|
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:5d19770ada64e5a76b25a703a2b1a2a2a67cdf479d11f38876a968166add3274
|
3 |
+
size 31313869
|
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
from model import create_effnetb2_model
|
6 |
+
from timeit import default_timer as timer
|
7 |
+
from typing import Dict, Tuple
|
8 |
+
|
9 |
+
|
10 |
+
# Setup class names
|
11 |
+
class_names = ['pizza', 'steak', 'sushi']
|
12 |
+
|
13 |
+
### 2. Model and transforms perparation ###
|
14 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=len(class_names))
|
15 |
+
|
16 |
+
# Load save weights
|
17 |
+
effnetb2.load_state_dict(torch.load(f='09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth',
|
18 |
+
map_location=torch.device('cpu'))) # load the model to the CPU
|
19 |
+
|
20 |
+
### 3. Predict function ###
|
21 |
+
def predict(img) -> Tuple[Dict, float]:
|
22 |
+
# Start a timer
|
23 |
+
start_time = timer()
|
24 |
+
|
25 |
+
# Transform the input image for use with EffNetB2
|
26 |
+
transformed_img = effnetb2_transforms(img).unsqueeze(dim=0) # unsqueeze = add batch dimension on 0th
|
27 |
+
|
28 |
+
# Put model into eval mode, make prediction
|
29 |
+
with torch.inference_mode():
|
30 |
+
effnetb2.eval()
|
31 |
+
|
32 |
+
# Pass the transformed image through the model and turn the prediction logits into probabilities
|
33 |
+
pred_prob = effnetb2(transformed_img).softmax(dim=1)
|
34 |
+
|
35 |
+
# Create a prediction label and prediction probability dictionary
|
36 |
+
pred_labels_and_probs = {class_names[i]: pred_prob[0][i].item() for i in range(len(class_names))}
|
37 |
+
|
38 |
+
# Calcualte pred time
|
39 |
+
end_time = timer()
|
40 |
+
inference_time = round(end_time - start_time, 4)
|
41 |
+
|
42 |
+
# Return pred dict and pred time
|
43 |
+
return pred_labels_and_probs, inference_time
|
44 |
+
|
45 |
+
### 4. Gradio app ###
|
46 |
+
# Create title, description and aritcle
|
47 |
+
title = 'FoodVision Mini ππ₯©π£'
|
48 |
+
description = 'An [EfficientNetB2 feature extractor](https://pytorch.org/vision/0.16/models/generated/torchvision.models.efficientnet_b2.html#efficientnet-b2) computer vision model to classify images as pizza, steak, sushi.'
|
49 |
+
article = 'Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/#74-building-a-gradio-interface).'
|
50 |
+
|
51 |
+
# Create example list
|
52 |
+
example_list = [['examples/' + example] for example in os.listdir('examples')]
|
53 |
+
|
54 |
+
# Create the Gradio demo
|
55 |
+
demo = gr.Interface(fn=predict, # maps inputs to outputs
|
56 |
+
inputs=gr.Image(type='pil'),
|
57 |
+
outputs=[gr.Label(num_top_classes=3, label='Predictions'),
|
58 |
+
gr.Number(label='Prediction time (s)')],
|
59 |
+
examples=example_list,
|
60 |
+
title=title,
|
61 |
+
description=description,
|
62 |
+
article=article)
|
63 |
+
|
64 |
+
# Launch the demo!
|
65 |
+
demo.launch(debug=False, # print errors locally?
|
66 |
+
share=True) # generate a publically shareable URL
|
examples/2582289.jpg
ADDED
![]() |
examples/3622237.jpg
ADDED
![]() |
examples/592799.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
from torch import nn
|
4 |
+
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classese: int = 3, # default output classes = 3 (pizza, steak , sushi)
|
7 |
+
seed: int = 42):
|
8 |
+
# 1, 2, 3 Create EffNetB2 pretained weights, transforms and model
|
9 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
10 |
+
transforms = weights.transforms()
|
11 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
12 |
+
|
13 |
+
# 4. Freeze all layers in the base model
|
14 |
+
for param in model.parameters():
|
15 |
+
param.requires_grad = False
|
16 |
+
|
17 |
+
# 5. Change classifier head with random seed for reproducibility
|
18 |
+
torch.manual_seed(seed)
|
19 |
+
model.classifier = nn.Sequential(
|
20 |
+
nn.Dropout(p=.3, inplace=True),
|
21 |
+
nn.Linear(in_features=1408, out_features=num_classese)
|
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==4.14.0
|