load_model() couldn't work on multipe same Custom layers
See original GitHub issueLike 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:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Note that the recommend way to write
get_config()
is by first calling the base class’sget_config()
and the adding items to it: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:
Following will raise
ValueError: The name "CustomLayer" is used 2 times in the model. All layer names should be unique.
: