Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- app.py +49 -0
- examples/367422.jpg +0 -0
- examples/648055.jpg +0 -0
- examples/705150.jpg +0 -0
- model.py +24 -0
- requirements.txt +4 -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:0ae2e70bf6105ef2b8789699268c7ca120fe480eba2b66ddf770f5e0125341fd
|
| 3 |
+
size 31314554
|
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 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 Tuple, Dict
|
| 8 |
+
|
| 9 |
+
class_names = ["pizza", "steak", "sushi"]
|
| 10 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=len(class_names))
|
| 11 |
+
|
| 12 |
+
effnetb2.load_state_dict(torch.load("09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth", map_location=torch.device("cpu")))
|
| 13 |
+
|
| 14 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 15 |
+
start_time = timer()
|
| 16 |
+
|
| 17 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
| 18 |
+
|
| 19 |
+
effnetb2.eval()
|
| 20 |
+
with torch.inference_mode():
|
| 21 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
| 25 |
+
|
| 26 |
+
end_timer = timer()
|
| 27 |
+
pred_time = round(end_timer-start_time, 4)
|
| 28 |
+
|
| 29 |
+
return pred_labels_and_probs, pred_time
|
| 30 |
+
|
| 31 |
+
example_list =[["examples/" + example] for example in os.listdir("examples")]
|
| 32 |
+
|
| 33 |
+
import gradio as gr
|
| 34 |
+
|
| 35 |
+
title="FoodVision Mini 🍕🥩🍣"
|
| 36 |
+
description = "An EfficientNetB2 feature extractor model that predicts pizza, steak and sushi"
|
| 37 |
+
article= "Created as a test"
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(fn=predict,
|
| 40 |
+
inputs=gr.Image(type="pil"),
|
| 41 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
| 42 |
+
gr.Number(label="Prediction Time (s)")],
|
| 43 |
+
examples=example_list,
|
| 44 |
+
title=title,
|
| 45 |
+
description=description,
|
| 46 |
+
article=article)
|
| 47 |
+
|
| 48 |
+
demo.launch(debug=False, share=True)
|
| 49 |
+
|
examples/367422.jpg
ADDED
|
examples/648055.jpg
ADDED
|
examples/705150.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 |
+
"""Creates an EfficientNet-B2 feature extractor model and transforms."""
|
| 9 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 10 |
+
transforms = weights.transforms()
|
| 11 |
+
model = torchvision.models.efficientnet_b2(weights=weights).to(device)
|
| 12 |
+
|
| 13 |
+
for param in model.parameters():
|
| 14 |
+
param.requires_grad = False
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
set_seeds()
|
| 18 |
+
|
| 19 |
+
model.classifier = nn.Sequential(
|
| 20 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 21 |
+
nn.Linear(in_features=1408, out_features=num_classes, bias=True)
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
torch==2.2.0.dev20230922+cu121
|
| 3 |
+
torchvision==0.17.0.dev20230925+cu121
|
| 4 |
+
gradio==3.50.2
|