Keras: ValueError: You are trying to load a weight file containing 3 layers into a model with 1 layers.

Created on 21 Oct 2017  路  6Comments  路  Source: keras-team/keras

I train a model with the following code:

base_model = VGG19(...)
..............
model.fit_generator(...., callbacks=[ModelCheckpoint(filepath=checkpoint_file, save_best_only=True,
                    save_weights_only=True)]....)

however, when I load the weights with the following code:

from keras.utils.training_utils import multi_gpu_model
model = load_vgg19(...)
model = multi_gpu_model(model, gpus=3)
model.load_weights(checkpoint_file)
model.fit_generator(...........)

I got the error: ValueError: You are trying to load a weight file containing 3 layers into a model with 1 layers and followed by AttributeError: 'NoneType' object has no attribute 'TF_DeleteStatus'. Is this a bug?

Most helpful comment

This probably has something to do with multi_gpu_model copying your model onto multiple gpus. do a model.summary() after multi_gpu_model and you will see that you can't see the layers directly.
Could you try loading the weights before multi_gpu_model? It might work that way.

from keras.utils.training_utils import multi_gpu_model
model = load_vgg19(...)
model.load_weights(checkpoint_file) # DO THIS FIRST
model = multi_gpu_model(model, gpus=3) # split amongst multiple gpus
model.fit_generator(...........) # enjoy!

All 6 comments

This probably has something to do with multi_gpu_model copying your model onto multiple gpus. do a model.summary() after multi_gpu_model and you will see that you can't see the layers directly.
Could you try loading the weights before multi_gpu_model? It might work that way.

from keras.utils.training_utils import multi_gpu_model
model = load_vgg19(...)
model.load_weights(checkpoint_file) # DO THIS FIRST
model = multi_gpu_model(model, gpus=3) # split amongst multiple gpus
model.fit_generator(...........) # enjoy!

@jamesben6688 I encountered the same problem with you. After reading the source code of function multi_gpu_model(...) and running parallel_model.summary(), I found that the original single gpu model structure is nested in the 'sequential_1' layer of parallel_model. And this 'sequential_1' layer is actually a keras.models.Sequential object(i.e. a sequential model), which means you can see the inside structure by running:

nested_layers = parallel_model.get_layer('sequential_1')
nested_layers.summary()

I encountered the same problem. Thank to @akshaychawla 's comment, I figured it's caused by multi_gpu_model. The saved model was trained on two GPUs, then I tried to load the weights into a model on a single GPU. This gave me the error. A simple solution is to make sure your new model use the same number of GPUs by calling multi_gpu_model.

model.add(tf.keras.layers.Flatten()) # takes our 28x28 and makes it 1x784
CHANGE THE PREVIOUS LINE TO
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))

ERROR RESOLVED : ValueError: You are trying to load a weight file containing 3 layers into a model with 0 layers.

As suggested by partivadas adding input shape fixed it for me.
Before:
tf.keras.layers.Flatten()
After:
tf.keras.layers.Flatten(input_shape=(28, 28))

Closing this issue since its resolved. Feel free to reopen if the problem still persists. Thanks!

Was this page helpful?
0 / 5 - 0 ratings