一、本文介绍
本文记录的是基于SCSA-CBAM注意力模块的YOLOv10目标检测改进方法研究。现有注意力方法在空间-通道协同方面未充分挖掘其潜力,缺乏对多语义信息的充分利用来引导特征和缓解语义差异。SCSA-CBAM注意力模块
构建一个空间-通道协同机制,使空间注意力引导通道注意力增强综合学习,通道注意力从多语义水平调节更丰富的空间特定模式。
文章目录
- 一、本文介绍
- 二、SCSA原理
- 2.1 原理
- 2.2 优势
- 三、SCSA的实现代码
- 四、创新模块
- 4.1 改进点1
- 4.2 改进点2⭐
- 五、添加步骤
- 5.1 修改ultralytics/nn/modules/block.py
- 5.2 修改ultralytics/nn/modules/__init__.py
- 5.3 修改ultralytics/nn/modules/tasks.py
- 六、yaml模型文件
- 6.1 模型改进版本一
- 6.2 模型改进版本二⭐
- 七、成功运行结果
二、SCSA原理
SCSA:空间注意与通道注意的协同效应研究
SCSA(Spatial and Channel Synergistic Attention)
是一种新颖的、即插即用的空间和通道协同注意力机制,其设计的原理和优势如下:
2.1 原理
- Shared Multi - Semantic Spatial Attention(SMSA):
- 空间和通道分解:将输入X沿高度和宽度维度分解,应用全局平均池化创建两个单向1D序列结构,然后将特征集划分为K个独立的子特征,每个子特征具有C / K个通道,便于高效提取多语义空间信息。
- 轻量级卷积策略:在四个子特征中应用核大小为3、5、7和9的深度一维卷积,以捕获不同的语义空间结构,并使用共享卷积来对齐,解决分解特征和应用一维卷积导致的有限感受野问题。使用Group Normalization对不同语义子特征进行归一化,最后使用Sigmoid激活函数生成空间注意力。
- Progressive Channel - wise Self - Attention(PCSA):
- 受ViT利用MHSA建模空间注意力中不同token之间相似性的启发,结合SMSA调制的空间先验来计算通道间相似性。
- 采用渐进压缩方法来保留和利用SMSA提取的多语义空间信息,并减少MHSA的计算成本。
- 具体实现过程包括池化、映射生成查询、键和值,进行注意力计算等。
- 协同效应:通过简单的串行连接集成SMSA和PCSA模块,空间注意力从每个特征中提取多语义空间信息,为通道注意力计算提供精确的空间先验;通道注意力利用整体特征图X来细化局部子特征的语义理解,缓解SMSA中多尺度卷积引起的语义差异。同时,不采用通道压缩,防止关键特征丢失。
2.2 优势
- 高效的SMSA:利用多尺度深度共享1D卷积捕获每个特征通道的多语义空间信息,有效整合全局上下文依赖和多语义空间先验。
- PCSA缓解语义差异:使用SMSA计算引导的压缩空间知识来计算通道相似性和贡献,缓解空间结构中的语义差异。
- 协同效应:通过维度解耦、轻量级多语义引导和语义差异缓解来探索协同效应,在各种视觉任务和复杂场景中优于当前最先进的注意力机制。
- 实验验证优势:
- 在图像分类任务中,SCSA在不同规模的网络中实现了最高的Top - 1准确率,且参数和计算复杂度较低,基于ResNet的推理速度仅次于CA,在准确性、速度和模型复杂度之间实现了较好的平衡。
- 在目标检测任务中,在各种检测器、模型大小和对象尺度上优于其他先进的注意力方法,在复杂场景(如小目标、黑暗环境和红外场景)中进一步证明了其有效性和泛化能力。
- 在分割任务中,基于多语义空间信息,在像素级任务中表现出色,显著优于其他注意力方法。
- 可视化分析:SCSA在相似的感受野条件下能明显关注多个关键区域,最大限度地减少关键信息丢失,为最终的下游任务提供丰富的特征信息,其协同设计在空间和通道域注意力计算中保留了关键信息,具有更优越的表示能力。
- 其他分析:SCSA具有更大的有效感受野,有利于网络利用丰富的上下文信息进行集体决策,从而提升性能;在计算复杂度方面,当模型宽度适当时,SCSA可以以线性复杂度进行推理;在推理吞吐量评估中,虽然SCSA比纯通道注意力略慢,但优于大多数混合注意力机制,在模型复杂性、推理速度和准确性之间实现了优化平衡。
论文:https://arxiv.org/pdf/2407.05128
源码:https://github.com/HZAI-ZJNU/SCSA
三、SCSA的实现代码
SCSA模块
的实现代码如下:
import typing as t
from einops import rearrange
from mmengine.model import BaseModule
class SCSA(BaseModule):
def __init__(
self,
dim: int,
head_num: int,
window_size: int = 7,
group_kernel_sizes: t.List[int] = [3, 5, 7, 9],
qkv_bias: bool = False,
fuse_bn: bool = False,
norm_cfg: t.Dict = dict(type='BN'),
act_cfg: t.Dict = dict(type='ReLU'),
down_sample_mode: str = 'avg_pool',
attn_drop_ratio: float = 0.,
gate_layer: str = 'sigmoid',
):
super(SCSA, self).__init__()
self.dim = dim
self.head_num = head_num
self.head_dim = dim // head_num
self.scaler = self.head_dim ** -0.5
self.group_kernel_sizes = group_kernel_sizes
self.window_size = window_size
self.qkv_bias = qkv_bias
self.fuse_bn = fuse_bn
self.down_sample_mode = down_sample_mode
assert self.dim // 4, 'The dimension of input feature should be divisible by 4.'
self.group_chans = group_chans = self.dim // 4
self.local_dwc = nn.Conv1d(group_chans, group_chans, kernel_size=group_kernel_sizes[0],
padding=group_kernel_sizes[0] // 2, groups=group_chans)
self.global_dwc_s = nn.Conv1d(group_chans, group_chans, kernel_size=group_kernel_sizes[1],
padding=group_kernel_sizes[1] // 2, groups=group_chans)
self.global_dwc_m = nn.Conv1d(group_chans, group_chans, kernel_size=group_kernel_sizes[2],
padding=group_kernel_sizes[2] // 2, groups=group_chans)
self.global_dwc_l = nn.Conv1d(group_chans, group_chans, kernel_size=group_kernel_sizes[3],
padding=group_kernel_sizes[3] // 2, groups=group_chans)
self.sa_gate = nn.Softmax(dim=2) if gate_layer == 'softmax' else nn.Sigmoid()
self.norm_h = nn.GroupNorm(4, dim)
self.norm_w = nn.GroupNorm(4, dim)
self.conv_d = nn.Identity()
self.norm = nn.GroupNorm(1, dim)
self.q = nn.Conv2d(in_channels=dim, out_channels=dim, kernel_size=1, bias=qkv_bias, groups=dim)
self.k = nn.Conv2d(in_channels=dim, out_channels=dim, kernel_size=1, bias=qkv_bias, groups=dim)
self.v = nn.Conv2d(in_channels=dim, out_channels=dim, kernel_size=1, bias=qkv_bias, groups=dim)
self.attn_drop = nn.Dropout(attn_drop_ratio)
self.ca_gate = nn.Softmax(dim=1) if gate_layer == 'softmax' else nn.Sigmoid()
if window_size == -1:
self.down_func = nn.AdaptiveAvgPool2d((1, 1))
else:
if down_sample_mode == 'recombination':
self.down_func = self.space_to_chans
# dimensionality reduction
self.conv_d = nn.Conv2d(in_channels=dim * window_size ** 2, out_channels=dim, kernel_size=1, bias=False)
elif down_sample_mode == 'avg_pool':
self.down_func = nn.AvgPool2d(kernel_size=(window_size, window_size), stride=window_size)
elif down_sample_mode == 'max_pool':
self.down_func = nn.MaxPool2d(kernel_size=(window_size, window_size), stride=window_size)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
The dim of x is (B, C, H, W)
"""
# Spatial attention priority calculation
b, c, h_, w_ = x.size()
# (B, C, H)
x_h = x.mean(dim=3)
l_x_h, g_x_h_s, g_x_h_m, g_x_h_l = torch.split(x_h, self.group_chans, dim=1)
# (B, C, W)
x_w = x.mean(dim=2)
l_x_w, g_x_w_s, g_x_w_m, g_x_w_l = torch.split(x_w, self.group_chans, dim=1)
x_h_attn = self.sa_gate(self.norm_h(torch.cat((
self.local_dwc(l_x_h),
self.global_dwc_s(g_x_h_s),
self.global_dwc_m(g_x_h_m),
self.global_dwc_l(g_x_h_l),
), dim=1)))
x_h_attn = x_h_attn.view(b, c, h_, 1)
x_w_attn = self.sa_gate(self.norm_w(torch.cat((
self.local_dwc(l_x_w),
self.global_dwc_s(g_x_w_s),
self.global_dwc_m(g_x_w_m),
self.global_dwc_l(g_x_w_l)
), dim=1)))
x_w_attn = x_w_attn.view(b, c, 1, w_)
x = x * x_h_attn * x_w_attn
# Channel attention based on self attention
# reduce calculations
y = self.down_func(x)
y = self.conv_d(y)
_, _, h_, w_ = y.size()
# normalization first, then reshape -> (B, H, W, C) -> (B, C, H * W) and generate q, k and v
y = self.norm(y)
q = self.q(y)
k = self.k(y)
v = self.v(y)
# (B, C, H, W) -> (B, head_num, head_dim, N)
q = rearrange(q, 'b (head_num head_dim) h w -> b head_num head_dim (h w)', head_num=int(self.head_num),
head_dim=int(self.head_dim))
k = rearrange(k, 'b (head_num head_dim) h w -> b head_num head_dim (h w)', head_num=int(self.head_num),
head_dim=int(self.head_dim))
v = rearrange(v, 'b (head_num head_dim) h w -> b head_num head_dim (h w)', head_num=int(self.head_num),
head_dim=int(self.head_dim))
# (B, head_num, head_dim, head_dim)
attn = q @ k.transpose(-2, -1) * self.scaler
attn = self.attn_drop(attn.softmax(dim=-1))
# (B, head_num, head_dim, N)
attn = attn @ v
# (B, C, H_, W_)
attn = rearrange(attn, 'b head_num head_dim (h w) -> b (head_num head_dim) h w', h=int(h_), w=int(w_))
# (B, C, 1, 1)
attn = attn.mean((2, 3), keepdim=True)
attn = self.ca_gate(attn)
return attn * x
四、创新模块
4.1 改进点1
模块改进方法1️⃣:直接加入SCSA模块
。
SCSA模块
添加后如下:
注意❗:在5.2和5.3小节
中需要声明的模块名称为:SCSA
。
4.2 改进点2⭐
模块改进方法2️⃣:基于SCSA模块
的C2f
。
第二种改进方法是对YOLOv10
中的C2f模块
进行改进。SCSA
的协同设计能够在空间和通道域注意力计算中保留了关键信息,最大限度地减少关键信息丢失,使C2f模块
具有更优越的表示能力。
改进代码如下:
class C2f_SCSA(nn.Module):
"""Faster Implementation of CSP Bottleneck with 2 convolutions."""
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5):
"""Initialize CSP bottleneck layer with two convolutions with arguments ch_in, ch_out, number, shortcut, groups,
expansion.
"""
super().__init__()
self.c = int(c2 * e) # hidden channels
self.cv1 = Conv(c1, 2 * self.c, 1, 1)
self.cv2 = Conv((2 + n) * self.c, c2, 1) # optional act=FReLU(c2)
self.m = nn.ModuleList(Bottleneck(self.c, self.c, shortcut, g, k=((3, 3), (3, 3)), e=1.0) for _ in range(n))
self.att = SCSA(c2)
def forward(self, x):
"""Forward pass through C2f layer."""
y = list(self.cv1(x).chunk(2, 1))
y.extend(m(y[-1]) for m in self.m)
return self.att(self.cv2(torch.cat(y, 1)))
def forward_split(self, x):
"""Forward pass using split() instead of chunk()."""
y = list(self.cv1(x).split((self.c, self.c), 1))
y.extend(m(y[-1]) for m in self.m)
return self.att(self.cv2(torch.cat(y, 1)))
注意❗:在5.2和5.3小节
中需要声明的模块名称为:C2f_SCSA
。
五、添加步骤
5.1 修改ultralytics/nn/modules/block.py
此处需要修改的文件是ultralytics/nn/modules/block.py
block.py中定义了网络结构的通用模块
,我们想要加入新的模块就只需要将模块代码放到这个文件内即可。
将SCSA
和C2f_SCSA
模块代码添加到此文件下。
5.2 修改ultralytics/nn/modules/init.py
此处需要修改的文件是ultralytics/nn/modules/__init__.py
__init__.py
文件中定义了所有模块的初始化,我们只需要将block.py
中的新的模块命添加到对应的函数即可。
SCSA
和C2f_SCSA
在block.py
中实现,所有要添加在from .block import
:
from .block import (
C1,
C2,
...
SCSA,
C2f_SCSA
)
5.3 修改ultralytics/nn/modules/tasks.py
在tasks.py
文件中,需要在两处位置添加各模块类名称。
首先:在函数声明中引入SCSA
和C2f_SCSA
其次:在parse_model函数
中注册SCSA
和C2f_SCSA
模块
六、yaml模型文件
6.1 模型改进版本一
在代码配置完成后,配置模型的YAML文件。
此处以ultralytics/cfg/models/v10/yolov10m.yaml
为例,在同目录下创建一个用于自己数据集训练的模型文件yolov10m-SCSA.yaml
。
将yolov10m.yaml
中的内容复制到yolov10m-SCSA.yaml
文件下,修改nc
数量等于自己数据中目标的数量。
在骨干网络中添加SCSA模块
,只需要填入一个参数,通道数。
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# Parameters
nc: 1 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
# YOLOv8.0n backbone
backbone:
# [from, repeats, module, args]
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
- [-1, 3, C2f, [128, True]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 6, C2f, [256, True]]
- [-1, 1, SCDown, [512, 3, 2]] # 5-P4/16
- [-1, 6, C2f, [512, True]]
- [-1, 1, SCDown, [1024, 3, 2]] # 7-P5/32
- [-1, 3, C2fCIB, [1024, True]]
- [-1, 1, SCSA, [1024]]
- [-1, 1, SPPF, [1024, 5]] # 10
- [-1, 1, PSA, [1024]] # 11
# YOLOv8.0n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 3, C2f, [512]] # 14
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 3, C2f, [256]] # 17 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 14], 1, Concat, [1]] # cat head P4
- [-1, 3, C2fCIB, [512, True]] # 20 (P4/16-medium)
- [-1, 1, SCDown, [512, 3, 2]]
- [[-1, 11], 1, Concat, [1]] # cat head P5
- [-1, 3, C2fCIB, [1024, True]] # 23 (P5/32-large)
- [[17, 20, 23], 1, v10Detect, [nc]] # Detect(P3, P4, P5)
6.2 模型改进版本二⭐
此处同样以ultralytics/cfg/models/v10/yolov10m.yaml
为例,在同目录下创建一个用于自己数据集训练的模型文件yolov10m-C2f_SCSA.yaml
。
将yolov10m.yaml
中的内容复制到yolov10m-C2f_SCSA.yaml
文件下,修改nc
数量等于自己数据中目标的数量。
📌 模型的修改方法是将骨干网络中的所有C2f模块
替换成C2f_SCSA模块
。
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# Parameters
nc: 1 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
# YOLOv8.0n backbone
backbone:
# [from, repeats, module, args]
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
- [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
- [-1, 3, C2f_SCSA, [128, True]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 6, C2f_SCSA, [256, True]]
- [-1, 1, SCDown, [512, 3, 2]] # 5-P4/16
- [-1, 6, C2f_SCSA, [512, True]]
- [-1, 1, SCDown, [1024, 3, 2]] # 7-P5/32
- [-1, 3, C2fCIB, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
- [-1, 1, PSA, [1024]] # 10
# YOLOv8.0n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 3, C2f, [512]] # 13
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 3, C2f, [256]] # 16 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 13], 1, Concat, [1]] # cat head P4
- [-1, 3, C2fCIB, [512, True]] # 19 (P4/16-medium)
- [-1, 1, SCDown, [512, 3, 2]]
- [[-1, 10], 1, Concat, [1]] # cat head P5
- [-1, 3, C2fCIB, [1024, True]] # 22 (P5/32-large)
- [[16, 19, 22], 1, v10Detect, [nc]] # Detect(P3, P4, P5)
七、成功运行结果
分别打印网络模型可以看到SCSA
和C2f_SCSA
已经加入到模型中,并可以进行训练了。
YOLOv10m-SCSA:
from n params module arguments
0 -1 1 1392 ultralytics.nn.modules.conv.Conv [3, 48, 3, 2]
1 -1 1 41664 ultralytics.nn.modules.conv.Conv [48, 96, 3, 2]
2 -1 2 111360 ultralytics.nn.modules.block.C2f [96, 96, 2, True]
3 -1 1 166272 ultralytics.nn.modules.conv.Conv [96, 192, 3, 2]
4 -1 4 813312 ultralytics.nn.modules.block.C2f [192, 192, 4, True]
5 -1 1 78720 ultralytics.nn.modules.block.SCDown [192, 384, 3, 2]
6 -1 4 3248640 ultralytics.nn.modules.block.C2f [384, 384, 4, True]
7 -1 1 228672 ultralytics.nn.modules.block.SCDown [384, 576, 3, 2]
8 -1 2 1689984 ultralytics.nn.modules.block.C2fCIB [576, 576, 2, True]
9 -1 1 9216 ultralytics.nn.modules.block.SCSA [576, 576]
10 -1 1 831168 ultralytics.nn.modules.block.SPPF [576, 576, 5]
11 -1 1 1253088 ultralytics.nn.modules.block.PSA [576, 576]
12 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
13 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1]
14 -1 2 1993728 ultralytics.nn.modules.block.C2f [960, 384, 2]
15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
16 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1]
17 -1 2 517632 ultralytics.nn.modules.block.C2f [576, 192, 2]
18 -1 1 332160 ultralytics.nn.modules.conv.Conv [192, 192, 3, 2]
19 [-1, 14] 1 0 ultralytics.nn.modules.conv.Concat [1]
20 -1 2 831744 ultralytics.nn.modules.block.C2fCIB [576, 384, 2, True]
21 -1 1 152448 ultralytics.nn.modules.block.SCDown [384, 384, 3, 2]
22 [-1, 11] 1 0 ultralytics.nn.modules.conv.Concat [1]
23 -1 2 1911168 ultralytics.nn.modules.block.C2fCIB [960, 576, 2, True]
24 [17, 20, 23] 1 2282134 ultralytics.nn.modules.head.v10Detect [1, [192, 384, 576]]
YOLOv10m-SCSA summary: 514 layers, 16494502 parameters, 16494486 gradients, 64.0 GFLOPs
YOLOv10m-C2f_SCSA:
from n params module arguments
0 -1 1 1392 ultralytics.nn.modules.conv.Conv [3, 48, 3, 2]
1 -1 1 41664 ultralytics.nn.modules.conv.Conv [48, 96, 3, 2]
2 -1 2 133248 ultralytics.nn.modules.block.C2f_SCSA [96, 96, True]
3 -1 1 166272 ultralytics.nn.modules.conv.Conv [96, 192, 3, 2]
4 -1 4 1049088 ultralytics.nn.modules.block.C2f_SCSA [192, 192, True]
5 -1 1 78720 ultralytics.nn.modules.block.SCDown [192, 384, 3, 2]
6 -1 4 4162560 ultralytics.nn.modules.block.C2f_SCSA [384, 384, True]
7 -1 1 228672 ultralytics.nn.modules.block.SCDown [384, 576, 3, 2]
8 -1 2 1689984 ultralytics.nn.modules.block.C2fCIB [576, 576, 2, True]
9 -1 1 831168 ultralytics.nn.modules.block.SPPF [576, 576, 5]
10 -1 1 1253088 ultralytics.nn.modules.block.PSA [576, 576]
11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
12 [-1, 6] 1 0 ultralytics.nn.modules.conv.Concat [1]
13 -1 2 1993728 ultralytics.nn.modules.block.C2f [960, 384, 2]
14 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
15 [-1, 4] 1 0 ultralytics.nn.modules.conv.Concat [1]
16 -1 2 517632 ultralytics.nn.modules.block.C2f [576, 192, 2]
17 -1 1 332160 ultralytics.nn.modules.conv.Conv [192, 192, 3, 2]
18 [-1, 13] 1 0 ultralytics.nn.modules.conv.Concat [1]
19 -1 2 831744 ultralytics.nn.modules.block.C2fCIB [576, 384, 2, True]
20 -1 1 152448 ultralytics.nn.modules.block.SCDown [384, 384, 3, 2]
21 [-1, 10] 1 0 ultralytics.nn.modules.conv.Concat [1]
22 -1 2 1911168 ultralytics.nn.modules.block.C2fCIB [960, 576, 2, True]
23 [16, 19, 22] 1 2282134 ultralytics.nn.modules.head.v10Detect [1, [192, 384, 576]]
YOLOv10m-C2f_SCSA summary: 717 layers, 17656870 parameters, 17656854 gradients, 70.7 GFLOPs