Spaces:
Sleeping
Sleeping
Commit
·
11edf28
1
Parent(s):
2330a67
Add application file
Browse files- .gitattributes +1 -0
- 09_pretrained_effnetb2_feature_extractor_pizza20%_10epochs.pth +3 -0
- app.py +71 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +26 -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 |
+
*.psd filter=lfs diff=lfs merge=lfs -text
|
09_pretrained_effnetb2_feature_extractor_pizza20%_10epochs.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a1315edc7d6b4e0c8f633752d463e0022abf1bb77ec8a2edaa365dff11018549
|
3 |
+
size 31297210
|
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from model import create_effnetb2
|
3 |
+
from typing import Tuple, Dict
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
from time import time
|
7 |
+
import torch
|
8 |
+
import torchvision
|
9 |
+
import gradio as gr
|
10 |
+
import os
|
11 |
+
|
12 |
+
from pathlib import Path
|
13 |
+
|
14 |
+
class_names = ["pizza", "steak", "sushi"]
|
15 |
+
|
16 |
+
effnetb2 , effnetb2_transforms = create_effnetb2()
|
17 |
+
# Load weights
|
18 |
+
PATH = "09_pretrained_effnetb2_feature_extractor_pizza20%_10epochs.pth"
|
19 |
+
|
20 |
+
effnetb2.load_state_dict(torch.load(f=PATH,
|
21 |
+
map_location=torch.device('cpu')
|
22 |
+
))
|
23 |
+
effnetb2.eval()
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
def predict(img) ->Tuple[Dict, float]:
|
29 |
+
start_time = time()
|
30 |
+
|
31 |
+
img_tr = img
|
32 |
+
img_tr = effnetb2_transforms(img_tr).unsqueeze(0)
|
33 |
+
#predict
|
34 |
+
effnetb2.eval()
|
35 |
+
with torch.inference_mode():
|
36 |
+
|
37 |
+
pred_prob = torch.softmax(effnetb2(img_tr), dim=1)
|
38 |
+
|
39 |
+
|
40 |
+
pred_labesls_and_pobs ={class_names[i]:pred_prob[0][i] for i in range(len(class_names)) }
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
end_time = time()
|
46 |
+
pred_time = round(end_time - start_time,4)
|
47 |
+
return pred_labesls_and_pobs ,pred_time
|
48 |
+
|
49 |
+
|
50 |
+
example_list = [["examples/"+example for example in os.listdir("examples") ]]
|
51 |
+
|
52 |
+
# Create title, description and article strings
|
53 |
+
title = "FoodVision Classification"
|
54 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
|
55 |
+
article = "Created at [Using pre-trained model efficientnet_b2](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html)."
|
56 |
+
|
57 |
+
# Create the Gradio demo
|
58 |
+
demo = gr.Interface(fn=predict,
|
59 |
+
inputs=gr.Image(type="pil"),
|
60 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
61 |
+
gr.Number(label="Prediction time (s)")],
|
62 |
+
examples=example_list,
|
63 |
+
title=title,
|
64 |
+
description=description,
|
65 |
+
article=article)
|
66 |
+
|
67 |
+
# Launch the demo!
|
68 |
+
demo.launch(debug=False,
|
69 |
+
share=True)#72H
|
70 |
+
|
71 |
+
|
examples/2582289.jpg
ADDED
![]() |
examples/3622237.jpg
ADDED
![]() |
examples/592799.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
def create_effnetb2():
|
9 |
+
torch.manual_seed(42)
|
10 |
+
torch.cuda.manual_seed(42)
|
11 |
+
|
12 |
+
|
13 |
+
effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
14 |
+
transforms = effnetb2_weights.transforms()
|
15 |
+
model = torchvision.models.efficientnet_b2(weights=effnetb2_weights)
|
16 |
+
for param in model.parameters():
|
17 |
+
param.requires_grad = False
|
18 |
+
|
19 |
+
model.classifier = nn.Sequential(
|
20 |
+
nn.Dropout(p=0.3, inplace=True),
|
21 |
+
nn.Linear(in_features=1408,
|
22 |
+
out_features=3))
|
23 |
+
model.name ='effnetb2'
|
24 |
+
return model, transforms
|
25 |
+
|
26 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.1.2
|
2 |
+
torchvision==0.16.2
|
3 |
+
gradio==4.12.0
|