学习资源站

YOLOv11改进-Conv篇-轻量级下采样方法ContextGuided(二次创新C3k2,助力多尺度特征融合)

一、本文介绍

本文给大家带来的是改进机制是一种替换Conv的模块 Context Guided Block (CG block) ,其是在CGNet论文中提出的一种模块,其基本原理是 模拟人类视觉系统依赖上下文信息来理解场景 。CG block 用于捕获局部特征、周围上下文和全局上下文,并将这些 信息融合 起来以提高准确性。 (经过我检验分别在三种数据集上,大中小均进行了150轮次的实验,均有一定程度上的涨点,下面我选取了一种中等大小的数据集的结果进行了对比) ,同时本文的修改方法和之前的普通卷积模块也有所不同,大家需要注意看章节四进行修改, 版本含二次创新C3k2, 助力多尺度特征融合。

适用检测目标: 所有的目标检测均有一定的提点



二、ContextGuidedBlock_Down模块原理

​​

论文地址: 官方论文地址

代码地址: 官方代码地址

​​


2.1  ContextGuidedBlock_Down的基本原理

Context Guided Block (CG block) 在CGNet中的基本原理是 模拟人类视觉系统依赖上下文信息来理解场景 。CG block 用于捕获局部特征、周围上下文和全局上下文,并将这些信息融合起来以提高语义分割的准确性。这一模块包含以下部分:

1. 局部特征提取器(floc): 使用标准卷积层学习局部特征。
2. 周围上下文提取器(fsur): 使用空洞/膨胀卷积层来学习更大接收野的周围上下文。
3. 联合特征提取器(fjoi): 通过连接层和批量归一化(BN)以及参数化ReLU(PReLU)操作来融合局部特征和周围上下文的输出,获取联合特征。
4. 全局上下文提取器(fglo): 使用全局平均池化层聚合全局上下文,并通过多层感知器来进一步提取全局上下文。然后,使用缩放层以提取的全局上下文对联合特征进行加权,以强调有用的组件并抑制无用的组件。

这个过程是自适应的,因为提取的全局上下文是基于输入图像生成的。CG block 的设计允许CGNet能够有效地从底层到顶层聚合 上下文信息 ,并在 语义层面(来自深层) 空间层面(来自浅层) 捕获上下文信息,这对于语义分割至关重要。

下面就为大家展示了 三种用于语义分割的不同架构:

(a) FCN-shape(全卷积网络形状): 模型 遵循图像分类的设计原则,忽略了上下文信息。它可能使用一系列卷积和池化层来处理输入图像,并生成输出,但没有显式地对各个层次的特征周围上下文进行建模。

(b) FCN-CM(全卷积网络-上下文模块): 此模型只在编码阶段后捕获上下文信息,通过执行一个上下文模块来从语义层次提取上下文信息。

(c) 我们提出的CGNet(上下文引导网络): 捕获所有阶段的上下文特征,从语义层次和空间层次两方面进行。

总结: CGB_Down的设计旨在充分利用局部特征、周围上下文和全局上下文,通过这种结构设计,CGNet能够在局部和全局上下文之间建立联系,这对于准确分类图像中的每个像素至关重要。此外,CGB_Down还采用了残差学习来帮助学习复杂特征并在训练期间改善梯度的 反向传播


2.2 局部特征提取器

局部特征提取器 (记为 floc(*) ​)是上下文引导块(CG block)的一个组成部分,专门用于 学习输入数据中的局部特征 在CGNet的设计中,这个局部特征提取器通过 标准的3×3卷积层 实现,其目的是从图像中的局部区域提取特征。这些局部特征随后与周围上下文特征结合,形成了网络对各个区域的全面理解,这对于语义分割尤为重要。

CGNet使用的局部特征提取器与 周围上下文提取器 fsur(*) ​) 一起工作,确保模型不仅能够理解每个像素或局部区域的信息,而且还能理解这些区域在整体上下文中的关系。这种提取器能够捕捉到的细节和局部变化信息对于精确地分类图像中的每个像素至关重要,特别是在需要细粒度预测的任务中,如在复杂场景中区分不同的物体和表面。

CGNet的结构设计还包括 减少参数数量 ,其中局部特征提取器和周围上下文提取器采用了 通道卷积(channel-wise convolutions) ,以减少跨通道的计算成本并大幅节约内存。这种设计允许CGNet即使在资源受限的环境中(如移动设备)也能有效运行,同时保持高准确率和实时性。


2.3 周围上下文提取器

周围上下文提取器 f_{sur}(*) )在CGNet架构中的 作用和原理 包括:

1. 提取更广泛的上下文信息: 周围上下文提取器使用扩展卷积(例如空洞卷积)来增加感受野的大小,从而捕获更宽广的上下文信息。这允许模型观察到更大区域的特征,而不仅仅是局部的细节。

2. 辅助局部特征理解: 通过结合局部特征和周围上下文,​能够提供额外的信息,帮助模型更好地理解复杂的场景。例如,在辨识一个物体时,除了物体本身的特征外,它的周围环境也提供了重要的线索。

3. 改进语义分割的准确性: 研究表明,周围上下文的信息对于提高语义分割的准确性非常有益。在不同的架构实验中,引入 f_{sur}(*) ​都能显著提升分割的准确率。

4. 在网络的所有块中使用: 为了充分利用周围上下文的优势, f_{sur}(*) ​在CGNet的所有块中都有应用,以保证整个网络都能受益于周围上下文信息的提取。

5. 空间金字塔池化: 在一些变体中, f_{sur}(*) ​可能会采用空间金字塔池化来聚合不同尺度的上下文信息,这有助于模型捕捉从最小的细节到整体布局的不同层面的信息。

总结: 通过这些设计,周围上下文提取器加强了CGNet处理各种尺度信息的能力,这在处理高分辨率图像和复杂场景的语义分割任务中尤其重要。


2.4 联合特征提取器

联合特征提取器 f_{joi}(*) ​)在CGNet中的作用是整合由局部特征提取器和周围上下文提取器提取的特征。这些特征分别捕捉到了输入数据的细节(局部特征)和更广阔区域内的信息(周围上下文)。联合特征提取器的设计目的是为了使得网络能够同时考虑局部和上下文信息,从而提高语义分割的准确性。 下面是它的一些关键点:

1. 特征融合: 联合特征提取器通过连接(concatenation)操作将局部特征和周围上下文特征结合起来,形成一个综合的特征表示。

2. 增强特征表示: 联合后的特征通过批量归一化(Batch Normalization, BN)和参数化的线性单元(Parametric ReLU, PReLU)等操作进行进一步的加工,以增强特征表示的能力。

3. 全局上下文的整合: 在某些设计中,联合特征还会与全局上下文特征( f_{glo}(*) ​)结合,以利用整个输入图像的信息来进一步优化特征。

联合特征提取器是上下文引导网络 实现其高效语义分割能力的关键连接点 ,它允许网络在局部精细度和全局上下文间达到平衡.

下图为大家展示了 上下文引导网络(Context Guided Network, CGNet)的架构 。这个网络通过以下阶段处理输入图像来生成预测:

​​

1. Stage 1: 包含连续的3x3卷积层,这些层负责提取输入图像的初步特征。

2. Stage 2: 由多个CG块组成,数量用"M"表示。每个CG块都结合了局部特征提取器和周围上下文提取器,它们一起工作以捕获更复杂的局部和上下文信息。

3. Stage 3: 包含更多的CG块,数量用"N"表示,这一阶段进一步提炼特征,以捕捉更高层次的上下文信息。

4. 1x1 Conv: 一个1x1的卷积层用于将特征映射到目标类别的数量,为最终的上采样和分类做准备。

5. 上采样(Upsample): 使用上采样或逆卷积操作将特征图尺寸扩大回输入图像的尺寸。

6. 预测(Prediction) :最终的预测图,其中每个像素被分配了一个类别标签,展示了对输入图像进行语义分割的结果。

总结: CGNet的设计旨在实现高效的语义分割,通过在网络的不同阶段利用局部和全局上下文信息来提高准确率,同时保持模型的轻量级特性。这使CGNet特别适合于资源受限的设备,如移动设备或 嵌入式系统 。在图中的预测示例中,可以看到网络已经将不同的交通参与者和背景要素成功地分割出来,用不同的颜色标记不同的类别。


2.5 全局上下文提取器

全局上下文提取器 f_{glo}(*) ​)在CGNet中的作用是 捕获并利用整个输入图像的全局信息 ,以增强联合特征提取器学习到的特征。 以下是它的基本原理

1. 全局特征汇总: 全局上下文提取器通过 全局平均池化(Global Average Pooling, GAP) 来聚合整个特征图的全局信息。这个步骤产生一个全局特征向量,它捕获了输入图像中每个通道的平均响应。

2. 多层感知机处理: 全局特征向量随后通过一个 多层感知机(Multilayer Perceptron, MLP) 进一步处理。MLP能够学习特征间的复杂非线性关系,进一步细化全局上下文特征。

3. 特征重标定: 提取的全局上下文通过 缩放层(scale layer) 与联合特征结合。这个操作相当于将全局上下文信息作为权重,通道级别地重新标定联合特征,强调有用的特征部分,抑制不重要的特征部分。

4. 自适应性: 全局上下文提取器的操作是自适应的,因为提取的全局上下文是根据输入图像生成的,使得网络能够针对不同的图像生成定制化的全局上下文。

5. 提高分割准确性: 在消融研究中,使用全局上下文提取器可以提高分割的准确性。这证明了全局上下文在提升模型 性能 方面的价值。

提供了 上下文引导块(Context Guided block)的概览 。在图中的全局上下文提取器 f_{glo}(*) ​部分,展示了使用 全局平均池化(GAP) 来提取全图的上下文信息,然后通过 两个全连接层(FC) 对这些信息进行进一步的处理。这有助于网络理解整个图像的全局信息,这对于分类图像中的局部区域特别重要,尤其是在这些局部区域的类别可能依赖于全局上下文的情况下。

​​

这些组件共同工作,提高了网络对复杂场景中各种尺度的特征的理解能力,使得CGNet能够更准确地进行语义分割。通过这样的设计,CGNet能够在局部和全局上下文之间建立联系,这对于准确分类图像中的每个像素至关重要。


三、 ContextGuided的核心代码

