Spaces:
Sleeping
Sleeping
initial commit
Browse files- .gitattributes +1 -0
- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20percent.pth +3 -0
- app.py +79 -0
- examples/1001116.jpg +0 -0
- examples/1523026.jpg +0 -0
- examples/1683426.jpg +0 -0
- model.py +37 -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 |
+
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20percent.pth filter=lfs diff=lfs merge=lfs -text
|
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20percent.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4aff8750b47940bc957d7ab187889f5ff6fece8890d2b9b1f47bfc9f0eb50129
|
3 |
+
size 31314042
|
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
### 1. Import and class names setup ###
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
from pathlib import Path
|
6 |
+
import torch
|
7 |
+
|
8 |
+
from model import create_effnetb2_model
|
9 |
+
from time import perf_counter
|
10 |
+
from typing import Tuple, Dict
|
11 |
+
|
12 |
+
from PIL import Image
|
13 |
+
import torchvision
|
14 |
+
|
15 |
+
# Setup class names (hardcoded, these shall reside in a json file or sth like that...)
|
16 |
+
class_names = ["pizza","steak","sushi"]
|
17 |
+
|
18 |
+
### 2. Model and transforms preparation ###
|
19 |
+
effnetb2_model, effnetb2_transforms = create_effnetb2_model(num_classes=len(class_names))
|
20 |
+
|
21 |
+
# Load save weights
|
22 |
+
effnetb2_model.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20percent.pth",
|
23 |
+
map_location=torch.device("cpu"))) # map location to cpu is a must, as we have trained our model in the GPU
|
24 |
+
|
25 |
+
### 3. Predict function
|
26 |
+
|
27 |
+
def predict(img) -> Tuple[Dict,float]:
|
28 |
+
# Start a timer
|
29 |
+
start_time = perf_counter()
|
30 |
+
|
31 |
+
# Transform the input image for use with EffNetB2
|
32 |
+
effnetb2_transforms = torchvision.models.EfficientNet_B2_Weights.DEFAULT.transforms()
|
33 |
+
img_tensor = effnetb2_transforms(img)
|
34 |
+
|
35 |
+
# Put model in eval and inference
|
36 |
+
effnetb2_model.eval()
|
37 |
+
with torch.inference_mode():
|
38 |
+
y_logits = effnetb2_model(img_tensor.unsqueeze(dim=0))
|
39 |
+
y_pred_probs = torch.softmax(y_logits,dim=1)
|
40 |
+
y_pred_probs_list = y_pred_probs.squeeze().tolist()
|
41 |
+
|
42 |
+
# Creatae a prediction probability dictionary
|
43 |
+
pred_prob_dict = {class_names[i]:float(prob) for i,prob in enumerate(y_pred_probs_list)}
|
44 |
+
|
45 |
+
# End timer
|
46 |
+
end_time = perf_counter()
|
47 |
+
|
48 |
+
return pred_prob_dict, round(end_time-start_time,4)
|
49 |
+
|
50 |
+
|
51 |
+
### 4. Launch app
|
52 |
+
|
53 |
+
import gradio as gr
|
54 |
+
|
55 |
+
foodvision_mini_examples_path = "examples"
|
56 |
+
|
57 |
+
example_list = [str(path) for path in foodvision_mini_examples_path.rglob("*.jpg")]
|
58 |
+
|
59 |
+
# Create title, description and article
|
60 |
+
title = "FoodVisionMini V0 πππ£"
|
61 |
+
description = "An <a href='https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2'>EfficientNetB2</a> feature extractor computer vision model to classify images into pizza, steak or sushi<br>I have yet to improve it to label non-food images. Paciencia amigos"
|
62 |
+
article = "Created at <a href='#'>09_pytorch_model_deploy.ipynb</a> Google Colab notebook"
|
63 |
+
|
64 |
+
# Create the Gradio demo
|
65 |
+
demo = gr.Interface(fn=predict,
|
66 |
+
inputs=gr.Image(type="pil"),
|
67 |
+
outputs=[gr.Label(num_top_classes=3, label="predictions"),
|
68 |
+
gr.Number(label="Prediction time (s)")],
|
69 |
+
examples=example_list,
|
70 |
+
title=title,
|
71 |
+
description=description,
|
72 |
+
article=article)
|
73 |
+
|
74 |
+
# Launch the demo
|
75 |
+
demo.launch(debug=False,# print errors locally in local colab debug=False
|
76 |
+
share=True) # run on public url
|
77 |
+
|
78 |
+
|
79 |
+
# *** IMPORTANTE: The Flag button of the interface will create a folder named "flagged" that will contain the images and predictions of those images that someone has Flagged***
|
examples/1001116.jpg
ADDED
examples/1523026.jpg
ADDED
examples/1683426.jpg
ADDED
model.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
from torch import nn
|
5 |
+
from torchvision.models._api import WeightsEnum
|
6 |
+
from torch.hub import load_state_dict_from_url
|
7 |
+
|
8 |
+
def create_effnetb2_model(num_classes:int=3, seed:int=42):
|
9 |
+
# https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2
|
10 |
+
|
11 |
+
def get_state_dict(self, *args, **kwargs):
|
12 |
+
kwargs.pop("check_hash")
|
13 |
+
return load_state_dict_from_url(self.url, *args, **kwargs)
|
14 |
+
WeightsEnum.get_state_dict = get_state_dict
|
15 |
+
|
16 |
+
|
17 |
+
# 1. Setup pretrained EffNetB2 weights
|
18 |
+
effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT # DEFAULT = BEST
|
19 |
+
|
20 |
+
# 2. Get EffNetB2 transforms
|
21 |
+
effnetb2_transforms = effnetb2_weights.transforms()
|
22 |
+
|
23 |
+
# 3. Setup pretrained model instance
|
24 |
+
effnetb2_model = torchvision.models.efficientnet_b2(weights=effnetb2_weights)
|
25 |
+
|
26 |
+
# 4. Freeze the base layers in the model
|
27 |
+
for param in effnetb2_model.features.parameters():
|
28 |
+
param.requires_grad = False
|
29 |
+
|
30 |
+
# 5. Modify the classifier
|
31 |
+
torch.manual_seed(seed)
|
32 |
+
effnetb2_model.classifier = nn.Sequential(
|
33 |
+
nn.Dropout(p=0.3, inplace=True),
|
34 |
+
nn.Linear(in_features=1408,out_features=num_classes,bias=True)
|
35 |
+
)
|
36 |
+
|
37 |
+
return effnetb2_model, effnetb2_transforms
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
gradio
|