当前位置:   article > 正文

【YOLOv5/v7改进系列】改进池化层为RFB

【YOLOv5/v7改进系列】改进池化层为RFB
一、导言

论文 "Receptive Field Block Net for Accurate and Fast Object Detection" 中提出的 RFB (Receptive Field Block) 模块旨在模仿人类视觉系统中的感受野结构,以增强深度学习模型对不同尺度和位置的目标检测能力。下面总结了RFB模块的主要优点和潜在局限性。

优点:

  1. 增强特征区分性和鲁棒性: RFB模块通过模拟人类视觉系统中感受野的大小和偏心率之间的关系,增强了轻量级CNN网络的特征表示,从而提高了特征的区分性和鲁棒性。

  2. 多尺度信息整合: RFB模块利用多个分支,每个分支有不同的卷积核大小和空洞卷积层,这有助于捕捉图像中不同尺度的信息,类似于人类视觉系统中感受野的多样性。

  3. 实时性能: 实验表明,将RFB模块添加到SSD之上构建的RFB Net不仅保持了实时处理速度,而且在Pascal VOC和MS COCO数据集上达到了与高级深层检测器相当的性能。

  4. 通用性: RFB模块可以很容易地链接到不同的轻量级网络架构上,如MobileNet,这证明了它的通用性和适应性。

  5. 从零开始训练的能力: RFB模块还显示出即使在没有预训练的情况下也能有效训练物体检测器的能力,这在某些情况下可能是一个优势。

局限性:

  1. 参数增加: 尽管RFB模块可以提高精度,但它也增加了额外的层和参数,这可能会影响计算效率和资源需求,尤其是在低端设备上。

  2. 特定设计的局限性: RFB模块的设计是手工制作的,这意味着它可能不如自适应机制灵活,例如可变形卷积神经网络中使用的那些,后者可以根据对象的尺度和形状调整感受野的分布。

  3. 对感受野的简化假设: RFB模块通过固定大小和偏心率的感受野来近似人类视觉系统,这可能忽略了更复杂的人类视觉处理机制,如动态调整或非均匀采样。

  4. 依赖于基础网络: RFB模块的性能很大程度上依赖于底层的基础网络,这意味着在不同的网络架构上的效果可能会有所不同。

尽管存在这些局限性,RFB模块在物体检测任务中表现出了显著的改进,并且在保持实时处理速度的同时,提供了一种增强轻量级模型检测精度的有效方法。

