keras-team/keras

Error while loading saved model

pandeyp opened this issue ยท 9 comments

I am using Keras 2.2.4 with Tensorflow 1.14.0, Python 3.6.12. While loading model from architecture and json file as show below, I am getting error given below. Please help.

with open(model_architecture_file, 'r') as f: model = model_from_json(f.read()) model.load_weights(model_weights_file)

Stacktrace:

File "/home/hadoop/run_inference.py", line 58, in
model.load_weights(model_weights_file) ##Issue here!
File "/usr/local/lib64/python3.6/site-packages/keras/engine/saving.py", line 458, in load_wrapper
return load_function(*args, **kwargs)
File "/usr/local/lib64/python3.6/site-packages/keras/engine/network.py", line 1217, in load_weights
f, self.layers, reshape=reshape)
File "/usr/local/lib64/python3.6/site-packages/keras/engine/saving.py", line 1145, in load_weights_from_hdf5_group
original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'

I had the same issue and was able to fix this by setting h5py < 3.0.0.
Looks like here was a 3.0 release of h5py recently where they changed how strings are stored/read.
https://docs.h5py.org/en/stable/whatsnew/3.0.html#what-s-new-in-h5py-3-0

Note: The h5py file I tried to load was wirtten with h5py version 2.10.0

The keras built into tensorflow has the same issue:
tensorflow/tensorflow#44467

Thanks Stephan. I was able to fix it by setting to a lower h5py version.

Thanks Stephan. I was able to fix it by setting to a lower h5py version.

hey, I have the same issue, can you tell me what command did you use in command prompt to set the h5py to lower versions?

Depends on how you installed keras in the first place. Typically it is done via pip, so the command to downgrade is:
pip install 'h5py<3.0.0'

The solution for me was not only to downgrade h5py lib, but also downgrade python version to 3.6.9 as per this answer on StackOverflow.

it works for me!

Depends on how you installed keras in the first place. Typically it is done via pip, so the command to downgrade is:
pip install 'h5py<3.0.0'

it works for me!

Has anyone got this solution to work on M1? I'm not able to downgrade h5py.

Solution:
Uninstall the original h5py module and install version 2.10. It's really helped me
pip install h5py==2.10 -i https://pypi.tuna.tsinghua.edu.cn/simple/