0


爆改YOLOv8| 利用ResNet18、34、50、101替换yolo主干网络

1,本文介绍

ResNet(深度残差网络)通过引入“快捷连接”来解决深层神经网络训练中的梯度消失问题。这些快捷连接允许网络的输入直接跳过中间的层,直接传递到后面的层,从而使得网络能够专注于学习输入与输出之间的残差(即差异),而非直接学习复杂的函数映射。这种设计方式使得网络在需要时可以简单地实现恒等映射,简化了训练过程,缓解了深层网络训练中的困难。因此,ResNet 能够有效地训练非常深的网络结构,并在多个视觉识别任务上取得显著的性能提升。

关于ResNet的详细介绍可以看论文:https://arxiv.org/pdf/1512.03385.pdf

本文将讲解如何将ResNet融合进yolov8

话不多说,上代码!

2, 将ResNet融合进yolov8

2.1 步骤一

首先找到如下的目录'ultralytics/nn',然后在这个目录下创建一个'Addmodules'文件夹,然后在这个目录下创建一个ResNet.py文件,文件名字可以根据你自己的习惯起,然后将ResNet的核心代码复制进去。

  1. from collections import OrderedDict
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. class ConvNormLayer(nn.Module):
  5. def __init__(self,
  6. ch_in,
  7. ch_out,
  8. filter_size,
  9. stride,
  10. groups=1,
  11. act=None):
  12. super(ConvNormLayer, self).__init__()
  13. self.act = act
  14. self.conv = nn.Conv2d(
  15. in_channels=ch_in,
  16. out_channels=ch_out,
  17. kernel_size=filter_size,
  18. stride=stride,
  19. padding=(filter_size - 1) // 2,
  20. groups=groups)
  21. self.norm = nn.BatchNorm2d(ch_out)
  22. def forward(self, inputs):
  23. out = self.conv(inputs)
  24. out = self.norm(out)
  25. if self.act:
  26. out = getattr(F, self.act)(out)
  27. return out
  28. class SELayer(nn.Module):
  29. def __init__(self, ch, reduction_ratio=16):
  30. super(SELayer, self).__init__()
  31. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  32. self.fc = nn.Sequential(
  33. nn.Linear(ch, ch // reduction_ratio, bias=False),
  34. nn.ReLU(inplace=True),
  35. nn.Linear(ch // reduction_ratio, ch, bias=False),
  36. nn.Sigmoid()
  37. )
  38. def forward(self, x):
  39. b, c, _, _ = x.size()
  40. y = self.avg_pool(x).view(b, c)
  41. y = self.fc(y).view(b, c, 1, 1)
  42. return x * y.expand_as(x)
  43. class BasicBlock(nn.Module):
  44. expansion = 1
  45. def __init__(self,
  46. ch_in,
  47. ch_out,
  48. stride,
  49. shortcut,
  50. act='relu',
  51. variant='b',
  52. att=False):
  53. super(BasicBlock, self).__init__()
  54. self.shortcut = shortcut
  55. if not shortcut:
  56. if variant == 'd' and stride == 2:
  57. self.short = nn.Sequential()
  58. self.short.add_sublayer(
  59. 'pool',
  60. nn.AvgPool2d(
  61. kernel_size=2, stride=2, padding=0, ceil_mode=True))
  62. self.short.add_sublayer(
  63. 'conv',
  64. ConvNormLayer(
  65. ch_in=ch_in,
  66. ch_out=ch_out,
  67. filter_size=1,
  68. stride=1))
  69. else:
  70. self.short = ConvNormLayer(
  71. ch_in=ch_in,
  72. ch_out=ch_out,
  73. filter_size=1,
  74. stride=stride)
  75. self.branch2a = ConvNormLayer(
  76. ch_in=ch_in,
  77. ch_out=ch_out,
  78. filter_size=3,
  79. stride=stride,
  80. act='relu')
  81. self.branch2b = ConvNormLayer(
  82. ch_in=ch_out,
  83. ch_out=ch_out,
  84. filter_size=3,
  85. stride=1,
  86. act=None)
  87. self.att = att
  88. if self.att:
  89. self.se = SELayer(ch_out)
  90. def forward(self, inputs):
  91. out = self.branch2a(inputs)
  92. out = self.branch2b(out)
  93. if self.att:
  94. out = self.se(out)
  95. if self.shortcut:
  96. short = inputs
  97. else:
  98. short = self.short(inputs)
  99. out = out + short
  100. out = F.relu(out)
  101. return out
  102. class BottleNeck(nn.Module):
  103. expansion = 4
  104. def __init__(self, ch_in, ch_out, stride, shortcut, act='relu', variant='d', att=False):
  105. super().__init__()
  106. if variant == 'a':
  107. stride1, stride2 = stride, 1
  108. else:
  109. stride1, stride2 = 1, stride
  110. width = ch_out
  111. self.branch2a = ConvNormLayer(ch_in, width, 1, stride1, act=act)
  112. self.branch2b = ConvNormLayer(width, width, 3, stride2, act=act)
  113. self.branch2c = ConvNormLayer(width, ch_out * self.expansion, 1, 1)
  114. self.shortcut = shortcut
  115. if not shortcut:
  116. if variant == 'd' and stride == 2:
  117. self.short = nn.Sequential(OrderedDict([
  118. ('pool', nn.AvgPool2d(2, 2, 0, ceil_mode=True)),
  119. ('conv', ConvNormLayer(ch_in, ch_out * self.expansion, 1, 1))
  120. ]))
  121. else:
  122. self.short = ConvNormLayer(ch_in, ch_out * self.expansion, 1, stride)
  123. self.att = att
  124. if self.att:
  125. self.se = SELayer(ch_out)
  126. def forward(self, x):
  127. out = self.branch2a(x)
  128. out = self.branch2b(out)
  129. out = self.branch2c(out)
  130. if self.att:
  131. out = self.se(out)
  132. if self.shortcut:
  133. short = x
  134. else:
  135. short = self.short(x)
  136. out = out + short
  137. out = F.relu(out)
  138. return out
  139. class Blocks(nn.Module):
  140. def __init__(self,
  141. ch_in,
  142. ch_out,
  143. count,
  144. block,
  145. stage_num,
  146. att=False,
  147. variant='b'):
  148. super(Blocks, self).__init__()
  149. self.blocks = nn.ModuleList()
  150. block = globals()[block]
  151. for i in range(count):
  152. self.blocks.append(
  153. block(
  154. ch_in,
  155. ch_out,
  156. stride=2 if i == 0 and stage_num != 2 else 1,
  157. shortcut=False if i == 0 else True,
  158. variant=variant,
  159. att=att)
  160. )
  161. if i == 0:
  162. ch_in = ch_out * block.expansion
  163. def forward(self, inputs):
  164. block_out = inputs
  165. for block in self.blocks:
  166. block_out = block(block_out)
  167. return block_out

2.2 步骤二

在Addmodules下创建一个新的py文件名字为'init.py',然后在其内部添加如下代码

2.3 步骤三

在task.py进行导入

2.4 步骤四

在task.py进行注册,即在parse_model添加代码

注意-共需要在三个位置修改

然后如下图所示,注释掉黄色框内代码,添加红色框内代码

到此注册成功,复制后面的yaml文件直接运行即可

yaml文件1-ResNet18

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
  3. # Parameters
  4. nc: 80 # number of classes
  5. scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
  8. s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
  9. m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
  10. l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
  11. x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
  12. # YOLOv8.0n backbone
  13. backbone:
  14. # [from, repeats, module, args]
  15. - [-1, 1, ConvNormLayer, [32, 3, 2, 1, 'relu']] # 0-P1
  16. - [-1, 1, ConvNormLayer, [32, 3, 1, 1, 'relu']] # 1
  17. - [-1, 1, ConvNormLayer, [64, 3, 1, 1, 'relu']] # 2
  18. - [-1, 1, nn.MaxPool2d, [3, 2, 1]] # 3-P2
  19. - [-1, 2, Blocks, [64, BasicBlock, 2, False]] # 4
  20. - [-1, 2, Blocks, [128, BasicBlock, 3, False]] # 5-P3
  21. - [-1, 2, Blocks, [256, BasicBlock, 4, False]] # 6-P4
  22. - [-1, 2, Blocks, [512, BasicBlock, 5, False]] # 7-P5
  23. - [-1, 1, SPPF, [1024, 5]] # 8
  24. # YOLOv8.0n head
  25. head:
  26. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  27. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  28. - [-1, 3, C2f, [512]] # 11
  29. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  30. - [[-1, 5], 1, Concat, [1]] # cat backbone P3
  31. - [-1, 3, C2f, [256]] # 14 (P3/8-small)
  32. - [-1, 1, Conv, [256, 3, 2]]
  33. - [[-1, 11], 1, Concat, [1]] # cat head P4
  34. - [-1, 3, C2f, [512]] # 17 (P4/16-medium)
  35. - [-1, 1, Conv, [512, 3, 2]]
  36. - [[-1, 8], 1, Concat, [1]] # cat head P5
  37. - [-1, 3, C2f, [1024]] # 20 (P5/32-large)
  38. - [[14, 17, 20], 1, Detect, [nc]] # Detect(P3, P4, P5)

yaml文件2-ResNet34

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
  3. # Parameters
  4. nc: 80 # number of classes
  5. scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
  8. s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
  9. m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
  10. l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
  11. x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
  12. # YOLOv8.0n backbone
  13. backbone:
  14. # [from, repeats, module, args]
  15. - [-1, 1, ConvNormLayer, [32, 3, 2, 1, 'relu']] # 0-P1
  16. - [-1, 1, ConvNormLayer, [32, 3, 1, 1, 'relu']] # 1
  17. - [-1, 1, ConvNormLayer, [64, 3, 1, 1, 'relu']] # 2
  18. - [-1, 1, nn.MaxPool2d, [3, 2, 1]] # 3-P2
  19. - [-1, 3, Blocks, [64, BasicBlock, 2, False]] # 4
  20. - [-1, 4, Blocks, [128, BasicBlock, 3, False]] # 5-P3
  21. - [-1, 6, Blocks, [256, BasicBlock, 4, False]] # 6-P4
  22. - [-1, 3, Blocks, [512, BasicBlock, 5, False]] # 7-P5
  23. - [-1, 1, SPPF, [1024, 5]] # 8
  24. # YOLOv8.0n head
  25. head:
  26. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  27. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  28. - [-1, 3, C2f, [512]] # 11
  29. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  30. - [[-1, 5], 1, Concat, [1]] # cat backbone P3
  31. - [-1, 3, C2f, [256]] # 14 (P3/8-small)
  32. - [-1, 1, Conv, [256, 3, 2]]
  33. - [[-1, 11], 1, Concat, [1]] # cat head P4
  34. - [-1, 3, C2f, [512]] # 17 (P4/16-medium)
  35. - [-1, 1, Conv, [512, 3, 2]]
  36. - [[-1, 8], 1, Concat, [1]] # cat head P5
  37. - [-1, 3, C2f, [1024]] # 20 (P5/32-large)
  38. - [[14, 17, 20], 1, Detect, [nc]] # Detect(P3, P4, P5)

yaml文件3-ResNet50

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
  3. # Parameters
  4. nc: 80 # number of classes
  5. scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
  8. s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
  9. m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
  10. l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
  11. x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
  12. # YOLOv8.0n backbone
  13. backbone:
  14. # [from, repeats, module, args]
  15. - [-1, 1, ConvNormLayer, [32, 3, 2, 1, 'relu']] # 0-P1
  16. - [-1, 1, ConvNormLayer, [32, 3, 1, 1, 'relu']] # 1
  17. - [-1, 1, ConvNormLayer, [64, 3, 1, 1, 'relu']] # 2
  18. - [-1, 1, nn.MaxPool2d, [3, 2, 1]] # 3-P2
  19. - [-1, 3, Blocks, [64, BasicBlock, 2, False]] # 4
  20. - [-1, 4, Blocks, [128, BasicBlock, 3, False]] # 5-P3
  21. - [-1, 6, Blocks, [256, BasicBlock, 4, False]] # 6-P4
  22. - [-1, 3, Blocks, [512, BasicBlock, 5, False]] # 7-P5
  23. - [-1, 1, SPPF, [1024, 5]] # 8
  24. # YOLOv8.0n head
  25. head:
  26. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  27. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  28. - [-1, 3, C2f, [512]] # 11
  29. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  30. - [[-1, 5], 1, Concat, [1]] # cat backbone P3
  31. - [-1, 3, C2f, [256]] # 14 (P3/8-small)
  32. - [-1, 1, Conv, [256, 3, 2]]
  33. - [[-1, 11], 1, Concat, [1]] # cat head P4
  34. - [-1, 3, C2f, [512]] # 17 (P4/16-medium)
  35. - [-1, 1, Conv, [512, 3, 2]]
  36. - [[-1, 8], 1, Concat, [1]] # cat head P5
  37. - [-1, 3, C2f, [1024]] # 20 (P5/32-large)
  38. - [[14, 17, 20], 1, Detect, [nc]] # Detect(P3, P4, P5)

yaml文件4-ResNet101

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
  3. # Parameters
  4. nc: 80 # number of classes
  5. scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
  8. s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
  9. m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
  10. l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
  11. x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs
  12. # YOLOv8.0n backbone
  13. backbone:
  14. # [from, repeats, module, args]
  15. - [-1, 1, ConvNormLayer, [32, 3, 2, 1, 'relu']] # 0-P1
  16. - [-1, 1, ConvNormLayer, [32, 3, 1, 1, 'relu']] # 1
  17. - [-1, 1, ConvNormLayer, [64, 3, 1, 1, 'relu']] # 2
  18. - [-1, 1, nn.MaxPool2d, [3, 2, 1]] # 3-P2
  19. - [-1, 3, Blocks, [64, BasicBlock, 2, False]] # 4
  20. - [-1, 4, Blocks, [128, BasicBlock, 3, False]] # 5-P3
  21. - [-1, 23, Blocks, [256, BasicBlock, 4, False]] # 6-P4
  22. - [-1, 3, Blocks, [512, BasicBlock, 5, False]] # 7-P5
  23. - [-1, 1, SPPF, [1024, 5]] # 8
  24. # YOLOv8.0n head
  25. head:
  26. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  27. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  28. - [-1, 3, C2f, [512]] # 11
  29. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  30. - [[-1, 5], 1, Concat, [1]] # cat backbone P3
  31. - [-1, 3, C2f, [256]] # 14 (P3/8-small)
  32. - [-1, 1, Conv, [256, 3, 2]]
  33. - [[-1, 11], 1, Concat, [1]] # cat head P4
  34. - [-1, 3, C2f, [512]] # 17 (P4/16-medium)
  35. - [-1, 1, Conv, [512, 3, 2]]
  36. - [[-1, 8], 1, Concat, [1]] # cat head P5
  37. - [-1, 3, C2f, [1024]] # 20 (P5/32-large)
  38. - [[14, 17, 20], 1, Detect, [nc]] # Detect(P3, P4, P5)

不知不觉已经看完了哦,动动小手留个点赞吧--_--


本文转载自: https://blog.csdn.net/weixin_43986124/article/details/141551149
版权归原作者 不想敲代码!!! 所有, 如有侵权,请联系我们删除。

“爆改YOLOv8| 利用ResNet18、34、50、101替换yolo主干网络”的评论:

还没有评论