Keras: Valueerror: Dimension 0 in Both shapes must be Equal (Vggnets)

Asked

Viewed 122 times

0

I’m following a tutorial and loaded modelo VGGNet16 pre-trained using Keras

vgg16_model = keras.applications.vgg16.VGG16()

model = Sequential()
for layer in vgg16_model.layers:
  model.add(layer)

model.layers.pop()

for layer in model.layers:
  layer.trainable = False

model.add(Dense(10, activation='softmax', name='predict'))
#model.summary()

I used to model.save('path/model_1.h5') to save the model after train with model.fit_generator(...)

So I ran out of time on "Colaboratory". so I wanted to use model = load_model('path/model_1.h5') to reload my model instead of loading showed as previously with vgg16_model = keras.applications.vgg16.VGG16()...

And now I’ve made this mistake:

ValueError: Dimension 0 in both shapes must be equal, but are 4096 and
1000. Shapes are [4096,10] and [1000,10]. for 'Assign_61' (op: 'Assign') with input shapes: [4096,10], [1000,10].

What am I doing wrong? Thank you!

1 answer

0


Thank you to the people of @deeplizard to help me find this reply in the English version of Stackoverflow.

Here the translation:

The problem is with the line model.layers.pop (). when we use .pop to skip a layer straight line model.layers, the topology of this model is not updated properly. Therefore, all the following operations will be bad, giving us a wrong definition of the model.

Specifically, when adding a camada model.add (camada) lista model.outputs is updated to be the output tensor layer. You can find the following lines in the source code of Sequential.add ():

        output_tensor = layer(self.outputs[0])
        # ... skipping irrelevant lines
        self.outputs = [output_tensor]

When model.layers.pop called, however, is not model.outputs updated accordingly as a result, the newly added layer will be called with a bad input tensor (because self.outputs [0] continues to be the release of the layer tensor removed).

This can be demonstrated with the following lines:

model = Sequential()
for layer in vgg16_model.layers:
    model.add(layer)

model.layers.pop()
model.add(Dense(3, activation='softmax'))

print(model.layers[-1].input)
# => Tensor("predictions_1/Softmax:0", shape=(?, 1000), dtype=float32)
# the new layer is called on a wrong input tensor

print(model.layers[-1].kernel)
# => <tf.Variable 'dense_1/kernel:0' shape=(1000, 3) dtype=float32_ref>
# the kernel shape is also wrong

The incorrectly kernel is because we are seeing the error of size incompatibility arrangements [4096,3] versus [1000,3].

To solve this problem, simply do not add the last layer to the template sequence Sequential.

model = Sequential()
for layer in vgg16_model.layers[:-1]:
    model.add(layer)

*I don’t speak Portuguese. I appreciate any improvement in writing this post for better understanding.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.