简体   繁体   中英

ValueError: Input 0 is incompatible with layer conv2d_5: expected ndim=4, found ndim=2

I am trying to build a CNN network and wuld like to probe the layer dimention using output_shape. But it's giving me an error as follows:

ValueError: Input 0 is incompatible with layer conv2d_5: expected ndim=4, found ndim=2

Below is the code I am trying to execute

from keras.layers import Activation

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

print(model.output_shape)

You can check if by default the number of channels is specified at the end

from keras import backend as K
print(K.image_data_format()) # print current format

On my system, this prints "channel_last", which means the last number of your input_shape (28), is the number of channels and 1 is the number of rows. This is also why Keras is giving the error as you cannot apply a 3 x 3 convolution mask to an image with only 1 row (with default padding set to "valid").

Most likely you want to set input_shape to be (28, 28, 1) .

On a separate note, if you want the kernel to be a 3 x 3 kernel, then it should be

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

What you currently have is a convolutional layer with kernel that is size 3 x 3 and stride 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM