Spaces:
Sleeping
Sleeping
FoodVisionMini initial commit
Browse files- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- app.py +51 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +20 -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:4335d48b1d742710550620d3cadf53d32b1d2b9528e4d1e4debd72a8e5a7b272
|
| 3 |
+
size 31315203
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
effnetb2.load_state_dict(
|
| 15 |
+
torch.load(
|
| 16 |
+
f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
|
| 17 |
+
map_location=torch.device("cpu")
|
| 18 |
+
)
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 22 |
+
start_time = timer()
|
| 23 |
+
|
| 24 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
| 25 |
+
|
| 26 |
+
effnetb2.eval()
|
| 27 |
+
with torch.inference_mode():
|
| 28 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
| 29 |
+
|
| 30 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
| 31 |
+
|
| 32 |
+
pred_time = round(timer() - start_time, 5)
|
| 33 |
+
|
| 34 |
+
return pred_labels_and_probs, pred_time
|
| 35 |
+
|
| 36 |
+
title = "FoodVision Mini 🍕🥩🍣"
|
| 37 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images as pizza, steak, or sushi."
|
| 38 |
+
article = "Created at 09. PyTorch Model Deployment."
|
| 39 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 40 |
+
|
| 41 |
+
# Create the Gradio demo
|
| 42 |
+
demo = gr.Interface(fn=predict,
|
| 43 |
+
inputs=gr.Image(type="pil"),
|
| 44 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
| 45 |
+
gr.Number(label="Prediction time (s)")],
|
| 46 |
+
examples=example_list,
|
| 47 |
+
title=title,
|
| 48 |
+
description=description,
|
| 49 |
+
article=article)
|
| 50 |
+
demo.launch(debug=False,
|
| 51 |
+
share=True)
|
examples/2582289.jpg
ADDED
|
examples/3622237.jpg
ADDED
|
examples/592799.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
from torch import nn
|
| 4 |
+
|
| 5 |
+
def create_effnetb2_model(num_classes:int=3,
|
| 6 |
+
seed:int=42):
|
| 7 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 8 |
+
transforms = weights.transforms()
|
| 9 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
| 10 |
+
|
| 11 |
+
for param in model.parameters():
|
| 12 |
+
param.requires_grad = False
|
| 13 |
+
|
| 14 |
+
torch.manual_seed(seed)
|
| 15 |
+
model.classifier = nn.Sequential(
|
| 16 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 17 |
+
nn.Linear(in_features=1408, out_features=num_classes)
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==6.4.0
|
| 2 |
+
torch==2.10.0
|
| 3 |
+
torchvision==0.25.0
|