mrdbourke commited on
Commit
d2a03f2
โ€ข
1 Parent(s): a7a4593

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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:
12
+ class_names = [food_name.strip() for food_name in f.readlines()]
13
+
14
+ ### 2. Model and transforms preparation ###
15
+ # Create model and transforms
16
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=101)
17
+
18
+ # Load saved weights
19
+ effnetb2.load_state_dict(
20
+ torch.load(f="09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth",
21
+ map_location=torch.device("cpu")) # load to CPU
22
+ )
23
+
24
+ ### 3. Predict function ###
25
+
26
+ def predict(img) -> Tuple[Dict, float]:
27
+ # Start a timer
28
+ start_time = timer()
29
+
30
+ # Transform the input image for use with EffNetB2
31
+ img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index
32
+
33
+ # Put model into eval mode, make prediction
34
+ effnetb2.eval()
35
+ with torch.inference_mode():
36
+ # Pass transformed image through the model and turn the prediction logits into probaiblities
37
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
38
+
39
+ # Create a prediction label and prediction probability dictionary
40
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
41
+
42
+ # Calculate pred time
43
+ end_time = timer()
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 BIG ๐Ÿ”๐Ÿ‘๐Ÿ’ช"
53
+ description = "An [EfficientNetB2 feature extractor](https://pytorch.org/vision/stable/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2) computer vision model to classify images [101 classes of food from the Food101 dataset](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/food101_class_names.txt)."
54
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/#11-turning-our-foodvision-big-model-into-a-deployable-app)."
55
+
56
+ # Create example list
57
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
58
+
59
+ # Create the Gradio demo
60
+ demo = gr.Interface(fn=predict, # maps inputs to outputs
61
+ inputs=gr.Image(type="pil"),
62
+ outputs=[gr.Label(num_top_classes=5, label="Predictions"),
63
+ gr.Number(label="Prediction time (s)")],
64
+ examples=example_list,
65
+ title=title,
66
+ description=description,
67
+ article=article)
68
+
69
+ # Launch the demo!
70
+ demo.launch()