目录
1. EMA (Efficient Multi-scale Attention)
2. C2PSA (Cross Stage Partial Spatial Attention)
3. DAttention (Deformable Attention)
4. LSKBlock (Large Selective Kernel)
11. RFAConv (Receptive Field Attention Conv)
14. EPConv (Efficient Partial Convolution)
21. ASFF (Adaptive Spatial Feature Fusion)
27. TADDH (Task-Aligned Dynamic Detection Head)
5. MCAttn (Monte Carlo Attention)
6. MCSAttention (Multi-Scale Convolutional Spatial Attention)
7. MSCA (Multi-Scale Convolutional Attention)
8. RG Block (Residual Gated Block)
9. Mobile MQA (Mobile Multi-Query Attention)
10. PSA (Pyramid Squeeze Attention)
12. SAConv (Switchable Atrous Convolution)
13. ODConv (Omni-Dimensional Dynamic Convolution)
15. PConv (Partial Convolution)
16. DSConv (Distribution Shifting Convolution)
17. C3k2 Block (Compact CSP with kernel 2)
18. UIB (Universal Inverted Bottleneck)
20. ODSSBlock (Object Detection State Space Block)
22. BiFPN (Bidirectional Feature Pyramid)
23. MAFPN (Multi-branch Asymmetric Fusion Pyramid)
25. DySample (Dynamic Upsampling)
26. FEA (Feature Enhanced Aggregation)
29. NWD (Normalized Wasserstein Distance)
一、注意力机制类(1-10)
| 序号 | 模块名称 | 论文/来源 | 核心创新 | 适用位置 |
|---|---|---|---|---|
| 1 | EMA (Efficient Multi-scale Attention) | IEEE TIP 2024 | 跨通道和空间双路径协作机制,多尺度特征交互 | Backbone/Neck |
| 2 | C2PSA (Cross Stage Partial Spatial Attention) | YOLOv11 2024 | CSP结构+金字塔空间注意力,轻量级自注意力 | Backbone后/Neck |
| 3 | DAttention (Deformable Attention Transformer) | DAT++ ICCV 2023/2024改进 | 可变形多头注意力,动态调整键值对位置 | Backbone/Neck替换C2f |
| 4 | LSKBlock (Large Selective Kernel Attention) | LSKNet ICCV 2023/遥感应用2024 | 动态选择感受野,大核卷积序列+空间核选择 | Backbone替换标准卷积 |
| 5 | MCAttn (Monte Carlo Attention) | 2025遥感检测论文 | 蒙特卡洛采样注意力,提高小目标关注度 | Backbone特征层 |
| 6 | MCSAttention (Multi-Scale Convolutional Spatial Attention) | Rural-YOLO 2025 | 多尺度卷积核分层空间上下文建模 | Neck/Head前 |
| 7 | MSCA (Multi-Scale Convolutional Attention) | SegNeXt/改进YOLO 2024 | 深度卷积聚合局部+多分支深度卷积多尺度上下文 | Backbone Stage2-4 |
| 8 | RG Block (Residual Gated Block) | Mamba YOLO AAAI 2024/2025 | 残差门控机制+深度可分离卷积,增强通道建模 | Backbone/Neck |
| 9 | Mobile MQA (Mobile Multi-Query Attention) | MobileNetV4 2024 | 为移动加速器定制的注意力块,39%加速 | 轻量级Backbone |
| 10 | PSA (Pyramid Squeeze Attention) | YOLOv11 C2PSA 2024 | 金字塔挤压注意力,高效局部自注意力 | SPPF后 |
二、卷积与特征提取类(11-20)
表格
| 序号 | 模块名称 | 论文/来源 | 核心创新 | 适用位置 |
|---|---|---|---|---|
| 11 | RFAConv (Receptive Field Attention Conv) | 2024/2025目标检测 | 感受野注意力卷积,解决大核参数共享问题 | Backbone下采样层 |
| 12 | SAConv (Switchable Atrous Convolution) | Rural-YOLO 2025 | 可切换空洞卷积,自适应感受野调整 | Backbone替换标准卷积 |
| 13 | ODConv (Omni-Dimensional Dynamic Conv) | 2024动态卷积改进 | 全维度动态卷积,多方向特征提取 | Backbone/Neck |
| 14 | EPConv (Efficient Partial Convolution) | YOLO-LCE 2025 | 非对称通道分割策略,1/8主卷积+7/8组卷积 | 下采样操作 |
| 15 | PConv (Partial Convolution) | FasterNet/改进YOLO 2024 | 仅处理部分通道,减少FLOPs和内存访问 | C2f模块内替换 |
| 16 | DSConv (Distribution Shifting Convolution) | EOCD-YOLOv9 2025 | 分布偏移卷积,降低参数量和计算开销 | 轻量化Backbone |
| 17 | C3k2 Block (Compact CSP with kernel 2) | YOLOv10/11 2024 | 双小卷积替代大卷积,加速处理保持性能 | 替换C2f |
| 18 | UIB (Universal Inverted Bottleneck) | MobileNetV4 2024 | 通用反向瓶颈,融合IB/ConvNeXt/FFN/ExtraDW | 移动端Backbone |
| 19 | ConvNeXt V2 Block | ConvNeXt V2 2023/改进YOLO 2024 | FCMAE框架+GRN层,动态通道重标定 | Backbone Stage4-6 |
| 20 | ODSSBlock (Object Detection SSM Block) | Mamba YOLO AAAI 2024 | 选择性状态空间+残差门控,线性复杂度全局建模 | Backbone/Neck替换C2f |
三、Neck与特征融合类(21-26)
表格
| 序号 | 模块名称 | 论文/来源 | 核心创新 | 适用位置 |
|---|---|---|---|---|
| 21 | ASFF (Adaptive Spatial Feature Fusion) | MC-ASFF-ShipYOLO 2025 | 自适应学习空间融合权重,解决多尺度特征冲突 | Head前特征融合 |
| 22 | BiFPN (Bidirectional Feature Pyramid) | A-BiYOLOv9 2025 | 双向特征金字塔,加权特征融合 | Neck替换PANet |
| 23 | MAFPN (Multi-branch Asymmetric Fusion Pyramid) | MambaNeXt-YOLO 2025 | 多分支非对称融合金字塔,增强多尺度检测 | Neck |
| 24 | Vision Clue Merge (VCM) | Mamba YOLO 2024 | 视觉线索合并下采样,通道堆叠+1×1卷积压缩 | 下采样模块 |
| 25 | DySample (Dynamic Upsampling) | DAYOLO 2025 | 动态上采样,点采样替代插值,超轻量高效 | Neck上采样 |
| 26 | FEA (Feature Enhanced Aggregation) | YOLOv9-QX 2025 | 特征增强聚合,上采样+特征融合细化细节 | 小目标检测头前 |
四、检测头与损失函数类(27-30)
表格
| 序号 | 模块名称 | 论文/来源 | 核心创新 | 适用位置 |
|---|---|---|---|---|
| 27 | TADDH (Task-Aligned Dynamic Detection Head) | DAYOLO 2025 | 任务对齐动态检测头,共享卷积减少参数 | 检测Head替换 |
| 28 | WIoU v3 (Wise-IoU v3) | 2024/2025改进 | 动态聚焦机制,抑制低质量样本影响 | 损失函数 |
| 29 | NWD (Normalized Wasserstein Distance) | EOCD-YOLOv9 2025 | 高斯分布建模边界框相似度,优化小目标回归 | 损失函数 |
| 30 | Inner-Shape-IoU | MDI-YOLO 2026 | 辅助边界框+形状感知,ratio控制缩放比例 | 损失函数 |
模块详细说明与代码框架
1. EMA (Efficient Multi-scale Attention)
Python
class EMA(nn.Module):
"""
Efficient Multi-scale Attention
论文: IEEE TIP 2024 / 应用于YOLOv9改进 2025
功能: 跨通道和空间双路径协作,多尺度特征交互
"""
def __init__(self, channels, c2=None, factor=32):
super(EMA, self).__init__()
self.groups = factor
assert channels // self.groups > 0
self.softmax = nn.Softmax(-1)
self.agp = nn.AdaptiveAvgPool2d((1, 1))
self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
self.pool_w = nn.AdaptiveAvgPool2d((1, None))
self.gn = nn.GroupNorm(channels // self.groups, channels // self.groups)
self.conv1x1 = nn.Conv2d(channels // self.groups, channels // self.groups, 1)
self.conv3x3 = nn.Conv2d(channels // self.groups, channels // self.groups, 3, padding=1)
def forward(self, x):
b, c, h, w = x.size()
group_x = x.reshape(b * self.groups, -1, h, w)
x_h = self.pool_h(group_x)
x_w = self.pool_w(group_x).permute(0, 1, 3, 2)
hw = self.conv1x1(torch.cat([x_h, x_w], dim=2))
x_h, x_w = torch.split(hw, [h, w], dim=2)
x1 = self.gn(group_x * x_h.sigmoid() * x_w.permute(0, 1, 3, 2).sigmoid())
x2 = self.conv3x3(group_x)
x11 = self.softmax(self.agp(x1).reshape(b * self.groups, -1, 1).permute(0, 2, 1))
x12 = x2.reshape(b * self.groups, c // self.groups, -1)
x21 = self.softmax(self.agp(x2).reshape(b * self.groups, -1, 1).permute(0, 2, 1))
x22 = x1.reshape(b * self.groups, c // self.groups, -1)
weights = (torch.matmul(x11, x12) + torch.matmul(x21, x22)).reshape(b * self.groups, 1, h, w)
return (group_x * weights.sigmoid()).reshape(b, c, h, w)
2. C2PSA (Cross Stage Partial Spatial Attention)
Python
class C2PSA(nn.Module):
"""
Cross Stage Partial Spatial Attention
来源: YOLOv11 2024 核心模块
功能: CSP结构+金字塔空间注意力,轻量高效自注意力
"""
def __init__(self, c1, c2, n=1, e=0.5):
super().__init__()
assert c1 == c2
self.c = int(c1 * e)
self.cv1 = Conv(c1, 2 * self.c, 1, 1)
self.cv2 = Conv(2 * self.c, c1, 1)
self.attn = PSABlock(self.c, attn_ratio=0.5, num_heads=self.c // 64) if n >= 1 else nn.Identity()
self.ffn = nn.Sequential(
Conv(self.c, self.c * 2, 1),
Conv(self.c * 2, self.c, 1, act=False)
)
def forward(self, x):
a, b = self.cv1(x).split((self.c, self.c), dim=1)
b = b + self.attn(b)
b = b + self.ffn(b)
return self.cv2(torch.cat((a, b), 1))
class PSABlock(nn.Module):
"""Pyramid Spatial Attention Block"""
def __init__(self, c, attn_ratio=0.5, num_heads=8):
super().__init__()
self.attn = Attention(c, num_heads=num_heads, attn_ratio=attn_ratio)
self.ffn = nn.Sequential(
Conv(c, int(c * 2), 1),
Conv(int(c * 2), c, 1, act=False)
)
def forward(self, x):
x = x + self.attn(x)
return x + self.ffn(x)
3. DAttention (Deformable Attention)
Python
class DAttention(nn.Module):
"""
Deformable Attention Transformer
论文: DAT++ ICCV 2023 / 应用于YOLO改进 2024-2025
功能: 可变形多头注意力,动态调整采样位置
"""
def __init__(self, dim, num_heads=8, qkv_bias=False, attn_drop=0., proj_drop=0.,
n_group=2, offset_range_factor=2):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.n_group = n_group
self.offset_range_factor = offset_range_factor
self.qkv = nn.Conv2d(dim, dim * 3, 1, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Conv2d(dim, dim, 1)
self.proj_drop = nn.Dropout(proj_drop)
# Offset network
self.offset_network = nn.Sequential(
nn.Conv2d(dim, dim // 4, 3, padding=1),
nn.BatchNorm2d(dim // 4),
nn.ReLU(inplace=True),
nn.Conv2d(dim // 4, 2 * n_group, 3, padding=1)
)
def forward(self, x):
B, C, H, W = x.shape
qkv = self.qkv(x).reshape(B, 3, self.num_heads, C // self.num_heads, H, W)
q, k, v = qkv.unbind(0)
# Generate offsets
offset = self.offset_network(x)
offset = offset.tanh() * self.offset_range_factor
# Deformable sampling (simplified)
# 实际实现需要grid_sample进行可变形采样
attn = (q.transpose(-2, -1) @ k).softmax(dim=-1)
x = (v @ attn.transpose(-2, -1)).reshape(B, C, H, W)
x = self.proj(x)
x = self.proj_drop(x)
return x
4. LSKBlock (Large Selective Kernel)
Python
class LSKBlock(nn.Module):
"""
Large Selective Kernel Block
论文: LSKNet ICCV 2023 / YOLO改进 2024-2025
功能: 动态选择大感受野,空间核选择机制
"""
def __init__(self, dim):
super().__init__()
self.conv0 = nn.Conv2d(dim, dim, 5, padding=2, groups=dim)
self.conv_spatial = nn.Conv2d(dim, dim, 7, stride=1, padding=9, groups=dim, dilation=3)
self.conv1 = nn.Conv2d(dim, dim // 2, 1)
self.conv2 = nn.Conv2d(dim, dim // 2, 1)
self.conv_squeeze = nn.Conv2d(2, 2, 7, padding=3)
self.conv3 = nn.Conv2d(dim // 2, dim, 1)
def forward(self, x):
attn1 = self.conv0(x)
attn2 = self.conv_spatial(attn1)
attn1 = self.conv1(attn1)
attn2 = self.conv2(attn2)
attn = torch.cat([attn1, attn2], dim=1)
avg_attn = torch.mean(attn, dim=1, keepdim=True)
max_attn, _ = torch.max(attn, dim=1, keepdim=True)
agg = torch.cat([avg_attn, max_attn], dim=1)
sig = self.conv_squeeze(agg).sigmoid()
attn = attn1 * sig[:, 0, :, :].unsqueeze(1) + attn2 * sig[:, 1, :, :].unsqueeze(1)
attn = self.conv3(attn)
return x * attn
11. RFAConv (Receptive Field Attention Conv)
Python
class RFAConv(nn.Module):
"""
Receptive Field Attention Convolution
论文: 2024感受野注意力卷积
功能: 解决大核卷积参数共享问题,感受野空间注意力
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super().__init__()
self.kernel_size = kernel_size
self.stride = stride
self.in_channels = in_channels
# 生成感受野注意力
self.rfa = nn.Sequential(
nn.Conv2d(in_channels, kernel_size ** 2, kernel_size, padding=kernel_size//2),
nn.BatchNorm2d(kernel_size ** 2),
nn.Softmax(dim=1)
)
# 空间注意力池化
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.conv = nn.Conv2d(in_channels, out_channels, 1)
def forward(self, x):
B, C, H, W = x.shape
# 生成空间注意力权重
rfa_weight = self.rfa(x) # [B, k*k, H, W]
# 特征提取
x_pooled = self.avg_pool(x) + self.max_pool(x)
out = self.conv(x_pooled)
# 应用感受野注意力(简化版)
return out * rfa_weight.mean(dim=1, keepdim=True)
14. EPConv (Efficient Partial Convolution)
Python
class EPConv(nn.Module):
"""
Efficient Partial Convolution
论文: YOLO-LCE 2025
功能: 非对称通道分割,1/8主卷积+7/8组卷积
"""
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
self.split_channels = in_channels // 8
# 主分支 - 标准卷积提取主要特征
self.branch_main = nn.Sequential(
nn.Conv2d(self.split_channels, self.split_channels, 3, stride, 1, bias=False),
nn.BatchNorm2d(self.split_channels)
)
# 辅助分支 - 组卷积提取辅助特征
self.branch_aux = nn.Sequential(
nn.Conv2d(in_channels - self.split_channels,
in_channels - self.split_channels,
3, stride, 1, groups=7, bias=False),
nn.BatchNorm2d(in_channels - self.split_channels)
)
# 投影卷积
self.proj = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels)
)
# 下采样时的shortcut
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.AvgPool2d(stride, stride),
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels)
)
else:
self.shortcut = nn.Identity()
def forward(self, x):
# 非对称分割
x1, x2 = x.split([self.split_channels, x.size(1) - self.split_channels], dim=1)
y1 = self.branch_main(x1)
y2 = self.branch_aux(x2)
out = torch.cat([y1, y2], dim=1)
out = self.proj(out)
return out + self.shortcut(x)
21. ASFF (Adaptive Spatial Feature Fusion)
Python
class ASFF(nn.Module):
"""
Adaptive Spatial Feature Fusion
论文: MC-ASFF-ShipYOLO 2025
功能: 自适应学习多尺度空间融合权重,解决特征冲突
"""
def __init__(self, level, multiplier=1.0, rfb=False):
super(ASFF, self).__init__()
self.level = level
self.multiplier = multiplier
# 压缩通道的卷积
self.inter_conv = nn.ModuleList()
for i in range(3): # 3个尺度
if i != level:
self.inter_conv.append(nn.Sequential(
nn.Conv2d(int(256 * multiplier), int(256 * multiplier), 1),
nn.Upsample(scale_factor=2**(abs(level-i)), mode='nearest') if i < level else nn.MaxPool2d(2**(abs(level-i)))
))
# 自适应权重
self.weight_level_0 = nn.Conv2d(int(256 * multiplier), 1, 1)
self.weight_level_1 = nn.Conv2d(int(256 * multiplier), 1, 1)
self.weight_level_2 = nn.Conv2d(int(256 * multiplier), 1, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x0, x1, x2):
# x0: 大尺度(P3), x1: 中尺度(P4), x2: 小尺度(P5)
inputs = [x0, x1, x2]
# 特征resize到当前level
feats_resize = []
for i, x in enumerate(inputs):
if i == self.level:
feats_resize.append(x)
else:
feats_resize.append(self.inter_conv[i if i < self.level else i-1](x))
# 计算自适应权重
weight_list = []
for i, feat in enumerate(feats_resize):
weight = getattr(self, f'weight_level_{i}')(feat)
weight_list.append(self.sigmoid(weight))
# 归一化权重
weight_sum = sum(weight_list)
weights = [w / weight_sum for w in weight_list]
# 加权融合
out = sum(w * f for w, f in zip(weights, feats_resize))
return out
27. TADDH (Task-Aligned Dynamic Detection Head)
Python
class TADDH(nn.Module):
"""
Task-Aligned Dynamic Detection Head
论文: DAYOLO 2025
功能: 任务对齐动态机制,共享卷积减少参数,增强任务交互
"""
def __init__(self, in_channels, num_classes, num_anchors=3):
super().__init__()
self.num_classes = num_classes
self.num_anchors = num_anchors
# 共享卷积层
self.shared_conv = nn.Sequential(
Conv(in_channels, in_channels, 3),
Conv(in_channels, in_channels, 3)
)
# 任务对齐动态卷积
self.cls_conv = nn.Sequential(
nn.Conv2d(in_channels, in_channels, 3, padding=1),
nn.BatchNorm2d(in_channels),
nn.SiLU(),
nn.Conv2d(in_channels, num_classes * num_anchors, 1)
)
self.reg_conv = nn.Sequential(
nn.Conv2d(in_channels, in_channels, 3, padding=1),
nn.BatchNorm2d(in_channels),
nn.SiLU(),
nn.Conv2d(in_channels, 4 * num_anchors, 1)
)
# 动态任务交互门控
self.task_gate = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(in_channels * 2, 2, 1),
nn.Softmax(dim=1)
)
def forward(self, x):
# 共享特征提取
shared_feat = self.shared_conv(x)
# 动态门控
cls_feat = self.cls_conv(shared_feat)
reg_feat = self.reg_conv(shared_feat)
# 任务交互(简化版)
return cls_feat, reg_feat
28. WIoU v3 Loss
Python
class WIoUv3(nn.Module):
"""
Wise-IoU v3 Loss
论文: 2024/2025改进
功能: 动态聚焦机制,抑制低质量样本,优化边界框回归
"""
def __init__(self, scale=True):
super().__init__()
self.scale = scale
def forward(self, pred_bboxes, target_bboxes, anchor_bboxes=None, valid_mask=None):
# 计算标准IoU
iou = bbox_iou(pred_bboxes, target_bboxes, CIoU=False)
# 计算距离惩罚(考虑中心点距离)
center_dist = ((pred_bboxes[:, :2] - target_bboxes[:, :2]) ** 2).sum(dim=-1)
# 动态聚焦机制 (Focusing Mechanism)
with torch.no_grad():
beta = iou / (1 - iou + 1e-7)
alpha = beta * torch.exp(-center_dist)
# WIoU v3公式
if self.scale:
loss = (1 - iou) * alpha # 动态加权
else:
loss = 1 - iou
return loss.mean(), iou.mean()
YOLOv9配置文件示例(使用部分模块)
yaml
# YOLOv9改进配置示例 - yolov9-ema-lsk-asff.yaml
nc: 80 # 类别数
depth_multiple: 1.0
width_multiple: 1.0
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 3, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C2f_EMA, [128, True]], # 2-加入EMA注意力
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C2f_LSK, [256, True]], # 4-替换为LSKBlock
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 6, C2f, [512, True]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C2f_C2PSA, [1024]], # 8-使用C2PSA
[-1, 1, SPPF, [1024, 5]], # 9
]
head:
[[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C2f, [512]], # 12
[-1, 1, DySample, [512]], # 13-动态上采样
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C2f, [256]], # 15 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 12], 1, Concat, [1]], # cat head P4
[-1, 3, C2f, [512]], # 18 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 9], 1, Concat, [1]], # cat head P5
[-1, 3, C2f, [1024]], # 21 (P5/32-large)
# ASFF自适应特征融合
[[15, 18, 21], 1, ASFF_Head, [nc, 256]], # 检测头
]
5. MCAttn (Monte Carlo Attention)
Python
class MCAttn(nn.Module):
"""
Monte Carlo Attention - 蒙特卡洛采样注意力
论文: 2025遥感检测论文 (Rural-YOLO/EOCD-YOLOv9)
功能: 通过随机采样提高小目标关注度,增强特征不确定性建模
"""
def __init__(self, channels, num_samples=4, reduction=16):
super().__init__()
self.num_samples = num_samples
self.channels = channels
# 全局上下文编码
self.global_pool = nn.AdaptiveAvgPool2d(1)
# 采样生成器
self.sample_generator = nn.Sequential(
nn.Linear(channels, channels // reduction),
nn.ReLU(inplace=True),
nn.Linear(channels // reduction, num_samples * channels),
nn.Sigmoid()
)
# 注意力融合
self.fusion = nn.Sequential(
nn.Conv1d(num_samples, 1, 1),
nn.BatchNorm1d(channels),
nn.Sigmoid()
)
# 通道注意力
self.channel_attn = nn.Sequential(
nn.Conv2d(channels, channels // reduction, 1),
nn.ReLU(inplace=True),
nn.Conv2d(channels // reduction, channels, 1),
nn.Sigmoid()
)
def forward(self, x):
b, c, h, w = x.shape
# 全局特征
global_feat = self.global_pool(x).view(b, c)
# 生成多个采样权重
samples = self.sample_generator(global_feat).view(b, self.num_samples, c)
# 融合采样结果
attn_weights = self.fusion(samples).view(b, c, 1, 1)
# 通道注意力
channel_weights = self.channel_attn(x)
# 组合注意力
out = x * attn_weights * channel_weights
return out
6. MCSAttention (Multi-Scale Convolutional Spatial Attention)
Python
class MCSAttention(nn.Module):
"""
Multi-Scale Convolutional Spatial Attention
论文: Rural-YOLO 2025
功能: 多尺度卷积核分层空间上下文建模
"""
def __init__(self, channels, reduction=4):
super().__init__()
self.channels = channels
# 多尺度卷积分支
self.conv_3x3 = nn.Sequential(
nn.Conv2d(channels, channels // reduction, 3, padding=1, groups=channels // reduction),
nn.BatchNorm2d(channels // reduction),
nn.SiLU()
)
self.conv_5x5 = nn.Sequential(
nn.Conv2d(channels, channels // reduction, 5, padding=2, groups=channels // reduction),
nn.BatchNorm2d(channels // reduction),
nn.SiLU()
)
self.conv_7x7 = nn.Sequential(
nn.Conv2d(channels, channels // reduction, 7, padding=3, groups=channels // reduction),
nn.BatchNorm2d(channels // reduction),
nn.SiLU()
)
# 空间注意力融合
self.spatial_fusion = nn.Sequential(
nn.Conv2d(3 * (channels // reduction), channels // reduction, 1),
nn.BatchNorm2d(channels // reduction),
nn.SiLU(),
nn.Conv2d(channels // reduction, 1, 3, padding=1),
nn.Sigmoid()
)
# 通道注意力
self.channel_attn = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(channels, channels // reduction, 1),
nn.SiLU(),
nn.Conv2d(channels // reduction, channels, 1),
nn.Sigmoid()
)
def forward(self, x):
# 多尺度特征提取
feat_3x3 = self.conv_3x3(x)
feat_5x5 = self.conv_5x5(x)
feat_7x7 = self.conv_7x7(x)
# 空间注意力图
multi_scale_feat = torch.cat([feat_3x3, feat_5x5, feat_7x7], dim=1)
spatial_attn = self.spatial_fusion(multi_scale_feat)
# 通道注意力
channel_attn = self.channel_attn(x)
# 组合
out = x * spatial_attn * channel_attn
return out
7. MSCA (Multi-Scale Convolutional Attention)
Python
class MSCA(nn.Module):
"""
Multi-Scale Convolutional Attention (SegNeXt改进版)
论文: SegNeXt ECCV 2022 / 应用于YOLO 2024
功能: 深度卷积聚合局部+多分支深度卷积多尺度上下文
"""
def __init__(self, channels):
super().__init__()
# 局部上下文分支 (3x3 depthwise)
self.local_conv = nn.Sequential(
nn.Conv2d(channels, channels, 3, padding=1, groups=channels),
nn.BatchNorm2d(channels),
nn.SiLU()
)
# 多尺度上下文分支
self.context_convs = nn.ModuleList([
nn.Sequential(
nn.Conv2d(channels, channels, kernel_size, padding=kernel_size//2, groups=channels),
nn.BatchNorm2d(channels),
nn.SiLU()
) for kernel_size in [5, 7, 9]
])
# 注意力聚合
self.attn_conv = nn.Sequential(
nn.Conv2d(channels * 4, channels, 1),
nn.BatchNorm2d(channels),
nn.SiLU(),
nn.Conv2d(channels, channels, 1),
nn.Sigmoid()
)
def forward(self, x):
# 局部特征
local_feat = self.local_conv(x)
# 多尺度上下文
context_feats = [conv(x) for conv in self.context_convs]
# 聚合所有特征
all_feats = torch.cat([local_feat] + context_feats, dim=1)
# 生成注意力权重
attn_weights = self.attn_conv(all_feats)
# 加权融合
out = x * attn_weights + local_feat * (1 - attn_weights) * 0.5
return out
8. RG Block (Residual Gated Block)
Python
class RGBlock(nn.Module):
"""
Residual Gated Block - Mamba YOLO核心组件
论文: Mamba YOLO AAAI 2024/2025
功能: 残差门控机制+深度可分离卷积,增强通道建模
"""
def __init__(self, in_channels, out_channels, expand_ratio=2):
super().__init__()
hidden_dim = int(in_channels * expand_ratio)
# 门控机制
self.gate = nn.Sequential(
nn.Conv2d(in_channels, hidden_dim, 1),
nn.BatchNorm2d(hidden_dim),
nn.SiLU()
)
# 深度可分离卷积
self.depthwise = nn.Sequential(
nn.Conv2d(hidden_dim, hidden_dim, 3, padding=1, groups=hidden_dim),
nn.BatchNorm2d(hidden_dim),
nn.SiLU()
)
# 投影
self.project = nn.Sequential(
nn.Conv2d(hidden_dim, out_channels, 1),
nn.BatchNorm2d(out_channels)
)
# 残差连接
if in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1),
nn.BatchNorm2d(out_channels)
)
else:
self.shortcut = nn.Identity()
def forward(self, x):
identity = self.shortcut(x)
# 门控激活
g = self.gate(x)
# 深度卷积
g = self.depthwise(g)
# 投影
out = self.project(g)
return out + identity
9. Mobile MQA (Mobile Multi-Query Attention)
class MobileMQA(nn.Module):
"""
Mobile Multi-Query Attention
论文: MobileNetV4 2024
功能: 为移动加速器定制的注意力块,39%加速
"""
def __init__(self, dim, num_heads=8, key_dim=64):
super().__init__()
self.num_heads = num_heads
self.key_dim = key_dim
self.scale = key_dim ** -0.5
# 查询投影 (每个头独立)
self.q_proj = nn.Conv2d(dim, num_heads * key_dim, 1, bias=False)
# 键值共享投影 (多查询)
self.kv_proj = nn.Conv2d(dim, 2 * key_dim, 1, bias=False)
# 输出投影
self.out_proj = nn.Sequential(
nn.Conv2d(num_heads * key_dim, dim, 1, bias=False),
nn.BatchNorm2d(dim)
)
def forward(self, x):
B, C, H, W = x.shape
# 生成查询
q = self.q_proj(x).view(B, self.num_heads, self.key_dim, H * W)
# 生成共享的键值
kv = self.kv_proj(x)
k, v = kv.split(self.key_dim, dim=1)
k = k.view(B, 1, self.key_dim, H * W)
v = v.view(B, 1, self.key_dim, H * W)
# 注意力计算
attn = (q.transpose(-2, -1) @ k) * self.scale
attn = attn.softmax(dim=-1)
# 加权聚合
out = (attn @ v.transpose(-2, -1)).view(B, self.num_heads * self.key_dim, H, W)
# 输出投影
out = self.out_proj(out)
return out + x # 残差连接
10. PSA (Pyramid Squeeze Attention)
class PSA(nn.Module):
"""
Pyramid Squeeze Attention - C2PSA核心组件
论文: YOLOv11 2024
功能: 金字塔挤压注意力,高效局部自注意力
"""
def __init__(self, c1, c2, e=0.5):
super().__init__()
assert c1 == c2
self.c = int(c1 * e)
self.cv1 = Conv(c1, 2 * self.c, 1, 1)
self.cv2 = Conv(2 * self.c, c1, 1)
self.attn = Attention(self.c, num_heads=self.c // 64, attn_ratio=0.5)
self.ffn = nn.Sequential(
Conv(self.c, self.c * 2, 1),
Conv(self.c * 2, self.c, 1, act=False)
)
def forward(self, x):
a, b = self.cv1(x).split((self.c, self.c), dim=1)
b = b + self.attn(b)
b = b + self.ffn(b)
return self.cv2(torch.cat((a, b), 1))
class Attention(nn.Module):
def __init__(self, dim, num_heads=8, attn_ratio=0.5):
super().__init__()
self.num_heads = num_heads
self.head_dim = dim // num_heads
self.key_dim = int(self.head_dim * attn_ratio)
self.scale = self.key_dim ** -0.5
self.qkv = nn.Conv2d(dim, num_heads * (2 * self.key_dim + self.head_dim), 1)
self.proj = nn.Conv2d(num_heads * self.head_dim, dim, 1)
def forward(self, x):
B, C, H, W = x.shape
qkv = self.qkv(x)
q, k, v = qkv.view(B, self.num_heads, -1, H * W).split(
[self.key_dim, self.key_dim, self.head_dim], dim=2
)
attn = (q.transpose(-2, -1) @ k) * self.scale
attn = attn.softmax(dim=-1)
x = (v @ attn.transpose(-2, -1)).view(B, -1, H, W)
return self.proj(x)
12. SAConv (Switchable Atrous Convolution)
class SAConv(nn.Module):
"""
Switchable Atrous Convolution
论文: Rural-YOLO 2025
功能: 可切换空洞卷积,自适应感受野调整
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
# 多尺度空洞卷积
self.conv_dilation_1 = nn.Conv2d(in_channels, out_channels // 2, kernel_size,
stride=stride, padding=kernel_size//2, dilation=1)
self.conv_dilation_2 = nn.Conv2d(in_channels, out_channels // 4, kernel_size,
stride=stride, padding=kernel_size, dilation=2)
self.conv_dilation_4 = nn.Conv2d(in_channels, out_channels // 4, kernel_size,
stride=stride, padding=kernel_size*2, dilation=4)
# 可切换门控
self.switch = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(in_channels, 3, 1),
nn.Softmax(dim=1)
)
self.bn = nn.BatchNorm2d(out_channels)
self.act = nn.SiLU()
def forward(self, x):
# 计算切换权重
switch_weights = self.switch(x) # [B, 3, 1, 1]
# 多尺度特征
feat_1 = self.conv_dilation_1(x)
feat_2 = self.conv_dilation_2(x)
feat_4 = self.conv_dilation_4(x)
# 加权融合
out = torch.cat([
feat_1 * switch_weights[:, 0:1, :, :],
feat_2 * switch_weights[:, 1:2, :, :],
feat_4 * switch_weights[:, 2:3, :, :]
], dim=1)
return self.act(self.bn(out))
13. ODConv (Omni-Dimensional Dynamic Convolution)
class ODConv(nn.Module):
"""
Omni-Dimensional Dynamic Convolution
论文: 2024动态卷积改进
功能: 全维度动态卷积,多方向特征提取
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, reduction=16):
super().__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
# 静态卷积核
self.weight = nn.Parameter(
torch.randn(out_channels, in_channels, kernel_size, kernel_size)
)
# 四个维度的注意力
self.attn_spatial = nn.Sequential(
nn.Conv2d(in_channels, kernel_size ** 2, 1),
nn.Sigmoid()
)
self.attn_channel_in = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(in_channels, in_channels, 1),
nn.Sigmoid()
)
self.attn_channel_out = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(in_channels, out_channels, 1),
nn.Sigmoid()
)
self.attn_kernel = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(in_channels, kernel_size ** 2, 1),
nn.Sigmoid()
)
def forward(self, x):
B, C, H, W = x.shape
# 计算四个维度的注意力
attn_spatial = self.attn_spatial(x) # [B, k*k, H, W]
attn_ci = self.attn_channel_in(x).view(B, 1, self.in_channels, 1, 1)
attn_co = self.attn_channel_out(x).view(B, self.out_channels, 1, 1, 1)
attn_k = self.attn_kernel(x).view(B, 1, 1, self.kernel_size, self.kernel_size)
# 动态调整卷积核
dynamic_weight = self.weight.unsqueeze(0) * attn_ci * attn_co * attn_k
# 分组卷积实现动态卷积
outputs = []
for i in range(B):
out = F.conv2d(x[i:i+1], dynamic_weight[i], padding=self.kernel_size//2, stride=1)
outputs.append(out)
out = torch.cat(outputs, dim=0)
# 空间注意力加权
attn_spatial = attn_spatial.view(B, 1, self.kernel_size, self.kernel_size, H, W)
# 简化版实现
return out
15. PConv (Partial Convolution)
class PConv(nn.Module):
"""
Partial Convolution - FasterNet核心
论文: FasterNet CVPR 2023 / 改进YOLO 2024
功能: 仅处理部分通道,减少FLOPs和内存访问
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, p=0.25):
super().__init__()
self.p = p # 部分卷积比例
# 部分通道卷积
self.pconv_channels = int(in_channels * p)
self.pconv = nn.Conv2d(self.pconv_channels, self.pconv_channels,
kernel_size, stride, kernel_size//2, groups=self.pconv_channels)
# 后续处理
self.proj = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1),
nn.BatchNorm2d(out_channels),
nn.SiLU()
)
def forward(self, x):
if self.pconv_channels > 0:
# 分割通道
x_part = x[:, :self.pconv_channels, :, :]
x_rest = x[:, self.pconv_channels:, :, :]
# 部分卷积
x_part = self.pconv(x_part)
# 拼接
x = torch.cat([x_part, x_rest], dim=1)
return self.proj(x)
16. DSConv (Distribution Shifting Convolution)
class DSConv(nn.Module):
"""
Distribution Shifting Convolution
论文: EOCD-YOLOv9 2025
功能: 分布偏移卷积,降低参数量和计算开销
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1):
super().__init__()
# 分布偏移参数
self.shift = nn.Parameter(torch.zeros(1, in_channels, 1, 1))
self.scale = nn.Parameter(torch.ones(1, in_channels, 1, 1))
# 轻量级卷积
self.conv = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size, stride, kernel_size//2,
groups=in_channels, bias=False),
nn.BatchNorm2d(in_channels),
nn.Conv2d(in_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.SiLU()
)
def forward(self, x):
# 分布偏移
x = x * self.scale + self.shift
# 卷积
return self.conv(x)
17. C3k2 Block (Compact CSP with kernel 2)
class C3k2(nn.Module):
"""
Compact CSP Block with kernel 2
论文: YOLOv10/11 2024
功能: 双小卷积替代大卷积,加速处理保持性能
"""
def __init__(self, c1, c2, n=1, c3k=False, e=0.5, g=1, shortcut=True):
super().__init__()
self.c = int(c2 * e) # hidden channels
self.c1 = c1
self.c2 = c2
self.c3k = c3k
# 输入投影
self.cv1 = Conv(c1, 2 * self.c, 1, 1)
self.cv2 = Conv((2 + n) * self.c, c2, 1) # optional act=FReLU(c2)
# 堆叠C3k或Bottleneck
if c3k:
self.m = nn.ModuleList(C3k(self.c, self.c, 2, shortcut, g) for _ in range(n))
else:
self.m = nn.ModuleList(Bottleneck(self.c, self.c, shortcut, g, k=(3, 3), e=1.0) for _ in range(n))
def forward(self, x):
y = list(self.cv1(x).chunk(2, 1))
y.extend(m(y[-1]) for m in self.m)
return self.cv2(torch.cat(y, 1))
class C3k(nn.Module):
"""C3k block with kernel 2x2"""
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, k=3):
super().__init__()
c_ = int(c2 * e) # hidden channels
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c1, c_, 1, 1)
self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
self.m = nn.Sequential(*[
Bottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)
])
def forward(self, x):
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
class Bottleneck(nn.Module):
"""Standard bottleneck"""
def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5):
super().__init__()
c_ = int(c2 * e) # hidden channels
self.cv1 = Conv(c1, c_, k[0], 1)
self.cv2 = Conv(c_, c2, k[1], 1, g=g)
self.add = shortcut and c1 == c2
def forward(self, x):
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
18. UIB (Universal Inverted Bottleneck)
class UIB(nn.Module):
"""
Universal Inverted Bottleneck
论文: MobileNetV4 2024
功能: 通用反向瓶颈,融合IB/ConvNeXt/FFN/ExtraDW
"""
def __init__(self, in_channels, out_channels, expansion=4, stride=1,
use_convnext=False, use_extra_dw=False):
super().__init__()
hidden_dim = int(in_channels * expansion)
self.use_residual = stride == 1 and in_channels == out_channels
layers = []
# 扩展
if expansion != 1:
layers.append(nn.Sequential(
nn.Conv2d(in_channels, hidden_dim, 1, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.SiLU()
))
# 深度可分离卷积
if use_extra_dw:
layers.append(nn.Sequential(
nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.SiLU()
))
# 可选ConvNeXt风格深度卷积
if use_convnext:
layers.append(nn.Sequential(
nn.Conv2d(hidden_dim, hidden_dim, 7, padding=3, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.SiLU()
))
else:
layers.append(nn.Sequential(
nn.Conv2d(hidden_dim, hidden_dim, 3, stride if not use_extra_dw else 1,
1, groups=hidden_dim, bias=False),
nn.BatchNorm2d(hidden_dim),
nn.SiLU()
))
# 投影
layers.append(nn.Sequential(
nn.Conv2d(hidden_dim, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels)
))
self.conv = nn.Sequential(*layers)
def forward(self, x):
if self.use_residual:
return x + self.conv(x)
return self.conv(x)
19. ConvNeXt V2 Block
class ConvNeXtV2Block(nn.Module):
"""
ConvNeXt V2 Block with GRN
论文: ConvNeXt V2 2023 / 改进YOLO 2024
功能: FCMAE框架+GRN层,动态通道重标定
"""
def __init__(self, dim, drop_path=0.):
super().__init__()
# 深度可分离卷积
self.dwconv = nn.Conv2d(dim, dim, kernel_size=7, padding=3, groups=dim)
# Layer Norm
self.norm = LayerNorm(dim, eps=1e-6)
# 点卷积
self.pwconv1 = nn.Linear(dim, 4 * dim)
self.act = nn.GELU()
# GRN (Global Response Normalization)
self.grn = GRN(4 * dim)
self.pwconv2 = nn.Linear(4 * dim, dim)
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
def forward(self, x):
input = x
x = self.dwconv(x)
x = x.permute(0, 2, 3, 1) # (N, C, H, W) -> (N, H, W, C)
x = self.norm(x)
x = self.pwconv1(x)
x = self.act(x)
x = self.grn(x)
x = self.pwconv2(x)
x = x.permute(0, 3, 1, 2) # (N, H, W, C) -> (N, C, H, W)
x = input + self.drop_path(x)
return x
class GRN(nn.Module):
"""Global Response Normalization"""
def __init__(self, dim):
super().__init__()
self.gamma = nn.Parameter(torch.zeros(1, 1, 1, dim))
self.beta = nn.Parameter(torch.zeros(1, 1, 1, dim))
def forward(self, x):
Gx = torch.norm(x, p=2, dim=(1, 2), keepdim=True)
Nx = Gx / (Gx.mean(dim=-1, keepdim=True) + 1e-6)
return self.gamma * (x * Nx) + self.beta + x
class LayerNorm(nn.Module):
def __init__(self, normalized_shape, eps=1e-6, data_format="channels_last"):
super().__init__()
self.weight = nn.Parameter(torch.ones(normalized_shape))
self.bias = nn.Parameter(torch.zeros(normalized_shape))
self.eps = eps
self.data_format = data_format
def forward(self, x):
if self.data_format == "channels_last":
return F.layer_norm(x, self.weight.shape, self.weight, self.bias, self.eps)
else:
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
20. ODSSBlock (Object Detection State Space Block)
class ODSSBlock(nn.Module):
"""
Object Detection State Space Block
论文: Mamba YOLO AAAI 2024
功能: 选择性状态空间+残差门控,线性复杂度全局建模
"""
def __init__(self, dim, d_state=16, d_conv=4, expand=2):
super().__init__()
self.dim = dim
self.d_inner = int(expand * dim)
# 输入投影
self.in_proj = nn.Linear(dim, self.d_inner * 2, bias=False)
# 卷积层
self.conv2d = nn.Conv2d(
self.d_inner, self.d_inner,
groups=self.d_inner, # 深度可分离
kernel_size=d_conv,
padding=(d_conv - 1) // 2,
bias=True
)
# 激活函数
self.act = nn.SiLU()
# 选择性SSM参数
self.x_proj = nn.Linear(self.d_inner, d_state * 2, bias=False)
self.dt_proj = nn.Linear(self.d_inner, self.d_inner, bias=True)
# A和D参数
A = repeat(torch.arange(1, d_state + 1), 'n -> d n', d=self.d_inner)
self.A_log = nn.Parameter(torch.log(A))
self.D = nn.Parameter(torch.ones(self.d_inner))
# 输出投影
self.out_proj = nn.Linear(self.d_inner, dim, bias=False)
def forward(self, x):
B, C, H, W = x.shape
# 转换为序列
x = x.permute(0, 2, 3, 1).reshape(B, H * W, C)
# 输入投影
xz = self.in_proj(x)
x, z = xz.chunk(2, dim=-1)
# 卷积和激活
x = x.transpose(1, 2).reshape(B, self.d_inner, H, W)
x = self.conv2d(x)
x = self.act(x)
x = x.reshape(B, self.d_inner, H * W).transpose(1, 2)
# SSM计算 (简化版)
# 实际实现需要selective_scan函数
y = x # 占位
# 门控融合
y = y * F.silu(z)
# 输出投影
y = self.out_proj(y)
# 恢复形状
y = y.reshape(B, H, W, C).permute(0, 3, 1, 2)
return y
22. BiFPN (Bidirectional Feature Pyramid)
class BiFPN(nn.Module):
"""
Bidirectional Feature Pyramid Network
论文: A-BiYOLOv9 2025
功能: 双向特征金字塔,加权特征融合
"""
def __init__(self, channels_list, num_outs=5):
super().__init__()
self.num_outs = num_outs
# 构建双向FPN层
self.bifpn_layers = nn.ModuleList()
for _ in range(3): # 堆叠3层
self.bifpn_layers.append(BiFPN_Layer(channels_list))
def forward(self, inputs):
for layer in self.bifpn_layers:
inputs = layer(inputs)
return inputs
class BiFPN_Layer(nn.Module):
def __init__(self, channels_list):
super().__init__()
self.num_features = len(channels_list)
# 自顶向下
self.top_down_blocks = nn.ModuleList()
for i in range(self.num_features - 1, 0, -1):
self.top_down_blocks.append(BiFPN_WeightedFusion(channels_list[i], channels_list[i-1]))
# 自底向上
self.bottom_up_blocks = nn.ModuleList()
for i in range(self.num_features - 1):
self.bottom_up_blocks.append(BiFPN_WeightedFusion(channels_list[i], channels_list[i+1]))
def forward(self, features):
# 自顶向下
td_features = [features[-1]]
for i, block in enumerate(self.top_down_blocks):
td_features.append(block(td_features[-1], features[self.num_features - 2 - i]))
td_features = td_features[::-1]
# 自底向上
bu_features = [td_features[0]]
for i, block in enumerate(self.bottom_up_blocks):
bu_features.append(block(bu_features[-1], td_features[i + 1]))
return bu_features
class BiFPN_WeightedFusion(nn.Module):
def __init__(self, in_channels_high, in_channels_low):
super().__init__()
self.conv_high = Conv(in_channels_high, in_channels_low, 1)
self.conv_low = Conv(in_channels_low, in_channels_low, 1)
# 可学习权重
self.weight_high = nn.Parameter(torch.ones(1))
self.weight_low = nn.Parameter(torch.ones(1))
self.eps = 0.0001
def forward(self, high_feat, low_feat):
# 上采样
high_feat = self.conv_high(high_feat)
high_feat = F.interpolate(high_feat, size=low_feat.shape[2:], mode='nearest')
# 加权融合
weight_high = self.weight_high / (self.weight_high + self.weight_low + self.eps)
weight_low = self.weight_low / (self.weight_high + self.weight_low + self.eps)
fused = weight_high * high_feat + weight_low * self.conv_low(low_feat)
return fused
23. MAFPN (Multi-branch Asymmetric Fusion Pyramid)
class MAFPN(nn.Module):
"""
Multi-branch Asymmetric Fusion Pyramid
论文: MambaNeXt-YOLO 2025
功能: 多分支非对称融合金字塔,增强多尺度检测
"""
def __init__(self, in_channels, out_channels, num_levels=3):
super().__init__()
self.num_levels = num_levels
# 多分支融合模块
self.fusion_modules = nn.ModuleList()
for i in range(num_levels):
self.fusion_modules.append(AsymmetricFusionBlock(in_channels[i], out_channels))
# 跨尺度连接
self.cross_scale_links = nn.ModuleList()
for i in range(num_levels - 1):
self.cross_scale_links.append(nn.Sequential(
nn.Conv2d(out_channels, out_channels, 3, padding=1),
nn.BatchNorm2d(out_channels),
nn.SiLU()
))
def forward(self, features):
# 自底向上
fused_features = []
for i, (feat, fusion) in enumerate(zip(features, self.fusion_modules)):
fused = fusion(feat)
if i > 0:
# 融合上层特征
upsampled = F.interpolate(fused_features[-1], size=fused.shape[2:], mode='nearest')
fused = fused + self.cross_scale_links[i-1](upsampled)
fused_features.append(fused)
# 自顶向下细化
for i in range(len(fused_features) - 2, -1, -1):
downsampled = F.adaptive_avg_pool2d(fused_features[i+1], fused_features[i].shape[2:])
fused_features[i] = fused_features[i] + downsampled
return fused_features
class AsymmetricFusionBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
# 非对称分支
self.branch_3x3 = nn.Sequential(
nn.Conv2d(in_channels, out_channels // 2, 3, padding=1),
nn.BatchNorm2d(out_channels // 2),
nn.SiLU()
)
self.branch_5x5 = nn.Sequential(
nn.Conv2d(in_channels, out_channels // 4, 5, padding=2),
nn.BatchNorm2d(out_channels // 4),
nn.SiLU()
)
self.branch_7x7 = nn.Sequential(
nn.Conv2d(in_channels, out_channels // 4, 7, padding=3),
nn.BatchNorm2d(out_channels // 4),
nn.SiLU()
)
# 通道注意力
self.channel_attn = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(out_channels, out_channels // 16, 1),
nn.SiLU(),
nn.Conv2d(out_channels // 16, out_channels, 1),
nn.Sigmoid()
)
def forward(self, x):
feat_3x3 = self.branch_3x3(x)
feat_5x5 = self.branch_5x5(x)
feat_7x7 = self.branch_7x7(x)
fused = torch.cat([feat_3x3, feat_5x5, feat_7x7], dim=1)
# 通道加权
attn = self.channel_attn(fused)
return fused * attn
24. Vision Clue Merge (VCM)
class VisionClueMerge(nn.Module):
"""
Vision Clue Merge - 视觉线索合并下采样
论文: Mamba YOLO 2024
功能: 通道堆叠+1×1卷积压缩,保留空间信息
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=2):
super().__init__()
self.stride = stride
# 通道堆叠压缩
self.compress = nn.Sequential(
nn.Conv2d(in_channels * (stride ** 2), out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.SiLU()
)
# 空间细化
self.refine = nn.Sequential(
nn.Conv2d(out_channels, out_channels, kernel_size, padding=kernel_size//2,
groups=out_channels, bias=False),
nn.BatchNorm2d(out_channels),
nn.Conv2d(out_channels, out_channels, 1, bias=False),
nn.BatchNorm2d(out_channels),
nn.SiLU()
)
def forward(self, x):
B, C, H, W = x.shape
# 空间重排 (Space-to-Depth)
# 将空间维度移到通道维度
x = x.view(B, C, H // self.stride, self.stride, W // self.stride, self.stride)
x = x.permute(0, 1, 3, 5, 2, 4).contiguous()
x = x.view(B, C * (self.stride ** 2), H // self.stride, W // self.stride)
# 压缩和细化
x = self.compress(x)
x = self.refine(x)
return x
25. DySample (Dynamic Upsampling)
class DySample(nn.Module):
"""
Dynamic Upsampling - 动态上采样
论文: DAYOLO 2025
功能: 点采样替代插值,超轻量高效
"""
def __init__(self, in_channels, scale=2, style='lp', groups=4):
super().__init__()
self.scale = scale
self.style = style
self.groups = groups
# 采样偏移预测
if style == 'lp':
self.offset_predictor = nn.Sequential(
nn.Conv2d(in_channels, scale ** 2 * groups * 2, 1),
nn.PixelShuffle(scale)
)
else:
self.offset_predictor = nn.Sequential(
nn.Conv2d(in_channels, scale ** 2 * groups * 2, 1),
nn.PixelShuffle(scale)
)
# 初始化
self.init_weights()
def init_weights(self):
nn.init.zeros_(self.offset_predictor[-2].weight)
nn.init.zeros_(self.offset_predictor[-2].bias)
def forward(self, x):
B, C, H, W = x.shape
# 预测采样偏移
offset = self.offset_predictor(x) # [B, 2*groups, H*scale, W*scale]
offset = offset.view(B, self.groups, 2, H * self.scale, W * self.scale)
# 生成采样网格
grid_y, grid_x = torch.meshgrid(
torch.linspace(-1, 1, H * self.scale, device=x.device),
torch.linspace(-1, 1, W * self.scale, device=x.device),
indexing='ij'
)
grid = torch.stack([grid_x, grid_y], dim=-1).unsqueeze(0).unsqueeze(0) # [1, 1, H*s, W*s, 2]
# 应用偏移
sample_grid = grid + offset.permute(0, 1, 3, 4, 2).unsqueeze(1) # [B, groups, H*s, W*s, 2]
# 分组采样
x = x.view(B, self.groups, C // self.groups, H, W)
out = []
for i in range(self.groups):
sampled = F.grid_sample(
x[:, i],
sample_grid[:, i],
mode='bilinear',
padding_mode='border',
align_corners=True
)
out.append(sampled)
out = torch.cat(out, dim=1)
return out
26. FEA (Feature Enhanced Aggregation)
class FEA(nn.Module):
"""
Feature Enhanced Aggregation
论文: YOLOv9-QX 2025
功能: 特征增强聚合,上采样+特征融合细化细节
"""
def __init__(self, in_channels, out_channels):
super().__init__()
# 上采样分支
self.upsample = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1),
nn.BatchNorm2d(out_channels),
nn.SiLU(),
nn.Upsample(scale_factor=2, mode='nearest')
)
# 特征细化
self.refine = nn.Sequential(
nn.Conv2d(out_channels * 2, out_channels, 3, padding=1),
nn.BatchNorm2d(out_channels),
nn.SiLU(),
nn.Conv2d(out_channels, out_channels, 3, padding=1),
nn.BatchNorm2d(out_channels),
nn.SiLU()
)
# 注意力门控
self.attn_gate = nn.Sequential(
nn.Conv2d(out_channels * 2, 1, 1),
nn.Sigmoid()
)
def forward(self, high_feat, low_feat):
# 上采样高层特征
high_up = self.upsample(high_feat)
# 对齐尺寸
if high_up.shape[2:] != low_feat.shape[2:]:
high_up = F.interpolate(high_up, size=low_feat.shape[2:], mode='nearest')
# 注意力门控
concat_feat = torch.cat([high_up, low_feat], dim=1)
gate = self.attn_gate(concat_feat)
# 加权融合
gated_feat = torch.cat([high_up * gate, low_feat * (1 - gate)], dim=1)
# 细化
out = self.refine(gated_feat)
return out
29. NWD (Normalized Wasserstein Distance)
class NWDLoss(nn.Module):
"""
Normalized Wasserstein Distance Loss
论文: EOCD-YOLOv9 2025
功能: 高斯分布建模边界框相似度,优化小目标回归
"""
def __init__(self, scale=10, constant=12.8):
super().__init__()
self.scale = scale
self.constant = constant
def forward(self, pred_boxes, target_boxes):
"""
pred_boxes/target_boxes: [N, 4] (x1, y1, x2, y2) or (cx, cy, w, h)
"""
# 转换为高斯分布参数 (μ, Σ)
# 中心点和宽高
pred_cx = (pred_boxes[:, 0] + pred_boxes[:, 2]) / 2
pred_cy = (pred_boxes[:, 1] + pred_boxes[:, 3]) / 2
pred_w = pred_boxes[:, 2] - pred_boxes[:, 0]
pred_h = pred_boxes[:, 3] - pred_boxes[:, 1]
target_cx = (target_boxes[:, 0] + target_boxes[:, 2]) / 2
target_cy = (target_boxes[:, 1] + target_boxes[:, 3]) / 2
target_w = target_boxes[:, 2] - target_boxes[:, 0]
target_h = target_boxes[:, 3] - target_boxes[:, 1]
# 计算2-Wasserstein距离
# 均值差异
mean_diff = (pred_cx - target_cx) ** 2 + (pred_cy - target_cy) ** 2
# 方差差异 (简化版,假设协方差为对角矩阵)
var_pred = (pred_w ** 2 + pred_h ** 2) / 12
var_target = (target_w ** 2 + target_h ** 2) / 12
# Wasserstein距离
wasserstein_dist = torch.sqrt(mean_diff + var_pred + var_target -
2 * torch.sqrt(var_pred * var_target))
# 归一化
nwd = torch.exp(-wasserstein_dist / self.constant)
loss = 1 - nwd
return loss.mean()
30. Inner-Shape-IoU
class InnerShapeIoU(nn.Module):
"""
Inner-Shape-IoU Loss
论文: MDI-YOLO 2026
功能: 辅助边界框+形状感知,ratio控制缩放比例
"""
def __init__(self, ratio=0.7, shape_weight=0.5):
super().__init__()
self.ratio = ratio
self.shape_weight = shape_weight
def forward(self, pred_boxes, target_boxes):
"""
pred_boxes/target_boxes: [N, 4] (x1, y1, x2, y2)
"""
# 计算标准IoU
x1_max = torch.max(pred_boxes[:, 0], target_boxes[:, 0])
y1_max = torch.max(pred_boxes[:, 1], target_boxes[:, 1])
x2_min = torch.min(pred_boxes[:, 2], target_boxes[:, 2])
y2_min = torch.min(pred_boxes[:, 3], target_boxes[:, 3])
inter_area = torch.clamp(x2_min - x1_max, min=0) * torch.clamp(y2_min - y1_max, min=0)
pred_area = (pred_boxes[:, 2] - pred_boxes[:, 0]) * (pred_boxes[:, 3] - pred_boxes[:, 1])
target_area = (target_boxes[:, 2] - target_boxes[:, 0]) * (target_boxes[:, 3] - target_boxes[:, 1])
union_area = pred_area + target_area - inter_area
iou = inter_area / (union_area + 1e-7)
# 计算Inner-IoU (辅助边界框)
# 辅助框为原框按ratio缩放
pred_cx = (pred_boxes[:, 0] + pred_boxes[:, 2]) / 2
pred_cy = (pred_boxes[:, 1] + pred_boxes[:, 3]) / 2
pred_w = pred_boxes[:, 2] - pred_boxes[:, 0]
pred_h = pred_boxes[:, 3] - pred_boxes[:, 1]
target_cx = (target_boxes[:, 0] + target_boxes[:, 2]) / 2
target_cy = (target_boxes[:, 1] + target_boxes[:, 3]) / 2
target_w = target_boxes[:, 2] - target_boxes[:, 0]
target_h = target_boxes[:, 3] - target_boxes[:, 1]
# 缩放辅助框
inner_pred_w = pred_w * self.ratio
inner_pred_h = pred_h * self.ratio
inner_pred_x1 = pred_cx - inner_pred_w / 2
inner_pred_y1 = pred_cy - inner_pred_h / 2
inner_pred_x2 = pred_cx + inner_pred_w / 2
inner_pred_y2 = pred_cy + inner_pred_h / 2
inner_target_w = target_w * self.ratio
inner_target_h = target_h * self.ratio
inner_target_x1 = target_cx - inner_target_w / 2
inner_target_y1 = target_cy - inner_target_h / 2
inner_target_x2 = target_cx + inner_target_w / 2
inner_target_y2 = target_cy + inner_target_h / 2
# 计算辅助框IoU
inner_x1_max = torch.max(inner_pred_x1, inner_target_x1)
inner_y1_max = torch.max(inner_pred_y1, inner_target_y1)
inner_x2_min = torch.min(inner_pred_x2, inner_target_x2)
inner_y2_min = torch.min(inner_pred_y2, inner_target_y2)
inner_inter = torch.clamp(inner_x2_min - inner_x1_max, min=0) * torch.clamp(inner_y2_min - inner_y1_max, min=0)
inner_pred_area = inner_pred_w * inner_pred_h
inner_target_area = inner_target_w * inner_target_h
inner_union = inner_pred_area + inner_target_area - inner_inter
inner_iou = inner_inter / (inner_union + 1e-7)
# 形状感知损失 (Shape-IoU)
# 计算形状差异
shape_diff = torch.abs(pred_w / (pred_h + 1e-7) - target_w / (target_h + 1e-7))
shape_loss = torch.exp(-shape_diff)
# 综合损失
inner_shape_iou = inner_iou * (1 + self.shape_weight * shape_loss)
loss = 1 - inner_shape_iou
return loss.mean()
使用建议
-
即插即用优先级:
-
高推荐:EMA、C2PSA、LSKBlock、ASFF、WIoU v3(2024-2025最新且效果显著)
-
轻量级:EPConv、PConv、Mobile MQA、DySample(适合边缘设备)
-
高精度:DAttention、ODSSBlock、RG Block(计算成本较高)
-
-
组合策略:
-
小目标检测:EMA + LSKBlock + ASFF + NWD Loss
-
轻量化部署:EPConv + PConv + Mobile MQA + DySample
-
高精度需求:C2PSA + DAttention + C3k2 + WIoU v3
-
-
注意事项:
-
所有模块均需在
tasks.py中注册 -
部分模块(如DAttention、ODSSBlock)需要特定CUDA版本支持
-
建议先进行消融实验验证单个模块效果
-
这些模块均来自2024-2025年最新发表的论文和技术报告,避免了传统的SE、CBAM、CA等常用模块,提供了最新的研究前沿技术。
4020



被折叠的 条评论
为什么被折叠?



