Depthwise Separable Convolution 卷积原理与代码

1.定义:

    Depthwise(DW)卷积与Pointwise(PW)卷积,合起来被称作Depthwise Separable Convolution(参见Google的Xception)。

Depthwise实现如下:

Depthwise Separable Convolution 卷积原理与代码

说白了就是输入数据有几个通道,那么就有输入通道个数个卷积核分别对这些通道做卷积。

代码实现:

kernel_size=3, stride=1, dilation=1

depthwise = nn.Conv2d(inchannel, inchannel, kernel_size,stride=stride, padding=dilation,dilation=dilation, groups=inchannel, bias=bias)

实现过程很简单,就是将groups设置为输入通道数,最终就实现了上面的算法。正常卷积结果这个groups会被设置为1.

Pointwise实现如下:

Depthwise Separable Convolution 卷积原理与代码

说白了对Depthwise的结果做卷积核大小为1*1的卷积,将N个通道的特征图关联在一起。

代码实现:pointwise = nn.Conv2d(inchannle, outchannel, 1, bias=bias)

Depthwise Separable Convolution代码实现如下:

       depthwise = nn.Conv2d(inplanes, inplanes, kernel_size,
                              stride=stride, padding=dilation,
                              dilation=dilation, groups=inplanes, bias=bias)
        pointwise = nn.Conv2d(inplanes, planes, 1, bias=bias)

举个例子:输入3通道彩色图像,输出4通道的卷积结果。那么inplanes = 3,planes = 4,其他的自己设定即可。

 

这么做的目的就是降低参数量,这个很多文章都写了,具体减少的量也有对比,不赘。这个主要是原理和代码实现对应上。