0


YOLOv5-7.0改进(一)MobileNetv3替换主干网络

前言

本篇博客主要讲解YOLOv5主干网络的替换,使用MobileNetv3实现模型轻量化,平衡速度和精度。以下为改进的具体流程~

一、改进MobileNetV3_Small

第一步:修改common.py,新增MobileNetV3

将以下代码复制到common.py末尾

  1. # Mobilenetv3Small
  2. class h_sigmoid(nn.Module):
  3. def __init__(self, inplace=True):
  4. super(h_sigmoid, self).__init__()
  5. self.relu = nn.ReLU6(inplace=inplace)
  6. def forward(self, x):
  7. return self.relu(x + 3) / 6
  8. class h_swish(nn.Module):
  9. def __init__(self, inplace=True):
  10. super(h_swish, self).__init__()
  11. self.sigmoid = h_sigmoid(inplace=inplace)
  12. def forward(self, x):
  13. return x * self.sigmoid(x)
  14. class SELayer(nn.Module):
  15. def __init__(self, channel, reduction=4):
  16. super(SELayer, self).__init__()
  17. # Squeeze操作
  18. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  19. # Excitation操作(FC+ReLU+FC+Sigmoid)
  20. self.fc = nn.Sequential(
  21. nn.Linear(channel, channel // reduction),
  22. nn.ReLU(inplace=True),
  23. nn.Linear(channel // reduction, channel),
  24. h_sigmoid()
  25. )
  26. def forward(self, x):
  27. b, c, _, _ = x.size()
  28. y = self.avg_pool(x)
  29. y = y.view(b, c)
  30. y = self.fc(y).view(b, c, 1, 1) # 学习到的每一channel的权重
  31. return x * y
  32. class conv_bn_hswish(nn.Module):
  33. """
  34. This equals to
  35. def conv_3x3_bn(inp, oup, stride):
  36. return nn.Sequential(
  37. nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
  38. nn.BatchNorm2d(oup),
  39. h_swish()
  40. )
  41. """
  42. def __init__(self, c1, c2, stride):
  43. super(conv_bn_hswish, self).__init__()
  44. self.conv = nn.Conv2d(c1, c2, 3, stride, 1, bias=False)
  45. self.bn = nn.BatchNorm2d(c2)
  46. self.act = h_swish()
  47. def forward(self, x):
  48. return self.act(self.bn(self.conv(x)))
  49. def fuseforward(self, x):
  50. return self.act(self.conv(x))
  51. class MobileNetV3(nn.Module):
  52. def __init__(self, inp, oup, hidden_dim, kernel_size, stride, use_se, use_hs):
  53. super(MobileNetV3, self).__init__()
  54. assert stride in [1, 2]
  55. self.identity = stride == 1 and inp == oup
  56. # 输入通道数=扩张通道数 则不进行通道扩张
  57. if inp == hidden_dim:
  58. self.conv = nn.Sequential(
  59. # dw
  60. nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
  61. bias=False),
  62. nn.BatchNorm2d(hidden_dim),
  63. h_swish() if use_hs else nn.ReLU(inplace=True),
  64. # Squeeze-and-Excite
  65. SELayer(hidden_dim) if use_se else nn.Sequential(),
  66. # pw-linear
  67. nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
  68. nn.BatchNorm2d(oup),
  69. )
  70. else:
  71. # 否则 先进行通道扩张
  72. self.conv = nn.Sequential(
  73. # pw
  74. nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
  75. nn.BatchNorm2d(hidden_dim),
  76. h_swish() if use_hs else nn.ReLU(inplace=True),
  77. # dw
  78. nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim,
  79. bias=False),
  80. nn.BatchNorm2d(hidden_dim),
  81. # Squeeze-and-Excite
  82. SELayer(hidden_dim) if use_se else nn.Sequential(),
  83. h_swish() if use_hs else nn.ReLU(inplace=True),
  84. # pw-linear
  85. nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
  86. nn.BatchNorm2d(oup),
  87. )
  88. def forward(self, x):
  89. y = self.conv(x)
  90. if self.identity:
  91. return x + y
  92. else:
  93. return y

第二步:在yolo.py的parse_model函数中添加类名

添加内容:

  1. h_sigmoid, h_swish, SELayer, conv_bn_hswish, MobileNetV3

