Spaces:
Runtime error
Runtime error
update files
Browse files- .gitattributes +3 -0
- 09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth +1 -1
- app.py +39 -16
- examples/04-pizza-dad.jpg +3 -0
- model.py +20 -4
- requirements.txt +1 -1
.gitattributes
CHANGED
@@ -29,3 +29,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
29 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
30 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
31 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
29 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
30 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
31 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
32 |
+
09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*/*.jpg filter=lfs diff=lfs merge=lfs -text
|
34 |
+
examples/04-pizza-dad.jpg filter=lfs diff=lfs merge=lfs -text
|
09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 31825353
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8c834646453534d566a227d53c28ec4a6fc7a956b546c77c3e13f305de7f8e5d
|
3 |
size 31825353
|
app.py
CHANGED
@@ -1,16 +1,21 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
|
4 |
from model import create_effnetb2_model
|
5 |
from timeit import default_timer as timer
|
|
|
6 |
|
7 |
# Setup class names
|
8 |
-
with open("class_names.txt", "r") as f:
|
9 |
class_names = [food_name.strip() for food_name in f.readlines()]
|
|
|
|
|
10 |
|
11 |
# Create model
|
12 |
model, transforms = create_effnetb2_model(
|
13 |
-
num_classes=101,
|
14 |
)
|
15 |
|
16 |
# Load saved weights
|
@@ -21,26 +26,44 @@ model.load_state_dict(
|
|
21 |
)
|
22 |
)
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
start_time = timer()
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
with torch.inference_mode():
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
pred_time = round(timer() - start_time, 5)
|
|
|
|
|
35 |
return pred_labels_and_probs, pred_time
|
36 |
|
|
|
37 |
|
38 |
-
# Create
|
39 |
title = "FoodVision Big ππ"
|
40 |
-
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food into 101 different classes."
|
41 |
article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
42 |
-
example_dir = "demos/foodvision_big/examples"
|
43 |
|
|
|
|
|
|
|
|
|
44 |
demo = gr.Interface(
|
45 |
fn=predict,
|
46 |
inputs=gr.Image(type="pil"),
|
@@ -48,11 +71,11 @@ demo = gr.Interface(
|
|
48 |
gr.Label(num_top_classes=5, label="Predictions"),
|
49 |
gr.Number(label="Prediction time (s)"),
|
50 |
],
|
51 |
-
|
52 |
-
interpretation="default",
|
53 |
title=title,
|
54 |
description=description,
|
55 |
article=article,
|
56 |
)
|
57 |
|
58 |
-
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
2 |
import gradio as gr
|
3 |
+
import os
|
4 |
import torch
|
5 |
|
6 |
from model import create_effnetb2_model
|
7 |
from timeit import default_timer as timer
|
8 |
+
from typing import Tuple, Dict
|
9 |
|
10 |
# Setup class names
|
11 |
+
with open("class_names.txt", "r") as f: # reading them in from class_names.txt
|
12 |
class_names = [food_name.strip() for food_name in f.readlines()]
|
13 |
+
|
14 |
+
### 2. Model and transforms preparation ###
|
15 |
|
16 |
# Create model
|
17 |
model, transforms = create_effnetb2_model(
|
18 |
+
num_classes=101, # could also use len(class_names)
|
19 |
)
|
20 |
|
21 |
# Load saved weights
|
|
|
26 |
)
|
27 |
)
|
28 |
|
29 |
+
### 3. Predict function ###
|
30 |
+
|
31 |
+
# Create predict function
|
32 |
+
def predict(img) -> Tuple[Dict, float]:
|
33 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
34 |
+
"""
|
35 |
+
# Start the timer
|
36 |
start_time = timer()
|
37 |
+
|
38 |
+
# Transform the target image and add a batch dimension
|
39 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
40 |
+
|
41 |
+
# Put model into evaluation mode and turn on inference mode
|
42 |
+
effnetb2.eval()
|
43 |
with torch.inference_mode():
|
44 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
45 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
46 |
+
|
47 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
48 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
49 |
+
|
50 |
+
# Calculate the prediction time
|
51 |
pred_time = round(timer() - start_time, 5)
|
52 |
+
|
53 |
+
# Return the prediction dictionary and prediction time
|
54 |
return pred_labels_and_probs, pred_time
|
55 |
|
56 |
+
### 4. Gradio app ###
|
57 |
|
58 |
+
# Create title, description and article strings
|
59 |
title = "FoodVision Big ππ"
|
60 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food into [101 different classes](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/food101_class_names.txt)."
|
61 |
article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
|
|
62 |
|
63 |
+
# Create examples list from "examples/" directory
|
64 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
65 |
+
|
66 |
+
# Create Gradio interface
|
67 |
demo = gr.Interface(
|
68 |
fn=predict,
|
69 |
inputs=gr.Image(type="pil"),
|
|
|
71 |
gr.Label(num_top_classes=5, label="Predictions"),
|
72 |
gr.Number(label="Prediction time (s)"),
|
73 |
],
|
74 |
+
examples=example_list,
|
|
|
75 |
title=title,
|
76 |
description=description,
|
77 |
article=article,
|
78 |
)
|
79 |
|
80 |
+
# Launch the app!
|
81 |
+
demo.launch()
|
examples/04-pizza-dad.jpg
ADDED
Git LFS Details
|
model.py
CHANGED
@@ -1,20 +1,36 @@
|
|
|
|
1 |
import torchvision
|
2 |
|
3 |
from torch import nn
|
4 |
|
5 |
|
6 |
-
def create_effnetb2_model(num_classes:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
8 |
transforms = weights.transforms()
|
9 |
model = torchvision.models.efficientnet_b2(weights=weights)
|
10 |
|
11 |
-
# Freeze base model
|
12 |
for param in model.parameters():
|
13 |
param.requires_grad = False
|
14 |
|
15 |
-
# Change classifier head
|
|
|
16 |
model.classifier = nn.Sequential(
|
17 |
nn.Dropout(p=0.3, inplace=True),
|
18 |
nn.Linear(in_features=1408, out_features=num_classes),
|
19 |
)
|
20 |
-
|
|
|
|
1 |
+
import torch
|
2 |
import torchvision
|
3 |
|
4 |
from torch import nn
|
5 |
|
6 |
|
7 |
+
def create_effnetb2_model(num_classes:int=3,
|
8 |
+
seed:int=42):
|
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 |
+
# Create EffNetB2 pretrained weights, transforms and model
|
21 |
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
22 |
transforms = weights.transforms()
|
23 |
model = torchvision.models.efficientnet_b2(weights=weights)
|
24 |
|
25 |
+
# Freeze all layers in base model
|
26 |
for param in model.parameters():
|
27 |
param.requires_grad = False
|
28 |
|
29 |
+
# Change classifier head with random seed for reproducibility
|
30 |
+
torch.manual_seed(seed)
|
31 |
model.classifier = nn.Sequential(
|
32 |
nn.Dropout(p=0.3, inplace=True),
|
33 |
nn.Linear(in_features=1408, out_features=num_classes),
|
34 |
)
|
35 |
+
|
36 |
+
return model, transforms
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
torch==1.12.0
|
2 |
torchvision==0.13.0
|
3 |
-
gradio==3.1.4
|
|
|
1 |
torch==1.12.0
|
2 |
torchvision==0.13.0
|
3 |
+
gradio==3.1.4
|