YOLO算法改进7【中阶改进篇】:主干网络C3替换为轻量化网络MobileNetV3

解决问题:YOLOv5主干特征提取网络采用C3结构,带来较大的参数量,检测速度较慢,应用受限,在某些真实的应用场景如移动或者嵌入式设备,如此大而复杂的模型时难以被应用的。首先是模型过于庞大,面临着内存不足的问题,其次这些场景要求低延迟,或者说响应速度要快,想象一下自动驾驶汽车的行人检测系统如果速度很慢会发生什么可怕的事情。所以,研究小而高效的CNN模型在这些场景至关重要,至少目前是这样,尽管未来硬件也会越来越快。本文尝试将主干特征提取网络替换为更轻量的MobileNet网络,以实现网络模型的轻量化,平衡速度和精度。
在这里插入图片描述

摘要:
我们提出下一代的mobilenet基于一种互补搜索技术的组合作为一种新颖的建筑设计。MobileNetV3调到手机cpu通过硬件组合感知网络架构搜索(NAS)的补充NetAdapt算法,然后进行改进通过新颖的建筑进步。这篇论文从探索如何自动搜索算法和网络工作设计可以一起利用互补的工作提高整体技术水平的方法。通过在这个过程中,我们创建了两个新的MobileNet模型:MobileNetV3-Large和MobileNetV3-Small针对高资源和低资源用例。这些然后调整模型并将其应用于对象检测和语义分割任务。为了完成任务语义分割(或任何密集像素预测),我们提出了一种新的高效分割解码器空间金字塔池(LR-ASPP)。我们实现新状态的艺术结果移动分类,检测和分割。MobileNetV3-Large则高出3.2%准确的ImageNet分类,同时减少延迟与MobileNetV2相比减少了20%。MobileNetV3-Small是与MobileNetV2模型相比,准确率提高了6.6%具有相当的延迟。MobileNetV3-Large检测与MobileNetV2在COCO检测上的精度大致相同,速度快了25%以上。MobileNetV3-Large LRASPP在类似情况下比MobileNetV2 R-ASPP快34%城市景观分割的准确性。

论文地址:https://arxiv.org/abs/1905.02244.pdf
代码:https://github.com/LeBron-Jian/DeepLearningNote

MobileNet V3 相关技术如下:

1,用 MnasNet 搜索网络结构
2,用 V1 的深度可分离
3,用 V2 的倒置残差线性瓶颈结构
4,引入 SE模块
5,新的激活函数 h-swish(x)
6,网络搜索中利用两个策略:资源受限的 NAS 和 NetAdapt
7,修改 V2 最后部分减小计算
在这里插入图片描述
方 法:

第一步修改common.py,增加MobileNetV3模块。

class StemBlock(nn.Module):
    def __init__(self, c1, c2, k=3, s=2, p=None, g=1, act=True):
        super(StemBlock, self).__init__()
        self.stem_1 = Conv(c1, c2, k, s, p, g, act)
        self.stem_2a = Conv(c2, c2 // 2, 1, 1, 0)
        self.stem_2b = Conv(c2 // 2, c2, 3, 2, 1)
        self.stem_2p = nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)
        self.stem_3 = Conv(c2 * 2, c2, 1, 1, 0)

    def forward(self, x):
        stem_1_out = self.stem_1(x)
        stem_2a_out = self.stem_2a(stem_1_out)
        stem_2b_out = self.stem_2b(stem_2a_out)
        stem_2p_out = self.stem_2p(stem_1_out)
        out = self.stem_3(torch.cat((stem_2b_out, stem_2p_out), 1))
        return out

class h_sigmoid(nn.Module):
    def __init__(self, inplace=True):
        super(h_sigmoid, self).__init__()
        self.relu = nn.ReLU6(inplace=inplace)

    def forward(self, x):
        return self.relu(x + 3) / 6


class h_swish(nn.Module):
    def __init__(self, inplace=True):
        super(h_swish, self).__init__()
        self.sigmoid = h_sigmoid(inplace=inplace)

    def forward(self, x):
        y = self.sigmoid(x)
        return x * y


class SELayer(nn.Module):
    def __init__(self, channel, reduction=4):
        super(SELayer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // reduction),
            nn.ReLU(inplace=True),
            nn.Linear(channel // reduction, channel),
            h_sigmoid()
        )

    def forward(self, x):
        b, c, _, _ = x.size()
        y = self.avg_pool(x)
        y = y.view(b, c)
        y = self.fc(y).view(b, c, 1, 1)
        return x * y


class conv_bn_hswish(nn.Module):
    """
    This equals to
    def conv_3x3_bn(inp, oup, stride):
        return nn.Sequential(
            nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
            nn.BatchNorm2d(oup),
            h_swish()
        )
    """

    def __init__(self, c1, c2, stride):
        super(conv_bn_hswish, self).__init__()
        self.conv = nn.Conv2d(c1, c2, 3, stride, 1, bias=False)
        self.bn = nn.BatchNorm2d(c2)
        self.act = h_swish()

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

    def fuseforward(self, x):
        return self.act(self.conv(x))


class MobileNetV3_InvertedResidual(nn.Module):
    def __init__(self, inp, oup, hidden_dim, kernel_size, stride, use_se, use_hs):
        super(MobileNetV3_InvertedResidual, self).__init__()
        assert stride in [1, 2]

        self.identity = stride == 1 and inp == oup

        if inp == hidden_dim:
            self.conv = nn.Sequential(
                # dw
                nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
                          bias=False),
                nn.BatchNorm2d(hidden_dim),
                h_swish() if use_hs else nn.ReLU(inplace=True),
                # Squeeze-and-Excite
                SELayer(hidden_dim) if use_se else nn.Sequential(),
                # Eca_layer(hidden_dim) if use_se else nn.Sequential(),#1.13.2022
                # pw-linear
                nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                nn.BatchNorm2d(oup),
            )
        else:
            self.conv = nn.Sequential(
                # pw
                nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
                nn.BatchNorm2d(hidden_dim),
                h_swish() if use_hs else nn.ReLU(inplace=True),
                # dw
                nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
                          bias=False),
                nn.BatchNorm2d(hidden_dim),
                # Squeeze-and-Excite
                SELayer(hidden_dim) if use_se else nn.Sequential(),
                # Eca_layer(hidden_dim) if use_se else nn.Sequential(),  # 1.13.2022
                h_swish() if use_hs else nn.ReLU(inplace=True),
                # pw-linear
                nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
                nn.BatchNorm2d(oup),
            )

    def forward(self, x):
        y = self.conv(x)
        if self.identity:
            return x + y
        else:
            return y

