YOLOv8改进 | 卷积篇 |手把手教你添加动态蛇形卷积(Dynamic Snake Convolution)

一、本文介绍

动态蛇形卷积的灵感来源于对管状结构的特殊性的观察和理解,在分割拓扑管状结构、血管和道路等类型的管状结构时,任务的复杂性增加,因为这些结构的局部结构可能非常细长和迂回,而整体形态也可能多变。
因此为了应对这个挑战,作者研究团队注意到了 管状结构的特殊性 ,并提出了动态蛇形卷积(Dynamic Snake Convolution)这个方法。 动态蛇形卷积通过自适应地聚焦于细长和迂回的局部结构 ,准确地捕捉管状结构的特征。 这种卷积方法的核心思想是, 通过动态形状的卷积核来增强感知能力,针对管状结构的特征提取进行优化。

总之动态蛇形卷积是一种针对管状结构分割任务的创新方法, 在许多模型上添加针对一些数据集都能够有效的涨点, 其具有重要性和广泛的应用领域。

动态蛇形卷积(Dynamic Snake Convolution)适用于多种模型, 可以在多种模型上添加或替换该卷积 本文主要针对的改进模型是YOLOv8模型, 并修复动态蛇形卷积官方代码中存在的BUG 例如: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!修复,同时以此来进行示例帮助大家理解和掌握动态蛇形卷积和YOLOv8模型。

PS-> 如果你只想学会如何修改如何在yolov8中添加动态蛇形卷积而不想学习其原理则可以直接忽略此下大部分, 直接跳读到官方代码Bug修复的章节阅读该章节和之后的章节即可。

专栏目录: YOLOv8改进有效系列目录 | 包含卷积、主干、检测头、注意力机制、Neck上百种创新机制

专栏回顾: YOLOv8改进系列专栏——本专栏持续复习各种顶会内容——科研必备

目录

一、本文介绍

二、动态蛇形卷积背景和原理

三、动态蛇形卷积的优势

四、实验和结果

4.1 数据集

4.2 实验

4.3 实验结果

​4.4 有效性展示

五、应用和未来展望

六、在YOLOv8中添加动态蛇形卷积(Dynamic Snake Convolution)

我的环境和版本

七、官方代码bug修复

八、需要改动代码的地方

修改一

修改二

修改三

修改四

​编辑修改五

修改模型配置文件

九、开始训练

十、结果对比

十一、修改完成的文件


二、动态蛇形卷积背景和原理

论文代码地址: 动态蛇形卷积官方代码下载地址
论文地址: 【免费】动态蛇形卷积(DynamicSnakeConvolution)资源-CSDN文库

背景-> 动态蛇形卷积(Dynamic Snake Convolution)来源于临床医学,清晰勾画血管是计算流体力学研究的关键前提,并能协助放射科医师进行诊断和定位病变。在遥感应用中,完整的道路分割为路径规划提供了坚实的基础。无论是哪个领域,这些结构都具有细长和曲折的共同特征,使得它们很难在图像中捕捉到,因为它们在图像中的比例很小。因此, 迫切需要提升对细长管状结构的感知能力 ,所以在这一背景下作者提出了动态蛇形卷积(Dynamic Snake Convolution)。

原理-> 上图展示了一个 三维心脏血管数据集 和一个 二维远程道路数据集 。这两个数据集旨在提取管状结构,但由于 脆弱的局部结构和复杂的整体形态 ,这个任务面临着挑战。标准的 卷积核 旨在提取局部特征。基于此,设计了可变形卷积核以丰富它们的应用,并适应不同目标的几何变形。然而,由于前面提到的挑战,有效地聚焦于细小的管状结构是困难的。

由于以下困难,这仍然是一个具有挑战性的任务:

  1. 细小而脆弱的局部结构: 如上面的图所示,细小的结构仅占整体图像的一小部分,并且由于像素组成有限,这些结构容易受到复杂背景的干扰,使模型难以精确地区分目标的细微变化。因此,模型可能难以区分这些结构,导致分割结果出现断裂。

  2. 复杂而多变的整体形态: 上面的图片展示了细小管状结构的复杂和多变形态,即使在同一图像中也如此。不同区域中的目标呈现出形态上的变化,包括分支数量、分叉位置和路径长度等。当数据呈现出前所未见的形态结构时,模型可能会过度拟合已经见过的特征,导致在新的形态结构下泛化能力较弱。

为了应对上述障碍,提出了如下解决方案, 其中包括管状感知卷积核、多视角特征融合策略和拓扑连续性约束损失函数 。具体如下:

1. 针对细小且脆弱的局部结构所占比例小且难以聚焦的挑战 ,提出了动态蛇形卷积,通过自适应地聚焦于管状结构的细长曲线局部特征,增强对几何结构的感知。与可变形卷积不同,DSConv考虑到管状结构的蛇形形态,并通过约束补充自由学习过程,有针对性地增强对管状结构的感知。

