Python – Perform 1d convolution using the 2d kernel in keras

Perform 1d convolution using the 2d kernel in keras… here is a solution to the problem.

Perform 1d convolution using the 2d kernel in keras

I’m currently working on a CNN network where I want to apply a 2D kernel on an image, but it only needs to perform 1D convolution, which means it only needs to move along one axis (here the x-axis of the case).

The shape of the kernel is the same as the y-axis of the image. The number of filters currently applied is not an issue.

An example:
The given size is an image of (6,3,3) = (rows, cols, color_channel).

How should I perform 2D convolution given a 1D filter?

Tried @Marcin Możejko’s suggestion

dim_x = 3
dim_y = 6
color_channels = 3
#model.add(ZeroPadding2D((6,4),input_shape=(6,3,3)))
model.add(Conv2D(filters = 32,kernel_size=(dim_y,1) , activation='linear' , input_shape = (6,3,3)))
print model.output_shape
model.add(Reshape((dim_x,color_channels)))

Error:

The total size of the new array must be unchanged

Solution

Suppose your image shape=(dim_x, dim_y, img_channels) you can get 1D convolution by setting:

conv1d_on_image = Convolution2D(output_channels, 1, dim_y, border_mode='valid')(input)

Remember that the output shape of the layer is (dim_x, 1, output_channels). If you want your input to be continuous, you can use the Reshape layer through Settings:

conv1d_on_image = Reshape((dim_x, output_channels))(conv1d_on_image)

This produces output with a shape of (dim_x, output_channels).

An interesting fact is that this is exactly how Conv1D works in Keras with tf backends.

Related Problems and Solutions