Keras: Got error of unknown layer by using load_model even used custom_objects

Created on 10 Aug 2017  路  2Comments  路  Source: keras-team/keras

I tried to use load_model(model, custom_objects={'MyLayer': MyLayer})
however, it came out with the error:
model = model_from_config(model_config, custom_objects=custom_objects) File " python2.7/site-packages/keras/models.py", line 309, in model_from_config return layer_module.deserialize(config, custom_objects=custom_objects) File " python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize printable_module_name='layer') File " python2.7/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object list(custom_objects.items()))) File " python2.7/site-packages/keras/models.py", line 1211, in from_config layer = layer_module.deserialize(conf, custom_objects=custom_objects) File " python2.7/site-packages/keras/layers/__init__.py", line 54, in deserialize printable_module_name='layer') File " /anaconda3/envs/proj1/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 133, in deserialize_keras_object ': ' + class_name) ValueError: Unknown layer: MyLayer
Should I define any names for the layer?

I also tried the following code, it also had the same error:
`
from keras.models import model_from_json
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
tmp_model = model_from_json(loaded_model_json, custom_objects={'attention': MyLayer()})

tmp_model.load_weights("model.h5")
`

Most helpful comment

Thanks. @xiaoleihuang I resolve it.
I think you should write like this custom_objects={'MyLayer': MyLayer()}

All 2 comments

see #6529

Thanks. @xiaoleihuang I resolve it.
I think you should write like this custom_objects={'MyLayer': MyLayer()}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amityaffliction picture amityaffliction  路  3Comments

kylemcdonald picture kylemcdonald  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments

KeironO picture KeironO  路  3Comments

anjishnu picture anjishnu  路  3Comments