当前位置:   article > 正文

【YOLOv5/v7改进系列】引入Slimneck-GSConv

【YOLOv5/v7改进系列】引入Slimneck-GSConv
一、导言

GSConv旨在平衡模型的准确度与速度,针对自动驾驶车辆中目标检测任务设计。从类脑研究中得到的直观理解是,具有更多神经元的模型能够获得更强的非线性表达能力。但是,不容忽视的是生物大脑处理信息的强大能力和低能耗远远超过计算机。强大的模型不能仅仅通过无限制地增加模型参数的数量来构建。当前阶段,轻量化设计对于缓解高计算成本是有效的,这主要通过使用深度可分离卷积(DSC)操作来减少参数量和浮点运算次数(FLOPs)来实现,效果明显。

尽管如此,DSC的缺点也很明显:在计算过程中,输入图像的通道信息被分开处理。这种缺陷导致DSC在特征提取和融合能力上远低于标准卷积(SC)。标准卷积在整个输出上保持了通道间的相互作用,而DSC则是先进行深度卷积再进行逐点卷积,虽然降低了计算成本,但也牺牲了通道间的信息交互。

为了解决这一问题,研究者提出了GSConv,这是一种新的轻量级卷积技术,旨在减轻模型重量的同时保持准确性,实现了模型准确度和速度之间的良好平衡。GSConv通过混合使用标准卷积、DSC和通道混洗(shuffle)来工作,使DSC的输出尽可能接近标准卷积的结果,从而提高了特征表示能力。这种方法允许来自标准卷积的信息充分混合到DSC产生的信息中,同时避免了额外的计算开销。

此外,文章还提出了一种设计范式“slim-neck”,即仅在模型的“neck”部分使用GSConv,而非整个模型。在neck阶段,特征图已经过足够的压缩(通道维度达到最大,而宽度和高度维度减至最小),且转换变得适度。此时,利用GSConv处理拼接的特征图恰到好处,因为它可以减少冗余重复信息,无需进一步压缩,并且注意力模块如SPP和CA等可以更好地发挥作用。

GSConv与slim-neck的优势和劣势可以从几个方面来解释:

优点:
  1. 提高效率与准确性平衡:GSConv通过结合标准卷积(SC)、深度可分离卷积(DSC)以及通道混洗操作,能够在不显著增加计算成本的情况下,增强模型的非线性表达能力,从而在保持轻量化的同时提升检测精度。

  2. 增强特征提取与融合:与仅使用DSC相比,GSConv通过混入SC产生的信息,使得输出特征图更接近于使用SC的结果,提高了特征的提取与融合能力。这在处理小物体检测时尤为重要,有助于提高平均精度(mAP)。

  3. 针对性应用:特别是在“细颈”(slim-neck)设计范式中,GSConv被应用于模型的中间连接部分,即neck,这时特征图已经过初步处理,变得较为精简,且宽度和高度减小到最小,而通道数达到最大。此时使用GSConv能有效处理这些特征,避免冗余信息重复压缩,同时促进注意力机制如空间金字塔池化(SPP)和通道注意力(CA)模块更好地工作。

  4. 轻量化设计:GSConv的设计有助于减轻高计算成本,对于车载边缘计算平台而言,能够在满足实时检测需求的同时,保持模型的轻便性,这对于工业应用至关重要。

缺点:
  1. 模型复杂度:虽然GSConv提升了性能,但若在整个模型的所有阶段都使用GSConv,会导致网络层加深,增加数据流的阻力并显著增加推理时间。这可能对资源有限的设备构成挑战。

  2. 实现难度:文中提到,为了在不引入额外浮点运算的情况下完成混洗操作,需要采取特定策略,比如转置操作或线性操作。这些非标准操作可能在某些硬件设备上不受支持,增加了实现的复杂度。

  3. 设计与优化要求:有效利用GSConv要求对模型架构有深入理解并进行细致调整,以确保在特定层(如neck部分)应用GSConv能带来最大的效益,这可能需要额外的调优工作。

