轻量级卷积

深度可分离卷积

描述

深度可分离卷积分为深度卷积和逐点卷积两部分,即SeparableConv由DepthWiseConv和PointWiseConv组成,是降低卷积运算参数量的一种有效方法。
在Google的Xception以及MobileNet论文中均有描述。它的核心思想是将一个完整的卷积运算分解为两步进行,分别为Depthwise Convolution与Pointwise Convolution。

Pytorch代码为:

from torch import nn

class SeparableConv2d(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, padding=0, onnx_compatible=False):
        super().__init__()
        ReLU = nn.ReLU if onnx_compatible else nn.ReLU6
        self.conv = nn.Sequential(
            nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size,
                      groups=in_channels, stride=stride, padding=padding),
            nn.BatchNorm2d(in_channels),
            ReLU(),
            nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1),
        )

    def forward(self, x):
        return self.conv(x)

分析

设特征图的输入层数为\(D_{in}\),输出层数为\(D_{out}\),卷积核的尺度为\(K\times K\)
深度可分离卷积的参数量为\(K^2D_{in} + D_{in}D_{out}\),常规卷积的参数量为\(K^2D_{in}D_{out}\),两者比例为 \(\frac{1}{D_{out}}+\frac{1}{K^2}\),因此可以减少参数量和计算量。

猜你喜欢

转载自www.cnblogs.com/zi-wang/p/12294263.html
今日推荐