二、准备工作

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

  1. from models.common import *
  2. # RFB
  3. # https://arxiv.org/pdf/1711.07767
  4. class BasicConv(nn.Module):
  5. def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, dilation=1, groups=1, relu=True,
  6. bn=True):
  7. super(BasicConv, self).__init__()
  8. self.out_channels = out_planes
  9. if bn:
  10. self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding,
  11. dilation=dilation, groups=groups, bias=False)
  12. self.bn = nn.BatchNorm2d(out_planes, eps=1e-5, momentum=0.01, affine=True)
  13. self.relu = nn.ReLU(inplace=True) if relu else None
  14. else:
  15. self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding,
  16. dilation=dilation, groups=groups, bias=True)
  17. self.bn = None
  18. self.relu = nn.ReLU(inplace=True) if relu else None
  19. def forward(self, x):
  20. x = self.conv(x)
  21. if self.bn is not None:
  22. x = self.bn(x)
  23. if self.relu is not None:
  24. x = self.relu(x)
  25. return x
  26. class BasicRFB(nn.Module):
  27. def __init__(self, in_planes, out_planes, stride=1, scale=0.1, map_reduce=8, vision=1, groups=1):
  28. super(BasicRFB, self).__init__()
  29. self.scale = scale
  30. self.out_channels = out_planes
  31. inter_planes = in_planes // map_reduce
  32. self.branch0 = nn.Sequential(
  33. BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),
  34. BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups),
  35. BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision, dilation=vision,
  36. relu=False, groups=groups)
  37. )
  38. self.branch1 = nn.Sequential(
  39. BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),
  40. BasicConv(inter_planes, 2 * inter_planes, kernel_size=(3, 3), stride=stride, padding=(1, 1), groups=groups),
  41. BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 2,
  42. dilation=vision + 2, relu=False, groups=groups)
  43. )
  44. self.branch2 = nn.Sequential(
  45. BasicConv(in_planes, inter_planes, kernel_size=1, stride=1, groups=groups, relu=False),
  46. BasicConv(inter_planes, (inter_planes // 2) * 3, kernel_size=3, stride=1, padding=1, groups=groups),
  47. BasicConv((inter_planes // 2) * 3, 2 * inter_planes, kernel_size=3, stride=stride, padding=1,
  48. groups=groups),
  49. BasicConv(2 * inter_planes, 2 * inter_planes, kernel_size=3, stride=1, padding=vision + 4,
  50. dilation=vision + 4, relu=False, groups=groups)
  51. )
  52. self.ConvLinear = BasicConv(6 * inter_planes, out_planes, kernel_size=1, stride=1, relu=False)
  53. self.shortcut = BasicConv(in_planes, out_planes, kernel_size=1, stride=stride, relu=False)
  54. self.relu = nn.ReLU(inplace=False)
  55. def forward(self, x):
  56. x0 = self.branch0(x)
  57. x1 = self.branch1(x)
  58. x2 = self.branch2(x)
  59. out = torch.cat((x0, x1, x2), 1)
  60. out = self.ConvLinear(out)
  61. short = self.shortcut(x)
  62. out = out * self.scale + short
  63. out = self.relu(out)
  64. return out

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

from models.morepooling import *

并搜索def parse_model(d, ch)

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

BasicRFB

三、YOLOv7-tiny改进工作

完成二后,在YOLOv7项目文件下的models文件夹下创建新的文件yolov7-tiny-rfb.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, BasicRFB, [256]], # 29
  46. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  47. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  48. [21, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P4
  49. [[-1, -2], 1, Concat, [1]],
  50. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  51. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  52. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  53. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  54. [[-1, -2, -3, -4], 1, Concat, [1]],
  55. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 39
  56. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  57. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  58. [14, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # route backbone P3
  59. [[-1, -2], 1, Concat, [1]],
  60. [-1, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  61. [-2, 1, Conv, [32, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  62. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  63. [-1, 1, Conv, [32, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  64. [[-1, -2, -3, -4], 1, Concat, [1]],
  65. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 49
  66. [-1, 1, Conv, [128, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
  67. [[-1, 39], 1, Concat, [1]],
  68. [-1, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  69. [-2, 1, Conv, [64, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  70. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  71. [-1, 1, Conv, [64, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  72. [[-1, -2, -3, -4], 1, Concat, [1]],
  73. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 57
  74. [-1, 1, Conv, [256, 3, 2, None, 1, nn.LeakyReLU(0.1)]],
  75. [[-1, 29], 1, Concat, [1]],
  76. [-1, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  77. [-2, 1, Conv, [128, 1, 1, None, 1, nn.LeakyReLU(0.1)]],
  78. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  79. [-1, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  80. [[-1, -2, -3, -4], 1, Concat, [1]],
  81. [-1, 1, Conv, [256, 1, 1, None, 1, nn.LeakyReLU(0.1)]], # 65
  82. [49, 1, Conv, [128, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  83. [57, 1, Conv, [256, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  84. [65, 1, Conv, [512, 3, 1, None, 1, nn.LeakyReLU(0.1)]],
  85. [[66, 67, 68], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
  86. ]
  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 1086528 models.morepooling.BasicRFB [512, 256]
  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: 284 layers, 6444108 parameters, 6444108 gradients, 13.5 GFLOPS

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

四、YOLOv5s改进工作

完成二后,在YOLOv5项目文件下的models文件夹下创建新的文件yolov5s-rfb.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, SPP, [1024]],
  24. #[-1, 1, SPPF, [1024, 5]], # 9
  25. #[-1, 1, SPPCSPC, [1024]],
  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, 6], 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, 4], 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, 14], 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, 10], 1, Concat, [1]], # cat head P5
  42. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  43. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  44. ]
  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 1316928 models.morepooling.BasicRFB [512, 512]
  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: 305 layers, 7682358 parameters, 7682358 gradients, 16.5 GFLOPs

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

五、YOLOv5n改进工作

完成二后,在YOLOv5项目文件下的models文件夹下创建新的文件yolov5n-rfb.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, SPP, [1024]],
  24. #[-1, 1, SPPF, [1024, 5]], # 9
  25. #[-1, 1, SPPCSPC, [1024]],
  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, 6], 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, 4], 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, 14], 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, 10], 1, Concat, [1]], # cat head P5
  42. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  43. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  44. ]
  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 330272 models.morepooling.BasicRFB [256, 256]
  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: 305 layers, 1930934 parameters, 1930934 gradients, 4.4 GFLOPs
六、代码部分的解释

BasicRFB 类继承自 PyTorch 的 nn.Module。构造函数接收输入通道数 in_planes、输出通道数 out_planes 和一些控制模块行为的参数,比如步长 stride、缩放因子 scale、中间层通道减少倍数 map_reduce、扩张率 vision 和分组卷积的组数 groups

三个分支分别定义为 branch0branch1branch2。每个分支都由多个 BasicConv 组件构成,其中包含卷积、批量归一化和激活函数。分支的内核大小、填充和扩张率各不相同,目的是捕捉不同尺度的信息。branch0 使用标准卷积和扩张卷积;branch1 使用稍大的扩张率;而 branch2 则使用更大的扩张率和额外的卷积层,进一步增加感受野的多样性。

最终,三个分支的输出被拼接在一起,并通过 ConvLinear 卷积层转换为期望的输出通道数。此外,还有一个快捷连接(shortcut)从输入直接传递到输出,与 ResNet 结构类似,用于缓解梯度消失问题。所有分支的输出和快捷连接的输出在相加前会乘以一个缩放因子 scale,然后经过 ReLU 激活函数,得到最终的输出。

整个 BasicRFB 模块在前向传播时,将输入通过三个分支并行处理,然后将结果拼接后进行线性变换,最后加上快捷连接的输出。这种设计使得模块能够高效地捕获多尺度特征,同时保持实时处理速度,适用于目标检测等计算机视觉任务。

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

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

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

闽ICP备14008679号