Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
model_path = "Pokemon_transfer_learning.keras"
|
| 6 |
-
model = tf.keras.models.load_model(model_path)
|
| 7 |
-
# Define the core prediction function
|
| 8 |
-
def predict_pokemon(image):
|
| 9 |
-
# Preprocess image
|
| 10 |
-
print(type(image))
|
| 11 |
-
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 12 |
-
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
| 13 |
-
image = np.array(image)
|
| 14 |
-
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 15 |
-
|
| 16 |
-
# Predict
|
| 17 |
-
prediction = model.predict(image)
|
| 18 |
-
|
| 19 |
-
# Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability
|
| 20 |
-
# we could also change the output layer to dense(1, activation='sigmoid')
|
| 21 |
-
prediction = np.round(prediction, 2)
|
| 22 |
-
# Separate the probabilities for each class
|
| 23 |
-
p_abra = prediction[0][0] # Probability for class 'abra'
|
| 24 |
-
p_beedrill = prediction[0][1] # Probability for class '
|
| 25 |
-
p_sandshrew = prediction[0][2] # Probability for class '
|
| 26 |
-
return {'abra': p_abra, 'beedrill': p_beedrill, 'sandshrew': p_sandshrew}
|
| 27 |
-
# Create the Gradio interface
|
| 28 |
-
input_image = gr.Image()
|
| 29 |
-
iface = gr.Interface(
|
| 30 |
-
fn=predict_pokemon,
|
| 31 |
-
inputs=input_image,
|
| 32 |
-
outputs=gr.Label(),
|
| 33 |
-
examples=["images/abra1.png", "images/abra2.jpg", "images/abra3.png", "images/beedrill1.png", "images/beedrill2.png", "images/beedrill3.jpg", "images/sandshrew1.png", "images/sandshrew2.jpg", "images/sandshrew3.png"],
|
| 34 |
-
description="A simple mlp classification model for image classification using the mnist dataset.")
|
| 35 |
iface.launch(share=True)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
model_path = "Pokemon_transfer_learning.keras"
|
| 6 |
+
model = tf.keras.models.load_model(model_path)
|
| 7 |
+
# Define the core prediction function
|
| 8 |
+
def predict_pokemon(image):
|
| 9 |
+
# Preprocess image
|
| 10 |
+
print(type(image))
|
| 11 |
+
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 12 |
+
image = image.resize((150, 150)) #resize the image to 28x28 and converts it to gray scale
|
| 13 |
+
image = np.array(image)
|
| 14 |
+
image = np.expand_dims(image, axis=0) # same as image[None, ...]
|
| 15 |
+
|
| 16 |
+
# Predict
|
| 17 |
+
prediction = model.predict(image)
|
| 18 |
+
|
| 19 |
+
# Because the output layer was dense(0) without an activation function, we need to apply sigmoid to get the probability
|
| 20 |
+
# we could also change the output layer to dense(1, activation='sigmoid')
|
| 21 |
+
prediction = np.round(prediction, 2)
|
| 22 |
+
# Separate the probabilities for each class
|
| 23 |
+
p_abra = prediction[0][0] # Probability for class 'abra'
|
| 24 |
+
p_beedrill = prediction[0][1] # Probability for class 'beedrill'
|
| 25 |
+
p_sandshrew = prediction[0][2] # Probability for class 'sandshrew'
|
| 26 |
+
return {'abra': p_abra, 'beedrill': p_beedrill, 'sandshrew': p_sandshrew}
|
| 27 |
+
# Create the Gradio interface
|
| 28 |
+
input_image = gr.Image()
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=predict_pokemon,
|
| 31 |
+
inputs=input_image,
|
| 32 |
+
outputs=gr.Label(),
|
| 33 |
+
examples=["images/abra1.png", "images/abra2.jpg", "images/abra3.png", "images/beedrill1.png", "images/beedrill2.png", "images/beedrill3.jpg", "images/sandshrew1.png", "images/sandshrew2.jpg", "images/sandshrew3.png"],
|
| 34 |
+
description="A simple mlp classification model for image classification using the mnist dataset.")
|
| 35 |
iface.launch(share=True)
|