bauckluc commited on
Commit
57403af
·
verified ·
1 Parent(s): d907f4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -1,39 +1,39 @@
1
  import gradio as gr
2
  import tensorflow as tf
3
- from PIL import Image
4
  import numpy as np
 
5
 
6
- # Load your custom regression model
7
- model_path = "pokemon_transferlearning2.keras"
8
- model = tf.keras.models.load_model(model_path)
9
 
10
- labels = ['Porygon', 'Seel', 'Vaporeon']
 
11
 
12
- # Define regression function
13
- def predict_regression(image):
14
  # Preprocess image
 
15
  image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
16
- image = image.resize((150, 150)).convert('RGB') # Resize the image to 150x150 and convert it to RGB
17
- image = np.array(image) / 255.0 # Normalize image to [0, 1] range
18
- image = np.expand_dims(image, axis=0) # Add batch dimension
19
-
20
- # Print statements for debugging
21
- print(f"Image shape (after preprocessing): {image.shape}")
22
- print(f"Image data (sample): {image[0, :5, :5, 0]}") # Print a small sample of the data for inspection
23
-
24
  # Predict
25
- prediction = model.predict(image) # Assuming single regression value
26
- print(f"Raw model prediction: {prediction}")
 
 
 
 
 
 
 
 
 
27
 
28
- confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
29
- return confidences
30
 
31
- # Create Gradio interface
32
  input_image = gr.Image()
33
- output_text = gr.Textbox(label="Predicted Pokemon")
34
- interface = gr.Interface(fn=predict_regression,
35
- inputs=input_image,
36
- outputs=gr.Label(),
37
- examples=["images/porygon.png", "images/seel.jpg", "images/vaporeon.png"],
38
- description="A simple MLP classification model for Pokemon classification.")
39
- interface.launch()
 
1
  import gradio as gr
2
  import tensorflow as tf
 
3
  import numpy as np
4
+ from PIL import Image
5
 
 
 
 
6
 
7
+ model_path = "pokemon-transferlearning2.keras"
8
+ model = tf.keras.models.load_model(model_path)
9
 
10
+ # Define the core prediction function
11
+ def predict_pokemon(image):
12
  # Preprocess image
13
+ print(type(image))
14
  image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
15
+ image = image.resize((150, 150)) #resize the image to 150x150
16
+ image = np.array(image)
17
+ image = np.expand_dims(image, axis=0) # same as image[None, ...]
18
+
 
 
 
 
19
  # Predict
20
+ prediction = model.predict(image)
21
+
22
+ # Apply softmax to get probabilities for each class
23
+ prediction = tf.nn.softmax(prediction)
24
+
25
+ # Create a dictionary with the probabilities for each Pokemon
26
+ porygon = np.round(float(prediction[0][0]), 2)
27
+ seel = np.round(float(prediction[0][1]), 2)
28
+ vaperon = np.round(float(prediction[0][2]), 2)
29
+
30
+ return {'Porygon': porygon, 'Seel': seel, 'Vaperon': vaperon}
31
 
 
 
32
 
 
33
  input_image = gr.Image()
34
+ iface = gr.Interface(
35
+ fn=predict_pokemon,
36
+ inputs=input_image,
37
+ outputs=gr.Label(),
38
+ description="A simple mlp classification model for image classification using the mnist dataset.")
39
+ iface.launch(share=True)