koesan commited on
Commit
5e5b44c
·
verified ·
1 Parent(s): df14132

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -24,21 +24,34 @@ os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
24
  # Try loading with h5py directly for better compatibility
25
  import h5py
26
 
27
- # Load model with old TensorFlow/Keras compatibility
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  try:
29
- # Direct loading with old Keras
30
- from keras.models import load_model as keras_load_model
31
- model = keras_load_model('cancer_model.h5', compile=False)
32
- print("✓ Model loaded successfully with Keras 2.4.3!")
33
  except Exception as e:
34
- print(f"Keras loader failed: {e}")
35
- try:
36
- # Try TensorFlow loader
37
- model = tf.keras.models.load_model('cancer_model.h5', compile=False)
38
- print("✓ Model loaded successfully with TensorFlow!")
39
- except Exception as e2:
40
- print(f"Error: {e2}")
41
- raise
42
 
43
  def resize_with_padding(img, target_size):
44
  """Resize image while maintaining aspect ratio and add padding"""
 
24
  # Try loading with h5py directly for better compatibility
25
  import h5py
26
 
27
+ # Custom InputLayer to handle old 'batch_shape' parameter
28
+ from tensorflow.python.keras.engine.input_layer import InputLayer as OriginalInputLayer
29
+
30
+ class CustomInputLayer(OriginalInputLayer):
31
+ def __init__(self, **kwargs):
32
+ # Convert old 'batch_shape' to new 'input_shape'
33
+ if 'batch_shape' in kwargs:
34
+ batch_shape = kwargs.pop('batch_shape')
35
+ if batch_shape and len(batch_shape) > 1:
36
+ kwargs['input_shape'] = batch_shape[1:]
37
+ # Remove any other problematic params
38
+ kwargs.pop('batch_input_shape', None)
39
+ super(CustomInputLayer, self).__init__(**kwargs)
40
+
41
+ # Register custom layer
42
+ custom_objects = {'InputLayer': CustomInputLayer}
43
+
44
+ # Load model with custom objects
45
  try:
46
+ model = tf.keras.models.load_model('cancer_model.h5',
47
+ custom_objects=custom_objects,
48
+ compile=False)
49
+ print("✓ Model loaded successfully!")
50
  except Exception as e:
51
+ print(f"Error loading model: {e}")
52
+ print("\n⚠️ Model file is incompatible with TensorFlow 2.4.1")
53
+ print("Your model appears to be from TensorFlow 1.x or very early 2.x")
54
+ raise
 
 
 
 
55
 
56
  def resize_with_padding(img, target_size):
57
  """Resize image while maintaining aspect ratio and add padding"""