'conv2d_2 / convolution'从1减去3导致的负尺寸大小

时间:2022-07-25 13:57:18

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

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

ValueError:通过输入形状为'conv2d_2 / convolution'(op:'Conv2D')从1减1而导致的负尺寸大小:[?,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

示例应用程序:https://github.com/IntellijSys/tensorflow/blob/master/Keras.ipynb

3 个解决方案

#1


13  

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.

默认情况下,Convolution2D(https://keras.io/layers/convolutional/)期望输入采用格式(样本,行,列,通道),即“通道最后”。您的数据似乎采用格式(样本,通道,行,列)。在声明Convolution2D层时,您应该能够使用可选关键字data_format ='channels_first'来解决此问题。

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

#2


4  

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)

图像大小是32乘32.在第一个卷积层之后,我们将它减少到30乘以30(我想,如果我正确理解卷积)

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

然后汇集层将它减少到一半,所以15乘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.

因此,通过使图像尺寸更大或减少卷积或合并层来容易地解决误差。

#3


0  

Keras is available with following backend compatibility:

Keras具有以下后端兼容性:

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

TensorFlow:谷歌,Theano:由LISA实验室开发,CNTK:微软

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:

每当你看到[?,X,X,X],[X,Y,Z,X]的错误时,修复此问题的通道问题就是使用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)

“tf”格式意味着卷积内核将具有形状(rows,cols,input_depth,depth)

This will always work ...

这将永远有效......

#1


13  

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.

默认情况下,Convolution2D(https://keras.io/layers/convolutional/)期望输入采用格式(样本,行,列,通道),即“通道最后”。您的数据似乎采用格式(样本,通道,行,列)。在声明Convolution2D层时,您应该能够使用可选关键字data_format ='channels_first'来解决此问题。

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

#2


4  

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)

图像大小是32乘32.在第一个卷积层之后,我们将它减少到30乘以30(我想,如果我正确理解卷积)

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

然后汇集层将它减少到一半,所以15乘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.

因此,通过使图像尺寸更大或减少卷积或合并层来容易地解决误差。

#3


0  

Keras is available with following backend compatibility:

Keras具有以下后端兼容性:

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

TensorFlow:谷歌,Theano:由LISA实验室开发,CNTK:微软

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:

每当你看到[?,X,X,X],[X,Y,Z,X]的错误时,修复此问题的通道问题就是使用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)

“tf”格式意味着卷积内核将具有形状(rows,cols,input_depth,depth)

This will always work ...

这将永远有效......