综上所述,GSConv通过创新的结构设计为轻量级目标检测模型提供了新的解决方案,尤其在自动驾驶领域,它能够帮助模型在保持高效运行的同时,提高对小目标的检测精度,但其应用也需考虑模型复杂度增加和实现上的局限性。

二、准备工作

首先在YOLOv5/v7的models文件夹下新建文件slimneck.py,导入如下代码

  1. from models.common import *
  2. class GSConv(nn.Module):
  3. # GSConv https://github.com/AlanLi1997/slim-neck-by-gsconv
  4. def __init__(self, c1, c2, k=1, s=1, g=1, act=True):
  5. super().__init__()
  6. c_ = c2 // 2
  7. self.cv1 = Conv(c1, c_, k, s, None, g, act)
  8. self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)
  9. def forward(self, x):
  10. x1 = self.cv1(x)
  11. x2 = torch.cat((x1, self.cv2(x1)), 1)
  12. # shuffle
  13. b, n, h, w = x2.data.size()
  14. b_n = b * n // 2
  15. y = x2.reshape(b_n, 2, h * w)
  16. y = y.permute(1, 0, 2)
  17. y = y.reshape(2, -1, n // 2, h, w)
  18. return torch.cat((y[0], y[1]), 1)
  19. class GSBottleneck(nn.Module):
  20. # GS Bottleneck https://github.com/AlanLi1997/slim-neck-by-gsconv
  21. def __init__(self, c1, c2, k=3, s=1):
  22. super().__init__()
  23. c_ = c2 // 2
  24. # for lighting
  25. self.conv_lighting = nn.Sequential(
  26. GSConv(c1, c_, 1, 1),
  27. GSConv(c_, c2, 1, 1, act=False))
  28. # for receptive field
  29. self.conv = nn.Sequential(
  30. GSConv(c1, c_, 3, 1),
  31. GSConv(c_, c2, 3, 1, act=False))
  32. self.shortcut = Conv(c1, c2, 3, 1, act=False)
  33. def forward(self, x):
  34. return self.conv_lighting(x) + self.shortcut(x)
  35. class VoVGSCSP(nn.Module):
  36. # VoV-GSCSP https://github.com/AlanLi1997/slim-neck-by-gsconv
  37. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
  38. super().__init__()
  39. c_ = int(c2 * e)
  40. self.cv1 = Conv(c1, c_, 1, 1)
  41. self.cv2 = Conv(2 * c_, c2, 1)
  42. self.m = nn.Sequential(*(GSBottleneck(c_, c_) for _ in range(n)))
  43. def forward(self, x):
  44. x1 = self.cv1(x)
  45. return self.cv2(torch.cat((self.m(x1), x1), dim=1))

其次在在YOLOv5/v7项目文件下的models/yolo.py中在文件首部添加代码

from models.slimneck import GSConv, VoVGSCSP

并搜索def parse_model(d, ch)

定位到如下行添加以下代码

GSConv, VoVGSCSP,

三、YOLOv7-tiny改进工作

完成二后,在YOLOv7项目文件下的models文件夹下创建新的文件yolov7-tiny-slimneck.yaml,导入如下代码。

  1. # parameters
  2. nc: 80 # number of classes
  3. depth_multiple: 1.0 # model depth multiple
  4. width_multiple: 1.0 # layer channel multiple
  5. # anchors
  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. # yolov7-tiny backbone
  11. backbone:
  12. # [from, number, module, args] c2, k=1, s=1, p=None, g=1, act=True
  13. [[-1, 1, Conv, [32, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 0-P1/2
  14. [-1, 1, Conv, [64, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 1-P2/4
  15. [-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  16. [-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  17. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  18. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  19. [[-1, -2, -3, -4], 1, Concat, [1]],
  20. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 7
  21. [-1, 1, MP, []], # 8-P3/8
  22. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  23. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  24. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  25. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  26. [[-1, -2, -3, -4], 1, Concat, [1]],
  27. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 14
  28. [-1, 1, MP, []], # 15-P4/16
  29. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  30. [-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  31. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  32. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  33. [[-1, -2, -3, -4], 1, Concat, [1]],
  34. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 21
  35. [-1, 1, MP, []], # 22-P5/32
  36. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  37. [-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  38. [-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  39. [-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  40. [[-1, -2, -3, -4], 1, Concat, [1]],
  41. [-1, 1, Conv, [512, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 28
  42. ]
  43. # yolov7-tiny head
  44. head:
  45. [[-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  46. [-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  47. [-1, 1, SP, [5]],
  48. [-2, 1, SP, [9]],
  49. [-3, 1, SP, [13]],
  50. [[-1, -2, -3, -4], 1, Concat, [1]],
  51. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  52. [[-1, -7], 1, Concat, [1]],
  53. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 37
  54. [-1, 1, GSConv, [128, 1, 1]],
  55. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  56. [21, 1, GSConv, [128, 1, 1]], # route backbone P4
  57. [[-1, -2], 1, Concat, [1]],
  58. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  59. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  60. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  61. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  62. [[-1, -2, -3, -4], 1, Concat, [1]],
  63. [-1, 1, VoVGSCSP, [128, 1, 1]], # 47
  64. [-1, 1, GSConv, [64, 1, 1]],
  65. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  66. [14, 1, GSConv, [64, 1, 1]], # route backbone P3
  67. [[-1, -2], 1, Concat, [1]],
  68. [-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  69. [-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  70. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  71. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  72. [[-1, -2, -3, -4], 1, Concat, [1]],
  73. [-1, 1, VoVGSCSP, [64, 1, 1]], # 57
  74. [-1, 1, Conv, [128, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
  75. [[-1, 47], 1, Concat, [1]],
  76. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  77. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  78. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  79. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  80. [[-1, -2, -3, -4], 1, Concat, [1]],
  81. [-1, 1, VoVGSCSP, [128, 1, 1]], # 65
  82. [-1, 1, Conv, [256, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
  83. [[-1, 37], 1, Concat, [1]],
  84. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  85. [-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  86. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  87. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  88. [[-1, -2, -3, -4], 1, Concat, [1]],
  89. [-1, 1, VoVGSCSP, [256, 1, 1]], # 73
  90. [57, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  91. [65, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  92. [73, 1, Conv, [512, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  93. [[74,75,76], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
  94. ]
  1. from n params module arguments
  2. 0 -1 1 928 models.common.Conv [3, 32, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]
  3. 1 -1 1 18560 models.common.Conv [32, 64, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]
  4. 2 -1 1 2112 models.common.Conv [64, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  5. 3 -2 1 2112 models.common.Conv [64, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  6. 4 -1 1 9280 models.common.Conv [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  7. 5 -1 1 9280 models.common.Conv [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  8. 6 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  9. 7 -1 1 8320 models.common.Conv [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  10. 8 -1 1 0 models.common.MP []
  11. 9 -1 1 4224 models.common.Conv [64, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  12. 10 -2 1 4224 models.common.Conv [64, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  13. 11 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  14. 12 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  15. 13 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  16. 14 -1 1 33024 models.common.Conv [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  17. 15 -1 1 0 models.common.MP []
  18. 16 -1 1 16640 models.common.Conv [128, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  19. 17 -2 1 16640 models.common.Conv [128, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  20. 18 -1 1 147712 models.common.Conv [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  21. 19 -1 1 147712 models.common.Conv [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  22. 20 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  23. 21 -1 1 131584 models.common.Conv [512, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  24. 22 -1 1 0 models.common.MP []
  25. 23 -1 1 66048 models.common.Conv [256, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  26. 24 -2 1 66048 models.common.Conv [256, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  27. 25 -1 1 590336 models.common.Conv [256, 256, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  28. 26 -1 1 590336 models.common.Conv [256, 256, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  29. 27 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  30. 28 -1 1 525312 models.common.Conv [1024, 512, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  31. 29 -1 1 131584 models.common.Conv [512, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  32. 30 -2 1 131584 models.common.Conv [512, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  33. 31 -1 1 0 models.common.SP [5]
  34. 32 -2 1 0 models.common.SP [9]
  35. 33 -3 1 0 models.common.SP [13]
  36. 34 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  37. 35 -1 1 262656 models.common.Conv [1024, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  38. 36 [-1, -7] 1 0 models.common.Concat [1]
  39. 37 -1 1 131584 models.common.Conv [512, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  40. 38 -1 1 18240 models.gsconv.GSConv [256, 128, 1, 1]
  41. 39 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
  42. 40 21 1 18240 models.gsconv.GSConv [256, 128, 1, 1]
  43. 41 [-1, -2] 1 0 models.common.Concat [1]
  44. 42 -1 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  45. 43 -2 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  46. 44 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  47. 45 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  48. 46 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  49. 47 -1 1 93408 models.gsconv.VoVGSCSP [256, 128, 1, 1]
  50. 48 -1 1 5024 models.gsconv.GSConv [128, 64, 1, 1]
  51. 49 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
  52. 50 14 1 5024 models.gsconv.GSConv [128, 64, 1, 1]
  53. 51 [-1, -2] 1 0 models.common.Concat [1]
  54. 52 -1 1 4160 models.common.Conv [128, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  55. 53 -2 1 4160 models.common.Conv [128, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  56. 54 -1 1 9280 models.common.Conv [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  57. 55 -1 1 9280 models.common.Conv [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  58. 56 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  59. 57 -1 1 24176 models.gsconv.VoVGSCSP [128, 64, 1, 1]
  60. 58 -1 1 73984 models.common.Conv [64, 128, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]
  61. 59 [-1, 47] 1 0 models.common.Concat [1]
  62. 60 -1 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  63. 61 -2 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  64. 62 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  65. 63 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  66. 64 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  67. 65 -1 1 93408 models.gsconv.VoVGSCSP [256, 128, 1, 1]
  68. 66 -1 1 295424 models.common.Conv [128, 256, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]
  69. 67 [-1, 37] 1 0 models.common.Concat [1]
  70. 68 -1 1 65792 models.common.Conv [512, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  71. 69 -2 1 65792 models.common.Conv [512, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  72. 70 -1 1 147712 models.common.Conv [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  73. 71 -1 1 147712 models.common.Conv [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  74. 72 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
  75. 73 -1 1 367040 models.gsconv.VoVGSCSP [512, 256, 1, 1]
  76. 74 57 1 73984 models.common.Conv [64, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  77. 75 65 1 295424 models.common.Conv [128, 256, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  78. 76 73 1 1180672 models.common.Conv [256, 512, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
  79. 77 [74, 75, 76] 1 17132 models.yolo.IDetect [1, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
  80. Model Summary: 479 layers, 6350908 parameters, 6350908 gradients, 13.6 GFLOPS

运行后若打印出如上文本代表改进成功。

四、YOLOv5s改进工作

完成二后,在YOLOv5项目文件下的models文件夹下创建新的文件yolov5s-slimneck.yaml,导入如下代码。

  1. # Parameters
  2. nc: 80 # number of classes
  3. depth_multiple: 0.33 # model depth multiple
  4. width_multiple: 0.50 # layer channel multiple
  5. anchors:
  6. - [10,13, 16,30, 33,23] # P3/8
  7. - [30,61, 62,45, 59,119] # P4/16
  8. - [116,90, 156,198, 373,326] # P5/32
  9. # YOLOv5 v6.0 backbone
  10. backbone:
  11. # [from, number, module, args]
  12. [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  13. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  14. [-1, 3, C3, [128]],
  15. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  16. [-1, 6, C3, [256]],
  17. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  18. [-1, 9, C3, [512]],
  19. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  20. [-1, 3, C3, [1024]],
  21. [-1, 1, SPPF, [1024, 5]], # 9
  22. ]
  23. # YOLOv5 v6.0 head
  24. head:
  25. [[-1, 1, GSConv, [512, 1, 1]],
  26. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  27. [[-1, 6], 1, Concat, [1]], # cat backbone P4
  28. [-1, 3, VoVGSCSP, [512]], # 13
  29. [-1, 1, GSConv, [256, 1, 1]],
  30. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  31. [[-1, 4], 1, Concat, [1]], # cat backbone P3
  32. [-1, 3, VoVGSCSP, [256]], # 17 (P3/8-small)
  33. [-1, 1, GSConv, [256, 3, 2]],
  34. [[-1, 14], 1, Concat, [1]], # cat head P4
  35. [-1, 3, VoVGSCSP, [512]], # 20 (P4/16-medium)
  36. [-1, 1, GSConv, [512, 3, 2]],
  37. [[-1, 10], 1, Concat, [1]], # cat head P5
  38. [-1, 3, VoVGSCSP, [1024]], # 23 (P5/32-large)
  39. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  40. ]
  1. from n params module arguments
  2. 0 -1 1 3520 models.common.Conv [3, 32, 6, 2, 2]
  3. 1 -1 1 18560 models.common.Conv [32, 64, 3, 2]
  4. 2 -1 1 18816 models.common.C3 [64, 64, 1]
  5. 3 -1 1 73984 models.common.Conv [64, 128, 3, 2]
  6. 4 -1 2 115712 models.common.C3 [128, 128, 2]
  7. 5 -1 1 295424 models.common.Conv [128, 256, 3, 2]
  8. 6 -1 3 625152 models.common.C3 [256, 256, 3]
  9. 7 -1 1 1180672 models.common.Conv [256, 512, 3, 2]
  10. 8 -1 1 1182720 models.common.C3 [512, 512, 1]
  11. 9 -1 1 656896 models.common.SPPF [512, 512, 5]
  12. 10 -1 1 69248 models.slimneck.GSConv [512, 256, 1, 1]
  13. 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
  14. 12 [-1, 6] 1 0 models.common.Concat [1]
  15. 13 -1 1 367040 models.slimneck.VoVGSCSP [512, 256]
  16. 14 -1 1 18240 models.slimneck.GSConv [256, 128, 1, 1]
  17. 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
  18. 16 [-1, 4] 1 0 models.common.Concat [1]
  19. 17 -1 1 93408 models.slimneck.VoVGSCSP [256, 128]
  20. 18 -1 1 75584 models.slimneck.GSConv [128, 128, 3, 2]
  21. 19 [-1, 14] 1 0 models.common.Concat [1]
  22. 20 -1 1 334272 models.slimneck.VoVGSCSP [256, 256]
  23. 21 -1 1 298624 models.slimneck.GSConv [256, 256, 3, 2]
  24. 22 [-1, 10] 1 0 models.common.Concat [1]
  25. 23 -1 1 1323904 models.slimneck.VoVGSCSP [512, 512]
  26. 24 [17, 20, 23] 1 16182 models.yolo.Detect [1, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
  27. Model Summary: 410 layers, 6767958 parameters, 6767958 gradients, 14.7 GFLOPs

运行后若打印出如上文本代表改进成功。

五、YOLOv5n改进工作

完成二后,在YOLOv5项目文件下的models文件夹下创建新的文件yolov5n-slimneck.yaml,导入如下代码。

  1. # Parameters
  2. nc: 80 # number of classes
  3. depth_multiple: 0.33 # model depth multiple
  4. width_multiple: 0.25 # layer channel multiple
  5. anchors:
  6. - [10,13, 16,30, 33,23] # P3/8
  7. - [30,61, 62,45, 59,119] # P4/16
  8. - [116,90, 156,198, 373,326] # P5/32
  9. # YOLOv5 v6.0 backbone
  10. backbone:
  11. # [from, number, module, args]
  12. [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  13. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  14. [-1, 3, C3, [128]],
  15. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  16. [-1, 6, C3, [256]],
  17. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  18. [-1, 9, C3, [512]],
  19. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  20. [-1, 3, C3, [1024]],
  21. [-1, 1, SPPF, [1024, 5]], # 9
  22. ]
  23. # YOLOv5 v6.0 head
  24. head:
  25. [[-1, 1, GSConv, [512, 1, 1]],
  26. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  27. [[-1, 6], 1, Concat, [1]], # cat backbone P4
  28. [-1, 3, VoVGSCSP, [512]], # 13
  29. [-1, 1, GSConv, [256, 1, 1]],
  30. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  31. [[-1, 4], 1, Concat, [1]], # cat backbone P3
  32. [-1, 3, VoVGSCSP, [256]], # 17 (P3/8-small)
  33. [-1, 1, GSConv, [256, 3, 2]],
  34. [[-1, 14], 1, Concat, [1]], # cat head P4
  35. [-1, 3, VoVGSCSP, [512]], # 20 (P4/16-medium)
  36. [-1, 1, GSConv, [512, 3, 2]],
  37. [[-1, 10], 1, Concat, [1]], # cat head P5
  38. [-1, 3, VoVGSCSP, [1024]], # 23 (P5/32-large)
  39. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  40. ]
  1. from n params module arguments
  2. 0 -1 1 1760 models.common.Conv [3, 16, 6, 2, 2]
  3. 1 -1 1 4672 models.common.Conv [16, 32, 3, 2]
  4. 2 -1 1 4800 models.common.C3 [32, 32, 1]
  5. 3 -1 1 18560 models.common.Conv [32, 64, 3, 2]
  6. 4 -1 2 29184 models.common.C3 [64, 64, 2]
  7. 5 -1 1 73984 models.common.Conv [64, 128, 3, 2]
  8. 6 -1 3 156928 models.common.C3 [128, 128, 3]
  9. 7 -1 1 295424 models.common.Conv [128, 256, 3, 2]
  10. 8 -1 1 296448 models.common.C3 [256, 256, 1]
  11. 9 -1 1 164608 models.common.SPPF [256, 256, 5]
  12. 10 -1 1 18240 models.slimneck.GSConv [256, 128, 1, 1]
  13. 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
  14. 12 [-1, 6] 1 0 models.common.Concat [1]
  15. 13 -1 1 93408 models.slimneck.VoVGSCSP [256, 128]
  16. 14 -1 1 5024 models.slimneck.GSConv [128, 64, 1, 1]
  17. 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
  18. 16 [-1, 4] 1 0 models.common.Concat [1]
  19. 17 -1 1 24176 models.slimneck.VoVGSCSP [128, 64]
  20. 18 -1 1 19360 models.slimneck.GSConv [64, 64, 3, 2]
  21. 19 [-1, 14] 1 0 models.common.Concat [1]
  22. 20 -1 1 85216 models.slimneck.VoVGSCSP [128, 128]
  23. 21 -1 1 75584 models.slimneck.GSConv [128, 128, 3, 2]
  24. 22 [-1, 10] 1 0 models.common.Concat [1]
  25. 23 -1 1 334272 models.slimneck.VoVGSCSP [256, 256]
  26. 24 [17, 20, 23] 1 8118 models.yolo.Detect [1, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [64, 128, 256]]
  27. Model Summary: 410 layers, 1709766 parameters, 1709766 gradients, 3.9 GFLOPs

运行后打印如上代码说明改进成功。

更多文章产出中,主打简洁和准确,欢迎关注我,共同探讨!

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

闽ICP备14008679号