2. 针对复杂和多变的整体形态的挑战 ,提出了一种多视角特征融合策略。在该方法中,基于DSConv生成多个形态学卷积核模板,从不同角度观察目标的结构特征,并通过总结典型的重要特征实现高效的特征融合。

3. 针对管状结构分割容易出现断裂的问题 ,提出了基于持久同调(Persistent Homology,PH)的拓扑连续性约束损失函数(TCLoss)。PH是一种从出现到消失的拓扑特征响应过程,能够从嘈杂的高维数据中获取足够的拓扑信息。相关的贝蒂数是描述拓扑空间连通性的一种方式。与其他方法不同, TCLoss将PH与点集相似性相结合 ,引导网络关注具有异常像素/体素分布的断裂区域,从拓扑角度实现连续性约束。

总结:为了克服挑战,提出了DSCNet框架,包括管状感知卷积核、多视角特征融合策略和拓扑连续性约束损失函数。DSConv增强了对细长曲线特征的感知,多视角特征融合策略提高了对复杂整体形态的处理能力,而TCLoss基于持久同调实现了从拓扑角度的连续性约束。


三、动态蛇形卷积的优势

为了提高对管状结构的性能,已经提出了各种方法,根据管状结构的形态设计了特定的网络架构和模块。具体如下:

1. 基于卷积核设计的方法: 著名的扩张卷积(dilated convolution)和可变形卷积(deformable convolution)等方法被提出来处理卷积神经网络中固有的几何变换限制,并在复杂的检测和分割任务中取得了出色的表现。这些方法还被设计用于动态感知对象的几何特征,以适应具有可变形态的结构。例如,DUNet。

2. 基于形态学的方法: 一些方法专注于利用形态学信息来处理管状结构。例如,形态学重建网络(Morphological Reconstruction Network)利用形态学重建操作来重建管状结构,从而实现更准确的分割。另外,形态学操作如开运算和闭运算也被广泛应用于处理管状结构。

3. 基于拓扑学的方法: 拓扑学方法被用来处理管状结构的拓扑特征。例如,基于持久同调(Persistent Homology)的方法可以从高维数据中获取拓扑信息,并用于分析管状结构的连通性和形态特征。

总结:为了处理管状结构,已经提出了多种方法。这些方法包括基于卷积核设计的方法、基于形态学的方法和基于拓扑学的方法。这些方法的目标是通过设计适应管状结构形态的网络架构和模块,提高对管状结构的检测和分割性能。

优势-> 以上所述的方法都只是从单一的角度去分析,DSConv提出了一种多角度特征融合策略,从多个角度补充对重要特征的关注。在这个策略中,基于动态蛇形卷积(DSConv)生成多个形态学卷积核模板,从多个角度观察目标的结构特征,并通过总结关键的标准特征实现特征融合,从而提高我们模型的性能。


四、实验和结果

4.1 数据集

使用了三个数据集来验证我们的框架,其中包括两个公开数据集和一个内部数据集。在2D方面,评估了DRIVE视网膜数据集和马萨诸塞道路数据集。在3D方面,使用了一个名为Cardiac CCTA Data的数据集。

4.2 实验

进行了比较实验和消融研究,以证明DSCN的优势。与经典的分割网络U-Net 和2021年提出的用于血管分割的CS2-Net 进行比较,以验证准确性。为了验证网络设计性能,将2022年提出的用于视网膜血管分割的DCU-Net 进行了比较。为了验证特征融合的优势,将2021年提出的用于医学图像分割的Transunet 进行了比较。为了验证损失函数约束,将2021年提出的clDice和基于Wasserstein距离的TCLoss LWTC进行了比较。这些模型在相同的数据集上进行训练,并进行了精确的实现,通过以下指标进行评估。所有指标都是针对每个图像进行计算并求平均。

1. 体积得分: 使用平均Dice系数(Dice)、相对Dice系数(RDice)、中心线Dice(clDice)、准确度(ACC)和AUC来评估结果的性能。
2. 拓扑错误: 计算基于拓扑的得分,包括Betti数β0和β1的Betti错误。同时,为了客观验证冠状动脉分割的连续性,使用直到第一个错误的重叠(OF)来评估提取的中心线的完整性。
3. 距离错误: Hausdorff距离(HD)也被广泛用于描述两组点之间的相似性,推荐用于评估薄管状结构的相似性。

4.3 实验结果

在下面的表格中展示了DSCNet方法在每个指标上的优势,结果表明提出的DSCNet在2D和3D数据集上取得了更好的结果。

