当前位置:   article > 正文

Yolov5 优化,包括Yolov8 c2f模块_yolov5 c2f

yolov5 c2f

目录

各种技巧实战测试

decouple head

yolo中添加:

Yolov5/Yolov7加入Yolov8 c2f模块,小目标涨点

1)加入backbone

 2) 加入head


各种技巧实战测试

基于Yolov5的道路缺陷识别,加入CVPR2023 InceptionNeXt、华为诺亚2023 VanillaNet、ASFF、EVC、Decoupled_Detect、TSCODE、WIoU优化_AI小怪兽的博客-CSDN博客

decouple head

原文:

涨点技巧:Detect系列---Yolov5/Yolov7加入解耦头Decoupled_Detect,涨点明显_AI小怪兽的博客-CSDN博客

yolov5中head修改为decouple head详解_python_

  1. class Detect(nn.Module):
  2. stride = None # strides computed during build
  3. onnx_dynamic = False # ONNX export parameter
  4. def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer
  5. super().__init__()
  6. self.nc = nc # number of classes
  7. self.no = nc + 5 # number of outputs per anchor
  8. self.nl = len(anchors) # number of detection layers
  9. self.na = len(anchors[0]) // 2 # number of anchors
  10. self.grid = [torch.zeros(1)] * self.nl # init grid
  11. self.anchor_grid = [torch.zeros(1)] * self.nl # init anchor grid
  12. self.register_buffer('anchors', torch.tensor(anchors).float().view(self.nl, -1, 2)) # shape(nl,na,2)
  13. # self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
  14. self.m_box = nn.ModuleList(nn.Conv2d(256, 4 * self.na, 1) for x in ch) # output conv
  15. self.m_conf = nn.ModuleList(nn.Conv2d(256, 1 * self.na, 1) for x in ch) # output conv
  16. self.m_labels = nn.ModuleList(nn.Conv2d(256, self.nc * self.na, 1) for x in ch) # output conv
  17. self.base_conv = nn.ModuleList(BaseConv(in_channels = x, out_channels = 256, ksize = 1, stride = 1) for x in ch)
  18. self.cls_convs = nn.ModuleList(BaseConv(in_channels = 256, out_channels = 256, ksize = 3, stride = 1) for x in ch)
  19. self.reg_convs = nn.ModuleList(BaseConv(in_channels = 256, out_channels = 256, ksize = 3, stride = 1) for x in ch)
  20. # self.m = nn.ModuleList(nn.Conv2d(x, 4 * self.na, 1) for x in ch, nn.Conv2d(x, 1 * self.na, 1) for x in ch,nn.Conv2d(x, self.nc * self.na, 1) for x in ch)
  21. self.inplace = inplace # use in-place ops (e.g. slice assignment)self.ch = ch
  22. def forward(self, x):
  23. z = [] # inference output
  24. for i in range(self.nl):
  25. # # x[i] = self.m[i](x[i]) # convs
  26. # print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&", i)
  27. # print(x[i].shape)
  28. # print(self.base_conv[i])
  29. # print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
  30. x_feature = self.base_conv[i](x[i])
  31. # x_feature = x[i]
  32. cls_feature = self.cls_convs[i](x_feature)
  33. reg_feature = self.reg_convs[i](x_feature)
  34. # reg_feature = x_feature
  35. m_box = self.m_box[i](reg_feature)
  36. m_conf = self.m_conf[i](reg_feature)
  37. m_labels = self.m_labels[i](cls_feature)
  38. x[i] = torch.cat((m_box,m_conf, m_labels),1)
  39. bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
  40. x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
  41. if not self.training: # inference
  42. if self.onnx_dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
  43. self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
  44. y = x[i].sigmoid()
  45. if self.inplace:
  46. y[..., 0:2] = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy
  47. y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
  48. else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953
  49. xy = (y[..., 0:2] * 2 - 0.5 + self.grid[i]) * self.stride[i] # xy
  50. wh = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh
  51. y = torch.cat((xy, wh, y[..., 4:]), -1)
  52. z.append(y.view(bs, -1, self.no))
  53. return x if self.training else (torch.cat(z, 1), x)

