学习资源站

YOLOv11改进-添加注意力篇-结合2024最新Mamba注意力机制MLLA助力yolov11有效涨点含二次创新C2PSA(全网独家首发改进)

一、本文介绍

本文给大家带来的改进机制是结合号称超越 Transformer架构的Mamba架构的最新注意力机制MLLA ,本文将其和我们YOLOv11进行结合, MLLA(Mamba-Like Linear Attention) 的原理是通过将 Mamba 模型 的一些核心设计融入线性 注意力机制 ,从而提升模型的性能。具体来说,MLLA主要整合了Mamba中的“忘记门”(forget gate)和模块设计(block design)这两个关键因素,同时MLLA通过使用位置编码(RoPE)来替代忘记门,从而在保持并行计算和快速推理速度的同时,提供必要的位置信息。这使得MLLA在处理非自回归的视觉任务时更加有效 , 本文内容为我独家整理全网首发。



二、原理介绍

官方论文地址: 官方论文地址点击此处即可跳转

官方代码地址: 官方代码地址点击此处即可跳转


在这篇论文中, MLLA(Mamba-Like Linear Attention) 的原理是通过将Mamba模型的一些核心设计融入线性注意力机制,从而提升模型的 性能 。具体来说,MLLA主要整合了Mamba中的“忘记门”(forget gate )和模块设计(block design)这两个关键因素,这些因素被认为是Mamba成功的主要原因。

以下是对MLLA原理的详细分析:

  1. 忘记门(Forget Gate)

    • 忘记门提供了局部偏差和位置信息。所有的忘记门元素严格限制在0到1之间,这意味着模型在接收到当前输入后会持续衰减先前的隐藏状态。这种特性确保了模型对输入序列的顺序敏感。
    • 忘记门的局部偏差和位置信息对于图像处理任务来说非常重要,尽管引入忘记门会导致计算需要采用递归的形式,从而降低并行计算的效率 。
  2. 模块设计(Block Design)

    • Mamba的模块设计在保持相似的浮点运算次数(FLOPs)的同时,通过替换注意力子模块为线性注意力来提升性能。结果表明,采用这种模块设计能够显著提高模型的表现 。
  3. 线性注意力的改进

    • 线性注意力被重新设计以整合忘记门和模块设计,这种改进后的模型被称为MLLA。实验结果显示,MLLA在图像分类和高分辨率密集预测任务中均优于各种视觉Mamba模型 。
  4. 并行计算和快速推理速度

    • MLLA通过使用位置编码(RoPE)来替代忘记门,从而在保持并行计算和快速推理速度的同时,提供必要的位置信息。这使得MLLA在处理非自回归的视觉任务时更加有效 。

通过这些改进,MLLA不仅继承了Mamba模型的优点,还解决了其在并行计算中的一些局限性,使其更适合于视觉任务。MLLA展示了通过合理设计,线性注意力机制也能够超越传统的高性能模型。


三、核心代码