我们复制下面的代码'ultralytics/nn'到该目录下,创建一个py文件粘贴进去,我这里起名为ContextGuided(这里有一个注意点是不要和定义的类重名有时候会报错)。

  1. import torch
  2. import torch.nn as nn
  3. __all__ = ['C3k2_ContextGuidedBlock', 'ContextGuidedBlock_Down']
  4. class ConvBNPReLU(nn.Module):
  5. def __init__(self, nIn, nOut, kSize, stride=1):
  6. """
  7. args:
  8. nIn: number of input channels
  9. nOut: number of output channels
  10. kSize: kernel size
  11. stride: stride rate for down-sampling. Default is 1
  12. """
  13. super().__init__()
  14. if isinstance(kSize, tuple):
  15. kSize = kSize[0]
  16. padding = int((kSize - 1) / 2)
  17. self.conv = nn.Conv2d(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias=False)
  18. self.bn = nn.BatchNorm2d(nOut, eps=1e-03)
  19. self.act = nn.PReLU(nOut)
  20. def forward(self, input):
  21. """
  22. args:
  23. input: input feature map
  24. return: transformed feature map
  25. """
  26. output = self.conv(input)
  27. output = self.bn(output)
  28. output = self.act(output)
  29. return output
  30. class BNPReLU(nn.Module):
  31. def __init__(self, nOut):
  32. """
  33. args:
  34. nOut: channels of output feature maps
  35. """
  36. super().__init__()
  37. self.bn = nn.BatchNorm2d(nOut, eps=1e-03)
  38. self.act = nn.PReLU(nOut)
  39. def forward(self, input):
  40. """
  41. args:
  42. input: input feature map
  43. return: normalized and thresholded feature map
  44. """
  45. output = self.bn(input)
  46. output = self.act(output)
  47. return output
  48. class ConvBN(nn.Module):
  49. def __init__(self, nIn, nOut, kSize, stride=1):
  50. """
  51. args:
  52. nIn: number of input channels
  53. nOut: number of output channels
  54. kSize: kernel size
  55. stride: optinal stide for down-sampling
  56. """
  57. super().__init__()
  58. if isinstance(kSize, tuple):
  59. kSize = kSize[0]
  60. padding = int((kSize - 1) / 2)
  61. self.conv = nn.Conv2d(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias=False)
  62. self.bn = nn.BatchNorm2d(nOut, eps=1e-03)
  63. def forward(self, input):
  64. """
  65. args:
  66. input: input feature map
  67. return: transformed feature map
  68. """
  69. output = self.conv(input)
  70. output = self.bn(output)
  71. return output
  72. class Conv(nn.Module):
  73. def __init__(self, nIn, nOut, kSize, stride=1):
  74. """
  75. args:
  76. nIn: number of input channels
  77. nOut: number of output channels
  78. kSize: kernel size
  79. stride: optional stride rate for down-sampling
  80. """
  81. super().__init__()
  82. if isinstance(kSize, tuple):
  83. kSize = kSize[0]
  84. padding = int((kSize - 1) / 2)
  85. self.conv = nn.Conv2d(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias=False)
  86. def forward(self, input):
  87. """
  88. args:
  89. input: input feature map
  90. return: transformed feature map
  91. """
  92. output = self.conv(input)
  93. return output
  94. class ChannelWiseConv(nn.Module):
  95. def __init__(self, nIn, nOut, kSize, stride=1):
  96. """
  97. Args:
  98. nIn: number of input channels
  99. nOut: number of output channels, default (nIn == nOut)
  100. kSize: kernel size
  101. stride: optional stride rate for down-sampling
  102. """
  103. super().__init__()
  104. if isinstance(kSize, tuple):
  105. kSize = kSize[0]
  106. padding = int((kSize - 1) / 2)
  107. self.conv = nn.Conv2d(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), groups=nIn,
  108. bias=False)
  109. def forward(self, input):
  110. """
  111. args:
  112. input: input feature map
  113. return: transformed feature map
  114. """
  115. output = self.conv(input)
  116. return output
  117. class DilatedConv(nn.Module):
  118. def __init__(self, nIn, nOut, kSize, stride=1, d=1):
  119. """
  120. args:
  121. nIn: number of input channels
  122. nOut: number of output channels
  123. kSize: kernel size
  124. stride: optional stride rate for down-sampling
  125. d: dilation rate
  126. """
  127. super().__init__()
  128. if isinstance(kSize, tuple):
  129. kSize = kSize[0]
  130. padding = int((kSize - 1) / 2) * d
  131. self.conv = nn.Conv2d(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), bias=False,
  132. dilation=d)
  133. def forward(self, input):
  134. """
  135. args:
  136. input: input feature map
  137. return: transformed feature map
  138. """
  139. output = self.conv(input)
  140. return output
  141. class ChannelWiseDilatedConv(nn.Module):
  142. def __init__(self, nIn, nOut, kSize, stride=1, d=1):
  143. """
  144. args:
  145. nIn: number of input channels
  146. nOut: number of output channels, default (nIn == nOut)
  147. kSize: kernel size
  148. stride: optional stride rate for down-sampling
  149. d: dilation rate
  150. """
  151. super().__init__()
  152. if isinstance(kSize, tuple):
  153. kSize = kSize[0]
  154. padding = int((kSize - 1) / 2) * d
  155. self.conv = nn.Conv2d(nIn, nOut, (kSize, kSize), stride=stride, padding=(padding, padding), groups=nIn,
  156. bias=False, dilation=d)
  157. def forward(self, input):
  158. """
  159. args:
  160. input: input feature map
  161. return: transformed feature map
  162. """
  163. output = self.conv(input)
  164. return output
  165. class FGlo(nn.Module):
  166. """
  167. the FGlo class is employed to refine the joint feature of both local feature and surrounding context.
  168. """
  169. def __init__(self, channel, reduction=16):
  170. super(FGlo, self).__init__()
  171. self.avg_pool = nn.AdaptiveAvgPool2d(1)
  172. self.fc = nn.Sequential(
  173. nn.Linear(channel, channel // reduction),
  174. nn.ReLU(inplace=True),
  175. nn.Linear(channel // reduction, channel),
  176. nn.Sigmoid()
  177. )
  178. def forward(self, x):
  179. b, c, _, _ = x.size()
  180. y = self.avg_pool(x).view(b, c)
  181. y = self.fc(y).view(b, c, 1, 1)
  182. return x * y
  183. class ContextGuidedBlock_Down(nn.Module):
  184. """
  185. the size of feature map divided 2, (H,W,C)---->(H/2, W/2, 2C)
  186. """
  187. def __init__(self, nIn, nOut, dilation_rate=2, reduction=16):
  188. """
  189. args:
  190. nIn: the channel of input feature map
  191. nOut: the channel of output feature map, and nOut=2*nIn
  192. """
  193. super().__init__()
  194. self.conv1x1 = ConvBNPReLU(nIn, nOut, 3, 2) # size/2, channel: nIn--->nOut
  195. self.F_loc = ChannelWiseConv(nOut, nOut, 3, 1)
  196. self.F_sur = ChannelWiseDilatedConv(nOut, nOut, 3, 1, dilation_rate)
  197. self.bn = nn.BatchNorm2d(2 * nOut, eps=1e-3)
  198. self.act = nn.PReLU(2 * nOut)
  199. self.reduce = Conv(2 * nOut, nOut, 1, 1) # reduce dimension: 2*nOut--->nOut
  200. self.F_glo = FGlo(nOut, reduction)
  201. def forward(self, input):
  202. output = self.conv1x1(input)
  203. loc = self.F_loc(output)
  204. sur = self.F_sur(output)
  205. joi_feat = torch.cat([loc, sur], 1) # the joint feature
  206. joi_feat = self.bn(joi_feat)
  207. joi_feat = self.act(joi_feat)
  208. joi_feat = self.reduce(joi_feat) # channel= nOut
  209. output = self.F_glo(joi_feat) # F_glo is employed to refine the joint feature
  210. return output
  211. class ContextGuidedBlock(nn.Module):
  212. def __init__(self, nIn, nOut, dilation_rate=2, reduction=16, add=True):
  213. """
  214. args:
  215. nIn: number of input channels
  216. nOut: number of output channels,
  217. add: if true, residual learning
  218. """
  219. super().__init__()
  220. n = int(nOut / 2)
  221. self.conv1x1 = ConvBNPReLU(nIn, n, 1, 1) # 1x1 Conv is employed to reduce the computation
  222. self.F_loc = ChannelWiseConv(n, n, 3, 1) # local feature
  223. self.F_sur = ChannelWiseDilatedConv(n, n, 3, 1, dilation_rate) # surrounding context
  224. self.bn_prelu = BNPReLU(nOut)
  225. self.add = add
  226. self.F_glo = FGlo(nOut, reduction)
  227. def forward(self, input):
  228. output = self.conv1x1(input)
  229. loc = self.F_loc(output)
  230. sur = self.F_sur(output)
  231. joi_feat = torch.cat([loc, sur], 1)
  232. joi_feat = self.bn_prelu(joi_feat)
  233. output = self.F_glo(joi_feat) # F_glo is employed to refine the joint feature
  234. # if residual version
  235. if self.add:
  236. output = input + output
  237. return output
  238. def autopad(k, p=None, d=1): # kernel, padding, dilation
  239. """Pad to 'same' shape outputs."""
  240. if d > 1:
  241. k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k] # actual kernel-size
  242. if p is None:
  243. p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
  244. return p
  245. class Conv(nn.Module):
  246. """Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)."""
  247. default_act = nn.SiLU() # default activation
  248. def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
  249. """Initialize Conv layer with given arguments including activation."""
  250. super().__init__()
  251. self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
  252. self.bn = nn.BatchNorm2d(c2)
  253. self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
  254. def forward(self, x):
  255. """Apply convolution, batch normalization and activation to input tensor."""
  256. return self.act(self.bn(self.conv(x)))
  257. def forward_fuse(self, x):
  258. """Perform transposed convolution of 2D data."""
  259. return self.act(self.conv(x))
  260. class Bottleneck(nn.Module):
  261. """Standard bottleneck."""
  262. def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5):
  263. """Initializes a standard bottleneck module with optional shortcut connection and configurable parameters."""
  264. super().__init__()
  265. c_ = int(c2 * e) # hidden channels
  266. self.cv1 = Conv(c1, c_, k[0], 1)
  267. self.cv2 = Conv(c_, c2, k[1], 1, g=g)
  268. self.add = shortcut and c1 == c2
  269. def forward(self, x):
  270. """Applies the YOLO FPN to input data."""
  271. return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
  272. class C2f(nn.Module):
  273. """Faster Implementation of CSP Bottleneck with 2 convolutions."""
  274. def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5):
  275. """Initializes a CSP bottleneck with 2 convolutions and n Bottleneck blocks for faster processing."""
  276. super().__init__()
  277. self.c = int(c2 * e) # hidden channels
  278. self.cv1 = Conv(c1, 2 * self.c, 1, 1)
  279. self.cv2 = Conv((2 + n) * self.c, c2, 1) # optional act=FReLU(c2)
  280. self.m = nn.ModuleList(Bottleneck(self.c, self.c, shortcut, g, k=((3, 3), (3, 3)), e=1.0) for _ in range(n))
  281. def forward(self, x):
  282. """Forward pass through C2f layer."""
  283. y = list(self.cv1(x).chunk(2, 1))
  284. y.extend(m(y[-1]) for m in self.m)
  285. return self.cv2(torch.cat(y, 1))
  286. def forward_split(self, x):
  287. """Forward pass using split() instead of chunk()."""
  288. y = list(self.cv1(x).split((self.c, self.c), 1))
  289. y.extend(m(y[-1]) for m in self.m)
  290. return self.cv2(torch.cat(y, 1))
  291. class C3(nn.Module):
  292. """CSP Bottleneck with 3 convolutions."""
  293. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
  294. """Initialize the CSP Bottleneck with given channels, number, shortcut, groups, and expansion values."""
  295. super().__init__()
  296. c_ = int(c2 * e) # hidden channels
  297. self.cv1 = Conv(c1, c_, 1, 1)
  298. self.cv2 = Conv(c1, c_, 1, 1)
  299. self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
  300. self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, k=((1, 1), (3, 3)), e=1.0) for _ in range(n)))
  301. def forward(self, x):
  302. """Forward pass through the CSP bottleneck with 2 convolutions."""
  303. return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
  304. class C3k(C3):
  305. """C3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks."""
  306. def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, k=3):
  307. """Initializes the C3k module with specified channels, number of layers, and configurations."""
  308. super().__init__(c1, c2, n, shortcut, g, e)
  309. c_ = int(c2 * e) # hidden channels
  310. # self.m = nn.Sequential(*(RepBottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))
  311. self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))
  312. class C3k2_ContextGuidedBlock(C2f):
  313. """Faster Implementation of CSP Bottleneck with 2 convolutions."""
  314. def __init__(self, c1, c2, n=1, c3k=False, e=0.5, g=1, shortcut=True):
  315. """Initializes the C3k2 module, a faster CSP Bottleneck with 2 convolutions and optional C3k blocks."""
  316. super().__init__(c1, c2, n, shortcut, g, e)
  317. self.m = nn.ModuleList(
  318. C3k(self.c, self.c, 2, shortcut, g) if c3k else ContextGuidedBlock(self.c, self.c) for _ in range(n)
  319. )
  320. # 解析 c3k在主干和网络最后一个C3k2的时候设置True走的是C3k, 否则我们走的是MSBlock
  321. if __name__ == "__main__":
  322. # Generating Sample image
  323. image_size = (1, 64, 240, 240)
  324. image = torch.rand(*image_size)
  325. # Model
  326. mobilenet_v1 = C3k2_ContextGuidedBlock(64, 64)
  327. out = mobilenet_v1(image)
  328. print(out.size())


