dogBreedVision / app.py
yussaaa's picture
lfs untrack non-binary files
0b96c2d
### 1. Imports and class names setup ###
import gradio as gr
import os
import tensorflow as tf
from timeit import default_timer as timer
from typing import Tuple, Dict
from helper import load_model
# Setup class names
with open("class_names.txt", "r") as f:
unique_breeds = [l.strip() for l in f.readlines()]
# 2. Model generation and weight
model = load_model("models/20230727-13521690480331-all-images.h5")
# 3. Prefict function
# Define image size
IMG_SIZE = 224
def process_image(image_path):
"""
Takes an image file path and turns it into a Tensor.
"""
# Read in image file
image = tf.io.read_file(image_path)
# Turn the jpeg image into numerical Tensor with 3 colour channels (Red, Green, Blue)
image = tf.image.decode_jpeg(image, channels=3)
# Convert the colour channel values from 0-225 values to 0-1 values
image = tf.image.convert_image_dtype(image, tf.float32)
# Resize the image to our desired size (224, 244)
image = tf.image.resize(image, size=[IMG_SIZE, IMG_SIZE])
return image
def predict(img) -> Tuple[Dict, float]:
"""Transforms and performs a prediction on img and returns prediction and time taken."""
# Start the timer
start_time = timer()
# Transform the target image and add a batch dimension
img = process_image(img)
img = tf.expand_dims(img, axis=0)
# Pass the transformed image through the model and turn the prediction logits into prediction probabilities
pred_probs = model(img)
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
pred_labels_and_probs = {
unique_breeds[i]: float(pred_probs[0][i]) for i in range(len(unique_breeds))
}
# Calculate the prediction time
pred_time = round(timer() - start_time, 5)
# Return the prediction dictionary and prediction time
return pred_labels_and_probs, pred_time
# 4. Gradio app
# Create title, description and article strings
title = "120 Dog Breed Vision classifier 🐶🐩🐕🐕‍🦺"
description = (
"An mobilenet feature extractor computer vision model to classify 120 dog breeds."
)
article = "Modele from [mobilenet](https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/5)."
example_list = example_list = [["examples/" + str(p)] for p in os.listdir("examples/")]
# Create the Gradio demo
demo = gr.Interface(
fn=predict, # mapping function from input to output
inputs=gr.Image(type="filepath"), # what are the inputs?
outputs=[
gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
gr.Number(label="Prediction time (s)"),
], # our fn has two outputs, therefore we have two outputs
examples=example_list,
title=title,
description=description,
article=article,
)
# Launch the demo!
demo.launch(debug=False)