0


将yolov5中的PANet层改为BiFPN

本文以YOLOv5-6.1版本为例

一、Add

1.在common.py后加入如下代码

  1. # 结合BiFPN 设置可学习参数 学习不同分支的权重
  2. # 两个分支add操作
  3. class BiFPN_Add2(nn.Module):
  4. def __init__(self, c1, c2):
  5. super(BiFPN_Add2, self).__init__()
  6. # 设置可学习参数 nn.Parameter的作用是:将一个不可训练的类型Tensor转换成可以训练的类型parameter
  7. # 并且会向宿主模型注册该参数 成为其一部分 即model.parameters()会包含这个parameter
  8. # 从而在参数优化的时候可以自动一起优化
  9. self.w = nn.Parameter(torch.ones(2, dtype=torch.float32), requires_grad=True)
  10. self.epsilon = 0.0001
  11. self.conv = nn.Conv2d(c1, c2, kernel_size=1, stride=1, padding=0)
  12. self.silu = nn.SiLU()
  13. def forward(self, x):
  14. w = self.w
  15. weight = w / (torch.sum(w, dim=0) + self.epsilon)
  16. return self.conv(self.silu(weight[0] * x[0] + weight[1] * x[1]))
  17. # 三个分支add操作
  18. class BiFPN_Add3(nn.Module):
  19. def __init__(self, c1, c2):
  20. super(BiFPN_Add3, self).__init__()
  21. self.w = nn.Parameter(torch.ones(3, dtype=torch.float32), requires_grad=True)
  22. self.epsilon = 0.0001
  23. self.conv = nn.Conv2d(c1, c2, kernel_size=1, stride=1, padding=0)
  24. self.silu = nn.SiLU()
  25. def forward(self, x):
  26. w = self.w
  27. weight = w / (torch.sum(w, dim=0) + self.epsilon) # 将权重进行归一化
  28. # Fast normalized fusion
  29. return self.conv(self.silu(weight[0] * x[0] + weight[1] * x[1] + weight[2] * x[2]))

2.yolov5s.yaml进行修改

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. # Parameters
  3. nc: 80 # number of classes
  4. depth_multiple: 0.33 # model depth multiple
  5. width_multiple: 0.50 # layer channel multiple
  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 v6.0 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]], # 9
  23. ]
  24. # YOLOv5 v6.0 BiFPN head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1, 6], 1, BiFPN_Add2, [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, BiFPN_Add2, [128, 128]], # cat backbone P3
  33. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, Conv, [512, 3, 2]], # 为了BiFPN正确add,调整channel数
  35. [[-1, 13, 6], 1, BiFPN_Add3, [256, 256]], # cat P4 <--- BiFPN change 注意v5s通道数是默认参数的一半
  36. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  37. [-1, 1, Conv, [512, 3, 2]],
  38. [[-1, 10], 1, BiFPN_Add2, [256, 256]], # cat head P5
  39. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  40. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  41. ]

3.修改yolo.py,在

  1. parse_model

函数中找到

  1. elif m is Concat:

语句,在其后面加上

  1. BiFPN_Add

相关语句:

  1. # 添加bifpn_add结构
  2. elif m in [BiFPN_Add2, BiFPN_Add3]:
  3. c2 = max([ch[x] for x in f])

4.修改train.py,向优化器中添加BiFPN的权重参数

  1. BiFPN_Add2

  1. BiFPN_Add3

函数中定义的

  1. w

参数,加入g1

  1. # BiFPN_Concat
  2. elif isinstance(v, BiFPN_Add2) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter):
  3. g1.append(v.w)
  4. elif isinstance(v, BiFPN_Add3) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter):
  5. g1.append(v.w)

然后导入一下这两个包

一、Concat

