Spaces:
Runtime error
Runtime error
import gradio as gr | |
import tensorflow as tf | |
from PIL import Image | |
import numpy as np | |
# Load your custom regression model | |
model_path = "pokemon_transferlearning.keras" | |
model = tf.keras.models.load_model(model_path) | |
labels = ['Porygon', 'Seel', 'Vaporeon'] | |
# Define regression function | |
def predict_regression(image): | |
# Preprocess image | |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image | |
image = image.resize((150, 150)).convert('RGB') # Resize the image to 150x150 and convert it to RGB | |
image = np.array(image) / 255.0 # Normalize image to [0, 1] range | |
image = np.expand_dims(image, axis=0) # Add batch dimension | |
# Print statements for debugging | |
print(f"Image shape (after preprocessing): {image.shape}") | |
print(f"Image data (sample): {image[0, :5, :5, 0]}") # Print a small sample of the data for inspection | |
# Predict | |
prediction = model.predict(image) # Assuming single regression value | |
print(f"Raw model prediction: {prediction}") | |
confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))} | |
return confidences | |
# Create Gradio interface | |
input_image = gr.Image() | |
output_text = gr.Textbox(label="Predicted Pokemon") | |
interface = gr.Interface(fn=predict_regression, | |
inputs=input_image, | |
outputs=gr.Label(), | |
examples=["images/porygon.png", "images/seel.jpg", "images/vaporeon.png"], | |
description="A simple MLP classification model for Pokemon classification.") | |
interface.launch() | |