其中包含了上面提到的Rope,但是这个模块是经过我重新设计的,因为原先的代码需要输入图片的宽和高再定义时,但是经过重新设计后改为实时计算,有兴趣的可以和开源代码对比下!

  1. import torch
  2. import torch.nn as nn
  3. __all__ = ['MLLAttention', 'C2PSAMLLA']
  4. class Mlp(nn.Module):
  5. def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
  6. super().__init__()
  7. out_features = out_features or in_features
  8. hidden_features = hidden_features or in_features
  9. self.fc1 = nn.Linear(in_features, hidden_features)
  10. self.act = act_layer()
  11. self.fc2 = nn.Linear(hidden_features, out_features)
  12. self.drop = nn.Dropout(drop)
  13. def forward(self, x):
  14. x = self.fc1(x)
  15. x = self.act(x)
  16. x = self.drop(x)
  17. x = self.fc2(x)
  18. x = self.drop(x)
  19. return x
  20. class ConvLayer(nn.Module):
  21. def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=0, dilation=1, groups=1,
  22. bias=True, dropout=0, norm=nn.BatchNorm2d, act_func=nn.ReLU):
  23. super(ConvLayer, self).__init__()
  24. self.dropout = nn.Dropout2d(dropout, inplace=False) if dropout > 0 else None
  25. self.conv = nn.Conv2d(
  26. in_channels,
  27. out_channels,
  28. kernel_size=(kernel_size, kernel_size),
  29. stride=(stride, stride),
  30. padding=(padding, padding),
  31. dilation=(dilation, dilation),
  32. groups=groups,
  33. bias=bias,
  34. )
  35. self.norm = norm(num_features=out_channels) if norm else None
  36. self.act = act_func() if act_func else None
  37. def forward(self, x: torch.Tensor) -> torch.Tensor:
  38. if self.dropout is not None:
  39. x = self.dropout(x)
  40. x = self.conv(x)
  41. if self.norm:
  42. x = self.norm(x)
  43. if self.act:
  44. x = self.act(x)
  45. return x
  46. class RoPE(torch.nn.Module):
  47. r"""Rotary Positional Embedding.
  48. """
  49. def __init__(self, base=10000):
  50. super(RoPE, self).__init__()
  51. self.base = base
  52. def generate_rotations(self, x):
  53. # 获取输入张量的形状
  54. *channel_dims, feature_dim = x.shape[1:-1][0], x.shape[-1]
  55. k_max = feature_dim // (2 * len(channel_dims))
  56. assert feature_dim % k_max == 0, "Feature dimension must be divisible by 2 * k_max"
  57. # 生成角度
  58. theta_ks = 1 / (self.base ** (torch.arange(k_max, dtype=x.dtype, device=x.device) / k_max))
  59. angles = torch.cat([t.unsqueeze(-1) * theta_ks for t in
  60. torch.meshgrid([torch.arange(d, dtype=x.dtype, device=x.device) for d in channel_dims],
  61. indexing='ij')], dim=-1)
  62. # 计算旋转矩阵的实部和虚部
  63. rotations_re = torch.cos(angles).unsqueeze(dim=-1)
  64. rotations_im = torch.sin(angles).unsqueeze(dim=-1)
  65. rotations = torch.cat([rotations_re, rotations_im], dim=-1)
  66. return rotations
  67. def forward(self, x):
  68. # 生成旋转矩阵
  69. rotations = self.generate_rotations(x)
  70. # 将 x 转换为复数形式
  71. x_complex = torch.view_as_complex(x.reshape(*x.shape[:-1], -1, 2))
  72. # 应用旋转矩阵
  73. pe_x = torch.view_as_complex(rotations) * x_complex
  74. # 将结果转换回实数形式并展平最后两个维度
  75. return torch.view_as_real(pe_x).flatten(-2)
  76. class MLLAttention(nn.Module):
  77. r""" Linear Attention with LePE and RoPE.
  78. Args:
  79. dim (int): Number of input channels.
  80. num_heads (int): Number of attention heads.
  81. qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
  82. """
  83. def __init__(self, dim=3, input_resolution=[160, 160], num_heads=4, qkv_bias=True, **kwargs):
  84. super().__init__()
  85. self.dim = dim
  86. self.input_resolution = input_resolution
  87. self.num_heads = num_heads
  88. self.qk = nn.Linear(dim, dim * 2, bias=qkv_bias)
  89. self.elu = nn.ELU()
  90. self.lepe = nn.Conv2d(dim, dim, 3, padding=1, groups=dim)
  91. self.rope = RoPE()
  92. def forward(self, x):
  93. """
  94. Args:
  95. x: input features with shape of (B, N, C)
  96. """
  97. x = x.reshape((x.size(0), x.size(2) * x.size(3), x.size(1)))
  98. b, n, c = x.shape
  99. h = int(n ** 0.5)
  100. w = int(n ** 0.5)
  101. # self.rope = RoPE(shape=(h, w, self.dim))
  102. num_heads = self.num_heads
  103. head_dim = c // num_heads
  104. qk = self.qk(x).reshape(b, n, 2, c).permute(2, 0, 1, 3)
  105. q, k, v = qk[0], qk[1], x
  106. # q, k, v: b, n, c
  107. q = self.elu(q) + 1.0
  108. k = self.elu(k) + 1.0
  109. q_rope = self.rope(q.reshape(b, h, w, c)).reshape(b, n, num_heads, head_dim).permute(0, 2, 1, 3)
  110. k_rope = self.rope(k.reshape(b, h, w, c)).reshape(b, n, num_heads, head_dim).permute(0, 2, 1, 3)
  111. q = q.reshape(b, n, num_heads, head_dim).permute(0, 2, 1, 3)
  112. k = k.reshape(b, n, num_heads, head_dim).permute(0, 2, 1, 3)
  113. v = v.reshape(b, n, num_heads, head_dim).permute(0, 2, 1, 3)
  114. z = 1 / (q @ k.mean(dim=-2, keepdim=True).transpose(-2, -1) + 1e-6)
  115. kv = (k_rope.transpose(-2, -1) * (n ** -0.5)) @ (v * (n ** -0.5))
  116. x = q_rope @ kv * z
  117. x = x.transpose(1, 2).reshape(b, n, c)
  118. v = v.transpose(1, 2).reshape(b, h, w, c).permute(0, 3, 1, 2)
  119. x = x + self.lepe(v).permute(0, 2, 3, 1).reshape(b, n, c)
  120. x = x.transpose(2, 1).reshape((b, c, h, w))
  121. return x
  122. def extra_repr(self) -> str:
  123. return f'dim={self.dim}, num_heads={self.num_heads}'
  124. def autopad(k, p=None, d=1): # kernel, padding, dilation
  125. """Pad to 'same' shape outputs."""
  126. if d > 1:
  127. k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k] # actual kernel-size
  128. if p is None:
  129. p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
  130. return p
  131. class Conv(nn.Module):
  132. """Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)."""
  133. default_act = nn.SiLU() # default activation
  134. def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
  135. """Initialize Conv layer with given arguments including activation."""
  136. super().__init__()
  137. self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
  138. self.bn = nn.BatchNorm2d(c2)
  139. self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
  140. def forward(self, x):
  141. """Apply convolution, batch normalization and activation to input tensor."""
  142. return self.act(self.bn(self.conv(x)))
  143. def forward_fuse(self, x):
  144. """Perform transposed convolution of 2D data."""
  145. return self.act(self.conv(x))
  146. class PSABlock(nn.Module):
  147. """
  148. PSABlock class implementing a Position-Sensitive Attention block for neural networks.
  149. This class encapsulates the functionality for applying multi-head attention and feed-forward neural network layers
  150. with optional shortcut connections.
  151. Attributes:
  152. attn (Attention): Multi-head attention module.
  153. ffn (nn.Sequential): Feed-forward neural network module.
  154. add (bool): Flag indicating whether to add shortcut connections.
  155. Methods:
  156. forward: Performs a forward pass through the PSABlock, applying attention and feed-forward layers.
  157. Examples:
  158. Create a PSABlock and perform a forward pass
  159. """
  160. def __init__(self, c, attn_ratio=0.5, num_heads=4, shortcut=True) -> None:
  161. """Initializes the PSABlock with attention and feed-forward layers for enhanced feature extraction."""
  162. super().__init__()
  163. self.attn = MLLAttention(c)
  164. self.ffn = nn.Sequential(Conv(c, c * 2, 1), Conv(c * 2, c, 1, act=False))
  165. self.add = shortcut
  166. def forward(self, x):
  167. """Executes a forward pass through PSABlock, applying attention and feed-forward layers to the input tensor."""
  168. x = x + self.attn(x) if self.add else self.attn(x)
  169. x = x + self.ffn(x) if self.add else self.ffn(x)
  170. return x
  171. class C2PSAMLLA(nn.Module):
  172. """
  173. C2PSA module with attention mechanism for enhanced feature extraction and processing.
  174. This module implements a convolutional block with attention mechanisms to enhance feature extraction and processing
  175. capabilities. It includes a series of PSABlock modules for self-attention and feed-forward operations.
  176. Attributes:
  177. c (int): Number of hidden channels.
  178. cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
  179. cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c.
  180. m (nn.Sequential): Sequential container of PSABlock modules for attention and feed-forward operations.
  181. Methods:
  182. forward: Performs a forward pass through the C2PSA module, applying attention and feed-forward operations.
  183. Notes:
  184. This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.
  185. """
  186. def __init__(self, c1, c2, n=1, e=0.5):
  187. """Initializes the C2PSA module with specified input/output channels, number of layers, and expansion ratio."""
  188. super().__init__()
  189. assert c1 == c2
  190. self.c = int(c1 * e)
  191. self.cv1 = Conv(c1, 2 * self.c, 1, 1)
  192. self.cv2 = Conv(2 * self.c, c1, 1)
  193. self.m = nn.Sequential(*(PSABlock(self.c, attn_ratio=0.5, num_heads=self.c // 64) for _ in range(n)))
  194. def forward(self, x):
  195. """Processes the input tensor 'x' through a series of PSA blocks and returns the transformed tensor."""
  196. a, b = self.cv1(x).split((self.c, self.c), dim=1)
  197. b = self.m(b)
  198. return self.cv2(torch.cat((a, b), 1))


四、手把手教你添加MLLA

4.1 修改一

第一还是建立文件,我们找到如下 ultralytics /nn文件夹下建立一个目录名字呢就是'Addmodules'文件夹 (用群内的文件的话已经有了无需新建) !然后在其内部建立一个新的py文件将核心代码复制粘贴进去即可。


4.2 修改二

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


4.3 修改三

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

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

​​


4.4 修改四

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


4.5 修改五

找到ultralytics/models/yolo/detect/train.py的DetectionTrainer class中的build_dataset函数中的rect=mode == 'val'改为rect=False

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


五、MLLA的yaml文件和运行记录

5.1 MLLA的yaml文件1

此版本训练信息:YOLO11-C2PSA-MLLA summary: 312 layers, 2,577,691 parameters, 2,577,675 gradients, 6.5 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, Conv, [128, 3, 2]] # 1-P2/4
  17. - [-1, 2, C3k2, [256, False, 0.25]]
  18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  19. - [-1, 2, C3k2, [512, False, 0.25]]
  20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  21. - [-1, 2, C3k2, [512, True]]
  22. - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
  23. - [-1, 2, C3k2, [1024, True]]
  24. - [-1, 1, SPPF, [1024, 5]] # 9
  25. - [-1, 2, C2PSAMLLA, [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, Conv, [256, 3, 2]]
  35. - [[-1, 13], 1, Concat, [1]] # cat head P4
  36. - [-1, 2, C3k2, [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, [1024, True]] # 22 (P5/32-large)
  40. - [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)

5.2 MLLA的yaml文件2

此版本训练信息:YOLO11-MLLA summary: 334 layers, 2,772,123 parameters, 2,772,107 gradients, 6.8 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, Conv, [128, 3, 2]] # 1-P2/4
  17. - [-1, 2, C3k2, [256, False, 0.25]]
  18. - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  19. - [-1, 2, C3k2, [512, False, 0.25]]
  20. - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  21. - [-1, 2, C3k2, [512, True]]
  22. - [-1, 1, Conv, [1024, 3, 2]] # 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, MLLAttention, []] # 17 (P3/8-small) 小目标检测层输出位置增加注意力机制
  35. - [-1, 1, Conv, [256, 3, 2]]
  36. - [[-1, 13], 1, Concat, [1]] # cat head P4
  37. - [-1, 2, C3k2, [512, False]] # 20 (P4/16-medium)
  38. - [-1, 1, MLLAttention, []] # 21 (P4/16-medium) 中目标检测层输出位置增加注意力机制
  39. - [-1, 1, Conv, [512, 3, 2]]
  40. - [[-1, 10], 1, Concat, [1]] # cat head P5
  41. - [-1, 2, C3k2, [1024, True]] # 24 (P5/32-large)
  42. - [-1, 1, MLLAttention, []] # 25 (P5/32-large) 大目标检测层输出位置增加注意力机制
  43. # 注意力机制我这里其实是添加了三个但是实际一般生效就只添加一个就可以了,所以大家可以自行注释来尝试, 上面三个仅建议大家保留一个, 但是from位置要对齐.
  44. # 具体在那一层用注意力机制可以根据自己的数据集场景进行选择。
  45. # 如果你自己配置注意力位置注意from[17, 21, 25]位置要对应上对应的检测层!
  46. - [[17, 21, 25], 1, Detect, [nc]] # Detect(P3, P4, P5)


5.3 训练代码

大家可以创建一个py文件将我给的代码复制粘贴进去,配置好自己的文件路径即可运行。

  1. import warnings
  2. warnings.filterwarnings('ignore')
  3. from ultralytics import YOLO
  4. if __name__ == '__main__':
  5. model = YOLO('yolov8-MLLA.yaml')
  6. # 如何切换模型版本, 上面的ymal文件可以改为 yolov8s.yaml就是使用的v8s,
  7. # 类似某个改进的yaml文件名称为yolov8-XXX.yaml那么如果想使用其它版本就把上面的名称改为yolov8l-XXX.yaml即可(改的是上面YOLO中间的名字不是配置文件的)!
  8. # model.load('yolov8n.pt') # 是否加载预训练权重,科研不建议大家加载否则很难提升精度
  9. model.train(data=r"C:\Users\Administrator\PycharmProjects\yolov5-master\yolov5-master\Construction Site Safety.v30-raw-images_latestversion.yolov8\data.yaml",
  10. # 如果大家任务是其它的'ultralytics/cfg/default.yaml'找到这里修改task可以改成detect, segment, classify, pose
  11. cache=False,
  12. imgsz=640,
  13. epochs=150,
  14. single_cls=False, # 是否是单类别检测
  15. batch=16,
  16. close_mosaic=0,
  17. workers=0,
  18. device='0',
  19. optimizer='SGD', # using SGD
  20. # resume='runs/train/exp21/weights/last.pt', # 如过想续训就设置last.pt的地址
  21. amp=True, # 如果出现训练损失为Nan可以关闭amp
  22. project='runs/train',
  23. name='exp',
  24. )


5.4 MLLA的训练过程截图


五、本文总结

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