koesan commited on
Commit
8b03efe
·
verified ·
1 Parent(s): 7af3d82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -18,17 +18,28 @@ print("Loading model...")
18
  import warnings
19
  warnings.filterwarnings('ignore')
20
 
21
- # Custom loader for compatibility with different TensorFlow versions
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  try:
23
- model = load_model('cancer_model.h5', compile=False)
 
 
24
  except Exception as e:
25
- print(f"First attempt failed: {e}")
26
- print("Trying with safe mode...")
27
- # Try loading with safe mode
28
- from tensorflow import keras
29
- model = keras.models.load_model('cancer_model.h5', compile=False, safe_mode=False)
30
-
31
- print("Model loaded successfully!")
32
 
33
  def resize_with_padding(img, target_size):
34
  """Resize image while maintaining aspect ratio and add padding"""
 
18
  import warnings
19
  warnings.filterwarnings('ignore')
20
 
21
+ # Custom object scope to handle old Keras model format
22
+ from tensorflow.keras.layers import InputLayer
23
+ import tensorflow.keras.backend as K
24
+
25
+ # Patch for old model format compatibility
26
+ def custom_input_layer(*args, **kwargs):
27
+ # Convert old 'batch_shape' to new 'shape'
28
+ if 'batch_shape' in kwargs:
29
+ batch_shape = kwargs.pop('batch_shape')
30
+ if batch_shape is not None and len(batch_shape) > 1:
31
+ kwargs['shape'] = batch_shape[1:]
32
+ return InputLayer(*args, **kwargs)
33
+
34
+ # Try loading with custom objects
35
  try:
36
+ with tf.keras.utils.custom_object_scope({'InputLayer': custom_input_layer}):
37
+ model = load_model('cancer_model.h5', compile=False)
38
+ print("Model loaded successfully!")
39
  except Exception as e:
40
+ print(f"Error loading model: {e}")
41
+ print("Please ensure the model file is compatible with TensorFlow 2.13.0")
42
+ raise
 
 
 
 
43
 
44
  def resize_with_padding(img, target_size):
45
  """Resize image while maintaining aspect ratio and add padding"""