Update app.py
Browse files
app.py
CHANGED
|
@@ -3,14 +3,16 @@ import numpy as np
|
|
| 3 |
import tensorflow as tf
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
#
|
| 7 |
MODEL_PATH = "cats-vs-dogs-finetuned.keras"
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
# Load model
|
| 11 |
model = tf.keras.models.load_model(MODEL_PATH)
|
| 12 |
|
| 13 |
def predict_image(img: Image.Image):
|
|
|
|
| 14 |
if img is None:
|
| 15 |
return {"Cat": 0.5, "Dog": 0.5}
|
| 16 |
|
|
@@ -20,7 +22,7 @@ def predict_image(img: Image.Image):
|
|
| 20 |
x = np.expand_dims(x, 0) # (1, H, W, 3)
|
| 21 |
|
| 22 |
# Predict: model outputs shape (1,1) with sigmoid for "Dog" probability
|
| 23 |
-
p_dog = float(model.predict(x
|
| 24 |
return {"Cat": 1.0 - p_dog, "Dog": p_dog}
|
| 25 |
|
| 26 |
demo = gr.Interface(
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Downloaded the image classification model we trained in lab 6.1 and uploaded it to the huggingface space
|
| 7 |
MODEL_PATH = "cats-vs-dogs-finetuned.keras"
|
| 8 |
+
# the input size the model expects to see
|
| 9 |
+
IMAGE_SIZE = (180, 180)
|
| 10 |
|
| 11 |
+
# Load model
|
| 12 |
model = tf.keras.models.load_model(MODEL_PATH)
|
| 13 |
|
| 14 |
def predict_image(img: Image.Image):
|
| 15 |
+
#if there is no input image, the probability of cat and dog is 50-50
|
| 16 |
if img is None:
|
| 17 |
return {"Cat": 0.5, "Dog": 0.5}
|
| 18 |
|
|
|
|
| 22 |
x = np.expand_dims(x, 0) # (1, H, W, 3)
|
| 23 |
|
| 24 |
# Predict: model outputs shape (1,1) with sigmoid for "Dog" probability
|
| 25 |
+
p_dog = float(model.predict(x)[0, 0])
|
| 26 |
return {"Cat": 1.0 - p_dog, "Dog": p_dog}
|
| 27 |
|
| 28 |
demo = gr.Interface(
|