Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- 09_pretrained_effnetb3_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- app.py +51 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +37 -0
- requirements.txt +3 -0
09_pretrained_effnetb3_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:7bedd1070c97d5d240622557659a5ebb6d46638cd2aafa553edeab6e661803f0
|
3 |
+
size 43397490
|
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
import os
|
5 |
+
|
6 |
+
from model import model_efficientb3
|
7 |
+
from timeit import default_timer as Timer
|
8 |
+
from typing import Tuple,Dict
|
9 |
+
|
10 |
+
class_name=["pizza","steak","sushi"]
|
11 |
+
|
12 |
+
effnetb3,effentb3_tranform=model_efficientb3(out_feature=3)
|
13 |
+
|
14 |
+
effnetb3.load_state_dict(
|
15 |
+
torch.load(
|
16 |
+
f="09_pretrained_effnetb3_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 |
+
|
23 |
+
start_time=Timer()
|
24 |
+
|
25 |
+
img=effnetb3_tranform(img).unsqueeze(0)
|
26 |
+
|
27 |
+
effnetb3.eval()
|
28 |
+
with torch.inference_mode():
|
29 |
+
pred_probs=torch.softmax(effnetb3(img),dim=1)
|
30 |
+
|
31 |
+
pred_labels_and_probs={class_name[i]: float(pred_probs[0][i]) for i in range(len(class_name))}
|
32 |
+
|
33 |
+
pred_time=round(Timer()-start_time,5)
|
34 |
+
|
35 |
+
return pred_labels_and_probs,pred_time
|
36 |
+
|
37 |
+
|
38 |
+
title="FoodVision Mini 🍕🥩🍣"
|
39 |
+
description= "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
|
40 |
+
article="tryin to learn pytorch"
|
41 |
+
|
42 |
+
demo=gr.Interface(fn=predict,
|
43 |
+
inputs=gr.Image(type="pil"),
|
44 |
+
outputs=[gr.Label(num_top_classes=3,label="Prediction"),
|
45 |
+
gr.Number(label="Prediction time (s)")],
|
46 |
+
examples=example_list,
|
47 |
+
title=title,
|
48 |
+
description=description,
|
49 |
+
article=article)
|
50 |
+
|
51 |
+
demo.launch()
|
examples/2582289.jpg
ADDED
![]() |
examples/3622237.jpg
ADDED
![]() |
examples/592799.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torchvision
|
3 |
+
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
|
7 |
+
def model_efficientb3(out_feature:int=3,
|
8 |
+
p:int=0.3):
|
9 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
num_classes (int, optional): number of classes in the classifier head.
|
13 |
+
Defaults to 3.
|
14 |
+
seed (int, optional): random seed value. Defaults to 42.
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
18 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
19 |
+
"""
|
20 |
+
|
21 |
+
weights=torchvision.models.EfficientNet_B3_Weights.DEFAULT
|
22 |
+
transform=weights.transforms()
|
23 |
+
model=torchvision.models.efficientnet_b3(weights=weights)
|
24 |
+
|
25 |
+
for params in model.parameters():
|
26 |
+
params.requires_grad=False
|
27 |
+
|
28 |
+
print(model.classifier)
|
29 |
+
|
30 |
+
model.classifier=nn.Sequential(
|
31 |
+
nn.Dropout(p=p,inplace=True),
|
32 |
+
nn.Linear(in_features=1536,out_features=out_feature,bias=True)
|
33 |
+
)
|
34 |
+
|
35 |
+
print(f"the new classifier as per your request \n {model.classifier}")
|
36 |
+
|
37 |
+
return model,transform
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.2.0
|
2 |
+
torchvision==0.17.0
|
3 |
+
gradio==4.20.1
|