Keras: ValueError: Dimension 0 in both shapes must be equal (VGGNets)

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 model.save('path/model_1.h5') to save the template after the 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 template rather than load showed as previously with vgg16_model = keras.applications.vgg16.VGG16()...

And now I have this error:

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? Thanks!

    
asked by anonymous 15.04.2018 / 21:02

1 answer

0

Thanks to the @deeplizard people to help me find this #

Here's the translation:

The problem is with the model.layers.pop () line. when we use .pop to skip a line from the model.layers layer, the topology of this template is not updated properly. So 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% wired%, however, is not% wrapped as%, as a result, the newly added layer will be called with a bad input tensor (because% w_% is still the output of the tensor of the removed layer).

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 incorrect model.layers.pop is because we are seeing the incompatibility arrangement error of size model.outputs versus self.outputs [0] .

To solve this problem, simply do not add the last layer to the string of the kernel template.

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

* I do not speak Portuguese. I appreciate any improvement in writing this publication for better understanding.

    
16.04.2018 / 18:23