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

血红的双手。 提交于 2019-11-28 20:48:09

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

I had the same problem and the solution provided in this thread did not help me. After a lot of thinking, I found the solution which solved it in my case.

To start, here is my code (I know that it is not good, I am still learning)

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

And only after going through every possibility, I found the error:

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

Then the pooling layer reduces it to half, so 15 by 15..

And so on.. In the end, my feature map is so small that my pooling layer (or convolution layer) is too big to go over it - and causes the error

Thus, the error was easily solved by making the image size bigger or reducing convolutional or pooling layers.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!