Spaces:
Runtime error
Runtime error
Commit
·
13f61f5
1
Parent(s):
a9840fb
initial commit
Browse files- app.py +38 -0
- effnetb2_20_percent.pth +3 -0
- examples/2274102.jpg +0 -0
- examples/2743100.jpg +0 -0
- examples/3785667.jpg +0 -0
- model.py +20 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Tuple, Dict
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
from timeit import default_timer as timer
|
5 |
+
import PIL
|
6 |
+
import gradio as gr
|
7 |
+
from model import create_effnetb2_model
|
8 |
+
|
9 |
+
class_names = ['pizza', 'steak', 'sushi']
|
10 |
+
examples = [os.path.join('examples', img) for img in os.listdir('examples')]
|
11 |
+
|
12 |
+
model, preprocess = create_effnetb2_model(num_classes=3, seed=42)
|
13 |
+
model.load_state_dict(torch.load('effnetb2_20_percent.pth'))
|
14 |
+
|
15 |
+
def predict(img: PIL.Image) -> Tuple[Dict, float]:
|
16 |
+
|
17 |
+
start_time = timer()
|
18 |
+
|
19 |
+
img = preprocess(img).unsqueeze(dim=0)
|
20 |
+
|
21 |
+
model.to('cpu')
|
22 |
+
model.eval()
|
23 |
+
with torch.inference_mode():
|
24 |
+
probs = model(img).softmax(dim=-1).squeeze().tolist()
|
25 |
+
preds = {class_name: prob for class_name, prob in zip(class_names, probs)}
|
26 |
+
|
27 |
+
pred_time = round(timer() - start_time, 8)
|
28 |
+
|
29 |
+
return preds, pred_time
|
30 |
+
|
31 |
+
demo = gr.Interface(fn=predict,
|
32 |
+
inputs=gr.Image(type='pil'),
|
33 |
+
outputs=[gr.Label(num_top_classes=3, label='Prediction probabilities'),
|
34 |
+
gr.Number(label='Prediction time (s)')],
|
35 |
+
examples=examples,
|
36 |
+
title='FoodVision Mini 🍕🥩🍣')
|
37 |
+
|
38 |
+
demo.launch()
|
effnetb2_20_percent.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6989447fa1117723216ed076d644bfe01a69b8d688ef75e474f02ff2ba5363f4
|
3 |
+
size 31273033
|
examples/2274102.jpg
ADDED
![]() |
examples/2743100.jpg
ADDED
![]() |
examples/3785667.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
from torch import nn
|
4 |
+
from torchvision.models import EfficientNet_B2_Weights, efficientnet_b2
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes: int = 3,
|
7 |
+
seed: int = 42):
|
8 |
+
torch.manual_seed(seed)
|
9 |
+
torch.cuda.manual_seed(seed)
|
10 |
+
|
11 |
+
weights = EfficientNet_B2_Weights.DEFAULT
|
12 |
+
transforms = weights.transforms()
|
13 |
+
model = efficientnet_b2(weights=weights)
|
14 |
+
|
15 |
+
for param in model.parameters():
|
16 |
+
param.requires_grad = False
|
17 |
+
|
18 |
+
model.classifier[1] = nn.Linear(in_features=1408, out_features=num_classes)
|
19 |
+
|
20 |
+
return model, transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==3.1.4
|