Keras: Error while loading saved model

Created on 2 Nov 2020  路  5Comments  路  Source: keras-team/keras

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'

tensorflow

Most helpful comment

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

All 5 comments

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:
https://github.com/tensorflow/tensorflow/issues/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'

Was this page helpful?
0 / 5 - 0 ratings