Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution'

53,005

Solution 1

By default, Convolution2D (https://keras.io/layers/convolutional/) expects the input to be in the format (samples, rows, cols, channels), which is "channels-last". Your data seems to be in the format (samples, channels, rows, cols). You should be able to fix this using the optional keyword data_format = 'channels_first' when declaring the Convolution2D layer.

model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(1,28,28), data_format='channels_first'))

Solution 2

I had the same problem, however the solution provided in this thread did not help me. In my case it was a different problem that caused this error:


Code

imageSize=32
classifier=Sequential() 

classifier.add(Conv2D(64, (3, 3), input_shape = (imageSize, imageSize, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(64, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(64, (3, 3), activation = 'relu')) 
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(64, (3, 3), activation = 'relu')) 
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(64, (3, 3), activation = 'relu')) 
classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Flatten())

Error

The image size is 32 by 32. After the first convolutional layer, we reduced it to 30 by 30. (If I understood convolution correctly)

Then the pooling layer divides it, so 15 by 15.

Then another convolutional layer reduces it to 13 by 13...

I hope you can see where this is going: In the end, my feature map is so small that my pooling layer (or convolution layer) is too big to go over it - and that causes the error


Solution

The easy solution to this error is to either make the image size bigger or use less convolutional or pooling layers.

Solution 3

Keras is available with following backend compatibility:

TensorFlow : By google, Theano : Developed by LISA lab, CNTK : By Microsoft

Whenever you see a error with [?,X,X,X], [X,Y,Z,X], its a channel issue to fix this use auto mode of Keras:

Import

from keras import backend as K
K.set_image_dim_ordering('th')

"tf" format means that the convolutional kernels will have the shape (rows, cols, input_depth, depth)

This will always work ...

Share:
53,005

Related videos on Youtube

Nurdin
Author by

Nurdin

My Profile - http://www.revivalx.com Data Consultant at Deepagi - https://deepagi.com KLSE & Crypto screener - https://deepagiscreener.com Github - https://github.com/datomnurdin Linkedin - http://www.linkedin.com/pub/mohammad-nurdin/57/290/692 Youtube - https://www.youtube.com/channel/UCP2lj6CO3Grss4v2ebJ1C1A #SOreadytohelp

Updated on May 20, 2021

Comments

  • Nurdin
    Nurdin almost 3 years

    I got this error message when declaring the input layer in Keras.

    ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution' (op: 'Conv2D') with input shapes: [?,1,28,28], [3,3,28,32].

    My code is like this

    model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
    

    Sample application: https://github.com/IntellijSys/tensorflow/blob/master/Keras.ipynb

    • ml4294
      ml4294 over 6 years
      I think you want to use a 3x3 kernel. In this case you should write (3, 3) instead of 3, 3.
    • kRazzy R
      kRazzy R about 6 years
      how to find out what difference using (3,3) or (4,4) or (5,5) make? @ml4294
  • Nurdin
    Nurdin over 6 years
    i will try it first
  • Shaohua Li
    Shaohua Li about 6 years
    Note that it can be set globally in ~/.keras/keras.json: "image_data_format": "channels_first"
  • nino_701
    nino_701 about 5 years
    “the simplest solution is almost always the best.”
  • Eduardo G.R.
    Eduardo G.R. almost 5 years
    This is very important! I was in the same situation.
  • Dave Seidman
    Dave Seidman over 3 years
    This answer let me to the solution on my own environment but seems that a few things have been renamed in later versions of Keras: github.com/keras-team/keras/issues/12649
  • Vasanth Nag K V
    Vasanth Nag K V almost 3 years
    Wonderful ! I had the same problem and now I know why !