四、 手把手教你添加ContextGuided(注意看此处)

4.1 修改一

我们复制下面的代码' ultralytics /nn'到该目录下,创建一个py文件粘贴进去,我这里起名为ContextGuide,如下图所示->


4.2 修改二

第二步我们在该目录下创建一个新的py文件名字为'__init__.py'( 用群内的文件的话已经有了无需新建) ,然后在其内部导入我们的检测头如下图所示。


4.3 修改三

第三步我门中到如下文件'ultralytics/nn/tasks.py'进行导入和注册我们的模块( 用群内的文件的话已经有了无需重新导入直接开始第四步即可)

从今天开始以后的教程就都统一成这个样子了,因为我默认大家用了我群内的文件来进行修改!!


4.4 修改四

按照我的添加在parse_model里添加即可。


到此就修改完成了,大家可以复制下面的yaml文件运行。


五、ContextGuided的yaml文件

5.1 yaml文件1

此版本参数量为:YOLO11-ContextGuided summary: 428 layers, 2,842,053 parameters, 2,842,037 gradients, 7.1 GFLOPs

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLO11 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=yolo11n.yaml' will call yolo11.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
  8. s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
  9. m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
  10. l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
  11. x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
  12. # YOLO11n backbone
  13. backbone:
  14. # [from, repeats, module, args]
  15. - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
  16. - [-1, 1, ContextGuidedBlock_Down, [128]] # 1-P2/4
  17. - [-1, 2, C3k2, [256, False, 0.25]]
  18. - [-1, 1, ContextGuidedBlock_Down, [256]] # 3-P3/8
  19. - [-1, 2, C3k2, [512, False, 0.25]]
  20. - [-1, 1, ContextGuidedBlock_Down, [512]] # 5-P4/16
  21. - [-1, 2, C3k2, [512, True]]
  22. - [-1, 1, ContextGuidedBlock_Down, [1024]] # 7-P5/32
  23. - [-1, 2, C3k2, [1024, True]]
  24. - [-1, 1, SPPF, [1024, 5]] # 9
  25. - [-1, 2, C2PSA, [1024]] # 10
  26. # YOLO11n head
  27. head:
  28. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  29. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  30. - [-1, 2, C3k2, [512, False]] # 13
  31. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  32. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
  33. - [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)
  34. - [-1, 1, ContextGuidedBlock_Down, [256]]
  35. - [[-1, 13], 1, Concat, [1]] # cat head P4
  36. - [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)
  37. - [-1, 1, ContextGuidedBlock_Down, [512]]
  38. - [[-1, 10], 1, Concat, [1]] # cat head P5
  39. - [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)
  40. - [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)


