Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pickle | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import os | |
import PIL | |
import tensorflow as tf | |
from tensorflow import keras | |
from tensorflow.keras import layers | |
from keras.models import load_model | |
img_height, img_width = 180, 180 | |
# Load the model without compilation | |
model_flower = keras.models.load_model('model_flower.h5', compile=False) | |
# Recompile the model with valid arguments | |
model_flower.compile( | |
optimizer='adam', | |
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True, reduction='sum_over_batch_size'), | |
metrics=['accuracy'] | |
) | |
class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips'] | |
def predict_image(img): | |
img_resized = tf.image.resize(img, (img_height, img_width)) | |
img_array = np.expand_dims(img_resized, axis=0) # Add batch dimension | |
prediction = model_flower.predict(img_array)[0] | |
return {class_names[i]: float(prediction[i]) for i in range(5)} | |
image = gr.Image(image_mode='RGB') | |
label = gr.Label(num_top_classes=5) | |
gr.Interface(fn=predict_image, inputs=image, outputs=label).launch() | |