Search code examples
machine-learningneural-networkdeep-learningkerasmax-pooling

keras vgg 16 shape error


im trying to fit the data with the following shape to the pretrained keras vgg19 model.

image input shape is (32383, 96, 96, 3) label shape is (32383, 17) and I got this error

expected block5_pool to have 4 dimensions, but got array with shape (32383, 17)

at this line

model.fit(x = X_train, y= Y_train, validation_data=(X_valid, Y_valid),
              batch_size=64,verbose=2, epochs=epochs,callbacks=callbacks,shuffle=True)

Here's how I define my model

model = VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(96,96,3),classes=17)

How did maxpool give me a 2d tensor but not a 4D tensor ? I'm using the original model from keras.applications.vgg16. How can I fix this error?


Solution

  • Your problem comes from VGG16(include_top=False,...) as this makes your solution to load only a convolutional part of VGG. This is why Keras is complaining that it got 2-dimensional output insted of 4-dimensional one (4 dimensions come from the fact that convolutional output has shape (nb_of_examples, width, height, channels)). In order to overcome this issue you need to either set include_top=True or add additional layers which will squash the convolutional part - to a 2d one (by e.g. using Flatten, GlobalMaxPooling2D, GlobalAveragePooling2D and a set of Dense layers - including a final one which should be a Dense with size of 17 and softmax activation function).