Spaces:
Sleeping
Sleeping
Commit
·
7dd34f4
1
Parent(s):
c5a2efd
first commit author
Browse files- .gitattributes +1 -0
- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- app.py +46 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +20 -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_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:eff07ee6a9faf1b1cbaf25837bd5990025f46ac083ea629919de57c82a86c157
|
3 |
+
size 31314554
|
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradios 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 |
+
class_names = ["pizza", "steak", "sushi"]
|
11 |
+
|
12 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(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 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
24 |
+
effnetb2.eval()
|
25 |
+
with torch.inference_mode():
|
26 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
27 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
28 |
+
pred_time = round(timer() - start_time, 5)
|
29 |
+
return pred_labels_and_probs, pred_time
|
30 |
+
|
31 |
+
title = "FoodVision Mini 🍕🥩🍣"
|
32 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
|
33 |
+
article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
34 |
+
|
35 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
36 |
+
|
37 |
+
demo = gr.Interface(fn=predict,
|
38 |
+
inputs=gr.Image(type="pil"),
|
39 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
40 |
+
gr.Number(label="Prediction time (s)")],
|
41 |
+
examples=example_list,
|
42 |
+
title=title,
|
43 |
+
description=description,
|
44 |
+
article=article)
|
45 |
+
|
46 |
+
demo.launch()
|
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 |
+
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes:int=3,
|
7 |
+
seed:int=42):
|
8 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
9 |
+
transforms = weights.transforms()
|
10 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
11 |
+
|
12 |
+
for param in model.parameters():
|
13 |
+
param.requires_grad = False
|
14 |
+
|
15 |
+
torch.manual_seed(seed)
|
16 |
+
model.classifier = nn.Sequential(
|
17 |
+
nn.Dropout(p=0.3, inplace=True),
|
18 |
+
nn.Linear(in_features=1408, out_features=num_classes)
|
19 |
+
)
|
20 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
gradio
|