koesan commited on
Commit
16f2ce8
·
verified ·
1 Parent(s): 8b03efe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -13,33 +13,34 @@ app.config['UPLOAD_FOLDER'] = 'uploads'
13
  # Create uploads folder if it doesn't exist
14
  os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
15
 
16
- # Load the model with custom objects to handle compatibility
17
  print("Loading model...")
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"""
 
13
  # Create uploads folder if it doesn't exist
14
  os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
15
 
16
+ # Load the model with compatibility handling
17
  print("Loading model...")
18
  import warnings
19
  warnings.filterwarnings('ignore')
20
 
21
+ # Set TensorFlow to use less memory
22
+ os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
 
23
 
24
+ # Try loading with h5py directly for better compatibility
25
+ import h5py
 
 
 
 
 
 
26
 
 
27
  try:
28
+ # Use legacy loader which is more compatible
29
+ from tensorflow.keras.saving import legacy
30
+ model = legacy.load_model.load_model('cancer_model.h5', compile=False)
31
+ print("Model loaded successfully with legacy loader!")
32
  except Exception as e:
33
+ print(f"Legacy loader failed: {e}")
34
+ try:
35
+ # Fallback: try standard loader
36
+ model = load_model('cancer_model.h5', compile=False)
37
+ print("Model loaded successfully!")
38
+ except Exception as e2:
39
+ print(f"Standard loader also failed: {e2}")
40
+ print("\nPlease re-save your model using:")
41
+ print(" model.save('cancer_model.h5', save_format='h5')")
42
+ print("Or convert to SavedModel format for better compatibility")
43
+ raise
44
 
45
  def resize_with_padding(img, target_size):
46
  """Resize image while maintaining aspect ratio and add padding"""