当前位置:   article > 正文

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

yolov5 mobilenet

前言

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

目录

一、改进MobileNetV3_Small

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

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

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

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

二、改进MobileNetV3_Large

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

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

三、改进训练

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

第二步:运行python train.py


一、改进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)在YOLOv5/v7项目文件下的models/yolo.py中在文件首部添加代码

from models.common import *

(2)添加内容:

h_sigmoid, h_swish, SELayer, conv_bn_hswish, MobileNetV3

(3)添加效果:

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

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
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/894240
    推荐阅读
    相关标签