当前位置:   article > 正文

YOLOv8改进 更换轻量化模型MobileNetV3_yolov8轻量化

yolov8轻量化

一、MobileNetV3论文

论文地址:1905.02244.pdf (arxiv.org)

二、 MobileNetV3网络结构

MobileNetV3引入了一种新的操作单元,称为"Mobile Inverted Residual Bottleneck",它由一个1x1卷积层和一个3x3深度可分离卷积层组成。这个操作单元通过使用非线性激活函数,如ReLU6,并且在残差连接中使用线性投影,来提高网络的特征表示能力。

MobileNetV3使用全局平均池化层来降低特征图的维度,并使用一个1x1卷积层将特征图的通道数压缩成最终的类别数量。最后,使用softmax函数对输出进行归一化,得到每个类别的概率分布。

MobileNetV3是一种高效轻量级的网络结构,可以在移动设备和资源受限的环境下进行实时图像分类和目标检测任务。它在准确性和计算效率之间取得了良好的平衡。

三、代码实现

1、在ultralytics\ultralytics\nn路径下新建一个文件夹命名为backbone,用于存放网络结构修改的代码。

并在该 backbone文件夹路径下新建py文件MobileNetV3.py,并在该文件里添加MobileNetV3相关的结构的代码:

  1. from torch import nn
  2. # ###### Mobilenetv3
  3. class h_sigmoid(nn.Module):
  4. def __init__(self, inplace=True):
  5. super(h_sigmoid, self).__init__()
  6. self.relu = nn.ReLU6(inplace=inplace)
  7. def forward(self, x):
  8. return self.relu(x + 3) / 6
  9. class SELayer(nn.Module):
  10. def __init__(self, channel, reduction=4):
  11. super(SELayer, self).__init__()
  12. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  13. self.fc = nn.Sequential(
  14. nn.Linear(channel, channel // reduction),
  15. nn.ReLU(inplace=True),
  16. nn.Linear(channel // reduction, channel),
  17. h_sigmoid()
  18. )
  19. def forward(self, x):
  20. b, c, _, _ = x.size()
  21. y = self.avg_pool(x)
  22. y = y.view(b, c)
  23. y = self.fc(y).view(b, c, 1, 1)
  24. return x * y
  25. class conv_bn_hswish(nn.Module):
  26. def __init__(self, c1, c2, stride):
  27. super(conv_bn_hswish, self).__init__()
  28. self.conv = nn.Conv2d(c1, c2, 3, stride, 1, bias=False)
  29. self.bn = nn.BatchNorm2d(c2)
  30. # self.act = h_swish()
  31. self.act = nn.Hardswish(inplace=True)
  32. def forward(self, x):
  33. return self.act(self.bn(self.conv(x)))
  34. def fuseforward(self, x):
  35. return self.act(self.conv(x))
  36. class MobileNetV3_InvertedResidual(nn.Module):
  37. def __init__(self, inp, oup, hidden_dim, kernel_size, stride, use_se, use_hs):
  38. super(MobileNetV3_InvertedResidual, self).__init__()
  39. assert stride in [1, 2]
  40. self.identity = stride == 1 and inp == oup
  41. if inp == hidden_dim:
  42. self.conv = nn.Sequential(
  43. # dw
  44. nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False),
  45. nn.BatchNorm2d(hidden_dim),
  46. # h_swish() if use_hs else nn.ReLU(inplace=True),
  47. nn.Hardswish(inplace=True) if use_hs else nn.ReLU(inplace=True),
  48. # Squeeze-and-Excite
  49. SELayer(hidden_dim) if use_se else nn.Sequential(),
  50. # pw-linear
  51. nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
  52. nn.BatchNorm2d(oup),
  53. )
  54. else:
  55. self.conv = nn.Sequential(
  56. # pw
  57. nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False),
  58. nn.BatchNorm2d(hidden_dim),
  59. # h_swish() if use_hs else nn.ReLU(inplace=True),
  60. nn.Hardswish(inplace=True) if use_hs else nn.ReLU(inplace=True),
  61. # dw
  62. nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False),
  63. nn.BatchNorm2d(hidden_dim),
  64. # Squeeze-and-Excite
  65. SELayer(hidden_dim) if use_se else nn.Sequential(),
  66. # h_swish() if use_hs else nn.ReLU(inplace=True),
  67. nn.Hardswish(inplace=True) if use_hs else nn.ReLU(inplace=True),
  68. # pw-linear
  69. nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False),
  70. nn.BatchNorm2d(oup),
  71. )
  72. def forward(self, x):
  73. y = self.conv(x)
  74. if self.identity:
  75. return x + y
  76. else:
  77. return y

2、在ultralytics\ultralytics\nn\tasks.py文件中加入MobileNetV3模块

开头先从新建的文件夹引入MobileNetV3的包:

from ultralytics.nn.backbone.MobileNetV3 import *

并且文件的def _predict_once函数模块要替换为更换网络结构后的预测模块:

替换为:

  1. def _predict_once(self, x, profile=False, visualize=False):
  2. """
  3. Perform a forward pass through the network.
  4. Args:
  5. x (torch.Tensor): The input tensor to the model.
  6. profile (bool): Print the computation time of each layer if True, defaults to False.
  7. visualize (bool): Save the feature maps of the model if True, defaults to False.
  8. Returns:
  9. (torch.Tensor): The last output of the model.
  10. """
  11. y, dt = [], [] # outputs
  12. for m in self.model:
  13. if m.f != -1: # if not from previous layer
  14. x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
  15. if profile:
  16. self._profile_one_layer(m, x, dt)
  17. if hasattr(m, 'backbone'):
  18. x = m(x)
  19. for _ in range(5 - len(x)):
  20. x.insert(0, None)
  21. for i_idx, i in enumerate(x):
  22. if i_idx in self.save:
  23. y.append(i)
  24. else:
  25. y.append(None)
  26. # for i in x:
  27. # if i is not None:
  28. # print(i.size())
  29. x = x[-1]
  30. else:
  31. x = m(x) # run
  32. y.append(x if m.i in self.save else None) # save output
  33. if visualize:
  34. feature_visualization(x, m.type, m.i, save_dir=visualize)
  35. return x

然后在def parse_model函数模块中加入MobileNetV3:

  1. elif m in {conv_bn_hswish, MobileNetV3_InvertedResidual}:
  2. c1, c2 = ch[f], args[0]
  3. if c2 != nc: # if not output
  4. c2 = make_divisible(min(c2, max_channels) * width, 8)
  5. args = [c1, c2, *args[1:]]

3、创建yolov8+MobileNetV3.yaml文件:

  1. # Ultralytics YOLO
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/615084
    推荐阅读
    相关标签