Spaces:
Sleeping
Sleeping
Init push
Browse files- Examples/194643.jpg +0 -0
- Examples/27415.jpg +0 -0
- Examples/46797.jpg +0 -0
- _Deploy_effB2.pth +3 -0
- app.py +45 -0
- model.py +16 -0
- requirements.txt +3 -0
Examples/194643.jpg
ADDED
|
Examples/27415.jpg
ADDED
|
Examples/46797.jpg
ADDED
|
_Deploy_effB2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a76208f15a11b5f6f2a6fcebe2e28408e00dec0741f5af4d0cfb68fe65d14b77
|
| 3 |
+
size 31276413
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from timeit import default_timer as timer
|
| 3 |
+
from typing import Tuple, Dict
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from model import create_model
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
title = "Food Vision Mini by Edesak"
|
| 11 |
+
desc = "EffitientNetB2 for recognition of Pizza,Steak,Sushi from [Zero To Mastery Course](https://www.udemy.com/course/pytorch-for-deep-learning/)"
|
| 12 |
+
article = "My Github page [Edesak](https://github.com/Edesak)"
|
| 13 |
+
|
| 14 |
+
class_names = ["pizza", "steak", "sushi"]
|
| 15 |
+
model,transform = create_model()
|
| 16 |
+
model.load_state_dict(torch.load(f="_Deploy_effB2.pth"))
|
| 17 |
+
model.to("cpu")
|
| 18 |
+
example_list = [["Examples/" + example] for example in os.listdir("Examples")]
|
| 19 |
+
|
| 20 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 21 |
+
start_timer = timer()
|
| 22 |
+
|
| 23 |
+
img = transform(img).unsqueeze(0)
|
| 24 |
+
|
| 25 |
+
model.eval()
|
| 26 |
+
with torch.inference_mode():
|
| 27 |
+
y = torch.softmax(model(img), dim=1)
|
| 28 |
+
pred_labels = {class_names[i]: float(y[0][i]) for i in range(len(class_names))}
|
| 29 |
+
end_time = timer()
|
| 30 |
+
pred_time = round(end_time - start_timer, 4)
|
| 31 |
+
|
| 32 |
+
return pred_labels, pred_time
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def greet(name):
|
| 36 |
+
return "Hello " + name + "!"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
demo = gr.Interface(fn=predict, inputs=gr.Image(type="pil"),
|
| 40 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), gr.Number(label="Prediction time (s)")],
|
| 41 |
+
title=title,
|
| 42 |
+
description=desc,
|
| 43 |
+
article=article)
|
| 44 |
+
|
| 45 |
+
demo.launch()
|
model.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from torch.nn import Dropout, Linear
|
| 2 |
+
from torchvision.models import EfficientNet_B2_Weights, efficientnet_b2
|
| 3 |
+
import torch
|
| 4 |
+
def create_model():
|
| 5 |
+
weights = EfficientNet_B2_Weights.DEFAULT
|
| 6 |
+
model = efficientnet_b2(weights=weights)
|
| 7 |
+
transform = weights.transforms()
|
| 8 |
+
|
| 9 |
+
classifier = torch.nn.Sequential(
|
| 10 |
+
Dropout(p=0.3, inplace=True),
|
| 11 |
+
Linear(in_features=1408, out_features=3)
|
| 12 |
+
)
|
| 13 |
+
for layer in model.features:
|
| 14 |
+
layer.requires_grad_(False)
|
| 15 |
+
model.classifier = classifier
|
| 16 |
+
return model,transform
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.32.0
|
| 2 |
+
torch==1.13.0
|
| 3 |
+
torchvision==0.14.0
|