5.2 yaml文件2

此版本参数量为: YOLO11-C3k2-ContextGuidedBlock summary: 380 layers, 2,509,216 parameters, 2,509,200 gradients, 5.9 GFLOPs

# 解析 c3k在主干和网络最后一个C3k2的时候设置True走的是C3k, 否则我们走的是ContextGuidedBlock

  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. # YOLO11 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=yolo11n.yaml' will call yolo11.yaml with scale 'n'
  6. # [depth, width, max_channels]
  7. n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
  8. s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
  9. m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
  10. l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
  11. x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
  12. # YOLO11n 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, 2, C3k2_ContextGuidedBlock, [256, False, 0.25]]
  18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  19. - [-1, 2, C3k2_ContextGuidedBlock, [512, False, 0.25]]
  20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  21. - [-1, 2, C3k2_ContextGuidedBlock, [512, True]]
  22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
  23. - [-1, 2, C3k2_ContextGuidedBlock, [1024, True]]
  24. - [-1, 1, SPPF, [1024, 5]] # 9
  25. - [-1, 2, C2PSA, [1024]] # 10
  26. # YOLO11n head
  27. head:
  28. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  29. - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  30. - [-1, 2, C3k2_ContextGuidedBlock, [512, False]] # 13
  31. - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  32. - [[-1, 4], 1, Concat, [1]] # cat backbone P3
  33. - [-1, 2, C3k2_ContextGuidedBlock, [256, False]] # 16 (P3/8-small)
  34. - [-1, 1, Conv, [256, 3, 2]]
  35. - [[-1, 13], 1, Concat, [1]] # cat head P4
  36. - [-1, 2, C3k2_ContextGuidedBlock, [512, False]] # 19 (P4/16-medium)
  37. - [-1, 1, Conv, [512, 3, 2]]
  38. - [[-1, 10], 1, Concat, [1]] # cat head P5
  39. - [-1, 2, C3k2_ContextGuidedBlock, [1024, True]] # 22 (P5/32-large)
  40. - [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)


六、成功运行记录

​​


七、全文总结

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

​​