Reputation: 451
I am working on a github code and there is a pre-trained Keras model saved as h5 file. On loading the file I get an error which is due to version mismatch i.e. the version of Keras while training is different from the version I am using to load it.
from keras.models import load_model
try:
model = load_model('data/cnn_model_preTrained.h5')
except IOError:
print("model file doesn't exit")
It results into this error:
Traceback (most recent call last):
File "C:\Python37\lib\site-packages\keras\models.py", line 1211, in from_config
if 'class_name' not in config[0] or config[0]['class_name'] == 'Merge':
KeyError: 0
The problem is that the version of Keras used while training is not known to me, thus I am unable to solve the version mismatch issue. Is there a way to solve this?
Upvotes: 3
Views: 1624
Reputation: 560
Besides, to avoid such error, always save the model graph in model.json. Then you can load the graph from JSON and then can use the load_weight
function.
Upvotes: 1
Reputation: 451
I got to know about the h5py package of python used to handle HDF5 binary data format or the h5 file from one of my mentors and this can be used to view the metadata of the saved model. So I tried this:
import h5py
## loading the saved model I was getting an error for
hf = h5py.File('cnn_model_preTrained.h5')
for i in hf.attrs:
print(i)
It resulted into:
keras_version
backend
model_config
training_config
Fortunately, there was the Keras version saved as metadata, so I tried printing it.
print(hf.attrs['keras_version'])
which resulted to
b'2.2.4'
And that was the Keras version required to load the pre-trained model. I installed the 2.2.4 version of Keras and my code worked like a piece of cake.
Upvotes: 2