【轻量化模型】mobilenet v2

MobileNet v2

onnx 导出参考:torchvision onnx 模型导出_星魂非梦的博客-CSDN博客

1. 模型描述

MobileNet v2 来自论文:MobileNetV2: Inverted Residuals and Linear Bottlenecks

MobileNet v2 架构基于inverted residual(逆残差) 结构,inverted residual(逆残差) 结构的输入和输出是thin bottleneck 层,与在输入中使用扩展表示的传统残差模型相反。MobileNet v2 使用轻量级depthwise 卷积(中间的DW卷积)来过滤中间扩张层中的特征。 此外,为了保持表示能力,去除了窄层中的非线性激活函数(这里指的是最后的压缩PW卷积没有激活函数)。

Model structureTop-1 errorTop-5 errormobilenet_v228.129.71 Model structure模型大小mobilenet_v2 onnx

14.2 MB

2. onxx 导出

model = torchvision.models.mobilenet_v2(pretrained=True).cuda()

export_onnx(model, im, file, 13, train = True, dynamic = False, simplify=True)  # opset 13

因为在测试模式,ONNX 会把 conv 和 BN 融合在一起。因此这里设置 train = True

【轻量化模型】mobilenet v2

恭喜,BN显示出来了。但是 Clip 是什么? Clip 其实是 Relu6。

【轻量化模型】mobilenet v2

图来自:ReLU6 — PyTorch 1.11.0 documentation

3. 架构图

【轻量化模型】mobilenet v2

论文中是:

【轻量化模型】mobilenet v2

注意:IR0和IR1卷积核大小都是:1×1 (PW 卷积)到 3×3(DW卷积) 到 1×1(PW卷积) 。

所谓逆残差结构就是先使用 1×1 卷积扩张通道数,再使用 3×3 卷积提特征,最后使用 1×1 卷积压缩通道数。

所谓的PW卷积(Pointwise Convolution)不过是 1×1 卷积;

所谓的DW卷积(Depthwise Convolution)不过是 一个卷积核负责一个通道。

之所以叫逆残差,是相对于resnet 中的残差结构而言的。

【轻量化模型】mobilenet v2

图来自:MobileNet V2 论文初读 – 知乎

显然:MobileNet v2 是先扩张通道再提特征再压缩通道;而Resnet 中的残差结构是先压缩通道在提特征再扩张通道。

4. 核心组件( 逆残差结构)

class InvertedResidual(nn.Module):
    def __init__(
        self,
        inp: int,
        oup: int,
        stride: int,
        expand_ratio: int,
        norm_layer: Optional[Callable[..., nn.Module]] = None
    ) -> None:
        super(InvertedResidual, self).__init__()
        self.stride = stride
        assert stride in [1, 2]

        if norm_layer is None:
            norm_layer = nn.BatchNorm2d

        hidden_dim = int(round(inp * expand_ratio))
        self.use_res_connect = self.stride == 1 and inp == oup

        layers: List[nn.Module] = []
        if expand_ratio != 1:
            # pw
            layers.append(ConvNormActivation(inp, hidden_dim, kernel_size=1, norm_layer=norm_layer,
                                             activation_layer=nn.ReLU6))
        layers.extend([
            # dw
            ConvNormActivation(hidden_dim, hidden_dim, stride=stride, groups=hidden_dim, norm_layer=norm_layer,
                               activation_layer=nn.ReLU6),
            # pw-linear
            nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
            norm_layer(oup),
        ])
        self.conv = nn.Sequential(*layers)
        self.out_channels = oup
        self._is_cn = stride > 1

    def forward(self, x: Tensor) -> Tensor:
        if self.use_res_connect:
            return x + self.conv(x)
        else:
            return self.conv(x)

【轻量化模型】mobilenet v2

参考:pytorch 官方 torchvision 代码。

Original: https://blog.csdn.net/hymn1993/article/details/124595441
Author: 理心炼丹
Title: 【轻量化模型】mobilenet v2

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/691611/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球