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

120,516

Solution 1

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).

Solution 2

The problem is with input_shape. Try adding an extra dimension/channel for letting keras know that you are working on a grayscale image ie -->1

input_shape= (56,56,1). Probably if you are using a normal Deep learning model then it won't raise an issue but for Convnet it does.

Solution 3

For reshape the data we need to add fourth dimensions i.e changing from (6000,28,28) to (6000,28,28,1)

My code is:

img_rows=x_train[0].shape[0]
img_cols=x_test[0].shape[1]

X_train=x_train.reshape(x_train.shape[0],img_rows,img_cols,1)

X_test=x_test.reshape(x_test.shape[0],img_rows,img_cols,1)


Input_shape=(img_rows,img_cols,**).  *->  I forgot to put 1 here.

I have face the same problem

Input 0 is incompatible with layer conv2d_4 : except ndim=4 ,found ndim=3

I solved this problem by simply putting value in the input shape

Input_shape=(img_rows,img_cols,1)#store the shape of single image.

With this problem is solved

Solution 4

you can use :

train_dataset= train_dataset.reshape(-1,32,32,1)

and now you can use input_shape(32,32,1) in the algorithm.

Share:
120,516
Lucky
Author by

Lucky

Updated on February 16, 2021

Comments

  • Lucky
    Lucky about 3 years

    I have checked all the solutions, but still, I am facing the 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)