abidlabs HF Staff Claude Opus 4.6 (1M context) commited on
Commit
c283188
·
1 Parent(s): 28a11f8

Fix model loading: use TFSMLayer for Keras 3 compatibility

Browse files

TFSMLayer 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>

Files changed (1) hide show
  1. app.py +13 -7
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(image):
18
- prediction = model.predict(image[tf.newaxis, ...])
19
- prediction[prediction > 0.5 ] = 1
20
- prediction[prediction !=1] = 0
21
- result = prediction[0]*255
 
 
 
 
 
 
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.models.load_model(model_path)
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):