深度可分离卷积是MobileNets、Xception等深度学习网络的基石,将它实现了,那些使用了它的深度学习网络也就可以轻易实现。
深度可分离卷积的原理(Depthwise Separable Convolution)
深度可分离卷积由两个过程组成:depthwise convolution和pointwise convolution(即1x1 convolution)。
不妨假设输入图像的高度和宽度相同,对于M个高度和宽度都为 D F D_F DF的输入特征图,先用 M M M个 D K ∗ D K ∗ 1 D_K * D_K*1 DK∗DK∗1卷积核对 M M M个通道的map进行的空间卷积,一个卷积核对应一个通道,得到通道数仍然为 M M M的中间结果,再用 N N N个 1 ∗ 1 ∗ M 1*1*M 1∗1∗M的卷积核对中间结果进行标准卷积,得到深度可分离卷积结果。
实现3x3的深度可分离卷积
depthwise convolution
假设输入特征图为32通道,将group和num_output设置为32,将kernel_size设置为3,pad设置为1(保证卷积结果的高宽保持不变),stride设置为1。
group参数说明:
http://caffe.berkeleyvision.org/tutorial/layers/convolution.html
group (g) [default 1]: If g > 1, we restrict the connectivity of each filter to a subset of the input. Specifically, the input and output channels are separated into g groups, and the ith output group channels will be only connected to the ith input group channels.
将group、num_output设置为输入特征图的通道数,即可实现一个滤波器只与输入特征图的一个通道卷积。
layer {
name: "conv_dw"
type: "Convolution"
bottom: "input_32"
top: "conv_dw"
param {
lr_mult: 1
decay_mult: 1
}
convolution_param {
num_output: 32
bias_term: false
pad: 1
kernel_size: 3
group: 32
engine: CAFFE
stride: 1
weight_filler {
type: "msra"
}
}
}
pointwise convolution(1x1 convolution标准卷积)
假设输入为前面的depthwise convolution的输出,pointwise convolution的输出结果的通道数为64。将num_output设置为64,kernel_size设置为1,pad设置为0
layer {
name: "conv_pw"
type: "Convolution"
bottom: "conv_dw"
top: "conv_pw"
param {
lr_mult: 1
decay_mult: 1
}
convolution_param {
num_output: 64
bias_term: false
pad: 0
kernel_size: 1
stride: 1
weight_filler {
type: "msra"
}
}
}