Keras: Loading models using load_model with custom layers

Created on 8 Jun 2017  路  3Comments  路  Source: keras-team/keras

I am having an issue with loading a model via load_model from an hdf5 file. The model contains custom layers, thus I needed to specify a dictionary to make this layer known to keras. But still it errors out with the following trace:

Traceback (most recent call last):
  File "Y:/Documents/Bachelor/ImageModels/weight_plots.py", line 4, in <module>
    model = load_model('AlexNet/checkpoints/checkpoint-05.hdf5', {'LRN2D': LRN2D})
  File "D:\guth\Miniconda\envs\ba_guth\lib\site-packages\keras\models.py", line 240, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "D:\guth\Miniconda\envs\ba_guth\lib\site-packages\keras\models.py", line 301, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "D:\guth\Miniconda\envs\ba_guth\lib\site-packages\keras\layers\__init__.py", line 46, in deserialize
    printable_module_name='layer')
  File "D:\guth\Miniconda\envs\ba_guth\lib\site-packages\keras\utils\generic_utils.py", line 140, in deserialize_keras_object
    list(custom_objects.items())))
  File "D:\guth\Miniconda\envs\ba_guth\lib\site-packages\keras\engine\topology.py", line 2395, in from_config
    return cls(inputs=input_tensors, outputs=output_tensors, name=name)
  File "D:\guth\Miniconda\envs\ba_guth\lib\site-packages\keras\legacy\interfaces.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "D:\guth\Miniconda\envs\ba_guth\lib\site-packages\keras\engine\topology.py", line 1726, in __init__
    ' times in the model. '
RuntimeError: The name "LRN2D" is used 6 times in the model. All layer names should be unique.

The error appears to arrise from the fact, that the custom layer is used multiple times. Surely, all layers do have different names, ranging from 'lr_n2d_1' to 'lr_n2d_6', each of them beeing a custom LRN2D layer.

I load the model like this, I am a bit unsure if I used the dictionary wrongly here:
model = load_model('AlexNet/checkpoints/checkpoint-05.hdf5', {'LRN2D': LRN2D})

Can anybody point me into the right direction or tell, if this is a bug?

stale

Most helpful comment

Hi,
I didn't solve it, but found away around. If you still have the model architecture, you can use the file produced by model.save() actually as well with the `model.load_weights()`` function. This does work with custom layers as well.

All 3 comments

Did you figure this out?

Hi,
I didn't solve it, but found away around. If you still have the model architecture, you can use the file produced by model.save() actually as well with the `model.load_weights()`` function. This does work with custom layers as well.

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