Spaces:
Runtime error
Runtime error
Commit
Β·
88891dd
1
Parent(s):
dec14ed
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
|
7 |
+
from model import create_effnet_b2
|
8 |
+
from timeit import default_timer as timer
|
9 |
+
from typing import Tuple, Dict
|
10 |
+
|
11 |
+
#setup class names
|
12 |
+
with open('class_names.txt', 'r') as f:
|
13 |
+
class_names = [food.strip() for food in f.readlines()]
|
14 |
+
|
15 |
+
|
16 |
+
#Create effnetb2 model
|
17 |
+
|
18 |
+
#create model and transforms preparation
|
19 |
+
effnetb2, effnetb2_transforms = create_effnetb2(num_classes = 101)
|
20 |
+
|
21 |
+
effnetb2.load_state_dict(
|
22 |
+
torch.load(
|
23 |
+
f='pretrained_effnetb2_feature_extractor_food101_20_percent.pth',
|
24 |
+
map_location = torch.device('cpu')))
|
25 |
+
|
26 |
+
|
27 |
+
#Predict function
|
28 |
+
def predict(img) -> Tuple[Dict, float]:
|
29 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
30 |
+
"""
|
31 |
+
# Start the timer
|
32 |
+
start_time = timer()
|
33 |
+
|
34 |
+
# Transform the target image and add a batch dimension
|
35 |
+
img = effnetb2_transforms(img).unsqueeze(0)
|
36 |
+
|
37 |
+
# Put model into evaluation mode and turn on inference mode
|
38 |
+
effnetb2.eval()
|
39 |
+
with torch.inference_mode():
|
40 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
41 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
42 |
+
|
43 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
44 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
45 |
+
|
46 |
+
# Calculate the prediction time
|
47 |
+
pred_time = round(timer() - start_time, 5)
|
48 |
+
|
49 |
+
# Return the prediction dictionary and prediction time
|
50 |
+
return pred_labels_and_probs, pred_time
|
51 |
+
|
52 |
+
### 4. Gradio app ###
|
53 |
+
|
54 |
+
# Create title, description and article strings
|
55 |
+
title = "FoodVision BIG ππ₯©π£"
|
56 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify 101 classes of food from the food 101 dataset"
|
57 |
+
|
58 |
+
# Create examples list from "examples/" directory
|
59 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
60 |
+
|
61 |
+
# Create the Gradio demo
|
62 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
63 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
64 |
+
outputs=[gr.Label(num_top_classes=5, label="Predictions"), # what are the outputs?
|
65 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
66 |
+
# Create examples list from "examples/" directory
|
67 |
+
examples=example_list,
|
68 |
+
title=title,
|
69 |
+
description=description
|
70 |
+
)
|
71 |
+
|
72 |
+
# Launch the demo!
|
73 |
+
demo.launch()
|
74 |
+
|
75 |
+
|