File size: 2,144 Bytes
62ca666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

import os
import tensorflow as tf
import logging

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("DebugModelLoad")

model_path = os.path.join("models", "tcn_hybrid_tuned.h5")

print(f"TF Version: {tf.__version__}")
print(f"Keras Version: {tf.keras.__version__}")

def try_load_standard():
    print("\n--- Attempting standard load_model ---")
    try:
        model = tf.keras.models.load_model(model_path)
        print("Standard load success!")
        return True
    except TypeError as e:
        print(f"Standard load failed as expected: {e}")
        return False
    except Exception as e:
        print(f"Standard load failed with unexpected error: {e}")
        return False

def try_load_with_fix():
    print("\n--- Attempting load_model with FixedInputLayer ---")
    
    class FixedInputLayer(tf.keras.layers.InputLayer):
        def __init__(self, batch_shape=None, **kwargs):
            # If batch_shape is present, convert it to batch_input_shape
            # or just pass it if the super handles it differently, 
            # but usually 'batch_solution' is the issue.
            # Keras 2.15 InputLayer expects batch_input_shape typically.
            
            if batch_shape is not None:
                # If batch_input_shape is not already set, use batch_shape
                if 'batch_input_shape' not in kwargs:
                    kwargs['batch_input_shape'] = batch_shape
                # Remove batch_shape to avoid the "Unrecognized keyword argument" error
                # if the superclass or subsequent logic doesn't like it.
            
            super().__init__(**kwargs)

    try:
        model = tf.keras.models.load_model(model_path, custom_objects={'InputLayer': FixedInputLayer})
        print("Fix load success!")
        model.summary()
        return True
    except Exception as e:
        print(f"Fix load failed: {e}")
        return False

if __name__ == "__main__":
    if not os.path.exists(model_path):
        print(f"Error: Model file not found at {model_path}")
    else:
        if not try_load_standard():
            try_load_with_fix()