yolo中添加:

  1. def get_activation(name="silu", inplace=True):
  2. if name == "silu":
  3. module = nn.SiLU(inplace=inplace)
  4. elif name == "relu":
  5. module = nn.ReLU(inplace=inplace)
  6. elif name == "lrelu":
  7. module = nn.LeakyReLU(0.1, inplace=inplace)
  8. else:
  9. raise AttributeError("Unsupported act type: {}".format(name))
  10. return module
  11. class BaseConv(nn.Module):
  12. """A Conv2d -> Batchnorm -> silu/leaky relu block"""
  13. def __init__(
  14. self, in_channels, out_channels, ksize, stride, groups=1, bias=False, act="silu"
  15. ):
  16. super().__init__()
  17. # same padding
  18. pad = (ksize - 1) // 2
  19. self.conv = nn.Conv2d(
  20. in_channels,
  21. out_channels,
  22. kernel_size=ksize,
  23. stride=stride,
  24. padding=pad,
  25. groups=groups,
  26. bias=bias,
  27. )
  28. self.bn = nn.BatchNorm2d(out_channels)
  29. self.act = get_activation(act, inplace=True)
  30. def forward(self, x):
  31. # print(self.bn(self.conv(x)).shape)
  32. return self.act(self.bn(self.conv(x)))
  33. # return self.bn(self.conv(x))
  34. def fuseforward(self, x):
  35. return self.act(self.conv(x))

Yolov5/Yolov7加入Yolov8 c2f模块,小目标涨点

  1. class v8_C2fBottleneck(nn.Module):
  2. # Standard bottleneck
  3. def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5): # ch_in, ch_out, shortcut, groups, kernels, expand
  4. super().__init__()
  5. c_ = int(c2 * e) # hidden channels
  6. self.cv1 = Conv(c1, c_, k[0], 1)
  7. self.cv2 = Conv(c_, c2, k[1], 1, g=g)
  8. self.add = shortcut and c1 == c2
  9. def forward(self, x):
  10. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  11. class C2f(nn.Module):
  12. # CSP Bottleneck with 2 convolutions
  13. def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
  14. super().__init__()
  15. self.c = int(c2 * e) # hidden channels
  16. self.cv1 = Conv(c1, 2 * self.c, 1, 1)
  17. self.cv2 = Conv((2 + n) * self.c, c2, 1) # optional act=FReLU(c2)
  18. self.m = nn.ModuleList(v8_C2fBottleneck(self.c, self.c, shortcut, g, k=((3, 3), (3, 3)), e=1.0) for _ in range(n))
  19. def forward(self, x):
  20. y = list(self.cv1(x).split((self.c, self.c), 1))
  21. y.extend(m(y[-1]) for m in self.m)
  22. return self.cv2(torch.cat(y, 1))
  23. ————————————————
  24. 版权声明:本文为CSDN博主「AI小怪兽」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
  25. 原文链接:https://blog.csdn.net/m0_63774211/article/details/129493630

2)在yolo.py中添加C2f(PS:快速搜索C3对应位置)

1)加入backbone

  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, 3, 2 ]], # 0-P1/2
  14. [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
  15. [-1, 3, C2f, [128, True]],
  16. [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
  17. [-1, 6, C2f, [256, True]],
  18. [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
  19. [-1, 6, C2f, [512, True]],
  20. [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
  21. [-1, 3, C2f, [1024, True]],
  22. [-1, 1, SPPF, [1024]]
  23. ]
  24. # YOLOv5 v6.0 head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1, 6], 1, Concat, [1]], # 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, [1]], # cat backbone P3
  33. [-1, 3, C3, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, Conv, [256, 3, 2]],
  35. [[-1, 14], 1, Concat, [1]], # cat head P4
  36. [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
  37. [-1, 1, Conv, [512, 3, 2]],
  38. [[-1, 10], 1, Concat, [1]], # 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. ]

 2) 加入head

  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 head
  25. head:
  26. [[-1, 1, Conv, [512, 1, 1]],
  27. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  28. [[-1, 6], 1, Concat, [1]], # cat backbone P4
  29. [-1, 3, C2f, [512, False]], # 13
  30. [-1, 1, Conv, [256, 1, 1]],
  31. [-1, 1, nn.Upsample, [None, 2, 'nearest']],
  32. [[-1, 4], 1, Concat, [1]], # cat backbone P3
  33. [-1, 3, C2f, [256, False]], # 17 (P3/8-small)
  34. [-1, 1, Conv, [256, 3, 2]],
  35. [[-1, 14], 1, Concat, [1]], # cat head P4
  36. [-1, 3, C2f, [512, False]], # 20 (P4/16-medium)
  37. [-1, 1, Conv, [512, 3, 2]],
  38. [[-1, 10], 1, Concat, [1]], # cat head P5
  39. [-1, 3, C2f, [1024, False]], # 23 (P5/32-large)
  40. [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
  41. ]

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

闽ICP备14008679号