GOWREESH M G commited on
Commit
94692c5
·
verified ·
1 Parent(s): dac1f53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -9,6 +9,12 @@ import tensorflow as tf
9
  from tensorflow.keras.models import load_model
10
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
11
 
 
 
 
 
 
 
12
  app = Flask(__name__)
13
 
14
  # -------------------- CONFIG --------------------
@@ -21,6 +27,35 @@ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
21
  MODEL = None
22
  LOAD_ERROR = None # Store the specific reason for failure
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  # -------------------- LOAD MODEL --------------------
25
  def init_model():
26
  global MODEL, LOAD_ERROR
@@ -30,7 +65,8 @@ def init_model():
30
  if os.path.exists(MODEL_FILE):
31
  print(f"[INIT] Model found: {MODEL_FILE}")
32
  try:
33
- MODEL = load_model(MODEL_FILE)
 
34
  print("[INIT] Model loaded successfully.")
35
  except Exception as e:
36
  print(f"[ERROR] Failed to load model: {e}")
 
9
  from tensorflow.keras.models import load_model
10
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
11
 
12
+ # Import all layers used in EfficientNet and our custom head to patch them
13
+ from tensorflow.keras.layers import (
14
+ Dense, GlobalAveragePooling2D, Dropout, Conv2D, BatchNormalization,
15
+ Activation, DepthwiseConv2D, Rescaling, ZeroPadding2D, Add, Multiply, InputLayer
16
+ )
17
+
18
  app = Flask(__name__)
19
 
20
  # -------------------- CONFIG --------------------
 
27
  MODEL = None
28
  LOAD_ERROR = None # Store the specific reason for failure
29
 
30
+ # -------------------- COMPATIBILITY FIX --------------------
31
+ # This function dynamically creates a fixed version of any Keras layer
32
+ # that ignores the Keras 3 specific arguments (like quantization_config)
33
+ # allowing models saved in new versions to load in older environments.
34
+ def fix_layer_config(cls):
35
+ class FixedLayer(cls):
36
+ def __init__(self, *args, **kwargs):
37
+ # Remove Keras 3 arguments not supported in Keras 2
38
+ kwargs.pop('quantization_config', None)
39
+ kwargs.pop('glitch_filter', None)
40
+ super().__init__(*args, **kwargs)
41
+ return FixedLayer
42
+
43
+ # Apply the fix to all layers likely to appear in EfficientNet
44
+ CUSTOM_OBJECTS = {
45
+ 'Dense': fix_layer_config(Dense),
46
+ 'Dropout': fix_layer_config(Dropout),
47
+ 'GlobalAveragePooling2D': fix_layer_config(GlobalAveragePooling2D),
48
+ 'Conv2D': fix_layer_config(Conv2D),
49
+ 'BatchNormalization': fix_layer_config(BatchNormalization),
50
+ 'Activation': fix_layer_config(Activation),
51
+ 'DepthwiseConv2D': fix_layer_config(DepthwiseConv2D),
52
+ 'Rescaling': fix_layer_config(Rescaling),
53
+ 'ZeroPadding2D': fix_layer_config(ZeroPadding2D),
54
+ 'Add': fix_layer_config(Add),
55
+ 'Multiply': fix_layer_config(Multiply),
56
+ 'InputLayer': fix_layer_config(InputLayer)
57
+ }
58
+
59
  # -------------------- LOAD MODEL --------------------
60
  def init_model():
61
  global MODEL, LOAD_ERROR
 
65
  if os.path.exists(MODEL_FILE):
66
  print(f"[INIT] Model found: {MODEL_FILE}")
67
  try:
68
+ # We pass the custom_objects dictionary to handle the version mismatch
69
+ MODEL = load_model(MODEL_FILE, custom_objects=CUSTOM_OBJECTS)
70
  print("[INIT] Model loaded successfully.")
71
  except Exception as e:
72
  print(f"[ERROR] Failed to load model: {e}")