Spaces:
Sleeping
Sleeping
Fix model loading: use TFSMLayer for Keras 3 compatibility
Browse filesTFSMLayer handles legacy SavedModel format that load_model no longer supports in Keras 3.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
app.py
CHANGED
|
@@ -13,12 +13,18 @@ from itertools import cycle, islice
|
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
-
#Function that predicts on only 1 sample
|
| 17 |
-
def predict_sample(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return result
|
| 23 |
|
| 24 |
|
|
@@ -45,7 +51,7 @@ def create_input_image(data, visualize=False):
|
|
| 45 |
return input
|
| 46 |
|
| 47 |
model_path = snapshot_download("tareknaous/unet-visual-clustering")
|
| 48 |
-
model = tf.keras.
|
| 49 |
|
| 50 |
|
| 51 |
def get_instances(prediction, data, max_filter_size=1):
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
+
#Function that predicts on only 1 sample
|
| 17 |
+
def predict_sample(img):
|
| 18 |
+
input_tensor = tf.cast(img[tf.newaxis, ...], tf.float32)
|
| 19 |
+
output = model(input_tensor)
|
| 20 |
+
# TFSMLayer returns a dict; get the first output
|
| 21 |
+
if isinstance(output, dict):
|
| 22 |
+
prediction = list(output.values())[0].numpy()
|
| 23 |
+
else:
|
| 24 |
+
prediction = output.numpy()
|
| 25 |
+
prediction[prediction > 0.5] = 1
|
| 26 |
+
prediction[prediction != 1] = 0
|
| 27 |
+
result = prediction[0] * 255
|
| 28 |
return result
|
| 29 |
|
| 30 |
|
|
|
|
| 51 |
return input
|
| 52 |
|
| 53 |
model_path = snapshot_download("tareknaous/unet-visual-clustering")
|
| 54 |
+
model = tf.keras.layers.TFSMLayer(model_path, call_endpoint='serving_default')
|
| 55 |
|
| 56 |
|
| 57 |
def get_instances(prediction, data, max_filter_size=1):
|