thomen commited on
Commit
71d89ec
1 Parent(s): ce36f5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -3,37 +3,39 @@ import tensorflow as tf
3
  import numpy as np
4
  from PIL import Image
5
 
6
-
7
- model_path = "pokemon-model_2_transferlearning.keras"
8
  model = tf.keras.models.load_model(model_path)
9
 
 
10
  def predict_pokemon(image):
11
  # Preprocess image
12
- print(type(image))
13
  image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
14
- image = image.resize((150, 150)) #resize the image to 150x150
15
  image = np.array(image)
16
- image = np.expand_dims(image, axis=0) # same as image[None, ...]
17
-
18
 
 
19
  prediction = model.predict(image)
20
 
21
  # Apply softmax to get probabilities for each class
22
  prediction = tf.nn.softmax(prediction)
23
 
24
- # Create a dictionary with the probabilities for each Pokemon
25
- evee = np.round(float(prediction[0][0]), 2)
26
- farfetched = np.round(float(prediction[0][1]), 2)
27
- graveler = np.round(float(prediction[0][2]), 2)
28
- venonta = np.round(float(prediction[0][3]), 2)
29
 
30
- return {'Evee': evee, 'Farfetched': farfetched, 'Graveler': graveler, 'Venonta': venonta}
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)
 
3
  import numpy as np
4
  from PIL import Image
5
 
6
+ # Load the model
7
+ model_path = "pokemon-predict-model_transferlearning.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
  image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
14
+ image = image.resize((150, 150)) # Resize the image to match model input size
15
  image = np.array(image)
16
+ image = np.expand_dims(image, axis=0) # Add batch dimension
 
17
 
18
+ # Predict
19
  prediction = model.predict(image)
20
 
21
  # Apply softmax to get probabilities for each class
22
  prediction = tf.nn.softmax(prediction)
23
 
24
+ # Extract class names from the provided list
25
+ class_names = ['Abra', 'Aerodactyl', 'Alakazam', 'Arbok', 'Arcanine', 'Articuno', 'Beedrill', 'Bellsprout', 'Blastoise', 'Bulbasaur', 'Butterfree', 'Caterpie', 'Chansey', 'Charizard', 'Charmander', 'Charmeleon', 'Clefable', 'Clefairy', 'Cloyster', 'Cubone', 'Dewgong', 'Diglett', 'Ditto', 'Dodrio', 'Doduo', 'Dragonair', 'Dragonite', 'Dratini', 'Drowzee', 'Dugtrio', 'Eevee', 'Ekans', 'Electabuzz', 'Electrode', 'Exeggcute', 'Exeggutor', 'Farfetchd', 'Fearow', 'Flareon', 'Gastly', 'Gengar', 'Geodude', 'Gloom', 'Golbat', 'Goldeen', 'Golduck', 'Graveler', 'Grimer', 'Growlithe', 'Gyarados', 'Haunter', 'Hitmonchan', 'Hitmonlee', 'Horsea', 'Hypno', 'Ivysaur', 'Jigglypuff', 'Jolteon', 'Jynx', 'Kabutops', 'Kadabra', 'Kakuna', 'Kangaskhan', 'Kingler', 'Koffing', 'Lapras', 'Lickitung', 'Machamp', 'Machoke', 'Machop', 'Magikarp', 'Magmar', 'Magnemite', 'Magneton', 'Mankey', 'Marowak', 'Meowth', 'Metapod', 'Mew', 'Mewtwo', 'Moltres', 'Mr. Mime', 'MrMime', 'Nidoking', 'Nidoqueen', 'Nidorina', 'Nidorino', 'Ninetales', 'Oddish', 'Omanyte', 'Omastar', 'Parasect', 'Pidgeot', 'Pidgeotto', 'Pidgey', 'Pikachu', 'Pinsir', 'Poliwag', 'Poliwhirl', 'Poliwrath', 'Ponyta', 'Porygon', 'Primeape', 'Psyduck', 'Raichu', 'Rapidash', 'Raticate', 'Rattata', 'Rhydon', 'Rhyhorn', 'Sandshrew', 'Sandslash', 'Scyther', 'Seadra', 'Seaking', 'Seel', 'Shellder', 'Slowbro', 'Slowpoke', 'Snorlax', 'Spearow', 'Squirtle', 'Starmie', 'Staryu', 'Tangela', 'Tauros', 'Tentacool', 'Tentacruel', 'Vaporeon', 'Venomoth', 'Venonat', 'Venusaur', 'Victreebel', 'Vileplume', 'Voltorb', 'Vulpix', 'Wartortle', 'Weedle', 'Weepinbell', 'Weezing', 'Wigglytuff', 'Zapdos', 'Zubat']
26
+
27
+ # Create a dictionary with the probabilities for each class
28
+ probabilities = {class_names[i]: np.round(float(prediction[0][i]), 2) for i in range(len(class_names))}
29
 
30
+ return probabilities
31
 
32
+ # Update input component to accept image uploads
33
+ input_image = gr.inputs.Image()
34
 
35
+ # Launch the interface
36
  iface = gr.Interface(
37
  fn=predict_pokemon,
38
  inputs=input_image,
39
+ outputs=gr.outputs.Label(),
40
+ description="A simple MLP classification model for Pokémon image classification.")
41
+ iface.launch(share=True)