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

烂漫一生 提交于 2021-02-11 15:19:09

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/54877516/valueerror-input-0-is-incompatible-with-layer-conv2d-5-expected-ndim-4-found

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