Spaces:
Build error
Build error
added the files to the repo
Browse files- .gitattributes +1 -0
- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- app.py +69 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +38 -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.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:9df742dbb48fecef3a11c84d17cc1c763812fa5d9e1bf117da2020f6268031a5
|
| 3 |
+
size 31315203
|
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### 1. Imports and class names setup
|
| 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 |
+
# Setup class names
|
| 11 |
+
class_names = ['pizza', 'steak', 'sushi']
|
| 12 |
+
|
| 13 |
+
### 2. Model and transforms setup
|
| 14 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
| 15 |
+
num_classes=3,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
#Load the saved weights
|
| 19 |
+
effnetb2.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
|
| 20 |
+
map_location=torch.device("cpu") # Load the model to the CPU
|
| 21 |
+
)
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
### 3. Predict function
|
| 25 |
+
def predict(img) -> Tuple [dict, float]:
|
| 26 |
+
# Start a timer
|
| 27 |
+
start_time = timer()
|
| 28 |
+
|
| 29 |
+
# TRansform the input image
|
| 30 |
+
transformed_image = effnetb2_transforms(img).unsqueeze(dim=0)
|
| 31 |
+
|
| 32 |
+
# Put model into eval mode, make prediction
|
| 33 |
+
effnetb2.eval()
|
| 34 |
+
with torch.inference_mode():
|
| 35 |
+
logits = effnetb2(transformed_image)
|
| 36 |
+
preds = torch.softmax(logits, dim=1)
|
| 37 |
+
|
| 38 |
+
# Create a prediction label and prediction probability dictionary
|
| 39 |
+
label = class_names[torch.argmax(preds)]
|
| 40 |
+
pred_labels_and_probs = {class_names[i]:float(preds[0][i]) for i in range(len(class_names))}
|
| 41 |
+
|
| 42 |
+
# Calaulate pred time
|
| 43 |
+
end_time = timer()
|
| 44 |
+
duration = round(end_time - start_time, 4)
|
| 45 |
+
|
| 46 |
+
# retrun pred dict and pred time
|
| 47 |
+
return pred_labels_and_probs, duration
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Create title, description and article
|
| 51 |
+
title = "Foodvision mini"
|
| 52 |
+
description="An EfficientNetB2 feature extractor computer vision model to classify images as pizza, steak or sushi."
|
| 53 |
+
article = "Created at 09. PyTorch model deployment ZTM course"
|
| 54 |
+
|
| 55 |
+
# Create example list
|
| 56 |
+
example_list = [["examples/" + examples] for examples in os.listdir("examples")]
|
| 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 |
+
title=title,
|
| 64 |
+
article=article,
|
| 65 |
+
examples=example_list)
|
| 66 |
+
|
| 67 |
+
# Launch the demo
|
| 68 |
+
demo.launch(debug=False, # print errors locally?
|
| 69 |
+
share=True) # generate a publically shatrable URL
|
examples/2582289.jpg
ADDED
|
examples/3622237.jpg
ADDED
|
examples/592799.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
|
| 4 |
+
from torch import nn
|
| 5 |
+
from helper_functions import set_seeds
|
| 6 |
+
|
| 7 |
+
def create_effnetb2_model(output_classes:int=3,
|
| 8 |
+
seed=42):
|
| 9 |
+
"""
|
| 10 |
+
Creates a pretrained EfficientNet B2 model feature extractor, with the base layers frozen and the output classifier adjusted to the target setup
|
| 11 |
+
|
| 12 |
+
returns:
|
| 13 |
+
(model, transforms)
|
| 14 |
+
model: The Feature extractor model instance of EfficientNetB2
|
| 15 |
+
"""
|
| 16 |
+
# 1. Setup poretrained EffNetB2 weights
|
| 17 |
+
effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 18 |
+
|
| 19 |
+
#2. Get the transforms
|
| 20 |
+
transforms = effnetb2_weights.transforms()
|
| 21 |
+
|
| 22 |
+
#3. Setup pretrines model instance
|
| 23 |
+
model = torchvision.models.efficientnet_b2(weights=effnetb2_weights)
|
| 24 |
+
|
| 25 |
+
#4. Freeze the base layers in the model - this will stop all base layers from training
|
| 26 |
+
for param in model.parameters():
|
| 27 |
+
param.requires_grad=False
|
| 28 |
+
|
| 29 |
+
#5. Change the classification head
|
| 30 |
+
#Set seed
|
| 31 |
+
set_seeds(42)
|
| 32 |
+
model.classifier = nn.Sequential(
|
| 33 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 34 |
+
nn.Linear(in_features=1408,
|
| 35 |
+
out_features=output_classes,
|
| 36 |
+
bias=True)
|
| 37 |
+
)
|
| 38 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
torch==2.10.0
|
| 3 |
+
torchvision==0.25.0
|
| 4 |
+
gradio==5.50.0
|