在DRIVE数据集上的评估中,DSCNet在分割准确性和拓扑连续性方面均优于其他模型。在下面的表格中,与其他方法相比,DSCNet在体积准确性方面取得了最佳的分割结果,Dice系数为82.06%,RDice系数为90.17%,clDice系数为82.07%,准确度为96.87%,AUC为90.27%。同时,从拓扑的角度来看,与其他方法相比,DSCNet在拓扑连续性上取得了最好的结果,β0错误为0.998,β1错误为0.803。结果显示,DSCNet方法更好地捕捉了薄管状结构的特征,并展现出更准确的分割性能和更连续的 拓扑结构 。正如表格1中第6行到第12行所示,在引入TCLoss后,不同的模型在分割的拓扑连续性方面均有所改善。结果表明,TCLoss能够准确地约束模型关注失去拓扑连续性的薄管状结构。在ROADS数据集上的评估中,DSCNet同样取得了最佳结果。如表格1所示,与其他方法相比,提出的带有TCLoss的DSCNet在分割结果上取得了最佳的效果,Dice系数为78.21%,RDice系数为85.85%,clDice系数为87.64%。与经典的分割网络UNet的结果相比,DSCNet的方法在Dice系数、RDice系数和clDice系数上分别改善了最多1.31%、1.78%和0.77%。结果显示,与其他模型相比,DSCNet的模型在结构复杂且形态多变的道路数据集上也表现良好。

在CORONARY数据集上的评估中,验证了DSCNet在3D薄管状结构分割任务上仍然取得了最佳结果。如下面的表格所示,与其他方法相比,提出的DSCNet在分割结果上取得了最佳的效果,Dice系数为80.27%,RDice系数为86.37%,clDice系数为85.26%。与经典的分割网络UNet的结果相比,DSCNet方法在Dice系数、RDice系数和clDice系数上分别改善了最多3.40%、1.89%和3.83%。同时,使用OF指标来评估分割的连续性。使用DSCNet的方法,LAD的OF指标提升了6.00%,LCX的OF指标提升了3.78%,而RCA的OF指标提升了3.30%

4.4 有效性展示

DSCNet和TCLoss在各个方面都具有决定性的视觉优势。

(1) 为了展示DSCNet的有效性下面的图片中。从左到右,第三到第五列展示了不同网络在分割准确性方面的表现。由于DSConv能够自适应地感知关键特征,DSCNet的方法在分割结果上表现出色。与其他方法相比,DSCNet的方法能够更好地捕捉和保留薄管状结构的细节。

(2) 为了验证DSCNet的TCLoss的有效性,第六列展示了在没有使用TCLoss的情况下的分割结果。可以看出,没有TCLoss的方法在拓扑连续性方面存在明显的问题,而DSCNet的方法能够通过TCLoss准确地约束分割结果的拓扑结构,使得分割结果更加连续。

(3) 在第七列和第八列中,展示了DSCNet在不同数据集上的分割结果。可以看到,DSCNet在DRIVE和ROADS数据集上都能取得准确且连续的分割结果,进一步证明了DSCNet的通用性和鲁棒性。

总的来说,从图6可以清楚地看到我们的DSCNet和TCLoss在分割准确性和拓扑连续性方面的显著优势,这进一步证明了我们方法的有效性和优越性。

DSConv能够动态地适应管状结构的形状,并且注意力能够很好地适配目标。

(1) 适应管状结构的形状。下面的图片中的顶部显示了卷积核的位置和形状。可视化结果显示,DSConv能够很好地适应管状结构并保持形状,而可变形卷积则在目标外部游走。

(2) 关注管状结构的位置。下面的图片的底部显示了给定点的注意力热力图。结果显示,DSConv最亮的区域集中在管状结构上,这表示DSConv对管状结构更加敏感。

这些结果表明,我们的DSConv能够有效地适应和关注管状结构,从而使得DSCNet能够更好地捕捉和分割这些结构。


五、核心代码

