Dogs / app.py
Nineylia's picture
Upload 14 files
49e8388 verified
raw
history blame contribute delete
No virus
1.77 kB
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
model_path = "Dog_transfer_learning_NASNetLarge.keras"
model = tf.keras.models.load_model(model_path)
# Define the core prediction function
def predict_dog(image):
# Preprocess image
print(type(image))
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
image = np.array(image)
image = np.expand_dims(image, axis=0) # same as image[None, ...]
# Predict
prediction = model.predict(image)
# No need to apply sigmoid, as the output layer already uses softmax
# Convert the probabilities to rounded values
prediction = np.round(prediction, 3)
# Separate the probabilities for each class
p_husky = prediction[0][0] # Probability for class 'articuno'
p_pomeranian = prediction[0][1] # Probability for class 'moltres'
p_rottwiler = prediction[0][2] # Probability for class 'zapdos'
p_shiba = prediction[0][3] # Probability for class 'zapdos'
return {'husky': p_husky, 'pomeranian': p_pomeranian, 'rottwiler': p_rottwiler, 'shiba': p_shiba}
# Create the Gradio interface
input_image = gr.Image()
iface = gr.Interface(
fn=predict_dog,
inputs=input_image,
outputs=gr.Label(),
examples=["images/husky_1.jpg", "images/husky_2.jpg", "images/husky_3.jpg", "images/pomeranian_1.jpg", "images/pomeranian_2.jpg", "images/pomeranian_3.jpg", "images/rottwiler_1.jpg", "images/rottwiler_2.jpg", "images/rottwiler_3.jpg", "images/shiba_1.jpg", "images/shiba_2.jpg", "images/shiba_3.jpg"],
description="TEST.")
iface.launch()