简体   繁体   中英

keras backend conv2d error

I am trying to use the keras.backend.conv2d function with my own kernel and I am getting the following error.

img = data[0,:,0]
img = img.reshape(240, 320)

kernelX = np.array([[-1, 1], 
                    [-1, 1]])

img = K.constant(img)
kernelX = K.constant(kernelX)

I_x = K.conv2d(img, kernelX)

ValueError: number of input channels does not match corresponding dimension of filter, 320 != 2

Could somebody please push me in the right direction?

Although you didn't specify which backend you use, the error is raised due to your reshaping operations. In this case, keras interprets the last element of the shape as the number of channels. That means, it supposes your image has 240 pixels with 320 channels and your kernel has two channels. For a convolution, these have to be the same size, though.

Therefore, you should create your image and kernels in a way, so that your image is of shape (240, 320, 1) and your kernel of shape (2, 2, 1).

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