kaiku03 commited on
Commit
10707d2
·
verified ·
1 Parent(s): 3691d6c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnet_b1
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Setup class names (Hard Coded)
11
+ class_names = ['bengal', 'domestic_shorthair', 'maine_coon', 'ragdoll', 'siamese']
12
+
13
+ ### 2. Model and transforms preparation ###
14
+
15
+ # Create EffNetB1 model
16
+ effnet_b1, effnet_b1_transforms = create_effnet_b1()
17
+
18
+ # Load saved weights
19
+ effnet_b1.load_state_dict(
20
+ torch.load(
21
+ f="effnet_b1.pth",
22
+ map_location=torch.device("cpu"), # load to CPU
23
+ )
24
+ )
25
+
26
+
27
+ ### 3. Predict function ###
28
+
29
+ # Create predict function
30
+ def predict(img) -> Tuple[Dict, float]:
31
+ """Transforms and performs a prediction on img and returns prediction and time taken.
32
+ """
33
+ # Start the timer
34
+ start_time = timer()
35
+
36
+ # Transform the target image and add a batch dimension
37
+ img = effnetb0_transforms(img).unsqueeze(0)
38
+
39
+ # Put model into evaluation mode and turn on inference mode
40
+ effnetb0.eval()
41
+ with torch.inference_mode():
42
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
43
+ pred_probs = torch.softmax(effnetb0(img), dim=1)
44
+
45
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
46
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
47
+
48
+ # Calculate the prediction time
49
+ pred_time = round(timer() - start_time, 5)
50
+
51
+ # Return the prediction dictionary and prediction time
52
+ return pred_labels_and_probs, pred_time
53
+
54
+
55
+ ### 4. Gradio app ###
56
+
57
+ # Create title, description and article strings
58
+ title = "Cat Breed Classifier"
59
+ description = "This application is designed to categorize cat breeds and is limited to only Bengal, Domestic Shorthair, Maine Coon, Ragdoll, and Siamese breeds."
60
+ article = "Created by kaiku meoowww 😻 "
61
+
62
+
63
+
64
+ # Create examples list from "examples/" directory
65
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
66
+
67
+ # Create the Gradio demo
68
+ demo = gr.Interface(fn=predict, # mapping function from input to output
69
+ inputs=gr.Image(type="pil"), # what are the inputs?
70
+ outputs=[gr.Label(num_top_classes=5, label="Predictions"), # what are the outputs?
71
+ gr.Number(label="Prediction time (s)")],
72
+ # our fn has two outputs, therefore we have two outputs
73
+ # Create examples list from "examples/" directory
74
+ examples=example_list,
75
+ title=title,
76
+ description=description,
77
+ article=article)
78
+
79
+ # Launch the demo!
80
+ demo.launch()