MobileNetV1 网络深度解析与Pytorch实现

Table of Contents

正文

1.原理解析

深度可分离卷积

2.标准卷积和深度可分离卷积的比较

结构比较

参数和计算量比较

3.网络结构

MobileNetV1

控制模型参数量的超参数

3.MobileNetV1实现

3.1 深度可分离卷积

 3.2 MobileNetV1整体结构实现

参考


论文名:MobileNets: Efficient Convolutional Neural Networks for MobileVision Applications

下载地址:https://arxiv.org/pdf/1704.04861.pdf

正文

        为了解决现有卷积神经网络模型因参数量大、计算量大而无法在移动端、嵌入式端应用的问题,Google提出了一个小且快的模型MobileNet,大幅度减小模型规模的同时保证模型性能下降很小

1.原理解析

        那么如何实现大幅度减小模型参数量的同时保证模型性能下降很小呢?一个很有用的trick就是分组卷积,而Mobilenet将分组卷积方法运用到极致,提出用深度可分离卷积(depthwise separable convolutions)。

深度可分离卷积

       为什么说depthwise separable convolutions是分组卷积的极致运用呢?因为它将输入数据分成和通道数相等的组,每个组分别进行卷积处理,输入数据分组处理使得不同通道的特征相互独立(不同通道信息无法交流),所以还得想办法结合啊,这个时候就可以采用神奇1*1卷积来完成.

  • depthwise conv:将输入数据的每个通道单独进行卷积处理(一般采用3*3的卷积核)
  • pointwise conv:标准的1*1卷积,用于将depthwise conv的输出组合到一起

2.标准卷积和深度可分离卷积的比较

结构比较

                                               

  上图中除了结构的不同外,还将激活函数由ReLU换成了ReLU6(论文给出的图片没有),据说该函数在float16/int8的嵌入式设备中效果很好,能较好地保持网络的鲁棒性。

                                                     

参数和计算量比较

1.标准卷积

                                     

2.深度可分离卷积

                                  

3.比较

                                    

        参数和计算量的比较是参考这篇博客:https://blog.csdn.net/u010712012/article/details/94888053

3.网络结构

MobileNetV1

                               

  dw代表depthwise conv,且用stride2代替了pool操作。

注意:最后一个深度可分离卷积的stride应该为1,而不是图中的2,这样才能保证尺寸为7*7不变.

控制模型参数量的超参数

        论文中为了能根据需求灵活的控制模型的大小,引入了两个超参数:

  • Width Multiplier:用于控制特征图的维数即通道数(其实是控制卷积核的个数)
  • Resolution Multiplier:用于控制特征图的尺寸即分辨率

3.MobileNetV1实现

  mobilenet结构主要由多个深度可分离卷积构成

3.1 深度可分离卷积

# define DepthSeperabelConv2d with depthwise+pointwise
class DepthSeperabelConv2d(nn.Module):
    def __init__(self, input_channels, output_channels, stride):

        super().__init__()

        self.depthwise = nn.Sequential(
            nn.Conv2d(input_channels, input_channels, 3, stride, 1, groups=input_channels, bias=False),
            nn.BatchNorm2d(input_channels),
            nn.ReLU6(inplace=True)
        )

        self.pointwise = nn.Sequential(
            nn.Conv2d(input_channels, output_channels, 1, bias=False),
            nn.BatchNorm2d(output_channels),
            nn.ReLU6(inplace=True)
        )

    def forward(self, x):
        x = self.depthwise(x)
        x = self.pointwise(x)

        return x

 3.2 MobileNetV1整体结构实现

class MobileNet(nn.Module):
    """
    Args:
        width multipler: The role of the width multiplier α is to thin
                         a network uniformly at each layer. For a given
                         layer and width multiplier α, the number of
                         input channels M becomes αM and the number of
                         output channels N becomes αN.
    """

    def __init__(self, width_multiplier=1, class_num=settings.CLASSES_NUM):
        super().__init__()

        alpha = width_multiplier

        self.conv1 = nn.Sequential(
            nn.Conv2d(3, int(alpha*32), 3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(int(alpha*32)),
            nn.ReLU6(inplace=True)
        )

        self.conv2 = nn.Sequential(
            DepthSeperabelConv2d(int(alpha * 32), int(alpha * 64), 1),
            DepthSeperabelConv2d(int(alpha * 64), int(alpha * 128), 2),
            DepthSeperabelConv2d(int(alpha * 128), int(alpha * 128), 1),
            DepthSeperabelConv2d(int(alpha * 128), int(alpha * 256), 2),
            DepthSeperabelConv2d(int(alpha * 256), int(alpha * 256), 1),
            DepthSeperabelConv2d(int(alpha * 256), int(alpha * 512), 2),
            DepthSeperabelConv2d(int(alpha * 512), int(alpha * 512), 1),
            DepthSeperabelConv2d(int(alpha * 512), int(alpha * 512), 1),
            DepthSeperabelConv2d(int(alpha * 512), int(alpha * 512), 1),
            DepthSeperabelConv2d(int(alpha * 512), int(alpha * 512), 1),
            DepthSeperabelConv2d(int(alpha * 512), int(alpha * 512), 1),
            DepthSeperabelConv2d(int(alpha * 512), int(alpha * 1024), 2),
            DepthSeperabelConv2d(int(alpha * 1024), int(alpha * 1024), 2)
        )

        self.avg = nn.AdaptiveAvgPool2d(1)

        self.fc = nn.Linear(int(alpha * 1024), class_num)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = self.avg(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)

        return x

参考

https://blog.csdn.net/hongbin_xu/article/details/82957426

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MobileNetV3是Google推出的第三代轻量化网络,用于在移动平台上进行神经网络的部署和应用。它在精度和计算量上都达到了新的水平。MobileNetV3的主要特点包括: 1. MobileNetV1MobileNetV1通过将普通的卷积操作分解为深度卷积和点卷积两步来减少通道融合时间和参数。深度卷积是仅卷积不求和的操作,而点卷积是对深度卷积的多通道结果进行融合的1x1卷积。 2. MobileNetV2:MobileNetV2引入了反转残差块,先将通道数增加再减少,以解决在输入通道数较多时丢失信息的问题。这种结构可以减少神经元抑制导致的信息丢失。 3. MobileNetV3:MobileNetV3重新设计了网络结构,并引入了H-Swish激活函数与ReLU搭配使用。此外,还引入了Squeeze-And-Excite模块,用于增强网络的表示能力。 关于MobileNetV3的PyTorch实现,您可以参考作者团队的GitHub项目,链接为:https://github.com/yichaojie/MobileNetV3。该项目是根据论文描述的网络结构进行复现,并使用oxFlower17数据集进行了训练,确保了可行性。 如果您想了解更多关于MobileNetV3的详细信息,可以参考原文《Searching for MobileNetV3》。该论文由Google AI和Google Brain团队撰写,原文链接为:https://arxiv.org/abs/1905.02244v3。 #### 引用[.reference_title] - *1* *2* [pytorch实现并训练MobileNetV3](https://blog.csdn.net/baidu_38406307/article/details/107374989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【MobileNetV3】Pytorch实现(图像分类)](https://blog.csdn.net/weixin_43312117/article/details/121236450)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值