I'm trying to load some models that were created using an old version of keras (v2.3.1) with the latest version of the library and I'm getting a strange error. The models are saved as .h5
files.
The models are loaded/saved using code like this:
from keras.models import load_model, Sequential
# Saved like this
model = Sequential()
...
model.save('model.h5')
# Loaded like this
model = load_model('model.h5')
I've checked that when I use the same old version of keras it all still works, but when I use the latest version I get this error when calling load_model()
:
ValueError: Kernel shape must have the same length as input, but received kernel of shape (4, 4, 1, 128) and input of shape (None, None, 6, 7, 1).
To ensure compatibility it is recommended to use keras from tensorflow instead of keras stand alone. As the model was saved with an old version of keras, the solution is to load it via tensorflow with:
from tensorflow.keras.models import load_model, Sequential