使用方式看章节六

  1. import torch
  2. import torch.nn as nn
  3. from ..modules.conv import Conv
  4. class DySnakeConv(nn.Module):
  5. def __init__(self, inc, ouc, k=3) -> None:
  6. super().__init__()
  7. self.conv_0 = Conv(inc, ouc, k)
  8. self.conv_x = DSConv(inc, ouc, 0, k)
  9. self.conv_y = DSConv(inc, ouc, 1, k)
  10. def forward(self, x):
  11. return torch.cat([self.conv_0(x), self.conv_x(x), self.conv_y(x)], dim=1)
  12. class DSConv(nn.Module):
  13. def __init__(self, in_ch, out_ch, morph, kernel_size=3, if_offset=True, extend_scope=1):
  14. """
  15. The Dynamic Snake Convolution
  16. :param in_ch: input channel
  17. :param out_ch: output channel
  18. :param kernel_size: the size of kernel
  19. :param extend_scope: the range to expand (default 1 for this method)
  20. :param morph: the morphology of the convolution kernel is mainly divided into two types
  21. along the x-axis (0) and the y-axis (1) (see the paper for details)
  22. :param if_offset: whether deformation is required, if it is False, it is the standard convolution kernel
  23. """
  24. super(DSConv, self).__init__()
  25. # use the <offset_conv> to learn the deformable offset
  26. self.offset_conv = nn.Conv2d(in_ch, 2 * kernel_size, 3, padding=1)
  27. self.bn = nn.BatchNorm2d(2 * kernel_size)
  28. self.kernel_size = kernel_size
  29. # two types of the DSConv (along x-axis and y-axis)
  30. self.dsc_conv_x = nn.Conv2d(
  31. in_ch,
  32. out_ch,
  33. kernel_size=(kernel_size, 1),
  34. stride=(kernel_size, 1),
  35. padding=0,
  36. )
  37. self.dsc_conv_y = nn.Conv2d(
  38. in_ch,
  39. out_ch,
  40. kernel_size=(1, kernel_size),
  41. stride=(1, kernel_size),
  42. padding=0,
  43. )
  44. self.gn = nn.GroupNorm(out_ch // 4, out_ch)
  45. self.act = Conv.default_act
  46. self.extend_scope = extend_scope
  47. self.morph = morph
  48. self.if_offset = if_offset
  49. def forward(self, f):
  50. offset = self.offset_conv(f)
  51. offset = self.bn(offset)
  52. # We need a range of deformation between -1 and 1 to mimic the snake's swing
  53. offset = torch.tanh(offset)
  54. input_shape = f.shape
  55. dsc = DSC(input_shape, self.kernel_size, self.extend_scope, self.morph)
  56. deformed_feature = dsc.deform_conv(f, offset, self.if_offset)
  57. if self.morph == 0:
  58. x = self.dsc_conv_x(deformed_feature.type(f.dtype))
  59. x = self.gn(x)
  60. x = self.act(x)
  61. return x
  62. else:
  63. x = self.dsc_conv_y(deformed_feature.type(f.dtype))
  64. x = self.gn(x)
  65. x = self.act(x)
  66. return x
  67. # Core code, for ease of understanding, we mark the dimensions of input and output next to the code
  68. class DSC(object):
  69. def __init__(self, input_shape, kernel_size, extend_scope, morph):
  70. self.num_points = kernel_size
  71. self.width = input_shape[2]
  72. self.height = input_shape[3]
  73. self.morph = morph
  74. self.extend_scope = extend_scope # offset (-1 ~ 1) * extend_scope
  75. # define feature map shape
  76. """
  77. B: Batch size C: Channel W: Width H: Height
  78. """
  79. self.num_batch = input_shape[0]
  80. self.num_channels = input_shape[1]
  81. """
  82. input: offset [B,2*K,W,H] K: Kernel size (2*K: 2D image, deformation contains <x_offset> and <y_offset>)
  83. output_x: [B,1,W,K*H] coordinate map
  84. output_y: [B,1,K*W,H] coordinate map
  85. """
  86. def _coordinate_map_3D(self, offset, if_offset):
  87. device = offset.device
  88. # offset
  89. y_offset, x_offset = torch.split(offset, self.num_points, dim=1)
  90. y_center = torch.arange(0, self.width).repeat([self.height])
  91. y_center = y_center.reshape(self.height, self.width)
  92. y_center = y_center.permute(1, 0)
  93. y_center = y_center.reshape([-1, self.width, self.height])
  94. y_center = y_center.repeat([self.num_points, 1, 1]).float()
  95. y_center = y_center.unsqueeze(0)
  96. x_center = torch.arange(0, self.height).repeat([self.width])
  97. x_center = x_center.reshape(self.width, self.height)
  98. x_center = x_center.permute(0, 1)
  99. x_center = x_center.reshape([-1, self.width, self.height])
  100. x_center = x_center.repeat([self.num_points, 1, 1]).float()
  101. x_center = x_center.unsqueeze(0)
  102. if self.morph == 0:
  103. """
  104. Initialize the kernel and flatten the kernel
  105. y: only need 0
  106. x: -num_points//2 ~ num_points//2 (Determined by the kernel size)
  107. !!! The related PPT will be submitted later, and the PPT will contain the whole changes of each step
  108. """
  109. y = torch.linspace(0, 0, 1)
  110. x = torch.linspace(
  111. -int(self.num_points // 2),
  112. int(self.num_points // 2),
  113. int(self.num_points),
  114. )
  115. y, x = torch.meshgrid(y, x)
  116. y_spread = y.reshape(-1, 1)
  117. x_spread = x.reshape(-1, 1)
  118. y_grid = y_spread.repeat([1, self.width * self.height])
  119. y_grid = y_grid.reshape([self.num_points, self.width, self.height])
  120. y_grid = y_grid.unsqueeze(0) # [B*K*K, W,H]
  121. x_grid = x_spread.repeat([1, self.width * self.height])
  122. x_grid = x_grid.reshape([self.num_points, self.width, self.height])
  123. x_grid = x_grid.unsqueeze(0) # [B*K*K, W,H]
  124. y_new = y_center + y_grid
  125. x_new = x_center + x_grid
  126. y_new = y_new.repeat(self.num_batch, 1, 1, 1).to(device)
  127. x_new = x_new.repeat(self.num_batch, 1, 1, 1).to(device)
  128. y_offset_new = y_offset.detach().clone()
  129. if if_offset:
  130. y_offset = y_offset.permute(1, 0, 2, 3)
  131. y_offset_new = y_offset_new.permute(1, 0, 2, 3)
  132. center = int(self.num_points // 2)
  133. # The center position remains unchanged and the rest of the positions begin to swing
  134. # This part is quite simple. The main idea is that "offset is an iterative process"
  135. y_offset_new[center] = 0
  136. for index in range(1, center):
  137. y_offset_new[center + index] = (y_offset_new[center + index - 1] + y_offset[center + index])
  138. y_offset_new[center - index] = (y_offset_new[center - index + 1] + y_offset[center - index])
  139. y_offset_new = y_offset_new.permute(1, 0, 2, 3).to(device)
  140. y_new = y_new.add(y_offset_new.mul(self.extend_scope))
  141. y_new = y_new.reshape(
  142. [self.num_batch, self.num_points, 1, self.width, self.height])
  143. y_new = y_new.permute(0, 3, 1, 4, 2)
  144. y_new = y_new.reshape([
  145. self.num_batch, self.num_points * self.width, 1 * self.height
  146. ])
  147. x_new = x_new.reshape(
  148. [self.num_batch, self.num_points, 1, self.width, self.height])
  149. x_new = x_new.permute(0, 3, 1, 4, 2)
  150. x_new = x_new.reshape([
  151. self.num_batch, self.num_points * self.width, 1 * self.height
  152. ])
  153. return y_new, x_new
  154. else:
  155. """
  156. Initialize the kernel and flatten the kernel
  157. y: -num_points//2 ~ num_points//2 (Determined by the kernel size)
  158. x: only need 0
  159. """
  160. y = torch.linspace(
  161. -int(self.num_points // 2),
  162. int(self.num_points // 2),
  163. int(self.num_points),
  164. )
  165. x = torch.linspace(0, 0, 1)
  166. y, x = torch.meshgrid(y, x)
  167. y_spread = y.reshape(-1, 1)
  168. x_spread = x.reshape(-1, 1)
  169. y_grid = y_spread.repeat([1, self.width * self.height])
  170. y_grid = y_grid.reshape([self.num_points, self.width, self.height])
  171. y_grid = y_grid.unsqueeze(0)
  172. x_grid = x_spread.repeat([1, self.width * self.height])
  173. x_grid = x_grid.reshape([self.num_points, self.width, self.height])
  174. x_grid = x_grid.unsqueeze(0)
  175. y_new = y_center + y_grid
  176. x_new = x_center + x_grid
  177. y_new = y_new.repeat(self.num_batch, 1, 1, 1)
  178. x_new = x_new.repeat(self.num_batch, 1, 1, 1)
  179. y_new = y_new.to(device)
  180. x_new = x_new.to(device)
  181. x_offset_new = x_offset.detach().clone()
  182. if if_offset:
  183. x_offset = x_offset.permute(1, 0, 2, 3)
  184. x_offset_new = x_offset_new.permute(1, 0, 2, 3)
  185. center = int(self.num_points // 2)
  186. x_offset_new[center] = 0
  187. for index in range(1, center):
  188. x_offset_new[center + index] = (x_offset_new[center + index - 1] + x_offset[center + index])
  189. x_offset_new[center - index] = (x_offset_new[center - index + 1] + x_offset[center - index])
  190. x_offset_new = x_offset_new.permute(1, 0, 2, 3).to(device)
  191. x_new = x_new.add(x_offset_new.mul(self.extend_scope))
  192. y_new = y_new.reshape(
  193. [self.num_batch, 1, self.num_points, self.width, self.height])
  194. y_new = y_new.permute(0, 3, 1, 4, 2)
  195. y_new = y_new.reshape([
  196. self.num_batch, 1 * self.width, self.num_points * self.height
  197. ])
  198. x_new = x_new.reshape(
  199. [self.num_batch, 1, self.num_points, self.width, self.height])
  200. x_new = x_new.permute(0, 3, 1, 4, 2)
  201. x_new = x_new.reshape([
  202. self.num_batch, 1 * self.width, self.num_points * self.height
  203. ])
  204. return y_new, x_new
  205. """
  206. input: input feature map [N,C,D,W,H];coordinate map [N,K*D,K*W,K*H]
  207. output: [N,1,K*D,K*W,K*H] deformed feature map
  208. """
  209. def _bilinear_interpolate_3D(self, input_feature, y, x):
  210. device = input_feature.device
  211. y = y.reshape([-1]).float()
  212. x = x.reshape([-1]).float()
  213. zero = torch.zeros([]).int()
  214. max_y = self.width - 1
  215. max_x = self.height - 1
  216. # find 8 grid locations
  217. y0 = torch.floor(y).int()
  218. y1 = y0 + 1
  219. x0 = torch.floor(x).int()
  220. x1 = x0 + 1
  221. # clip out coordinates exceeding feature map volume
  222. y0 = torch.clamp(y0, zero, max_y)
  223. y1 = torch.clamp(y1, zero, max_y)
  224. x0 = torch.clamp(x0, zero, max_x)
  225. x1 = torch.clamp(x1, zero, max_x)
  226. input_feature_flat = input_feature.flatten()
  227. input_feature_flat = input_feature_flat.reshape(
  228. self.num_batch, self.num_channels, self.width, self.height)
  229. input_feature_flat = input_feature_flat.permute(0, 2, 3, 1)
  230. input_feature_flat = input_feature_flat.reshape(-1, self.num_channels)
  231. dimension = self.height * self.width
  232. base = torch.arange(self.num_batch) * dimension
  233. base = base.reshape([-1, 1]).float()
  234. repeat = torch.ones([self.num_points * self.width * self.height
  235. ]).unsqueeze(0)
  236. repeat = repeat.float()
  237. base = torch.matmul(base, repeat)
  238. base = base.reshape([-1])
  239. base = base.to(device)
  240. base_y0 = base + y0 * self.height
  241. base_y1 = base + y1 * self.height
  242. # top rectangle of the neighbourhood volume
  243. index_a0 = base_y0 - base + x0
  244. index_c0 = base_y0 - base + x1
  245. # bottom rectangle of the neighbourhood volume
  246. index_a1 = base_y1 - base + x0
  247. index_c1 = base_y1 - base + x1
  248. # get 8 grid values
  249. value_a0 = input_feature_flat[index_a0.type(torch.int64)].to(device)
  250. value_c0 = input_feature_flat[index_c0.type(torch.int64)].to(device)
  251. value_a1 = input_feature_flat[index_a1.type(torch.int64)].to(device)
  252. value_c1 = input_feature_flat[index_c1.type(torch.int64)].to(device)
  253. # find 8 grid locations
  254. y0 = torch.floor(y).int()
  255. y1 = y0 + 1
  256. x0 = torch.floor(x).int()
  257. x1 = x0 + 1
  258. # clip out coordinates exceeding feature map volume
  259. y0 = torch.clamp(y0, zero, max_y + 1)
  260. y1 = torch.clamp(y1, zero, max_y + 1)
  261. x0 = torch.clamp(x0, zero, max_x + 1)
  262. x1 = torch.clamp(x1, zero, max_x + 1)
  263. x0_float = x0.float()
  264. x1_float = x1.float()
  265. y0_float = y0.float()
  266. y1_float = y1.float()
  267. vol_a0 = ((y1_float - y) * (x1_float - x)).unsqueeze(-1).to(device)
  268. vol_c0 = ((y1_float - y) * (x - x0_float)).unsqueeze(-1).to(device)
  269. vol_a1 = ((y - y0_float) * (x1_float - x)).unsqueeze(-1).to(device)
  270. vol_c1 = ((y - y0_float) * (x - x0_float)).unsqueeze(-1).to(device)
  271. outputs = (value_a0 * vol_a0 + value_c0 * vol_c0 + value_a1 * vol_a1 +
  272. value_c1 * vol_c1)
  273. if self.morph == 0:
  274. outputs = outputs.reshape([
  275. self.num_batch,
  276. self.num_points * self.width,
  277. 1 * self.height,
  278. self.num_channels,
  279. ])
  280. outputs = outputs.permute(0, 3, 1, 2)
  281. else:
  282. outputs = outputs.reshape([
  283. self.num_batch,
  284. 1 * self.width,
  285. self.num_points * self.height,
  286. self.num_channels,
  287. ])
  288. outputs = outputs.permute(0, 3, 1, 2)
  289. return outputs
  290. def deform_conv(self, input, offset, if_offset):
  291. y, x = self._coordinate_map_3D(offset, if_offset)
  292. deformed_feature = self._bilinear_interpolate_3D(input, y, x)
  293. return deformed_feature
  294. class Bottleneck(nn.Module):
  295. """Standard bottleneck."""
  296. def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5):
  297. """Initializes a bottleneck module with given input/output channels, shortcut option, group, kernels, and
  298. expansion.
  299. """
  300. super().__init__()
  301. c_ = int(c2 * e) # hidden channels
  302. self.cv1 = Conv(c1, c_, k[0], 1)
  303. self.cv2 = Conv(c_, c2, k[1], 1, g=g)
  304. self.add = shortcut and c1 == c2
  305. def forward(self, x):
  306. """'forward()' applies the YOLO FPN to input data."""
  307. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  308. class Bottleneck_DySnakeConv(Bottleneck):
  309. """Standard bottleneck with DySnakeConv."""
  310. def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5): # ch_in, ch_out, shortcut, groups, kernels, expand
  311. super().__init__(c1, c2, shortcut, g, k, e)
  312. c_ = int(c2 * e) # hidden channels
  313. self.cv2 = DySnakeConv(c_, c2, k[1])
  314. self.cv3 = Conv(c2 * 3, c2, k=1)
  315. def forward(self, x):
  316. """'forward()' applies the YOLOv5 FPN to input data."""
  317. return x + self.cv3(self.cv2(self.cv1(x))) if self.add else self.cv3(self.cv2(self.cv1(x)))
  318. class C2f_DSConv(nn.Module):
  319. """Faster Implementation of CSP Bottleneck with 2 convolutions."""
  320. def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5):
  321. """Initialize CSP bottleneck layer with two convolutions with arguments ch_in, ch_out, number, shortcut, groups,
  322. expansion.
  323. """
  324. super().__init__()
  325. self.c = int(c2 * e) # hidden channels
  326. self.cv1 = Conv(c1, 2 * self.c, 1, 1)
  327. self.cv2 = Conv((2 + n) * self.c, c2, 1) # optional act=FReLU(c2)
  328. self.m = nn.ModuleList(Bottleneck_DySnakeConv(self.c, self.c, shortcut, g, k=(3, 3), e=1.0) for _ in range(n))
  329. def forward(self, x):
  330. """Forward pass through C2f layer."""
  331. y = list(self.cv1(x).chunk(2, 1))
  332. y.extend(m(y[-1]) for m in self.m)
  333. return self.cv2(torch.cat(y, 1))
  334. def forward_split(self, x):
  335. """Forward pass using split() instead of chunk()."""
  336. y = list(self.cv1(x).split((self.c, self.c), 1))
  337. y.extend(m(y[-1]) for m in self.m)
  338. return self.cv2(torch.cat(y, 1))

六、需要改动代码的地方

6.1 添加步骤

6.1.1 步骤一

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

6.1.2 步骤二

之后我们找到'ultralytics/nn/tasks.py'文件,在其中注册我们的MLCA和C2f_MLCA模块。

首先我们需要在文件的开头导入我们的MLCA和C2f_MLCA模块, 如下图所示->

6.1.3 步骤三

我们找到parse_model这个方法,可以用搜索也可以自己手动找,大概在六百多行吧。 我们找到如下的地方,然后将我导入的模块添加进去即可,模仿我添加即可(需要注意的是该卷积不可替换主干上的卷积,该卷积的构造比较特殊)。

到此我们就注册成功了,可以修改yaml文件中输入MLCA和C2f_MLCA使用这个模块了。


七、DSConv的yaml文件和运行记录

7.1 DSConv的yaml文件一

下面的添加 DSConv 是我实验结果的版本。

  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, Conv, [64, 3, 2]] # 0-P1/2
  16. - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
  17. - [-1, 3, C2f_DSConv, [128, True]]
  18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  19. - [-1, 6, C2f_DSConv, [256, True]]
  20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  21. - [-1, 6, C2f_DSConv, [512, True]]
  22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
  23. - [-1, 3, C2f_DSConv, [1024, True]]
  24. - [-1, 1, SPPF, [1024, 5]] # 9
  25. # YOLOv8.0n head
  26. head:
  27. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  28. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  29. - [-1, 3, C2f, [512]] # 12
  30. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  31. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
  32. - [-1, 3, C2f_DSConv, [256]] # 15 (P3/8-small)
  33. - [-1, 1, Conv, [256, 3, 2]]
  34. - [[-1, 12], 1, Concat, [1]] # cat head P4
  35. - [-1, 3, C2f_DSConv, [512]] # 18 (P4/16-medium)
  36. - [-1, 1, Conv, [512, 3, 2]]
  37. - [[-1, 9], 1, Concat, [1]] # cat head P5
  38. - [-1, 3, C2f_DSConv, [1024]] # 21 (P5/32-large)
  39. - [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)

7.2 DSConv的yaml文件二

  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, Conv, [64, 3, 2]] # 0-P1/2
  16. - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
  17. - [-1, 3, C2f, [128, True]]
  18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  19. - [-1, 6, C2f, [256, True]]
  20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  21. - [-1, 6, C2f, [512, True]]
  22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
  23. - [-1, 3, C2f, [1024, True]]
  24. - [-1, 1, SPPF, [1024, 5]] # 9
  25. # YOLOv8.0n head
  26. head:
  27. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  28. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  29. - [-1, 3, C2f, [512]] # 12
  30. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  31. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
  32. - [-1, 3, C2f_DSConv, [256]] # 15 (P3/8-small)
  33. - [-1, 1, Conv, [256, 3, 2]]
  34. - [[-1, 12], 1, Concat, [1]] # cat head P4
  35. - [-1, 3, C2f_DSConv, [512]] # 18 (P4/16-medium)
  36. - [-1, 1, Conv, [512, 3, 2]]
  37. - [[-1, 9], 1, Concat, [1]] # cat head P5
  38. - [-1, 3, C2f_DSConv, [1024]] # 21 (P5/32-large)
  39. - [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)

7.3 DSConv的yaml文件三

  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, Conv, [64, 3, 2]] # 0-P1/2
  16. - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
  17. - [-1, 3, C2f, [128, True]]
  18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  19. - [-1, 6, C2f, [256, True]]
  20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  21. - [-1, 6, C2f, [512, True]]
  22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
  23. - [-1, 3, C2f, [1024, True]]
  24. - [-1, 1, SPPF, [1024, 5]] # 9
  25. # YOLOv8.0n head
  26. head:
  27. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  28. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  29. - [-1, 3, C2f_DSConv, [512]] # 12
  30. - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  31. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
  32. - [-1, 3, C2f_DSConv, [256]] # 15 (P3/8-small)
  33. - [-1, 1, Conv, [256, 3, 2]]
  34. - [[-1, 12], 1, Concat, [1]] # cat head P4
  35. - [-1, 3, C2f_DSConv, [512]] # 18 (P4/16-medium)
  36. - [-1, 1, Conv, [512, 3, 2]]
  37. - [[-1, 9], 1, Concat, [1]] # cat head P5
  38. - [-1, 3, C2f_DSConv, [1024]] # 21 (P5/32-large)
  39. - [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)


7.4 DSConv的训练过程截图

下面是添加了 DSConv 的训练截图。

大家可以看下面的运行结果和添加的位置所以不存在我发的代码不全或者运行不了的问题大家有问题也可以在评论区评论我看到都会为大家解答(我知道的)。


八、本文总结

到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv8改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~

专栏回顾: YOLOv8改进系列专栏——本专栏持续复习各种顶会内容——科研必备

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树 首页 概览 62117 人正在系统学习中
购买专栏的读者,可加Qq加交流群
QQ名片
  • 24
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
成就一亿技术人!
拼手气红包 6.0元
  • weixin_60532004
    TypeError: group_norm(): argument 'num_groups' (position 2) must be int, not bool
    • java1314777
      Snu77 作者 回复 甲853 2023.11.08
      1
      分类名称是在你数据集里面定义的,你指定数据集的时候有一个data.yaml文件分类的名称会在里面定义的,你可以看我的有一篇一键导出数据集的roboflow教程,导出一个你应该就明白了 表情包
    • weixin_60532004
      甲853 回复 Snu77 2023.11.08
      你好,为什么您这里训练不用改数据的分类名称这些的?
    • java1314777
      Snu77 作者 回复 甲853 2023.11.08
      可以在yolv8.yaml文件中你添加Dscn哪里的True前面加一个数字1,这个是不同版本之间的报错问题
    • 查看全部 3 条回复
  • community_717
    恭喜您撰写了第20篇博客!标题中提到的"动态蛇形卷积(Dynamic Snake Convolution)"听起来非常有趣和创新。您的持续创作展现了您对YOLOv8的深入理解,并且您的文章标题也很吸引人。看到您的工作不断取得进展,我感到非常鼓舞。

    在接下来的创作中,我想提出一个建议,希望能对您有所帮助。或许您可以考虑加入一些实例或案例研究,以展示动态蛇形卷积在具体问题中的应用。这样的实例可以帮助读者更好地理解该方法,并在实践中应用它。当然,这只是一个建议,希望能给您一些启发。期待您未来更多的创作!
    此评论由机器自动生成
2 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额 3.43 前往充值 >
需支付: 10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Snu77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付: ¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或 充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值

举报

选择你想要举报的内容(必选)
  • 内容涉黄
  • 政治相关
  • 内容抄袭
  • 涉嫌广告
  • 内容侵权
  • 侮辱谩骂
  • 样式问题
  • 其他
新手
引导
客服 举报 返回
顶部
QQ名片
QQ ID:3638732121 QQ扫码添加好友或搜索 ID
复制QQ ID