Spaces:
Sleeping
Sleeping
initial commit
Browse files- .gitattributes +1 -0
- 09_effnetb2_sushi_steak_pizza_20.pth +3 -0
- app.py +59 -0
- examples/167716.jpg +0 -0
- examples/482858.jpg +0 -0
- examples/595836.jpg +0 -0
- model.py +21 -0
- requirements.txt +3 -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_effnetb2_sushi_steak_pizza_20.pth filter=lfs diff=lfs merge=lfs -text
|
09_effnetb2_sushi_steak_pizza_20.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1089e94d426a33e77384b11f0af45279e289b7ccd747c968f8efbf026aaf8885
|
| 3 |
+
size 31287545
|
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 8 |
+
# Setup class names
|
| 9 |
+
class_names = ["pizza", "steak", "sushi"]
|
| 10 |
+
|
| 11 |
+
# Model and transforms
|
| 12 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
| 13 |
+
num_classes=3,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Load saved weights
|
| 17 |
+
effnetb2.load_state_dict(
|
| 18 |
+
torch.load(f="09_effnetb2_sushi_steak_pizza_20.pth", map_location=("cpu")) # load the model to the CPU
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# prediction function
|
| 22 |
+
def predict(img) -> tuple:
|
| 23 |
+
start_time = timer()
|
| 24 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
| 25 |
+
effnetb2.eval()
|
| 26 |
+
with torch.inference_mode():
|
| 27 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
| 28 |
+
|
| 29 |
+
pred_labesl_and_probs = {
|
| 30 |
+
class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
|
| 31 |
+
}
|
| 32 |
+
end_time = timer()
|
| 33 |
+
pred_time = round(end_time - start_time, 4)
|
| 34 |
+
return pred_labesl_and_probs, pred_time
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# gradio app
|
| 38 |
+
title = "FoodVision Mini 🍕🥩🍣"
|
| 39 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images as pizza, steak, sushi."
|
| 40 |
+
article = "Created at 09 PyTorch Model Deployment."
|
| 41 |
+
|
| 42 |
+
# create example list
|
| 43 |
+
foodvision_min_examples_path = "examples"
|
| 44 |
+
example_list = [
|
| 45 |
+
[os.path.join(foodvision_min_examples_path, file)]
|
| 46 |
+
for file in os.listdir(foodvision_min_examples_path)
|
| 47 |
+
if file.lower().endswith((".jpg", ".jpeg", ".png"))
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
demo = gr.Interface(
|
| 51 |
+
fn=predict,
|
| 52 |
+
inputs=gr.Image(type="pil"),
|
| 53 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), gr.Number(label="Prediction time (s)")],
|
| 54 |
+
title=title,
|
| 55 |
+
description=description,
|
| 56 |
+
article=article,
|
| 57 |
+
examples=example_list
|
| 58 |
+
)
|
| 59 |
+
demo.launch(share=False, server_name="0.0.0.0", debug=False)
|
examples/167716.jpg
ADDED
|
examples/482858.jpg
ADDED
|
examples/595836.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torchvision
|
| 2 |
+
from torch import nn
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def create_effnetb2_model(num_classes: int = 3):
|
| 6 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 7 |
+
# Get EffNetB2 transforms
|
| 8 |
+
transforms = weights.transforms()
|
| 9 |
+
|
| 10 |
+
# Setup pretrained model instance
|
| 11 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
| 12 |
+
|
| 13 |
+
# Freeze the base layers in the model ( this will stop all layers from training)
|
| 14 |
+
for param in model.parameters():
|
| 15 |
+
param.requires_grad = False
|
| 16 |
+
|
| 17 |
+
model.classifier = nn.Sequential(
|
| 18 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 19 |
+
nn.Linear(in_features=1408, out_features=num_classes, bias=True),
|
| 20 |
+
)
|
| 21 |
+
return model, transforms
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.11.0
|
| 2 |
+
torchvision==0.26.0
|
| 3 |
+
gradio==6.12.0
|