【轻量化模型】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 error
mobilenet_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

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

图来自:ReLU6 — PyTorch 1.11.0 documentation

3. 架构图

  论文中是:

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

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

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

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

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

 图来自: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)

                                                 


参考:pytorch 官方 torchvision 代码。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值