• 【YOLOv5/v7改进系列】改进池化层为YOLOv9的SPPELAN


    一、导言

    YOLOv9提出了一种新的方法和架构,旨在解决深度神经网络中的信息瓶颈问题并提升模型的性能。以下是该研究的主要优点:

    1. 理论分析与创新

      提出了可编程梯度信息(PGI)的概念,以应对深度网络为实现多重目标所需的各种变化,确保目标任务可以获得完整的输入信息来计算损失函数,从而获得可靠的梯度信息更新网络权重。分析了现有深度神经网络架构从可逆函数的角度,成功解释了过去难以理解的许多现象,这有助于更深入地理解网络内部的信息流。
    2. 解决信息瓶颈问题

      设计了辅助可逆分支,避免了在推理阶段增加大量成本的同时,解决了传统可逆架构在深层网络中保持完整信息的难题,允许主分支从辅助分支获取可靠梯度信息,以协助提取正确和重要的信息。
    3. 高效网络架构

      提出了一种新型轻量化网络架构——Generalized Efficient Layer Aggregation Network(GELAN),基于梯度路径规划设计,它仅使用常规卷积操作就达到了比基于深度分离卷积的最新技术更高的参数利用率,同时表现出轻量、快速和准确的特点。
    4. 广泛适用性

      PGI机制不仅适用于极其深层的神经网络,也适用于轻量级模型,克服了深度监督只能用于非常深的网络架构的问题,使新架构能够应用于日常生活场景。
    5. 实验验证与结果

      在MS COCO数据集上的实验表明,结合PGI和GELAN设计的新一代YOLO系列对象检测系统(YOLOv9)在所有比较中都取得了顶级性能,尤其在实时对象检测方面超越了现有技术。
    6. 灵活性与通用性

      辅助可逆分支可以在推理阶段移除,保留原始网络的推理能力;并且可以自由选择任何可逆架构作为PGI的辅助可逆分支,增强了模型设计的灵活性。

    二、准备工作

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

    1. from models.common import *
    2. # SPPELAN
    3. # https://arxiv.org/pdf/2402.13616
    4. def autopad(k, p=None, d=1): # kernel, padding, dilation
    5. # Pad to 'same' shape outputs
    6. if d > 1:
    7. k = (
    8. d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k]
    9. ) # actual kernel-size
    10. if p is None:
    11. p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
    12. return p
    13. class Conv(nn.Module):
    14. # Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)
    15. default_act = nn.SiLU() # default activation
    16. def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
    17. super().__init__()
    18. self.conv = nn.Conv2d(
    19. c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False
    20. )
    21. self.bn = nn.BatchNorm2d(c2)
    22. self.act = (
    23. self.default_act
    24. if act is True
    25. else act
    26. if isinstance(act, nn.Module)
    27. else nn.Identity()
    28. )
    29. def forward(self, x):
    30. return self.act(self.bn(self.conv(x)))
    31. def forward_fuse(self, x):
    32. return self.act(self.conv(x))
    33. class SP(nn.Module):
    34. def __init__(self, k=3, s=1):
    35. super(SP, self).__init__()
    36. self.m = nn.MaxPool2d(kernel_size=k, stride=s, padding=k // 2)
    37. def forward(self, x):
    38. return self.m(x)
    39. class SPPELAN(nn.Module):
    40. def __init__(
    41. self, c1, c2, c3
    42. ): # ch_in, ch_out, number, shortcut, groups, expansion
    43. super().__init__()
    44. self.c = c3
    45. self.cv1 = Conv(c1, c3, 1, 1)
    46. self.cv2 = SP(5)
    47. self.cv3 = SP(5)
    48. self.cv4 = SP(5)
    49. self.cv5 = Conv(4 * c3, c2, 1, 1)
    50. def forward(self, x):
    51. y = [self.cv1(x)]
    52. y.extend(m(y[-1]) for m in [self.cv2, self.cv3, self.cv4])
    53. return self.cv5(torch.cat(y, 1))

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

    from models.sppelan import SPPELAN

    并搜索def parse_model(d, ch)

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

    SPPELAN,

    三、YOLOv7-tiny改进工作

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

    1. nc: 80 # number of classes
    2. depth_multiple: 1.0 # model depth multiple
    3. width_multiple: 1.0 # layer channel multiple
    4. # anchors
    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. # yolov7-tiny backbone
    10. backbone:
    11. # [from, number, module, args] c2, k=1, s=1, p=None, g=1, act=True
    12. [[-1, 1, Conv, [32, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 0-P1/2
    13. [-1, 1, Conv, [64, 3, 2, None, 1, nn.LeakyReLU(0.1)]], # 1-P2/4
    14. [-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    15. [-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    16. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    17. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    18. [[-1, -2, -3, -4], 1, Concat, [1]],
    19. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 7
    20. [-1, 1, MP, []], # 8-P3/8
    21. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    22. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    23. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    24. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    25. [[-1, -2, -3, -4], 1, Concat, [1]],
    26. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 14
    27. [-1, 1, MP, []], # 15-P4/16
    28. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    29. [-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    30. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    31. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    32. [[-1, -2, -3, -4], 1, Concat, [1]],
    33. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 21
    34. [-1, 1, MP, []], # 22-P5/32
    35. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    36. [-2, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    37. [-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    38. [-1, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    39. [[-1, -2, -3, -4], 1, Concat, [1]],
    40. [-1, 1, Conv, [512, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 28
    41. ]
    42. # yolov7-tiny head
    43. head:
    44. [[-1, 1, SPPELAN, [256, 128]], # 29
    45. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    46. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
    47. [21, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P4
    48. [[-1, -2], 1, Concat, [1]],
    49. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    50. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    51. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    52. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    53. [[-1, -2, -3, -4], 1, Concat, [1]],
    54. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 39
    55. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    56. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
    57. [14, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P3
    58. [[-1, -2], 1, Concat, [1]],
    59. [-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    60. [-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    61. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    62. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    63. [[-1, -2, -3, -4], 1, Concat, [1]],
    64. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 49
    65. [-1, 1, Conv, [128, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
    66. [[-1, 39], 1, Concat, [1]],
    67. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    68. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    69. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    70. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    71. [[-1, -2, -3, -4], 1, Concat, [1]],
    72. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 57
    73. [-1, 1, Conv, [256, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
    74. [[-1, 29], 1, Concat, [1]],
    75. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    76. [-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
    77. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    78. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    79. [[-1, -2, -3, -4], 1, Concat, [1]],
    80. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 65
    81. [49, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    82. [57, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    83. [65, 1, Conv, [512, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
    84. [[66, 67, 68], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
    85. ]
    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 197376 models.sppelan.SPPELAN [512, 256, 128]
    32. 30 -1 1 33024 models.common.Conv [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    33. 31 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
    34. 32 21 1 33024 models.common.Conv [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    35. 33 [-1, -2] 1 0 models.common.Concat [1]
    36. 34 -1 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    37. 35 -2 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    38. 36 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    39. 37 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    40. 38 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
    41. 39 -1 1 33024 models.common.Conv [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    42. 40 -1 1 8320 models.common.Conv [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    43. 41 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
    44. 42 14 1 8320 models.common.Conv [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    45. 43 [-1, -2] 1 0 models.common.Concat [1]
    46. 44 -1 1 4160 models.common.Conv [128, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    47. 45 -2 1 4160 models.common.Conv [128, 32, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    48. 46 -1 1 9280 models.common.Conv [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    49. 47 -1 1 9280 models.common.Conv [32, 32, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    50. 48 [-1, -2, -3, -4] 1 0 models.common.Concat [1]
    51. 49 -1 1 8320 models.common.Conv [128, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    52. 50 -1 1 73984 models.common.Conv [64, 128, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]
    53. 51 [-1, 39] 1 0 models.common.Concat [1]
    54. 52 -1 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    55. 53 -2 1 16512 models.common.Conv [256, 64, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    56. 54 -1 1 36992 models.common.Conv [64, 64, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    57. 55 -1 1 36992 models.common.Conv [64, 64, 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 33024 models.common.Conv [256, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    60. 58 -1 1 295424 models.common.Conv [128, 256, 3, 2, None, 1, LeakyReLU(negative_slope=0.1)]
    61. 59 [-1, 29] 1 0 models.common.Concat [1]
    62. 60 -1 1 65792 models.common.Conv [512, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    63. 61 -2 1 65792 models.common.Conv [512, 128, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    64. 62 -1 1 147712 models.common.Conv [128, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    65. 63 -1 1 147712 models.common.Conv [128, 128, 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 131584 models.common.Conv [512, 256, 1, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    68. 66 49 1 73984 models.common.Conv [64, 128, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    69. 67 57 1 295424 models.common.Conv [128, 256, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    70. 68 65 1 1180672 models.common.Conv [256, 512, 3, 1, None, 1, LeakyReLU(negative_slope=0.1)]
    71. 69 [66, 67, 68] 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]]
    72. Model Summary: 253 layers, 5554956 parameters, 5554956 gradients, 12.8 GFLOPS

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

    四、YOLOv5s改进工作

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

    1. # Parameters
    2. nc: 1 # 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, ASPP, [1024]], # 9
    22. #[-1, 1, BasicRFB, [1024]],
    23. #[-1, 1, SimSPPF, [1024, 5]],
    24. [-1, 1, SPPELAN, [1024, 5]],
    25. #[-1, 1, SPP, [1024]],
    26. #[-1, 1, SPPF, [1024, 5]], # 9
    27. #[-1, 1, SPPCSPC, [1024]],
    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, 6], 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, 4], 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, 14], 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, 10], 1, Concat, [1]], # cat head P5
    44. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
    45. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
    46. ]
    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 13834 models.sppelan.SPPELAN [512, 512, 5]
    12. 10 -1 1 131584 models.common.Conv [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 361984 models.common.C3 [512, 256, 1, False]
    16. 14 -1 1 33024 models.common.Conv [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 90880 models.common.C3 [256, 128, 1, False]
    20. 18 -1 1 147712 models.common.Conv [128, 128, 3, 2]
    21. 19 [-1, 14] 1 0 models.common.Concat [1]
    22. 20 -1 1 296448 models.common.C3 [256, 256, 1, False]
    23. 21 -1 1 590336 models.common.Conv [256, 256, 3, 2]
    24. 22 [-1, 10] 1 0 models.common.Concat [1]
    25. 23 -1 1 1182720 models.common.C3 [512, 512, 1, False]
    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: 274 layers, 6379264 parameters, 6379264 gradients, 15.4 GFLOPs

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

    五、YOLOv5n改进工作

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

    1. # Parameters
    2. nc: 1 # 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, ASPP, [1024]], # 9
    22. #[-1, 1, BasicRFB, [1024]],
    23. #[-1, 1, SimSPPF, [1024, 5]],
    24. [-1, 1, SPPELAN, [1024, 5]],
    25. #[-1, 1, SPP, [1024]],
    26. #[-1, 1, SPPF, [1024, 5]], # 9
    27. #[-1, 1, SPPCSPC, [1024]],
    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, 6], 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, 4], 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, 14], 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, 10], 1, Concat, [1]], # cat head P5
    44. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
    45. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
    46. ]
    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 6922 models.sppelan.SPPELAN [256, 256, 5]
    12. 10 -1 1 33024 models.common.Conv [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 90880 models.common.C3 [256, 128, 1, False]
    16. 14 -1 1 8320 models.common.Conv [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 22912 models.common.C3 [128, 64, 1, False]
    20. 18 -1 1 36992 models.common.Conv [64, 64, 3, 2]
    21. 19 [-1, 14] 1 0 models.common.Concat [1]
    22. 20 -1 1 74496 models.common.C3 [128, 128, 1, False]
    23. 21 -1 1 147712 models.common.Conv [128, 128, 3, 2]
    24. 22 [-1, 10] 1 0 models.common.Concat [1]
    25. 23 -1 1 296448 models.common.C3 [256, 256, 1, False]
    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: 274 layers, 1607584 parameters, 1607584 gradients, 4.1 GFLOPs
    六、注意

    第二步中的激活函数若使用的是YOLOv7-tiny记得修改为LeakyReLU。

    做论文修改激活函数时,最好模块中的激活函数,否则做说明。

    七、代码的优点

    下面是SPPELAN模块的一些主要优点:

    1. 多尺度特征捕获

      通过使用不同的池化核大小(在这个例子中是5x5,但可以是更多尺寸),SPPELAN能够捕获不同尺度的特征。这对于处理具有各种大小的目标物体特别有用,因为较大的池化核可以捕获全局信息,而较小的池化核则可以捕捉局部细节。
    2. 特征融合

      SPPELAN模块将不同尺度的池化结果拼接在一起,这使得网络能够同时考虑全局和局部特征,这对于提高物体检测的准确性是非常有益的。
    3. 维度调整

      通过Conv层的使用,SPPELAN可以调整每个尺度特征图的通道数,使其适合后续层的输入要求。这有助于保持计算资源的合理使用,避免因特征图维度过大而造成的计算开销。
    4. 参数高效性

      使用Conv层而不是更复杂的层类型(如深度可分离卷积)来聚合特征,这在保持模型表达力的同时,也减少了参数数量,有助于降低模型复杂度和训练时间。
    5. 灵活性和可扩展性

      SPPELAN的设计允许容易地添加或删除池化层,以适应不同任务的需求。例如,可以通过改变cv2cv3cv4SP层的数量来调整金字塔的层数,以适应特定的输入大小或检测任务。
    6. 简化和标准化

      使用autopad函数自动确定适当的填充量,简化了网络设计,并确保了不同卷积层之间的一致性,减少了人为错误。

    通过这些优点,SPPELAN模块能够增强YOLOv9在物体检测任务中的表现,特别是在处理包含多种尺度目标的复杂场景时。它通过有效利用多尺度特征信息,提升了模型的鲁棒性和泛化能力,同时也保持了较高的计算效率。

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

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

  • 相关阅读:
    年薪高达50W的测开,到底是做什么的?
    基于b/s架构搭建一个支持多路摄像头的实时处理系统(2) ---- 使用yolo v5 模型基于GPU 多进程处理视频流
    Beyond Homophily: Structure-aware Path Aggregation Graph Neural Network
    在 openEuler 22.03 上安装 KubeSphere 实战教程
    代码自动化审查——repo hooks
    RT Preempt linux学习笔记
    String(一)———了解编码
    oracle 执行计划详解
    互联网Java工程师面试题·Java 面试篇·第五弹
    Git学习
  • 原文地址:https://blog.csdn.net/2401_84870184/article/details/140452537