hb-setosys commited on
Commit
68ae99e
·
verified ·
1 Parent(s): 9ca4f01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -2,19 +2,24 @@ import gradio as gr
2
  import tensorflow as tf
3
  import numpy as np
4
  from tensorflow.keras.preprocessing import image
 
5
  from PIL import Image
6
 
7
  # Load the trained model
8
  MODEL_PATH = "setosys_dogs_model.h5"
9
  model = tf.keras.models.load_model(MODEL_PATH)
10
 
11
- # Image preprocessing function
 
 
 
 
12
  def preprocess_image(img: Image.Image) -> np.ndarray:
13
  """Preprocess the image to match the model's input requirements."""
14
  img = img.resize((224, 224)) # Resize image to model input size
15
- img_array = image.img_to_array(img)
 
16
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
17
- img_array /= 255.0 # Normalize pixel values
18
  return img_array
19
 
20
  # Prediction function
@@ -24,15 +29,9 @@ def predict_dog_breed(img: Image.Image) -> dict:
24
  predictions = model.predict(img_array)
25
  class_idx = np.argmax(predictions) # Index of the highest prediction probability
26
  confidence = float(np.max(predictions)) # Confidence score
27
-
28
- # Get class labels from the model
29
- class_labels = model.classes_ if hasattr(model, 'classes_') else None
30
 
31
- # If class labels are available, return the predicted breed with confidence score
32
- if class_labels is not None:
33
- predicted_breed = class_labels[class_idx]
34
- else:
35
- predicted_breed = "Unknown"
36
 
37
  return {predicted_breed: confidence}
38
 
 
2
  import tensorflow as tf
3
  import numpy as np
4
  from tensorflow.keras.preprocessing import image
5
+ from tensorflow.keras.applications.efficientnet_v2 import EfficientNetV2S, preprocess_input
6
  from PIL import Image
7
 
8
  # Load the trained model
9
  MODEL_PATH = "setosys_dogs_model.h5"
10
  model = tf.keras.models.load_model(MODEL_PATH)
11
 
12
+ # Ideally, you would have access to train_generator's class_indices, or you can load them manually
13
+ # Example if you manually define class labels
14
+ class_labels = ["Labrador Retriever", "German Shepherd", "Golden Retriever", "Bulldog", "Poodle"] # Adjust with actual labels
15
+
16
+ # Image preprocessing function using EfficientNetV2S
17
  def preprocess_image(img: Image.Image) -> np.ndarray:
18
  """Preprocess the image to match the model's input requirements."""
19
  img = img.resize((224, 224)) # Resize image to model input size
20
+ img_array = np.array(img)
21
+ img_array = EfficientNetV2S.preprocess_input(img_array) # EfficientNetV2 preprocessing
22
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
 
23
  return img_array
24
 
25
  # Prediction function
 
29
  predictions = model.predict(img_array)
30
  class_idx = np.argmax(predictions) # Index of the highest prediction probability
31
  confidence = float(np.max(predictions)) # Confidence score
 
 
 
32
 
33
+ # Get predicted breed and its confidence score
34
+ predicted_breed = class_labels[class_idx] if class_idx < len(class_labels) else "Unknown"
 
 
 
35
 
36
  return {predicted_breed: confidence}
37