Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import requests
|
|
| 2 |
import gradio as gr
|
| 3 |
import logging
|
| 4 |
import json
|
| 5 |
-
import
|
| 6 |
import tensorflow_hub as hub
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
|
@@ -32,8 +32,7 @@ def load_model():
|
|
| 32 |
|
| 33 |
# Define custom objects dictionary
|
| 34 |
custom_objects = {
|
| 35 |
-
'KerasLayer': hub.KerasLayer
|
| 36 |
-
'BatchNormalization': tf.keras.layers.BatchNormalization,
|
| 37 |
# Add more custom objects if needed
|
| 38 |
}
|
| 39 |
|
|
@@ -41,11 +40,11 @@ def load_model():
|
|
| 41 |
try:
|
| 42 |
logger.info("Attempting to load model with custom objects...")
|
| 43 |
with tf.keras.utils.custom_object_scope(custom_objects):
|
| 44 |
-
model =
|
| 45 |
except Exception as e:
|
| 46 |
logger.error(f"Failed to load with custom objects: {str(e)}")
|
| 47 |
logger.info("Attempting to load model without custom objects...")
|
| 48 |
-
model =
|
| 49 |
|
| 50 |
# Verify model loaded correctly
|
| 51 |
if model is None:
|
|
@@ -72,25 +71,12 @@ except Exception as e:
|
|
| 72 |
|
| 73 |
def preprocess_image(image):
|
| 74 |
try:
|
| 75 |
-
# Convert to numpy array if needed
|
| 76 |
-
if isinstance(image, Image.Image):
|
| 77 |
-
image = np.array(image)
|
| 78 |
-
|
| 79 |
# Log image shape and type for debugging
|
| 80 |
logger.info(f"Input image shape: {image.shape}, dtype: {image.dtype}")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
image = np.stack((image,) * 3, axis=-1)
|
| 86 |
-
elif len(image.shape) == 3 and image.shape[2] == 4: # RGBA image
|
| 87 |
-
logger.info("Converting RGBA to RGB")
|
| 88 |
-
image = image[:, :, :3]
|
| 89 |
-
|
| 90 |
-
# Resize image to match model's expected input shape
|
| 91 |
-
target_size = (224, 224) # Change this to match your model's input size
|
| 92 |
-
image = tf.image.resize(image, target_size)
|
| 93 |
-
logger.info(f"Resized image shape: {image.shape}")
|
| 94 |
|
| 95 |
# Normalize pixel values
|
| 96 |
image = image / 255.0
|
|
@@ -125,7 +111,7 @@ def gradio_interface(patient_info, image):
|
|
| 125 |
logger.info("Running model prediction")
|
| 126 |
prediction = model.predict(processed_image)
|
| 127 |
logger.info(f"Raw prediction shape: {prediction.shape}")
|
| 128 |
-
|
| 129 |
# Format prediction results
|
| 130 |
image_analysis = {
|
| 131 |
"prediction": float(prediction[0][0]),
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import logging
|
| 4 |
import json
|
| 5 |
+
import tf_keras
|
| 6 |
import tensorflow_hub as hub
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
|
|
|
| 32 |
|
| 33 |
# Define custom objects dictionary
|
| 34 |
custom_objects = {
|
| 35 |
+
'KerasLayer': hub.KerasLayer
|
|
|
|
| 36 |
# Add more custom objects if needed
|
| 37 |
}
|
| 38 |
|
|
|
|
| 40 |
try:
|
| 41 |
logger.info("Attempting to load model with custom objects...")
|
| 42 |
with tf.keras.utils.custom_object_scope(custom_objects):
|
| 43 |
+
model = tf_keras.models.load_model(model_path, compile=False)
|
| 44 |
except Exception as e:
|
| 45 |
logger.error(f"Failed to load with custom objects: {str(e)}")
|
| 46 |
logger.info("Attempting to load model without custom objects...")
|
| 47 |
+
model = tf_keras.models.load_model(model_path, compile=False)
|
| 48 |
|
| 49 |
# Verify model loaded correctly
|
| 50 |
if model is None:
|
|
|
|
| 71 |
|
| 72 |
def preprocess_image(image):
|
| 73 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# Log image shape and type for debugging
|
| 75 |
logger.info(f"Input image shape: {image.shape}, dtype: {image.dtype}")
|
| 76 |
|
| 77 |
+
image = image.convert('rgb')
|
| 78 |
+
image = image.resize((256, 256, 3))
|
| 79 |
+
image = np.array(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Normalize pixel values
|
| 82 |
image = image / 255.0
|
|
|
|
| 111 |
logger.info("Running model prediction")
|
| 112 |
prediction = model.predict(processed_image)
|
| 113 |
logger.info(f"Raw prediction shape: {prediction.shape}")
|
| 114 |
+
logger.info(f"Prediction: {prediction}")
|
| 115 |
# Format prediction results
|
| 116 |
image_analysis = {
|
| 117 |
"prediction": float(prediction[0][0]),
|