Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,64 +8,78 @@ 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:
|
12 |
-
|
13 |
|
14 |
-
### 2. Model and transforms preparation ###
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
|
18 |
# Load saved weights
|
19 |
effnetb2.load_state_dict(
|
20 |
-
torch.load(
|
21 |
-
|
|
|
|
|
22 |
)
|
23 |
|
24 |
### 3. Predict function ###
|
25 |
|
|
|
26 |
def predict(img) -> Tuple[Dict, float]:
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
pred_time = round(end_time - start_time, 4)
|
45 |
|
46 |
-
# Return pred dict and pred time
|
47 |
-
return pred_labels_and_probs, pred_time
|
48 |
|
49 |
### 4. Gradio app ###
|
50 |
|
51 |
-
# Create title, description and article
|
52 |
-
title = "FoodVision
|
53 |
-
description = "An
|
54 |
-
#(https://
|
55 |
-
#article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment
|
56 |
|
57 |
-
# Create
|
58 |
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
59 |
|
60 |
-
# Create
|
61 |
-
demo = gr.Interface(
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
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 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(
|
18 |
+
num_classes=101, # could also use len(class_names)
|
19 |
+
)
|
20 |
|
21 |
# Load saved weights
|
22 |
effnetb2.load_state_dict(
|
23 |
+
torch.load(
|
24 |
+
f="09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth",
|
25 |
+
map_location=torch.device("cpu"), # load to CPU
|
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 = {
|
49 |
+
class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
|
50 |
+
}
|
|
|
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 |
|
59 |
### 4. Gradio app ###
|
60 |
|
61 |
+
# Create title, description and article strings
|
62 |
+
title = "FoodVision 101 ๐๐"
|
63 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food into [101 different classes]"
|
64 |
+
#(https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/food101_class_names.txt)."
|
65 |
+
#article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
|
66 |
|
67 |
+
# Create examples list from "examples/" directory
|
68 |
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
69 |
|
70 |
+
# Create Gradio interface
|
71 |
+
demo = gr.Interface(
|
72 |
+
fn=predict,
|
73 |
+
inputs=gr.Image(type="pil"),
|
74 |
+
outputs=[
|
75 |
+
gr.Label(num_top_classes=5, label="Predictions"),
|
76 |
+
gr.Number(label="Prediction time (s)"),
|
77 |
+
],
|
78 |
+
examples=example_list,
|
79 |
+
title=title,
|
80 |
+
description=description,
|
81 |
+
article=article,
|
82 |
+
)
|
83 |
+
|
84 |
+
# Launch the app!
|
85 |
+
demo.launch()
|