62

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)
nbro
  • 15,395
  • 32
  • 113
  • 196
Lucky
  • 875
  • 1
  • 9
  • 19

6 Answers6

69

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

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • 2
    No, that number is free. Keras will show that dimension as `None` in the `model.summry()`, for instance. – Daniel Möller Dec 06 '17 at 11:18
  • 3
    My dimension for training data is array: `(26721, 32, 32)` and for valid. dimension is `(6680,32,32)`. Now I explicitly define image size (32,32,1) , then it gives me error `ValueError: Error when checking input: expected conv2d_9_input to have 4 dimensions, but got array with shape (6680, 32, 32)` . I have also edit model_fit.generator in post, could you please check there? – Lucky Dec 06 '17 at 11:25
  • 6
    Now the problem is in your data. Your data lacks the `channel` dimension: `x_validation = x_validation.reshape(6680,32,32,1)` – Daniel Möller Dec 06 '17 at 11:32
  • 2
    Thanks very much for your help – Lucky Dec 06 '17 at 11:55
  • Can you help us here @DanielMöller. https://stackoverflow.com/questions/64612084/valueerror-input-0-of-layer-sequential-157-is-incompatible-with-the-layer-expe – Brown Oct 30 '20 at 16:29
  • @DanielMöller I need an input shape of ndim=5, But my input shape= (18, 64, 1688), so I reshaped it as: data=data.reshape(18, 64, 1688, 1, 1). And pass this to my model that contains conv2D and convLSTM2D layers. But it gives me an error: Input 0 of layer conv_lst_m2d_88 is incompatible with the layer: expected ndim=5, found ndim=6. Full shape received: (None, None, 64, 211, 1, 128). How do I resolve it, kindly guide me, it will be a great favor. – TariqS Jul 07 '21 at 09:38
9

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.

SANDEEP KUMAR H
  • 121
  • 1
  • 1
7

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

pcampana
  • 2,413
  • 2
  • 21
  • 39
Akash Desai
  • 498
  • 5
  • 11
3

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.

Sanketsz
  • 138
  • 4
  • 4
    Rather than just posting short comments `you could do this and then this` as an answer, you should illustrate how this can be achieved by for example showing the code implemented in the question. As it is now, this is a comment not an answer. Consider editing your answer to give more details. – Oliver Feb 15 '21 at 17:52
  • @Sanketsz I need an input shape of ndim=5, But my input shape= (18, 64, 1688), so I reshaped it as: data=data.reshape(18, 64, 1688, 1, 1). And pass this to my model that contains conv2D and convLSTM2D layers. But it gives me an error: Input 0 of layer conv_lst_m2d_88 is incompatible with the layer: expected ndim=5, found ndim=6. Full shape received: (None, None, 64, 211, 1, 128). How do I resolve it, kindly guide me, it will be a great favor. – TariqS Jul 07 '21 at 09:39
1

I have faced 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. .. & the problem is solved
Dharman
  • 30,962
  • 25
  • 85
  • 135
Akash Desai
  • 498
  • 5
  • 11
0

Here you need to check the "channels_first" whenever CNN is used as 2d,Also reshape your train_data and test data as:

if K.image_data_format() == 'channels_first':   #check for channels_first
 train_img.reshape(train_img.shape[0],1,x,x)
 Input_shape=(1,x,x)                            #In your case x is 32
else:
 train_img.reshape(train_img.shape[0],x,x,1)
 Input_shape=(x,x,1)
dgamer
  • 155
  • 2
  • 3