File size: 1,605 Bytes
503c7ba
 
 
 
 
 
f042ac5
420f9d2
503c7ba
f5f472c
503c7ba
 
 
 
 
656a7fb
afa7f02
656a7fb
 
afa7f02
 
 
 
503c7ba
656a7fb
afa7f02
 
503c7ba
 
 
 
 
f5f472c
503c7ba
 
 
bec246f
656a7fb
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np

# Load your custom regression model
model_path = "pokemon_transferlearning2.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()