first commit. Added all files
Browse files- .gitattributes +2 -0
- 09_pretrained_effnet_b2_feature_extractor_20%.pth +3 -0
- app.py +57 -0
- examples/2582289.jpg +0 -0
- examples/3622237.jpg +0 -0
- examples/592799.jpg +0 -0
- model.py +27 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -32,3 +32,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
09_pretrained_effnet_b2_feature_extractor_20%.pth filter=lfs diff=lfs merge=lfs -text
|
36 |
+
.pth filter=lfs diff=lfs merge=lfs -text
|
09_pretrained_effnet_b2_feature_extractor_20%.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bea198b7a2ad0730c7370bdf84475ac3183ba05a662849b4a3c13ae5c79a3a50
|
3 |
+
size 31296189
|
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
from model import create_effnet_b2_model
|
5 |
+
from timeit import default_timer as timer
|
6 |
+
from typing import List, Dict
|
7 |
+
|
8 |
+
class_names = ['pizza', 'steak', 'sushi']
|
9 |
+
|
10 |
+
effnetb2, effnetb2_transforms = create_effnet_b2_model(
|
11 |
+
num_classes=3)
|
12 |
+
|
13 |
+
#load weigths
|
14 |
+
effnetb2.load_state_dict(
|
15 |
+
torch.load(
|
16 |
+
f='09_pretrained_effnet_b2_feature_extractor_20%.pth',
|
17 |
+
map_location=torch.device('cpu')
|
18 |
+
)
|
19 |
+
)
|
20 |
+
|
21 |
+
|
22 |
+
#predict
|
23 |
+
def predict(img) -> Tuple[Dict, float]:
|
24 |
+
#start a timer
|
25 |
+
start_time = timer()
|
26 |
+
#transform input image
|
27 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
28 |
+
#set model to eval mode
|
29 |
+
effnetb2.eval()
|
30 |
+
with torch.inference_mode():
|
31 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
32 |
+
pred_labels_and_probs = {class_names[i] :float(pred_probs[0,i]) for i in \
|
33 |
+
range(len(class_names))}
|
34 |
+
end_time = timer()
|
35 |
+
pred_time = round(end_time - start_time, 4)
|
36 |
+
|
37 |
+
return pred_labels_and_probs, pred_time
|
38 |
+
|
39 |
+
|
40 |
+
examples_list = [['examples/' + example] for example in os.listdir('examples')]
|
41 |
+
examples_list
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
title = 'foodvision mini'
|
46 |
+
description = 'effnet feature extractor for image classification'
|
47 |
+
article = 'course type-along'
|
48 |
+
|
49 |
+
|
50 |
+
demo = gr.Interface(fn=predict,
|
51 |
+
inputs=gr.Image(type='pil'),
|
52 |
+
outputs = [gr.Label(num_top_classes=3,label='predictions'),
|
53 |
+
gr.Number(label='Prediction time(s)')],
|
54 |
+
examples=example_list,
|
55 |
+
title=title,
|
56 |
+
description=description,
|
57 |
+
article=article)
|
examples/2582289.jpg
ADDED
examples/3622237.jpg
ADDED
examples/592799.jpg
ADDED
model.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
def create_effnet_b2_model(num_classes : int = 3,
|
7 |
+
seed : int = 42):
|
8 |
+
|
9 |
+
effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
10 |
+
effnetb2_transforms = effnetb2_weights.transforms()
|
11 |
+
|
12 |
+
effnetb2 = torchvision.models.efficientnet_b2(weights=effnetb2_weights)
|
13 |
+
|
14 |
+
for p in effnetb2.parameters():
|
15 |
+
p.requires_grad = False
|
16 |
+
|
17 |
+
torch.manual_seed(seed)
|
18 |
+
#torch.cuda.manual_seed(seed)
|
19 |
+
effnetb2.classifier = nn.Sequential(
|
20 |
+
torch.nn.Dropout(p=0.3,
|
21 |
+
inplace=True),
|
22 |
+
torch.nn.Linear(in_features=1408,
|
23 |
+
out_features=num_classes,
|
24 |
+
bias=True)
|
25 |
+
)
|
26 |
+
|
27 |
+
return effnetb2, effnetb2_transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.0.1
|
2 |
+
torchvision==0.15.2
|
3 |
+
gradio==3.32.0
|