Sa3d100 commited on
Commit
f79327c
β€’
1 Parent(s): 142ffcc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
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
+ with open('class_names.txt','r') as f:
11
+ class_names = [food.strip() for food in f.readlines()]
12
+
13
+ effnetb2 , effnetb2_transforms = create_effnetb2_model(num_classes=101)
14
+
15
+ effnetb2.load_state_dict(torch.load(f='09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth',
16
+ map_location= torch.device('cpu')))
17
+
18
+ def predict(img) -> Tuple[Dict,float]:
19
+
20
+ start_time = timer()
21
+
22
+ img = effnetb2_transforms(img).unsqueeze(0)
23
+
24
+ effnetb2.eval()
25
+ with torch.inference_mode():
26
+ pred_prob = torch.softmax(effnetb2(img),dim=1)
27
+ pred_labels_and_probs = {class_names[i]:float(pred_prob[0][i])for i in range(len(class_names))}
28
+ end_time = timer()
29
+ pred_time = round(end_time - start_time,4)
30
+ return pred_labels_and_probs,pred_time
31
+
32
+
33
+
34
+
35
+ # Create title, description and article strings
36
+ title = "FoodVision Big πŸ”πŸ‘"
37
+ 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)."
38
+ example_list = [['examples'+example] for example in os.listdir('examples') ]
39
+
40
+ # Create the Gradio demo
41
+ demo = gr.Interface(fn=predict, # mapping function from input to output
42
+ inputs=gr.Image(type="pil"), # what are the inputs?
43
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
44
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
45
+ examples=example_list,
46
+ title=title,
47
+ description=description,
48
+ )
49
+
50
+ # Launch the demo!
51
+ demo.launch() # generate a publically shareable URL?