1.在common.py后加入如下代码

  1. # 结合BiFPN 设置可学习参数 学习不同分支的权重
  2. # 两个分支concat操作
  3. class BiFPN_Concat2(nn.Module):
  4. def __init__(self, dimension=1):
  5. super(BiFPN_Concat2, self).__init__()
  6. self.d = dimension
  7. self.w = nn.Parameter(torch.ones(2, dtype=torch.float32), requires_grad=True)
  8. self.epsilon = 0.0001
  9. def forward(self, x):
  10. w = self.w
  11. weight = w / (torch.sum(w, dim=0) + self.epsilon) # 将权重进行归一化
  12. # Fast normalized fusion
  13. x = [weight[0] * x[0], weight[1] * x[1]]
  14. return torch.cat(x, self.d)
  15. # 三个分支concat操作
  16. class BiFPN_Concat3(nn.Module):
  17. def __init__(self, dimension=1):
  18. super(BiFPN_Concat3, self).__init__()
  19. self.d = dimension
  20. # 设置可学习参数 nn.Parameter的作用是:将一个不可训练的类型Tensor转换成可以训练的类型parameter
  21. # 并且会向宿主模型注册该参数 成为其一部分 即model.parameters()会包含这个parameter
  22. # 从而在参数优化的时候可以自动一起优化
  23. self.w = nn.Parameter(torch.ones(3, dtype=torch.float32), requires_grad=True)
  24. self.epsilon = 0.0001
  25. def forward(self, x):
  26. w = self.w
  27. weight = w / (torch.sum(w, dim=0) + self.epsilon) # 将权重进行归一化
  28. # Fast normalized fusion
  29. x = [weight[0] * x[0], weight[1] * x[1], weight[2] * x[2]]
  30. return torch.cat(x, self.d)

2.yolov5s.yaml进行修改

  1. # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
  2. # Parameters
  3. nc: 80 # number of classes
  4. depth_multiple: 0.33 # model depth multiple
  5. width_multiple: 0.50 # layer channel multiple
  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 v6.0 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]], # 9
  23. ]
  24. # YOLOv5 v6.0 BiFPN head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1, 6], 1, BiFPN_Concat2, [1]], # cat backbone P4 <--- BiFPN change
  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, BiFPN_Concat2, [1]], # cat backbone P3 <--- BiFPN change
  33. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, Conv, [256, 3, 2]],
  35. [[-1, 14, 6], 1, BiFPN_Concat3, [1]], # cat P4 <--- BiFPN change
  36. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  37. [-1, 1, Conv, [512, 3, 2]],
  38. [[-1, 10], 1, BiFPN_Concat2, [1]], # cat head P5 <--- BiFPN change
  39. [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
  40. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  41. ]

3.修改yolo.py,在

  1. parse_model

函数中找到

  1. elif m is Concat:

语句,在其后面加上

  1. BiFPN_

Concat相关语句:

  1. # 添加bifpn_concat结构
  2. elif m in [Concat, BiFPN_Concat2, BiFPN_Concat3]:
  3. c2 = sum(ch[x] for x in f)

4.修改train.py,向优化器中添加BiFPN的权重参数

添加复方式同上(Add)

  1. # BiFPN_Concat
  2. elif isinstance(v, BiFPN_Concat2) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter):
  3. g1.append(v.w)
  4. elif isinstance(v, BiFPN_Concat3) and hasattr(v, 'w') and isinstance(v.w, nn.Parameter):
  5. g1.append(v.w)

至此,大功告成~~~

reference:【YOLOv5-6.x】设置可学习权重结合BiFPN(Add操作)_嗜睡的篠龙的博客-CSDN博客
【YOLOv5-6.x】设置可学习权重结合BiFPN(Concat操作)_嗜睡的篠龙的博客-CSDN博客_bifpn代码


本文转载自: https://blog.csdn.net/m0_56247038/article/details/124891449
版权归原作者 有温度的AI 所有, 如有侵权,请联系我们删除。

“将yolov5中的PANet层改为BiFPN”的评论:

还没有评论