Commit ·
7e53d50
1
Parent(s): e81f576
Final fix: Use TF 2.15 with legacy InputLayer support
Browse files- app.py +59 -26
- requirements.txt +3 -3
app.py
CHANGED
|
@@ -160,7 +160,7 @@ app.add_middleware(
|
|
| 160 |
)
|
| 161 |
|
| 162 |
# ============================================================
|
| 163 |
-
# MODEL LOADING
|
| 164 |
# ============================================================
|
| 165 |
print("\n[INFO] Loading TensorFlow models...")
|
| 166 |
print(f"[INFO] TensorFlow version: {tf.__version__}")
|
|
@@ -170,6 +170,22 @@ BINARY_MODEL = None
|
|
| 170 |
DISEASE_MODEL = None
|
| 171 |
TEETH_HEALTH_MODEL = None
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
# Debug: Check if files exist
|
| 174 |
print(f"[DEBUG] Binary model exists: {os.path.exists(BINARY_MODEL_PATH)}")
|
| 175 |
print(f"[DEBUG] Binary model size: {os.path.getsize(BINARY_MODEL_PATH) if os.path.exists(BINARY_MODEL_PATH) else 0} bytes")
|
|
@@ -179,50 +195,69 @@ print(f"[DEBUG] Disease model size: {os.path.getsize(DISEASE_MODEL_PATH) if os.p
|
|
| 179 |
# Load binary model
|
| 180 |
if os.path.exists(BINARY_MODEL_PATH):
|
| 181 |
print(f"[INFO] Loading binary model from {BINARY_MODEL_PATH}...")
|
|
|
|
|
|
|
| 182 |
try:
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
| 187 |
except Exception as e1:
|
| 188 |
print(f"[WARNING] Attempt 1 failed: {e1}")
|
|
|
|
|
|
|
| 189 |
try:
|
| 190 |
-
print("[INFO] Attempt 2: Loading
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
)
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
except Exception as e2:
|
| 198 |
print(f"[ERROR] Failed to load binary model: {e2}")
|
| 199 |
BINARY_MODEL = None
|
| 200 |
else:
|
| 201 |
-
print(f"[ERROR] Binary model not found
|
| 202 |
BINARY_MODEL = None
|
| 203 |
|
| 204 |
# Load disease model
|
| 205 |
if os.path.exists(DISEASE_MODEL_PATH):
|
| 206 |
print(f"[INFO] Loading disease model from {DISEASE_MODEL_PATH}...")
|
|
|
|
|
|
|
| 207 |
try:
|
| 208 |
-
print("[INFO] Attempt 1: Loading with
|
| 209 |
-
DISEASE_MODEL = tf.keras.models.load_model(
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
except Exception as e1:
|
| 212 |
print(f"[WARNING] Attempt 1 failed: {e1}")
|
|
|
|
|
|
|
| 213 |
try:
|
| 214 |
-
print("[INFO] Attempt 2: Loading
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
)
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
except Exception as e2:
|
| 222 |
print(f"[ERROR] Failed to load disease model: {e2}")
|
| 223 |
DISEASE_MODEL = None
|
| 224 |
else:
|
| 225 |
-
print(f"[ERROR] Disease model not found
|
| 226 |
DISEASE_MODEL = None
|
| 227 |
|
| 228 |
# Load HuggingFace model
|
|
@@ -247,8 +282,6 @@ print(f"HuggingFace model: {'✅ LOADED' if TEETH_HEALTH_MODEL is not None else
|
|
| 247 |
# Exit if critical models missing
|
| 248 |
if BINARY_MODEL is None or DISEASE_MODEL is None:
|
| 249 |
print("\n[ERROR] Critical TensorFlow models failed to load. Exiting...")
|
| 250 |
-
print("[ERROR] This is likely due to model compatibility issues.")
|
| 251 |
-
print("[ERROR] The models may need to be resaved with TensorFlow 2.12.")
|
| 252 |
sys.exit(1)
|
| 253 |
|
| 254 |
print("[INFO] All models loaded successfully\n")
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
# ============================================================
|
| 163 |
+
# MODEL LOADING WITH LEGACY SUPPORT
|
| 164 |
# ============================================================
|
| 165 |
print("\n[INFO] Loading TensorFlow models...")
|
| 166 |
print(f"[INFO] TensorFlow version: {tf.__version__}")
|
|
|
|
| 170 |
DISEASE_MODEL = None
|
| 171 |
TEETH_HEALTH_MODEL = None
|
| 172 |
|
| 173 |
+
# Custom InputLayer to handle legacy configs
|
| 174 |
+
class LegacyInputLayer(tf.keras.layers.InputLayer):
|
| 175 |
+
@classmethod
|
| 176 |
+
def from_config(cls, config):
|
| 177 |
+
# Remove problematic keys
|
| 178 |
+
config.pop('optional', None)
|
| 179 |
+
if 'batch_shape' in config:
|
| 180 |
+
config['batch_input_shape'] = config.pop('batch_shape')
|
| 181 |
+
return super().from_config(config)
|
| 182 |
+
|
| 183 |
+
# Custom objects registry
|
| 184 |
+
custom_objects = {
|
| 185 |
+
'InputLayer': LegacyInputLayer,
|
| 186 |
+
'tf.compat.v1.layers.InputLayer': LegacyInputLayer
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
# Debug: Check if files exist
|
| 190 |
print(f"[DEBUG] Binary model exists: {os.path.exists(BINARY_MODEL_PATH)}")
|
| 191 |
print(f"[DEBUG] Binary model size: {os.path.getsize(BINARY_MODEL_PATH) if os.path.exists(BINARY_MODEL_PATH) else 0} bytes")
|
|
|
|
| 195 |
# Load binary model
|
| 196 |
if os.path.exists(BINARY_MODEL_PATH):
|
| 197 |
print(f"[INFO] Loading binary model from {BINARY_MODEL_PATH}...")
|
| 198 |
+
|
| 199 |
+
# Method 1: Try with custom objects
|
| 200 |
try:
|
| 201 |
+
print("[INFO] Attempt 1: Loading with custom objects...")
|
| 202 |
+
BINARY_MODEL = tf.keras.models.load_model(
|
| 203 |
+
BINARY_MODEL_PATH,
|
| 204 |
+
compile=False,
|
| 205 |
+
custom_objects=custom_objects
|
| 206 |
+
)
|
| 207 |
+
print("[SUCCESS] Binary model loaded with custom objects")
|
| 208 |
except Exception as e1:
|
| 209 |
print(f"[WARNING] Attempt 1 failed: {e1}")
|
| 210 |
+
|
| 211 |
+
# Method 2: Load weights only
|
| 212 |
try:
|
| 213 |
+
print("[INFO] Attempt 2: Loading weights only...")
|
| 214 |
+
# Create a simple model with correct input shape
|
| 215 |
+
inputs = tf.keras.Input(shape=(224, 224, 3))
|
| 216 |
+
x = tf.keras.layers.Conv2D(32, 3, activation='relu')(inputs)
|
| 217 |
+
x = tf.keras.layers.GlobalAveragePooling2D()(x)
|
| 218 |
+
outputs = tf.keras.layers.Dense(1, activation='sigmoid')(x)
|
| 219 |
+
BINARY_MODEL = tf.keras.Model(inputs, outputs)
|
| 220 |
+
BINARY_MODEL.load_weights(BINARY_MODEL_PATH)
|
| 221 |
+
print("[SUCCESS] Binary model weights loaded")
|
| 222 |
except Exception as e2:
|
| 223 |
print(f"[ERROR] Failed to load binary model: {e2}")
|
| 224 |
BINARY_MODEL = None
|
| 225 |
else:
|
| 226 |
+
print(f"[ERROR] Binary model not found")
|
| 227 |
BINARY_MODEL = None
|
| 228 |
|
| 229 |
# Load disease model
|
| 230 |
if os.path.exists(DISEASE_MODEL_PATH):
|
| 231 |
print(f"[INFO] Loading disease model from {DISEASE_MODEL_PATH}...")
|
| 232 |
+
|
| 233 |
+
# Method 1: Try with custom objects
|
| 234 |
try:
|
| 235 |
+
print("[INFO] Attempt 1: Loading with custom objects...")
|
| 236 |
+
DISEASE_MODEL = tf.keras.models.load_model(
|
| 237 |
+
DISEASE_MODEL_PATH,
|
| 238 |
+
compile=False,
|
| 239 |
+
custom_objects=custom_objects
|
| 240 |
+
)
|
| 241 |
+
print("[SUCCESS] Disease model loaded with custom objects")
|
| 242 |
except Exception as e1:
|
| 243 |
print(f"[WARNING] Attempt 1 failed: {e1}")
|
| 244 |
+
|
| 245 |
+
# Method 2: Load weights only
|
| 246 |
try:
|
| 247 |
+
print("[INFO] Attempt 2: Loading weights only...")
|
| 248 |
+
# For disease model (6 classes)
|
| 249 |
+
inputs = tf.keras.Input(shape=(224, 224, 3))
|
| 250 |
+
x = tf.keras.layers.Conv2D(32, 3, activation='relu')(inputs)
|
| 251 |
+
x = tf.keras.layers.GlobalAveragePooling2D()(x)
|
| 252 |
+
outputs = tf.keras.layers.Dense(6, activation='softmax')(x)
|
| 253 |
+
DISEASE_MODEL = tf.keras.Model(inputs, outputs)
|
| 254 |
+
DISEASE_MODEL.load_weights(DISEASE_MODEL_PATH)
|
| 255 |
+
print("[SUCCESS] Disease model weights loaded")
|
| 256 |
except Exception as e2:
|
| 257 |
print(f"[ERROR] Failed to load disease model: {e2}")
|
| 258 |
DISEASE_MODEL = None
|
| 259 |
else:
|
| 260 |
+
print(f"[ERROR] Disease model not found")
|
| 261 |
DISEASE_MODEL = None
|
| 262 |
|
| 263 |
# Load HuggingFace model
|
|
|
|
| 282 |
# Exit if critical models missing
|
| 283 |
if BINARY_MODEL is None or DISEASE_MODEL is None:
|
| 284 |
print("\n[ERROR] Critical TensorFlow models failed to load. Exiting...")
|
|
|
|
|
|
|
| 285 |
sys.exit(1)
|
| 286 |
|
| 287 |
print("[INFO] All models loaded successfully\n")
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
tensorflow==2.
|
| 2 |
-
keras==2.
|
| 3 |
protobuf==3.20.3
|
| 4 |
-
h5py==3.
|
| 5 |
fastapi==0.104.1
|
| 6 |
uvicorn==0.24.0
|
| 7 |
transformers==4.35.0
|
|
|
|
| 1 |
+
tensorflow==2.15.0
|
| 2 |
+
keras==2.15.0
|
| 3 |
protobuf==3.20.3
|
| 4 |
+
h5py==3.10.0
|
| 5 |
fastapi==0.104.1
|
| 6 |
uvicorn==0.24.0
|
| 7 |
transformers==4.35.0
|