我目前正在开发一个CNN网络,我希望在图像上应用一个2D卷积核,但只需要执行1D卷积,意味着它只需要沿一个轴(在本例中是x轴)移动。
卷积核的形状与图像的y轴相同。目前不考虑应用的滤波器数量。
举个例子:给定一个大小为(6,3,3)的图像=(行, 列, 颜色通道)
给定一个2D滤波器,我应该如何执行1D卷积?
我尝试了@Marcin Możejko建议的方法
dim_x = 3dim_y = 6color_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_shapemodel.add(Reshape((dim_x,color_channels)))
错误:
The total size of the new array must be unchanged
回答:
假设你的图像shape=(dim_x, dim_y, img_channels)
,你可以通过设置来获得1D
卷积:
conv1d_on_image = Convolution2D(output_channels, 1, dim_y, border_mode='valid')(input)
请记住,这一层的输出形状将是(dim_x, 1, output_channels)
。如果你希望你的输入是顺序的,你可以通过设置Reshape
层来实现:
conv1d_on_image = Reshape((dim_x, output_channels))(conv1d_on_image)
这将产生形状为(dim_x, output_channels)
的输出。
有趣的是,这正是Keras
中使用tf
后端的Conv1D
的工作方式。