Upload the App.py file
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
### 1. Imports and class names setup ###
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
import torchvision.transforms as T
|
| 6 |
+
|
| 7 |
+
from model import FlowerClassificationModel
|
| 8 |
+
from timeit import default_timer as timer
|
| 9 |
+
from typing import Tuple, Dict
|
| 10 |
+
from data_setup import classes, model_tsfm
|
| 11 |
+
from utils import *
|
| 12 |
+
|
| 13 |
+
# Setup class names
|
| 14 |
+
#class_names = ['pizza', 'steak', 'sushi']
|
| 15 |
+
|
| 16 |
+
### 2. Model and transforms preparation ###
|
| 17 |
+
#test_tsfm = T.Compose([T.Resize((224,224)),
|
| 18 |
+
# T.ToTensor(),
|
| 19 |
+
# T.Normalize(mean=[0.485, 0.456, 0.406], # 3. A mean of [0.485, 0.456, 0.406] (across each colour channel)
|
| 20 |
+
# std=[0.229, 0.224, 0.225]) # 4. A standard deviation of [0.229, 0.224, 0.225] (across each colour channel),
|
| 21 |
+
# ])
|
| 22 |
+
|
| 23 |
+
# Create ResNet50 Model
|
| 24 |
+
flower_model = FlowerClassificationModel(num_classes=len(classes), pretrained=True)
|
| 25 |
+
|
| 26 |
+
saved_path = 'flower_model_29.pth'
|
| 27 |
+
|
| 28 |
+
print('Loading Model State Dictionary')
|
| 29 |
+
# Load saved weights
|
| 30 |
+
flower_model.load_state_dict(
|
| 31 |
+
torch.load(f=saved_path,
|
| 32 |
+
map_location=torch.device('cpu'), # load to CPU
|
| 33 |
+
)
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
print('Model Loaded ...')
|
| 37 |
+
### 3. Predict function ###
|
| 38 |
+
|
| 39 |
+
# Create predict function
|
| 40 |
+
from typing import Tuple, Dict
|
| 41 |
+
|
| 42 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 43 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
| 44 |
+
"""
|
| 45 |
+
# Start the timer
|
| 46 |
+
start_time = timer()
|
| 47 |
+
|
| 48 |
+
# Transform the target image and add a batch dimension
|
| 49 |
+
#img = get_image(img_path, model_tsfm).unsqueeze(0)
|
| 50 |
+
img = model_tsfm(image=np.array(img))["image"]
|
| 51 |
+
img = img.unsqueeze(0)
|
| 52 |
+
|
| 53 |
+
# Put model into evaluation mode and turn on inference mode
|
| 54 |
+
flower_model.eval()
|
| 55 |
+
with torch.inference_mode():
|
| 56 |
+
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
|
| 57 |
+
pred_probs = torch.softmax(flower_model(img), dim=1)
|
| 58 |
+
|
| 59 |
+
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 60 |
+
pred_labels_and_probs = {classes[i]: float(pred_probs[0][i]) for i in range(len(classes))}
|
| 61 |
+
|
| 62 |
+
# Calculate the prediction time
|
| 63 |
+
pred_time = round(timer() - start_time, 5)
|
| 64 |
+
|
| 65 |
+
# Return the prediction dictionary and prediction time
|
| 66 |
+
return pred_labels_and_probs, pred_time
|
| 67 |
+
|
| 68 |
+
### 4. Gradio App ###
|
| 69 |
+
|
| 70 |
+
# Create title, description and article strings
|
| 71 |
+
title= 'United Kingdom Flower Classification Mini 🌻🌼🌸❀💐🌷'
|
| 72 |
+
description = "An ResNet50 computer vision model to classify images of Flower Categories."
|
| 73 |
+
article = "<p>Flower Classification Created by Chukwuka </p><p style='text-align: center'><a href='https://github.com/Sylvesterchuks/dogbreed_app'>Github Repo</a></p>"
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# Create examples list from "examples/" directory
|
| 77 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 78 |
+
|
| 79 |
+
# Create the Gradio demo
|
| 80 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
| 81 |
+
inputs=gr.Image(type='pil'), # What are the inputs?
|
| 82 |
+
outputs=[gr.Label(num_top_classes=5, label="Predictions"), # what are the outputs?
|
| 83 |
+
gr.Number(label='Prediction time (s)')], # Our fn has two outputs, therefore we have two outputs
|
| 84 |
+
examples=example_list,
|
| 85 |
+
title=title,
|
| 86 |
+
description=description,
|
| 87 |
+
article=article
|
| 88 |
+
)
|
| 89 |
+
# Launch the demo
|
| 90 |
+
print('Gradio Demo Launched')
|
| 91 |
+
demo.launch()
|
| 92 |
+
|