HBlackwood commited on
Commit
a1609c3
Β·
verified Β·
1 Parent(s): 22610b2

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -39
app.py CHANGED
@@ -1,4 +1,4 @@
1
- ### 1. Imports and class names setup
2
  import gradio as gr
3
  import os
4
  import torch
@@ -8,62 +8,70 @@ from timeit import default_timer as timer
8
  from typing import Tuple, Dict
9
 
10
  # Setup class names
11
- class_names = ['pizza', 'steak', 'sushi']
12
 
13
- ### 2. Model and transforms preparation
14
- effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
 
 
 
 
15
 
16
  # Load saved weights
17
  effnetb2.load_state_dict(
18
  torch.load(
19
  f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
20
- map_location=torch.device("cpu") # load the model to the CPU
21
  )
22
  )
23
- ### Predict function
24
-
25
- def predict(img) -> Tuple[Dict, float]:
26
- # Start a timer
27
- start_time = timer()
28
 
29
- # Transform the input image for use with EffNetB2
30
- img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th index.
31
 
32
- # put model into eval mode, make predictions
33
- effnetb2.eval()
34
- with torch.inference_mode():
35
- # Pass transformed image through model and turn the prediction logits into probabilities
36
- pred_probs = torch.softmax(effnetb2(img), dim=1)
37
-
38
- # Create a prediction label and prediction probability dictionary
 
 
 
 
 
 
 
 
 
 
39
  pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
40
-
41
- # Calculate pred time
42
- end_time = timer()
43
- pred_time = round(end_time - start_time, 4)
44
-
45
- # Return pred dict and pred time
46
  return pred_labels_and_probs, pred_time
47
 
48
- ## 4. Gradio app
49
 
50
- # Create title, description and article.
51
  title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
52
- 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 as pizza, steak or sushi."
53
- article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/#74-building-a-gradio-interface)."
54
 
55
- # Create example list
56
- example_list = [["examples/" + example] for exmple in os.listdir("examples")]
57
 
58
  # Create the Gradio demo
59
- demo = gr.Interface(fn=predict, # maps inputs to outputs
60
- inputs=gr.Image(type="pil"),
61
- outputs=[gr.Label(num_top_classes=3, label="Predictions"),
62
- gr.Number(label="Prediction time (s)")],
63
- examples=example_list,
64
- title=title,
65
- description=description,
66
- article=article)
 
67
 
68
  # Launch the demo!
69
  demo.launch()
 
1
+ ### 1. Imports and class names setup ###
2
  import gradio as gr
3
  import os
4
  import torch
 
8
  from typing import Tuple, Dict
9
 
10
  # Setup class names
11
+ class_names = ["pizza", "steak", "sushi"]
12
 
13
+ ### 2. Model and transforms preparation ###
14
+
15
+ # Create EffNetB2 model
16
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
17
+ num_classes=3, # len(class_names) would also work
18
+ )
19
 
20
  # Load saved weights
21
  effnetb2.load_state_dict(
22
  torch.load(
23
  f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
24
+ map_location=torch.device("cpu"), # load to CPU
25
  )
26
  )
 
 
 
 
 
27
 
28
+ ### 3. Predict function ###
 
29
 
30
+ # Create predict function
31
+ def predict(img) -> Tuple[Dict, float]:
32
+ """Transforms and performs a prediction on img and returns prediction and time taken.
33
+ """
34
+ # Start the timer
35
+ start_time = timer()
36
+
37
+ # Transform the target image and add a batch dimension
38
+ img = effnetb2_transforms(img).unsqueeze(0)
39
+
40
+ # Put model into evaluation mode and turn on inference mode
41
+ effnetb2.eval()
42
+ with torch.inference_mode():
43
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
44
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
45
+
46
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
47
  pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
48
+
49
+ # Calculate the prediction time
50
+ pred_time = round(timer() - start_time, 5)
51
+
52
+ # Return the prediction dictionary and prediction time
 
53
  return pred_labels_and_probs, pred_time
54
 
55
+ ### 4. Gradio app ###
56
 
57
+ # Create title, description and article strings
58
  title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
59
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
60
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
61
 
62
+ # Create examples list from "examples/" directory
63
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
64
 
65
  # Create the Gradio demo
66
+ demo = gr.Interface(fn=predict, # mapping function from input to output
67
+ inputs=gr.Image(type="pil"), # what are the inputs?
68
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
69
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
70
+ # Create examples list from "examples/" directory
71
+ examples=example_list,
72
+ title=title,
73
+ description=description,
74
+ article=article)
75
 
76
  # Launch the demo!
77
  demo.launch()