当前位置:   article > 正文

[YOLOv8/YOLOv7/YOLOv5系列算法改进NO.5]改进特征融合网络PANET为BIFPN(更新添加小目标检测层yaml)_yolo融合panet

yolo融合panet

 前      言:作为当前先进的深度学习目标检测算法YOLOv5,已经集合了大量的trick,但是在处理一些复杂背景问题的时候,还是容易出现错漏检的问题。此后的系列文章,将重点对YOLOv5的如何改进进行详细的介绍,目的是为了给那些搞科研的同学需要创新点或者搞工程项目的朋友需要达到更好的效果提供自己的微薄帮助和参考。

解决问题:加入BIFPN加权双向金字塔结构,提升不同尺度的检测效果。


2023.1.8更新

有朋友问在添加小目标检测层,四个检测层的基础上如何改进特征融合网络,改进方法其他不变,需要修改yaml文件,有需要可关注私信我。 部分yaml内容如下所示:完整见百度网盘链接:链接:https://pan.baidu.com/s/1Qnn6QtGbZ7H3_h89QYA2vQ 
提取码:关注私信后获取

  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. anchors:
  7. - [ 19,27, 44,40, 38,94 ] # P3/8
  8. - [ 96,68, 86,152, 180,137 ] # P4/16
  9. - [ 140,301, 303,264, 238,542 ] # P5/32
  10. - [ 436,615, 739,380, 925,792 ] # P6/64
  11. # YOLOv5 backbone
  12. backbone:
  13. # [from, number, module, args]
  14. [ [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  15. [ -1, 1, Conv, [ 128, 3, 2 ] ], # 1-P2/4
  16. [ -1, 3, C3, [ 128 ] ],
  17. [ -1, 1, Conv, [ 256, 3, 2 ] ], # 3-P3/8
  18. [ -1, 6, C3, [ 256 ] ], #4
  19. [ -1, 1, Conv, [ 512, 3, 2 ] ], # 5-P4/16
  20. [ -1, 9, C3, [ 512 ] ], #6
  21. [ -1, 1, Conv, [ 768, 3, 2 ] ], # 7-P5/32
  22. [ -1, 3, C3, [ 768 ] ], #8
  23. [ -1, 1, Conv, [ 1024, 3, 2 ] ], # 9-P6/64
  24. [ -1, 3, C3, [ 1024 ] ],
  25. [ -1, 1, SPPF, [ 1024, 5 ] ], # 11
  26. ]
  27. # BIFPN garph
  28. #
  29. # p6 ----------------- --------Concat_bifpn----> P6(out)
  30. # / \ \ \
  31. # /-------------------------------------------->
  32. # / Upsample Concat_bifpn Concat_bifpn
  33. # / | \ |
  34. # p5 ---Concat_bifpn---> head 5 ---Concat_bifpn----> P5(out)
  35. # \ \
  36. # Upsample Concat_bifpn
  37. # ---------------- | -----------------------> /
  38. # / \ \ \
  39. # / | Concat_bifpn |
  40. # / \ \ |
  41. # p4 ---Concat_bifpn---> head 4 ---Concat_bifpn---> P4(out)
  42. # \ \
  43. # ----Upsample----> Concat_bifpn
  44. # \ /
  45. # p3 ---Concat_bifpn------------------------------> P3(out)
  46. # YOLOv5 head
  47. head:
  48. [ [ -1, 1, Conv, [ 768, 1, 1 ] ], # 12 head
  49. [ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
  50. [ [ -1, 8 ], 1, Concat_bifpn, [ 384,384] ], # cat backbone P5
  51. [ -1, 3, C3, [ 768, False ] ], # 15
  52. [ -1, 1, Conv, [ 512, 1, 1 ] ],
  53. [ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
  54. [ [ -1, 6 ], 1, Concat_bifpn, [ 256,256] ], # cat backbone P4
  55. [ -1, 3, C3, [ 512, False ] ], # 19

添加方法(以下改进步骤方法为在三个检测层的基础上)

第一步:common.py构建Concat_BIFPN模块

  1. class Concat_bifpn(nn.Module):
  2. # Concatenate a list of tensors along dimension
  3. def __init__(self, c1, c2):
  4. super(Concat_bifpn, self).__init__()
  5. self.w1 = nn.Parameter(torch.ones(2, dtype=torch.float32), requires_grad=True)
  6. self.w2 = nn.Parameter(torch.ones(3, dtype=torch.float32), requires_grad=True)
  7. # self.w3 = nn.Parameter(torch.ones(3, dtype=torch.float32), requires_grad=True)
  8. self.epsilon = 0.0001
  9. self.conv = Conv(c1, c2, 1 ,1 ,0 )
  10. self.act= nn.ReLU()
  11. def forward(self, x): # mutil-layer 1-3 layers #ADD or Concat
  12. #print("bifpn:",x.shape)
  13. if len(x) == 2:
  14. w = self.w1
  15. weight = w / (torch.sum(w, dim=0) + self.epsilon)
  16. x = self.conv(self.act(weight[0] * x[0] + weight[1] * x[1]))
  17. elif len(x) == 3:
  18. w = self.w2
  19. weight = w / (torch.sum(w, dim=0) + self.epsilon)
  20. x = self.conv(self.act (weight[0] * x[0] + weight[1] * x[1] + weight[2] * x[2]))
  21. # elif len(x) == 4:
  22. # w = self.w3
  23. # weight = w / (torch.sum(w, dim=0) + self.epsilon)
  24. # x = self.conv(self.act(weight[0] * x[0] + weight[1] * x[1] + weight[2] *x[2] + weight[3]*x[3] ))
  25. return x

第二步:yolo.py中注册Concat_BIFPNt模块

  1. elif m is Concat_bifpn:
  2. c2 = max([ch[x] for x in f])

第三步:修改yaml文件(以修改官方YOLOv5s.yaml为例),需要修改head(特征融合网络)

  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. 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. # YOLOv5 backbone
  11. backbone:
  12. # [from, number, module, args]
  13. [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  14. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  15. [-1, 3, C3, [128]],
  16. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  17. [-1, 6, C3, [256]],
  18. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  19. [-1, 9, C3, [512]],
  20. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  21. [-1, 3, C3, [1024]],
  22. [-1, 1, SPPF, [1024, 5]],
  23. ]
  24. # YOLOv5 head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1,6], 1, Concat_bifpn, [256,256]], # cat backbone P4
  29. [-1, 3, C3, [512, False]], # 13
  30. [-1, 1, Conv, [256, 1, 1]],
  31. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  32. [[-1, 4], 1, Concat_bifpn, [128,128]], # cat backbone P3
  33. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, Conv, [512, 3, 2]], # 320, 640 #
  35. [[-1, 6, 13], 1, Concat_bifpn, [256,256]], # cat head P4
  36. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  37. [-1, 1, Conv, [1024, 3, 2]], # 640, 1280 #
  38. [[-1, 9], 1, Concat_bifpn, [512, 512]], # cat head P5 cat 20,20 #22
  39. [-1, 3, C3, [1024, False]], # 25 (P5/32-large) # 1280, 1280 #23
  40. [[17, 20, 23], 1, Detect, [nc, anchors]] # Detect(P3, P4, P5)
  41. ]
Model Summary: 290 layers, 8114651 parameters, 8114651 gradients, 17.4 GFLOPs

2023.2.19补充:如果需要在YOLOv5l.yaml等网络结构进行修改的话,不可直接用以上的yaml文件或者就简单修改depth_multiple为1.0,而是 需要修改Concat_bifpn, [256,256]中的通道数为对应网络实际通道数。具体如下所示:

  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. # YOLOv5 backbone
  11. backbone:
  12. # [from, number, module, args]
  13. [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
  14. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  15. [-1, 3, C3, [128]],
  16. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  17. [-1, 6, C3, [256]],
  18. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  19. [-1, 9, C3, [512]],
  20. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  21. [-1, 3, C3, [1024]],
  22. [-1, 1, SPPF, [1024, 5]],
  23. ]
  24. # YOLOv5 head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1,6], 1, Concat_bifpn, [512,512]], # cat backbone P4
  29. [-1, 3, C3, [512, False]], # 13
  30. [-1, 1, Conv, [256, 1, 1]],
  31. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  32. [[-1, 4], 1, Concat_bifpn, [256,256]], # cat backbone P3
  33. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, Conv, [512, 3, 2]], # 320, 640 #
  35. [[-1, 6, 13], 1, Concat_bifpn, [512,512]], # cat head P4
  36. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  37. [-1, 1, Conv, [1024, 3, 2]], # 640, 1280 #
  38. [[-1, 9], 1, Concat_bifpn, [1024, 1024]], # cat head P5 cat 20,20 #22
  39. [-1, 3, C3, [1024, False]], # 25 (P5/32-large) # 1280, 1280 #23
  40. [[17, 20, 23], 1, Detect, [nc, anchors]] # Detect(P3, P4, P5)
  41. ]

第四步:将train.py中改为本文的yaml文件即可,开始训练

 结    果:本人在多个数据集上做了大量实验,针对不同的数据集效果不同,同一个数据集的不同添加位置方法也是有差异,需要大家进行实验。有效果有提升的情况占大多数。

预告一下:下一篇内容分享增加小目标检测层。有兴趣的朋友可以关注一下我,有问题可以留言或者私聊我哦

PS:,不仅仅是可以添加进YOLOv5,也可以添加进任何其他的深度学习网络,不管是分类还是检测还是分割,主要是计算机视觉领域,都可能会有不同程度的提升效果。

最后,四个检测层的基础上改进特征融合网络为BIFPN的话,需要修改yaml文件,有需要可关注私信我。

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

闽ICP备14008679号