Keras ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5

I have checked all solutions, but still I am facing same error. My training images shape is (26721, 32, 32, 1), which I believe it is 4 dimension, but I don't know why error shows it is 5 dimension.

 model = Sequential()

 model.add(Convolution2D(16, 5, 5, border_mode='same',
                            input_shape= input_shape ))

So this is how I am defining model.fit_generator

model.fit_generator(train_dataset, train_labels, nb_epoch=epochs, verbose=1,validation_data=(valid_dataset, valid_labels), nb_val_samples=valid_dataset.shape[0],callbacks=model_callbacks)

Can anyone please help me with this?

1 answer

  • answered 2017-12-06 01:28 Daniel Möller

    The problem is input_shape.

    It should actually contain 3 dimensions only. And internally keras will add the batch dimension making it 4.

    Since you probably used input_shape with 4 dimensions (batch included), keras is adding the 5th.

    You should use input_shape=(32,32,1).