Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- .gitattributes +1 -35
- 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth +3 -0
- __pycache__/model.cpython-311.pyc +0 -0
- app.py +81 -0
- examples/1844723.jpg +0 -0
- examples/39461.jpg +0 -0
- examples/61656.jpg +0 -0
- model.py +34 -0
- requirements.txt +3 -0
.gitattributes
CHANGED
@@ -1,35 +1 @@
|
|
1 |
-
|
2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
-
*.xz 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
|
|
|
1 |
+
09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
09_pretrained_effnetb2_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:63fb57aadb1617dffc3ddcf292e2b1a01ff32e9ca31e54af3aac63ffe066d8be
|
3 |
+
size 31313869
|
__pycache__/model.cpython-311.pyc
ADDED
Binary file (1.7 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
### 1. Imports and class names setup ###
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
import torch
|
6 |
+
|
7 |
+
from model import create_effnetb2_model
|
8 |
+
from timeit import default_timer as timer
|
9 |
+
from typing import Tuple, Dict
|
10 |
+
|
11 |
+
# Setup class names
|
12 |
+
class_names = ["pizza", "steak", "sushi"]
|
13 |
+
|
14 |
+
### 2. Model and transforms preparation ###
|
15 |
+
|
16 |
+
# Create EffNetB2 model
|
17 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
18 |
+
num_classes=3, # len(class_names) would also work
|
19 |
+
)
|
20 |
+
|
21 |
+
os.chdir(r"C:\\Users\\bogdz\\OneDrive\\Máy tính\\LearnPytorch\\09_Model_Deploying\\demos\\foodvision_mini")
|
22 |
+
|
23 |
+
# Load saved weights
|
24 |
+
effnetb2.load_state_dict(
|
25 |
+
torch.load(
|
26 |
+
f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
|
27 |
+
map_location=torch.device("cpu"), # load to CPU
|
28 |
+
)
|
29 |
+
)
|
30 |
+
|
31 |
+
### 3. Predict function ###
|
32 |
+
|
33 |
+
# Create predict function
|
34 |
+
def predict(img) -> Tuple[Dict, float]:
|
35 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
36 |
+
"""
|
37 |
+
# Start the timer
|
38 |
+
start_time = timer()
|
39 |
+
|
40 |
+
# Transform the target image and add a batch dimension
|
41 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
42 |
+
|
43 |
+
# Put model into evaluation mode and turn on inference mode
|
44 |
+
effnetb2.eval()
|
45 |
+
with torch.inference_mode():
|
46 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
47 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
48 |
+
|
49 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
50 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
51 |
+
|
52 |
+
# Calculate the prediction time
|
53 |
+
pred_time = round(timer() - start_time, 5)
|
54 |
+
|
55 |
+
# Return the prediction dictionary and prediction time
|
56 |
+
return pred_labels_and_probs, pred_time
|
57 |
+
|
58 |
+
### 4. Gradio app ###
|
59 |
+
|
60 |
+
# Create title, description and article strings
|
61 |
+
title = "FoodVision Mini 🍕🥩🍣"
|
62 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
|
63 |
+
article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
64 |
+
|
65 |
+
# Create examples list from "examples/" directory
|
66 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
67 |
+
|
68 |
+
# Create the Gradio demo
|
69 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
70 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
71 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
|
72 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
73 |
+
# Create examples list from "examples/" directory
|
74 |
+
examples=example_list,
|
75 |
+
title=title,
|
76 |
+
description=description,
|
77 |
+
article=article)
|
78 |
+
|
79 |
+
# Launch the demo!
|
80 |
+
demo.launch(debug = False,
|
81 |
+
share = True)
|
examples/1844723.jpg
ADDED
![]() |
examples/39461.jpg
ADDED
![]() |
examples/61656.jpg
ADDED
![]() |
model.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
from torch import nn
|
5 |
+
|
6 |
+
def create_effnetb2_model(num_classes: int = 3,
|
7 |
+
seeds: int = 42):
|
8 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
9 |
+
|
10 |
+
Args:
|
11 |
+
num_classes (int, optional): number of classes in the classifier head.
|
12 |
+
Defaults to 3.
|
13 |
+
seed (int, optional): random seed value. Defaults to 42.
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
17 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
18 |
+
"""
|
19 |
+
|
20 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
21 |
+
transforms = weights.transforms()
|
22 |
+
|
23 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
24 |
+
|
25 |
+
for params in model.parameters():
|
26 |
+
params.requires_grad = False
|
27 |
+
|
28 |
+
model.classifier = nn.Sequential(
|
29 |
+
nn.Dropout(p=0.3, inplace=True),
|
30 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
31 |
+
)
|
32 |
+
|
33 |
+
return model, transforms
|
34 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch==1.12.0
|
2 |
+
torchvision==0.13.0
|
3 |
+
gradio==3.1.4
|