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

后端 未结 6 1721
情书的邮戳
情书的邮戳 2020-12-14 02:33

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/

6条回答
  •  渐次进展
    2020-12-14 03:15

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

    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.

提交回复
热议问题