Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,17 +12,21 @@ def predict_bmwX(image):
|
|
| 12 |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 13 |
image = image.convert("RGB") # Ensure the image is in RGB format
|
| 14 |
image = image.resize((150, 150)) # Resize the image to 150x150
|
| 15 |
-
image = np.array(image)
|
| 16 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 17 |
|
| 18 |
# Predict
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
# Apply softmax to get probabilities for each class
|
| 22 |
-
prediction = tf.nn.softmax(
|
| 23 |
|
| 24 |
# Debug statement to check the shape of the prediction
|
| 25 |
-
print(f"Prediction
|
|
|
|
| 26 |
|
| 27 |
# Define class names
|
| 28 |
class_names = ['Afghan', 'African Wild Dog', 'Airedale', 'American Hairless', 'American Spaniel', 'Basenji', 'Basset', 'Beagle',
|
|
@@ -36,22 +40,21 @@ def predict_bmwX(image):
|
|
| 36 |
'Shiba Inu', 'Shih-Tzu', 'Siberian Husky', 'Vizsla', 'Yorkie']
|
| 37 |
|
| 38 |
# Check if the number of predictions matches the number of class names
|
| 39 |
-
if len(prediction
|
| 40 |
-
return f"Error: Number of model outputs ({len(prediction
|
| 41 |
|
| 42 |
# Create a dictionary with the probabilities for each dog breed
|
| 43 |
-
prediction_dict = {class_names[i]: np.round(float(prediction[
|
| 44 |
|
| 45 |
# Sort the dictionary by value in descending order and get the top 3 classes
|
| 46 |
sorted_predictions = dict(sorted(prediction_dict.items(), key=lambda item: item[1], reverse=True))
|
| 47 |
|
| 48 |
return sorted_predictions
|
| 49 |
|
| 50 |
-
|
| 51 |
input_image = gr.Image()
|
| 52 |
iface = gr.Interface(
|
| 53 |
fn=predict_bmwX,
|
| 54 |
inputs=input_image,
|
| 55 |
outputs=gr.Label(),
|
| 56 |
-
description="A simple
|
| 57 |
iface.launch(share=True)
|
|
|
|
| 12 |
image = Image.fromarray(image.astype('uint8')) # Convert numpy array to PIL image
|
| 13 |
image = image.convert("RGB") # Ensure the image is in RGB format
|
| 14 |
image = image.resize((150, 150)) # Resize the image to 150x150
|
| 15 |
+
image = np.array(image) / 255.0 # Normalize the image
|
| 16 |
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 17 |
|
| 18 |
# Predict
|
| 19 |
+
raw_prediction = model.predict(image)
|
| 20 |
+
|
| 21 |
+
# Debug statement to check the raw prediction values
|
| 22 |
+
print(f"Raw prediction: {raw_prediction}")
|
| 23 |
|
| 24 |
# Apply softmax to get probabilities for each class
|
| 25 |
+
prediction = tf.nn.softmax(raw_prediction).numpy()[0]
|
| 26 |
|
| 27 |
# Debug statement to check the shape of the prediction
|
| 28 |
+
print(f"Prediction after softmax: {prediction}")
|
| 29 |
+
print(f"Sum of probabilities: {np.sum(prediction)}")
|
| 30 |
|
| 31 |
# Define class names
|
| 32 |
class_names = ['Afghan', 'African Wild Dog', 'Airedale', 'American Hairless', 'American Spaniel', 'Basenji', 'Basset', 'Beagle',
|
|
|
|
| 40 |
'Shiba Inu', 'Shih-Tzu', 'Siberian Husky', 'Vizsla', 'Yorkie']
|
| 41 |
|
| 42 |
# Check if the number of predictions matches the number of class names
|
| 43 |
+
if len(prediction) != len(class_names):
|
| 44 |
+
return f"Error: Number of model outputs ({len(prediction)}) does not match number of class names ({len(class_names)})."
|
| 45 |
|
| 46 |
# Create a dictionary with the probabilities for each dog breed
|
| 47 |
+
prediction_dict = {class_names[i]: np.round(float(prediction[i]), 2) for i in range(len(class_names))}
|
| 48 |
|
| 49 |
# Sort the dictionary by value in descending order and get the top 3 classes
|
| 50 |
sorted_predictions = dict(sorted(prediction_dict.items(), key=lambda item: item[1], reverse=True))
|
| 51 |
|
| 52 |
return sorted_predictions
|
| 53 |
|
|
|
|
| 54 |
input_image = gr.Image()
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=predict_bmwX,
|
| 57 |
inputs=input_image,
|
| 58 |
outputs=gr.Label(),
|
| 59 |
+
description="A simple dog breed classification model.")
|
| 60 |
iface.launch(share=True)
|