添加效果:

第三步:制作模型配置文件

1、复制models/yolov5s.yaml文件,并重命名

2、根据MobileNetv3的网络结构修改配置文件

根据网络结构我们可以看出MobileNetV3模块包含六个参数[out_ch, hidden_ch, kernel_size, stride, use_se, use_hs]:
• out_ch: 输出通道
• hidden_ch: 表示在Inverted residuals中的扩张通道数
• kernel_size: 卷积核大小
• stride: 步长
• use_se: 表示是否使用 SELayer,使用了是1,不使用是0
• use_hs: 表示使用 h_swish 还是 ReLU,使用h_swish是1,使用 ReLU是0

修改的时候,需要注意/8,/16,/32等位置特征图的变换

3、修改head部分concat的层数

修改完成代码如下:

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. # Parameters
  3. nc: 12 # number of classes
  4. depth_multiple: 0.33 # model depth multiple
  5. width_multiple: 0.50 # layer channel multiple
  6. anchors:
  7. - [10,13, 16,30, 33,23] # P3/8
  8. - [30,61, 62,45, 59,119] # P4/16
  9. - [116,90, 156,198, 373,326] # P5/32
  10. # Mobilenetv3-small backbone
  11. # MobileNetV3_InvertedResidual [out_ch, hid_ch, k_s, stride, SE, HardSwish]
  12. backbone:
  13. # [from, number, module, args]
  14. [[-1, 1, conv_bn_hswish, [16, 2]], # 0-p1/2 320*320
  15. [-1, 1, MobileNetV3, [16, 16, 3, 2, 1, 0]], # 1-p2/4 160*160
  16. [-1, 1, MobileNetV3, [24, 72, 3, 2, 0, 0]], # 2-p3/8 80*80
  17. [-1, 1, MobileNetV3, [24, 88, 3, 1, 0, 0]], # 3 80*80
  18. [-1, 1, MobileNetV3, [40, 96, 5, 2, 1, 1]], # 4-p4/16 40*40
  19. [-1, 1, MobileNetV3, [40, 240, 5, 1, 1, 1]], # 5 40*40
  20. [-1, 1, MobileNetV3, [40, 240, 5, 1, 1, 1]], # 6 40*40
  21. [-1, 1, MobileNetV3, [48, 120, 5, 1, 1, 1]], # 7 40*40
  22. [-1, 1, MobileNetV3, [48, 144, 5, 1, 1, 1]], # 8 40*40
  23. [-1, 1, MobileNetV3, [96, 288, 5, 2, 1, 1]], # 9-p5/32 20*20
  24. [-1, 1, MobileNetV3, [96, 576, 5, 1, 1, 1]], # 10 20*20
  25. [-1, 1, MobileNetV3, [96, 576, 5, 1, 1, 1]], # 11 20*20
  26. ]
  27. # YOLOv5 v6.0 head
  28. head:
  29. [[-1, 1, Conv, [512, 1, 1]],
  30. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  31. [[-1, 8], 1, Concat, [1]], # cat backbone P4
  32. [-1, 3, C3, [512, False]], # 13
  33. [-1, 1, Conv, [256, 1, 1]],
  34. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  35. [[-1, 3, 1, Concat, [1]], # cat backbone P3
  36. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  37. [-1, 1, Conv, [256, 3, 2]],
  38. [[-1, 16], 1, Concat, [1]], # cat head P4
  39. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  40. [-1, 1, Conv, [512, 3, 2]],
  41. [[-1, 12], 1, Concat, [1]], # cat head P5
  42. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  43. [[19, 22, 25], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  44. ]

第四步:验证新加入的主干网络

1、修改yolo.py中以下两个地方

(1)DetectionModel函数下的cfg

(2)parser = argparse.ArgumentParser()下的sfg

2、运行yolo.py

(1)yolov5s_MobileNetV3_Small.yaml

(2)yolov5s.yaml

实验结果对比:可以看到替换主干网络为MobileNetV3_Small之后层数变多,可以学习到更多的特征,参数量从723万减少到338万,GFLOPs由16.6变为6.1

二、改进MobileNetV3_Large

MobileNetV3_Small和MobileNetV3_Large区别在于yaml文件中head中concat连接不同,深度因子和宽度因子不同。前两步参考以上内容

第一步:制作模型配置文件

1、复制models/yolov5s.yaml文件,并重命名

2、根据MobileNetv3的网络结构修改配置文件

3、修改head部分concat的层数

修改完成代码如下:

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. # Parameters
  3. nc: 12 # number of classes
  4. depth_multiple: 0.33 # model depth multiple
  5. width_multiple: 0.50 # layer channel multiple
  6. anchors:
  7. - [10,13, 16,30, 33,23] # P3/8
  8. - [30,61, 62,45, 59,119] # P4/16
  9. - [116,90, 156,198, 373,326] # P5/32
  10. # MobileNetV3_Large
  11. backbone:
  12. [[-1, 1, conv_bn_hswish, [16, 2]], # 0-p1/2
  13. [-1, 1, MobileNetV3, [ 16, 16, 3, 1, 0, 0]], # 1-p1/2
  14. [-1, 1, MobileNetV3, [ 24, 64, 3, 2, 0, 0]], # 2-p2/4
  15. [-1, 1, MobileNetV3, [ 24, 72, 3, 1, 0, 0]], # 3-p2/4
  16. [-1, 1, MobileNetV3, [ 40, 72, 5, 2, 1, 0]], # 4-p3/8
  17. [-1, 1, MobileNetV3, [ 40, 120, 5, 1, 1, 0]], # 5-p3/8
  18. [-1, 1, MobileNetV3, [ 40, 120, 5, 1, 1, 0]], # 6-p3/8
  19. [-1, 1, MobileNetV3, [ 80, 240, 3, 2, 0, 1]], # 7-p4/16
  20. [-1, 1, MobileNetV3, [ 80, 200, 3, 1, 0, 1]], # 8-p4/16
  21. [-1, 1, MobileNetV3, [ 80, 184, 3, 1, 0, 1]], # 9-p4/16
  22. [-1, 1, MobileNetV3, [ 80, 184, 3, 1, 0, 1]], # 10-p4/16
  23. [-1, 1, MobileNetV3, [112, 480, 3, 1, 1, 1]], # 11-p4/16
  24. [-1, 1, MobileNetV3, [112, 672, 3, 1, 1, 1]], # 12-p4/16
  25. [-1, 1, MobileNetV3, [160, 672, 5, 1, 1, 1]], # 13-p4/16
  26. [-1, 1, MobileNetV3, [160, 960, 5, 2, 1, 1]], # 14-p5/32
  27. [-1, 1, MobileNetV3, [160, 960, 5, 1, 1, 1]], # 15-p5/32
  28. ]
  29. # YOLOv5 v6.0 head
  30. head:
  31. [[-1, 1, Conv, [512, 1, 1]],
  32. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  33. [[-1, 13], 1, Concat, [1]], # cat backbone P4
  34. [-1, 3, C3, [512, False]], # 13
  35. [-1, 1, Conv, [256, 1, 1]],
  36. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  37. [[-1, 6], 1, Concat, [1]], # cat backbone P3
  38. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  39. [-1, 1, Conv, [256, 3, 2]],
  40. [[-1, 20], 1, Concat, [1]], # cat head P4
  41. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  42. [-1, 1, Conv, [512, 3, 2]],
  43. [[-1, 16], 1, Concat, [1]], # cat head P5
  44. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  45. [[23, 26, 29], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  46. ]

第二步:验证新加入的主干网络

1、运行yolo.py

(1)yolov5s_MobileNetV3_Large.yaml

可以看到MobileNetV3-large模型比MobileNetV3-small多了更多的MobileNet_Block结构,残差倒置结构中通道数维度也增大了许多,速度比YOLOv5s慢将近一半,但是参数变少,效果介乎MobileNetV3-small和YOLOv5s之间。

三、改进训练

第一步:修改train.py中的cfg参数

将模型配置文件修改为yolov5s_MobileNetV3_Small.yaml

第二步:运行python train.py

开始训练:

训练结束后结果保存到run/train文件夹下~

好了,到这里关于YOLOv5和MobileNetV3结合的改进就完成了!


本文转载自: https://blog.csdn.net/weixin_54678439/article/details/138416752
版权归原作者 雨秒的对望 所有, 如有侵权,请联系我们删除。

“YOLOv5-7.0改进(一)MobileNetv3替换主干网络”的评论:

还没有评论