keras: ValueError: Unable to create group (Name already exists) with model.save_weights()
This is a similar issue to https://github.com/keras-team/keras/issues/6005 but I believe it is caused by the way h5py
defines groups. In particular, if a layer named foo
is in a network after a layer named foo/bar
, h5py
throws an exception. But the same does not occur if foo
comes first. To reproduce, see the snippet below.
from keras import layers, models
# This raises an exception.
input_layer = layers.Input((None, None, 3), name='test_input')
x = layers.Conv2D(1, 1, name='conv1/conv')(input_layer)
x = layers.BatchNormalization(name='conv1/bn')(x)
x = layers.Activation('relu', name='conv1')(x)
models.Model(inputs=input_layer, outputs=x).save_weights('test.h5')
# This doesn't raise an exception
input_layer = layers.Input((None, None, 3), name='test_input')
x = layers.Conv2D(1, 1, name='conv1')(input_layer)
x = layers.BatchNormalization(name='conv1/bn')(x)
x = layers.Activation('relu', name='conv1/relu')(x)
models.Model(inputs=input_layer, outputs=x).save_weights('test.h5')
Perhaps we could provide a more helpful error message in keras/engine/saving.py
? For example, changing part of save_weights_to_hdf5_group
to the following would help trace the offending layer name.
for layer in layers:
try:
g = group.create_group(layer.name)
except ValueError:
raise ValueError('An error occurred creating weights group for {0}.'.format(layer.name))
symbolic_weights = layer.weights
weight_values = K.batch_get_value(symbolic_weights)
Happy to create PR if this is helpful.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 9
- Comments: 17 (3 by maintainers)
Links to this issue
Commits related to this issue
- Fix group naming in keras/engine/saving.py (#12195) 1. Sort model layers before groups creation — committed to Tbuhet/keras by thibault-buhet 5 years ago
- Add test test_saving_group_naming_h5py in keras/tests/test_model_saving.py (#12195) 1. Add test for h5py group_name fix (a group name couldn't be prefix to an existing group name) — committed to Tbuhet/keras by thibault-buhet 5 years ago
Hello, I also have this issue with tensorflow 2.0.0 If some of you still want to use the .h5 format, I’ve found a potential fix. Since the problem lays in the order of creation of the h5py groups: a group name can’t be prefix of a previous group name, it is possible to sort the layers by name before saving them. This change worked for me : File: $CUSTOM_PATH/tensorflow_core/python/keras/saving/hdf5_format.py Function: save_weights_to_hdf5_group
replaced by:
With this modification I was able to save my model in .h5 format and then to load my model from scratch and run inferences. I can do a pull request if you think it’s a good idea
I met the same error, I solved it by saving the model with .tf instead of .h5 Plus, I am using TensorFlow 2.0, the default saving format is .tf
This issue has a larger effect than suggested in the top of the thread. It actually completely prevents saving the weights of a keras model that uses
tf.ones_like
.Raises:
ValueError: Unable to create group (name already exists)
This is because of the issue described above by @faustomorales and can be seen if we look at how
ones_like
gets added to the keras model:Output:
I haven’t looked around for other places where this is happening but I assume that there are more cases where this causes problems. Given that this effectively breaks a core part of the Keras API in tensorflow–the ability to save certain models in h5 format–I’d argue that this needs a more serious fix than just an error message.
How can we replicate this in Colab??
hello, I also get trouble with this issue. how did u do that ,model.save(xxxx.tf)?
I’m still getting this issue no matter what I try. Any ideas on what I’m doing wrong? I’m trying to save the YAM net model so I can then convert it to tensorflow.js.
Versions:
One walk around is to use the Lambda layer to wrap these type of operations