第二步:将yolo.py中注册模块。

if m in [Conv,MobileNetV3_InvertedResidual,ShuffleNetV2_InvertedResidual,
               ]:

第三步:修改yaml文件

backbone:
  # MobileNetV3-large
  # [from, number, module, args]
  [[-1, 1, conv_bn_hswish, [16, 2]],                   # 0-p1/2
   [-1, 1, MobileNetV3_InvertedResidual, [ 16,  16, 3, 1, 0, 0]],  # 1-p1/2
   [-1, 1, MobileNetV3_InvertedResidual, [ 24,  64, 3, 2, 0, 0]],  # 2-p2/4
   [-1, 1, MobileNetV3_InvertedResidual, [ 24,  72, 3, 1, 0, 0]],  # 3-p2/4
   [-1, 1, MobileNetV3_InvertedResidual, [ 40,  72, 5, 2, 1, 0]],  # 4-p3/8
   [-1, 1, MobileNetV3_InvertedResidual, [ 40, 120, 5, 1, 1, 0]],  # 5-p3/8
   [-1, 1, MobileNetV3_InvertedResidual, [ 40, 120, 5, 1, 1, 0]],  # 6-p3/8
   [-1, 1, MobileNetV3_InvertedResidual, [ 80, 240, 3, 2, 0, 1]],  # 7-p4/16
   [-1, 1, MobileNetV3_InvertedResidual, [ 80, 200, 3, 1, 0, 1]],  # 8-p4/16
   [-1, 1, MobileNetV3_InvertedResidual, [ 80, 184, 3, 1, 0, 1]],  # 9-p4/16
   [-1, 1, MobileNetV3_InvertedResidual, [ 80, 184, 3, 1, 0, 1]],  # 10-p4/16
   [-1, 1, MobileNetV3_InvertedResidual, [112, 480, 3, 1, 1, 1]],  # 11-p4/16
   [-1, 1, MobileNetV3_InvertedResidual, [112, 672, 3, 1, 1, 1]],  # 12-p4/16
   [-1, 1, MobileNetV3_InvertedResidual, [160, 672, 5, 1, 1, 1]],  # 13-p4/16
   [-1, 1, MobileNetV3_InvertedResidual, [160, 960, 5, 2, 1, 1]],  # 14-p5/32   原672改为原算法960
   [-1, 1, MobileNetV3_InvertedResidual, [160, 960, 5, 1, 1, 1]],  # 15-p5/32
  ]

结 果:本人在多个数据集上做了大量实验,针对不同的数据集效果不同,map值有所下降,但是权值模型大小降低,参数量下降。

:主干网络的替换不仅仅是适用改进YOLOv5,也可以改进其他的YOLO网络以及目标检测网络,比如YOLOv4、v3等。

最近更新

  1. TCP协议是安全的吗?

    2023-12-25 08:04:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-25 08:04:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-25 08:04:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-25 08:04:04       20 阅读

热门阅读

  1. 利用std::unique_ptr释放资源

    2023-12-25 08:04:04       37 阅读
  2. 喝酒、工作与自律

    2023-12-25 08:04:04       40 阅读
  3. 设计循环队列

    2023-12-25 08:04:04       45 阅读
  4. Milvus向量数据库基础用法及注意细节

    2023-12-25 08:04:04       47 阅读
  5. 力扣56.合并区间

    2023-12-25 08:04:04       36 阅读
  6. ARM AArch64的TrustZone架构详解(上)

    2023-12-25 08:04:04       35 阅读
  7. 阿里云公有云平台

    2023-12-25 08:04:04       36 阅读
  8. Iceberg: COW模式下的MERGE INTO的执行流程

    2023-12-25 08:04:04       37 阅读
  9. 设计模式之工厂方法模式

    2023-12-25 08:04:04       42 阅读
  10. 手写爬虫框架

    2023-12-25 08:04:04       46 阅读
  11. FFmpeg常见命令行

    2023-12-25 08:04:04       37 阅读
  12. Vue3和Vue2的区别

    2023-12-25 08:04:04       32 阅读