philipperemy / keras-tcn

Keras Temporal Convolutional Network.
MIT License
1.87k stars 454 forks source link

Saving a loaded model gets warning and then fails to open #250

Closed NEGU93 closed 1 year ago

NEGU93 commented 1 year ago

Describe the bug Saving a loaded model gets following error message

WARNING:absl:<keras.src.saving.legacy.saved_model.load.Conv1D object at 0x7f2760744c40> has the same name 'Conv1D' as a built-in Keras object. Consider renaming <class 'keras.src.saving.legacy.saved_model.load.Conv1D'> to avoid naming conflicts when loading with `tf.keras.models.load_model`. If renaming is not possible, pass the object in the `custom_objects` parameter of the load function.

After that, trying to open the model again gets:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-d17d4952d201> in <module>
----> 1 loaded_model = load_model(SAVE_PATH)

~/.local/lib/python3.8/site-packages/keras/src/saving/saving_api.py in load_model(filepath, custom_objects, compile, safe_mode, **kwargs)
    236 
    237     # Legacy case.
--> 238     return legacy_sm_saving_lib.load_model(
    239         filepath, custom_objects=custom_objects, compile=compile, **kwargs
    240     )

~/.local/lib/python3.8/site-packages/keras/src/utils/traceback_utils.py in error_handler(*args, **kwargs)
     68             # To get the full stack trace, call:
     69             # `tf.debugging.disable_traceback_filtering()`
---> 70             raise e.with_traceback(filtered_tb) from None
     71         finally:
     72             del filtered_tb

~/.local/lib/python3.8/site-packages/keras/src/engine/base_layer.py in input_spec(self, value)
   1294         for v in tf.nest.flatten(value):
   1295             if v is not None and not isinstance(v, input_spec.InputSpec):
-> 1296                 raise TypeError(
   1297                     "Layer input_spec must be an instance of InputSpec. "
   1298                     "Got: {}".format(v)

TypeError: Layer input_spec must be an instance of InputSpec. Got: <keras.src.initializers.initializers.Zeros object at 0x7f27107741f0>

Paste a snippet

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tcn import TCN
from tensorflow.keras.models import load_model

model = Sequential([TCN(input_shape=(4, 23)), Dense(1)])
model.save(SAVE_PATH)
loaded_model = load_model(SAVE_PATH)
loaded_model.save(SAVE_PATH)     # Warning message
loaded_model = load_model(SAVE_PATH)   # TypeError
NEGU93 commented 1 year ago

I found the fix, you should do:

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
from tcn import TCN
from tensorflow.keras.models import load_model

model = Sequential([TCN(input_shape=(4, 23)), Dense(1)])
model.save(SAVE_PATH)
loaded_model = load_model(SAVE_PATH, custom_objects={'TCN': TCN})
loaded_model.save(SAVE_PATH)
loaded_model = load_model(SAVE_PATH, custom_objects={'TCN': TCN})