Keras: "load_model()" Error When Updating Keras 2.0

Created on 15 Mar 2017  路  15Comments  路  Source: keras-team/keras

Dear all, we are excited to see Keras has been updated to version 2.0. However, error occurred when I want to load a model that previously trained by keras.models.load_model().

 File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/models.py", line 232, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/models.py", line 293, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/layers/__init__.py", line 46, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
    list(custom_objects.items())))
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/engine/topology.py", line 2361, in from_config
    process_layer(layer_data)
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/engine/topology.py", line 2330, in process_layer
    custom_objects=custom_objects)
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/layers/__init__.py", line 46, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/utils/generic_utils.py", line 141, in deserialize_keras_object
    return cls.from_config(config['config'])
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.0-py2.7.egg/keras/engine/topology.py", line 1193, in from_config
    return cls(**config)
TypeError: __init__() got an unexpected keyword argument 'input_dtype'
stale

Most helpful comment

It turns out that in the current 2.0 version, we can load a .h5 model trained by keras 2.0. However, we could not load the model trained by keras 1.x beforehand.

All 15 comments

+1 ... we are facing the same issue here

input_dtype updated to dtype
I modified it in h5 file by hand, then it's ok.
but there's another problem if you use input_dim, you can see it in #5786

As for now, I rolled back to the version 1.2 ...

It has been fixed!

It turns out that in the current 2.0 version, we can load a .h5 model trained by keras 2.0. However, we could not load the model trained by keras 1.x beforehand.

I had the same problem and wrote a small script to convert old model files.

https://gist.github.com/mgraczyk/269714a749dab895d176600d9c9441a5

Isn't this a huge breaking change if 2.0 can't load pre 2.0 models?

Having this issue, as a temporary workaround I hacked a bit of the compatibility code in legacy.interfaces into the model.load_model code. It sucks and definitely isn't complete but I don't have enough time to get to know the code well enough to make a permanent fix right now.

Basic idea:
-check keras version on load, if it's below 2 try and repair param names before calling model_from_config
-combine all the replacements from legacy.interfaces into a single list/dict(I know there are conflicting replacements/layer specific replacements, part of the reason I didn't proceed)
-make all replacements

some sample code here, sorry if it's unclear! Works for me though:
https://gist.github.com/crimedude22/8f34876c57a829375e78de55e10e230e

This should be fixed by PR https://github.com/fchollet/keras/pull/6248

I tried updating the master but this issue still isn't resolved. Has anyone found a fix?

Weird, is it the same 'input_dtype' error? When you look at your local version of the code, does it have the change that datumbox linked to (https://github.com/fchollet/keras/commit/cd6bbe72908ad6ac144ec9a3647318381b642354)? If that's not working, the (much less elegant) hack I linked to as a gist above fixed the problem for me before the update was out.

@jchen114 If you upload a model that causes the problem, the python snippet that you use and the error message I'm happy to debug it. Just validate that you have indeed the updated version as @crimedude22 mentioned.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

I had an issue:
I tried to run
test_layer = CustomerLayer() load_model(model_path, custom_objects={'layers': test_layer})
or
load_model(model_path, custom_objects={'layers': CustomerLayer})
It always prompt error below:
TypeError: load_model() got an unexpected keyword argument 'custom_objects'

My settings are:
Ubuntu 16; python 2 with latest keras 2.0.6

I have fixed the issue by using json to initialize the model architecture.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

Was this page helpful?
0 / 5 - 0 ratings