question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

load_model() couldn't work on multipe same Custom layers

See original GitHub issue

Like what is described in #6900, The code load_model() for a single custom layer works well: model = load_model('Network.hdf5', custom_objects= {'Myclass': Myclass})

but not works for the Model which contains multipe same Custom layers, like AlexNet, which contains 3 LRN, and LRN is my custom layer.

Since I will get something like that:

File "D:/AlexNet.py", line 114, in <module>
   model = load_model('AlexNet.h5', custom_objects= {'LRN': LRN})
 File "E:\Anaconda3\lib\site-packages\keras\engine\saving.py", line 260, in load_model
   model = model_from_config(model_config, custom_objects=custom_objects)
 File "E:\Anaconda3\lib\site-packages\keras\engine\saving.py", line 334, in model_from_config
   return deserialize(config, custom_objects=custom_objects)
 File "E:\Anaconda3\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
   printable_module_name='layer')
 File "E:\Anaconda3\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object
   list(custom_objects.items())))
 File "E:\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 293, in from_config
   model.add(layer)
 File "E:\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 193, in add
   self.build()
 File "E:\Anaconda3\lib\site-packages\keras\engine\sequential.py", line 233, in build
   name=self.name)
 File "E:\Anaconda3\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network
   self.inputs, self.outputs)
 File "E:\Anaconda3\lib\site-packages\keras\engine\network.py", line 1442, in _map_graph_network
   ' times in the model. 
ValueError: The name "LRN2D" is used 2 times in the model. All layer names should be unique.

Which is caused by multipe same Custom layers Since I am working on network prune experiment, it is a little complicated to use model.load_weights(), because the model architecture is always changing. Any idea about this? Appreciation in advance!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
farizrahman4ucommented, Oct 13, 2018

Note that the recommend way to write get_config() is by first calling the base class’s get_config() and the adding items to it:

def get_config(self):
   config = super(CustomLayer, self).get_config()
    config['alpha'] = self.alpha
    config['k'] = self.k
    return config
1reaction
farizrahman4ucommented, Oct 12, 2018

This is probably an issue with the Custom Layer. Make sure that the Custom Layer class is not explicitly setting the .name attribute.

For e.g, the following code works:

from keras.layers import *
from keras.models import *

class CustomLayer(Layer):
    def __init__(self, dim=10, **kwargs):
        self.dim = dim
        super(CustomLayer, self).__init__(**kwargs)

a = Input((2,))
b = CustomLayer(3)(a)
c = CustomLayer(4')(b)
model = Model(a, c)

model.save('test.h5')
model2 = load_model('test.h5')

Following will raise ValueError: The name "CustomLayer" is used 2 times in the model. All layer names should be unique.:

from keras.layers import *
from keras.models import *

class CustomLayer(Layer):
    def __init__(self, dim=10, **kwargs):
        self.dim = dim
        super(CustomLayer, self).__init__(**kwargs)
        self.name = 'CustomLayer'   ### DONT DO THIS!

a = Input((2,))
b = CustomLayer(3)(a)
c = CustomLayer(4')(b)
model = Model(a, c)

model.save('test.h5')
model2 = load_model('test.h5')
Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving and loading a model with custom layers in tensorflow ...
When I run the code above I keep getting this error when I try to load the model: ValueError: Attempt to convert a...
Read more >
Cant properly load saved model and call predict method
hi, I am having trouble loading large model after saving. have tried ... custom layers and your “get_config” and “from_config” don't work ......
Read more >
How to Save and Load Your Keras Deep Learning Model
You can save your model by calling the save() function on the model and specifying the filename. The example below demonstrates this by...
Read more >
Training a CNN - WekaDeeplearning4j
We're going to start with a simple network, progressively adding layers and noting the effect this has on performance & model size.
Read more >
Writing ResNet from Scratch in PyTorch - Paperspace Blog
Then there are two main functions inside every custom model. First is the initialization function, __init__ , where we define the various layers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found