Spaces:
Sleeping
Sleeping
Commit
•
70d64c3
1
Parent(s):
79b5796
feat: add app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### 1. Imports and class names setup ###
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import create_effnetv2_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 |
+
effnetv2, effnetv2_transforms = create_effnetv2_model(
|
18 |
+
num_classes=101, # could also use len(class_names)
|
19 |
+
)
|
20 |
+
|
21 |
+
# Load saved weights
|
22 |
+
effnetv2.load_state_dict(
|
23 |
+
torch.load(
|
24 |
+
f="effnetv2_m_food101_100_percent.pth",
|
25 |
+
map_location=torch.device("cpu"), # Huggingface offer cpu only
|
26 |
+
)
|
27 |
+
)
|
28 |
+
|
29 |
+
### 3. Predict function ###
|
30 |
+
|
31 |
+
|
32 |
+
# Create predict function
|
33 |
+
def predict(img) -> Tuple[Dict, float]:
|
34 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken."""
|
35 |
+
# Start the timer
|
36 |
+
start_time = timer()
|
37 |
+
|
38 |
+
# Transform the target image and add a batch dimension
|
39 |
+
img = effnetv2_transforms(img).unsqueeze(0)
|
40 |
+
|
41 |
+
# Put model into evaluation mode and turn on inference mode
|
42 |
+
effnetv2.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(effnetv2(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 = "🍰TasteNet-Big🍰"
|
63 |
+
description = "A computer vision model based on EfficientNetV2_M designed for the classification of food images into 101 specific classes."
|
64 |
+
|
65 |
+
# Create examples list from "examples/" directory
|
66 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
67 |
+
|
68 |
+
# Create Gradio interface
|
69 |
+
demo = gr.Interface(
|
70 |
+
fn=predict,
|
71 |
+
inputs=gr.Image(type="pil"),
|
72 |
+
outputs=[
|
73 |
+
gr.Label(num_top_classes=5, label="Predictions"),
|
74 |
+
gr.Number(label="Prediction time (s)"),
|
75 |
+
],
|
76 |
+
examples=example_list,
|
77 |
+
title=title,
|
78 |
+
description=description,
|
79 |
+
)
|
80 |
+
|
81 |
+
# Launch the app!
|
82 |
+
demo.launch()
|