1.yolo11.yaml文件
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
# Ultralytics YOLO11 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolo11
# Task docs: https://docs.ultralytics.com/tasks/detect
# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.50, 0.25, 1024] # summary: 181 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
s: [0.50, 0.50, 1024] # summary: 181 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
m: [0.50, 1.00, 512] # summary: 231 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
l: [1.00, 1.00, 512] # summary: 357 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
x: [1.00, 1.50, 512] # summary: 357 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
# YOLO11n 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, 2, C3k2, [256, False, 0.25]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 2, C3k2, [512, False, 0.25]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 2, C3k2, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 2, C3k2, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
- [-1, 2, C2PSA, [1024]] # 10
# YOLO11n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 2, C3k2, [512, False]] # 13
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 13], 1, Concat, [1]] # cat head P4
- [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 10], 1, Concat, [1]] # cat head P5
- [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)
- [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)
2.yolo11-seg.yaml
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
# Ultralytics YOLO11-seg instance segmentation model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolo11
# Task docs: https://docs.ultralytics.com/tasks/segment
# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n-seg.yaml' will call yolo11-seg.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.50, 0.25, 1024] # summary: 203 layers, 2876848 parameters, 2876832 gradients, 10.5 GFLOPs
s: [0.50, 0.50, 1024] # summary: 203 layers, 10113248 parameters, 10113232 gradients, 35.8 GFLOPs
m: [0.50, 1.00, 512] # summary: 253 layers, 22420896 parameters, 22420880 gradients, 123.9 GFLOPs
l: [1.00, 1.00, 512] # summary: 379 layers, 27678368 parameters, 27678352 gradients, 143.0 GFLOPs
x: [1.00, 1.50, 512] # summary: 379 layers, 62142656 parameters, 62142640 gradients, 320.2 GFLOPs
# YOLO11n 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, 2, C3k2, [256, False, 0.25]] #输出通道256,False:不使用shortcut连接,0.25:bottleneck层的通道压缩比。 参数:[输出通道, 是否用shortcut, 压缩比]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 2, C3k2, [512, False, 0.25]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 2, C3k2, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 2, C3k2, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
- [-1, 2, C2PSA, [1024]] # 10
# YOLO11n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 2, C3k2, [512, False]] # 13
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 13], 1, Concat, [1]] # cat head P4
- [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 10], 1, Concat, [1]] # cat head P5
- [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)
- [[16, 19, 22], 1, Segment, [nc, 32, 256]] # Detect(P3, P4, P5)
关键模块说明
C3k2模块:
YOLOv11改进的残差模块
结合了CSP结构和高效卷积设计
参数:
[输出通道, 是否用shortcut, 压缩比]
C2PSA模块:
Channel and Position Spatial Attention
同时学习通道重要性和空间位置重要性
增强关键特征,抑制噪声
Segment模块:
实例分割专用头
包含两个组件:
检测分支:输出边界框和类别
Proto分支:生成原型掩码
最终掩码 = 检测输出 × 原型掩码
多尺度输出
P3/8 (第16层):
高分辨率特征图(输入1/8)
适合检测小物体
P4/16 (第19层):
中等分辨率特征图(输入1/16)
平衡精度和速度
P5/32 (第22层):
低分辨率特征图(输入1/32)
适合检测大物体
这种多尺度设计使模型能有效检测不同尺寸的物体,同时保持高精度的实例分割能力。
网络结构图
(网上查询)
(来源:YOLOv11 | 一文带你深入理解ultralytics最新作品yolov11的创新 | 训练、推理、验证、导出 (附网络结构图)-CSDN博客)
3.yolo11.yaml各模块解析
C3k2模块
代码
class C3k2(C2f):
"""Faster Implementation of CSP Bottleneck with 2 convolutions."""
# “更快地实现带有 2 个卷积的 CSP 瓶颈结构”。
def __init__(
self, c1: int, c2: int, n: int = 1, c3k: bool = False, e: float = 0.5, g: int = 1, shortcut: bool = True
):
"""
Initialize C3k2 module.
Args:
c1 (int): Input channels.
c2 (int): Output channels.
n (int): Number of blocks.
c3k (bool): Whether to use C3k blocks.
e (float): Expansion ratio.
g (int): Groups for convolutions.
shortcut (bool): Whether to use shortcut connections.
"""
super().__init__(c1, c2, n, shortcut, g, e)
self.m = nn.ModuleList(
C3k(self.c, self.c, 2, shortcut, g) if c3k else Bottleneck(self.c, self.c, shortcut, g) for _ in range(n)
)
(注意:C3k2模块 就是 C3k模块重复两次)
解析
继承自:C2f
模块(YOLOv8 的高效特征提取模块)
核心改进:提供可选的瓶颈块类型(C3k
或标准 Bottleneck
)
初始化参数详解
def __init__(
self, c1: int, c2: int, n: int = 1, c3k: bool = False,
e: float = 0.5, g: int = 1, shortcut: bool = True
):
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
c1 |
int | - | 输入通道数 |
c2 |
int | - | 输出通道数 |
n |
int | 1 | 瓶颈块重复次数 |
c3k |
bool | False | 核心开关:是否使用 C3k 块替代标准瓶颈块 |
e |
float | 0.5 | 通道扩展因子(hidden_channels = c2 * e) |
g |
int | 1 | 分组卷积的分组数 |
shortcut |
bool | True | 是否使用残差连接 |
关键实现解析
super().__init__(c1, c2, n, shortcut, g, e) # 调用父类初始化
self.m = nn.ModuleList(
C3k(self.c, self.c, 2, shortcut, g) if c3k
else Bottleneck(self.c, self.c, shortcut, g)
for _ in range(n)
1. 父类初始化流程
执行
C2f
的标准初始化:计算隐藏通道数
self.c = int(c2 * e)
创建
cv1
和cv2
卷积层注意:原始
C2f
中的self.m
会被覆盖
2. 模块列表覆盖 (self.m
)
动态选择瓶颈类型:
if c3k:
block = C3k(self.c, self.c, 2, shortcut, g)
"""
标准瓶颈块 (Bottleneck)
结构:两个 3×3 卷积 + 可选残差连接
"""
else:
block = Bottleneck(self.c, self.c, shortcut, g)
"""
C3k 块 (C3k)
结构:???????
关键参数:2 表示内部重复次数
"""
结构图
C3k2 与 C2f 的区别详解
C3k2 和 C2f 都是 YOLOv8 架构中的特征提取模块,它们共享相同的设计理念但存在关键差异。以下是两者的主要区别:
1. 核心结构差异
特性 | C2f | C3k2 |
---|---|---|
基本结构 | 固定使用标准 Bottleneck 块 | 可选择使用 C3k 块或标准 Bottleneck |
模块类型 | 单一结构 | 可配置结构 |
默认块 | Bottleneck | 可选择 C3k 或 Bottleneck |
CSP模块(Cross Stage Partial Network Bottleneck)
CSP瓶颈模块(Cross Stage Partial Network Bottleneck)(跨阶段局部网络瓶颈)是一种常用于计算机视觉任务的卷积神经网络组件,尤其在目标检测算法中表现出色。以下是其详细介绍:
### 基本结构
CSP瓶颈模块通过将输入特征图分割成两个部分,来实现特征的有效提取。具体来说,一个部分直接进行卷积处理,以保持特征的原始信息;另一个部分则经过一系列的卷积和连接操作后,再与直接处理的部分进行融合。这种设计旨在减少网络的计算复杂性和内存需求,同时提高性能。
### 关键组件
1. **输入特征图分割**:将输入特征图分割成两部分,分别进行处理。
2. **直接卷积路径**:对其中一部分特征图进行简单的卷积处理,以保持其原始信息。
3. **复杂卷积路径**:对另一部分特征图进行一系列的卷积和连接操作,提取更复杂的特征。
4. **特征融合**:将直接卷积路径和复杂卷积路径的输出进行融合,以生成最终的特征图。
### 变种模块
在CSP瓶颈模块的基础上,研究人员提出了多种变种模块,以适应不同的目标检测任务,常见的变种模块包括:
1. **Bottleneck**:最基础的模块,用于构建更复杂的CSP结构。它包含两个卷积层,能够有效地减少计算量并提取特征。这个模块还可以选择是否使用shortcut连接,以增强梯度传播。
2. **C3**:CSP瓶颈模块的一个基础版本,包含三个卷积层和一系列瓶颈层,能够高效提取不同层次的特征。特征被分为两条路径,一条路径通过多层瓶颈层来提取复杂的特征,另一条路径直接传递输入特征。最后,通过拼接两条路径的输出,来增加模型的表达能力。
3. **C3k**:C3模块的一个变体,主要改进在于它允许自定义卷积核的大小。这使得C3k可以更好地适应不同尺寸的图像特征,尤其是当我们需要捕捉更大范围的上下文信息时。
4. **C2f**:为了提升处理速度而设计的CSP瓶颈模块。它通过减少卷积层的数量和采用更高效的特征合并策略来提高速度。
综上所述,CSP瓶颈模块通过将输入特征图分割成两部分并分别处理,再进行特征融合的方式,有效地减少了网络的计算复杂性和内存需求,同时提高了性能。其变种模块则在不同的目标检测任务中发挥着重要作用。
C3k模块
代码
class C3k(C3):
"""C3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks."""
#C3k 是一种具有可定制核大小的 CSP 瓶颈模块,用于神经网络中的特征提取。
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5, k: int = 3):
"""
Initialize C3k module.
Args:
c1 (int): Input channels.
c2 (int): Output channels.
n (int): Number of Bottleneck blocks.
shortcut (bool): Whether to use shortcut connections.
g (int): Groups for convolutions.
e (float): Expansion ratio.
k (int): Kernel size.
"""
super().__init__(c1, c2, n, shortcut, g, e)
c_ = int(c2 * e) # hidden channels
# self.m = nn.Sequential(*(RepBottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n)))
解析
初始化参数详解
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True,
g: int = 1, e: float = 0.5, k: int = 3):
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
c1 |
int | - | 输入通道数 |
c2 |
int | - | 输出通道数 |
n |
int | 1 | Bottleneck 块重复次数 |
shortcut |
bool | True | 是否使用残差连接 |
g |
int | 1 | 分组卷积的分组数 |
e |
float | 0.5 | 通道扩展因子(hidden_channels = c2 * e) (注意:v11中e=0.25) |
k |
int | 3 | 核心参数:卷积核大小 |
关键实现解析
super().__init__(c1, c2, n, shortcut, g, e) # 调用父类初始化
c_ = int(c2 * e) # 隐藏通道计算
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0) for _ in range(n))
1. 父类初始化流程
继承
C3
的基础结构:输入卷积
cv1
分割-处理-合并结构
输出卷积
cv2
2. 核心改进:可配置卷积核
标准 Bottleneck:固定使用 (3,3) 卷积核
C3k Bottleneck:使用
(k, k)
卷积核灵活性:可在 1×1 到 7×7 之间自由配置
3. Bottleneck 结构定制
Bottleneck(c_, c_, shortcut, g, k=(k, k), e=1.0)
参数说明:
输入/输出通道:
c_
(隐藏通道)卷积核:
k=(k,k)
(自定义大小)扩展因子:
e=1.0
(无通道压缩)
设计优势分析
1. 感受野可调
小核 (k=1):局部特征提取,轻量计算
中核 (k=3):平衡感受野与计算量
大核 (k=5,7):扩大感受野,捕捉全局上下文
2. 任务适应性
任务类型 | 推荐k值 | 优势 |
---|---|---|
小目标检测 | 3-5 | 增强局部细节捕捉 |
大场景分割 | 5-7 | 扩大上下文感知 |
实时应用 | 1-3 | 最小化计算延迟 |
高精度模型 | 5-7 | 提升特征质量 |
结构图(C2与C3k)
C3模块
代码
class C3(nn.Module):
"""CSP Bottleneck with 3 convolutions."""
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
"""
Initialize the CSP Bottleneck with 3 convolutions.
Args:
c1 (int): Input channels.
c2 (int): Output channels.
n (int): Number of Bottleneck blocks.
shortcut (bool): Whether to use shortcut connections.
g (int): Groups for convolutions.
e (float): Expansion ratio.
"""
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=((1, 1), (3, 3)), e=1.0) for _ in range(n)))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass through the CSP bottleneck with 3 convolutions."""
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
解析
class C3(nn.Module):
"""CSP Bottleneck with 3 convolutions."""
"""
作用:定义 CSP 瓶颈模块类
核心思想:通过特征分割和部分跨阶段连接减少计算冗余
"""
def __init__(self, c1: int, c2: int, n: int = 1, shortcut: bool = True, g: int = 1, e: float = 0.5):
"""
Initialize the CSP Bottleneck with 3 convolutions.
Args:
c1 (int): Input channels.
c2 (int): Output channels.
n (int): Number of Bottleneck blocks.
shortcut (bool): Whether to use shortcut connections.
g (int): Groups for convolutions.
e (float): Expansion ratio.
"""
"""
参数说明:
c1:输入通道数
c2:输出通道数
n:Bottleneck 块重复次数(默认1)
shortcut:是否使用残差连接(默认是)
g:分组卷积的分组数(默认1)
e:通道扩展因子(默认0.5)
"""
super().__init__()
c_ = int(c2 * e) # hidden channels
"""
作用:计算隐藏层通道数
公式:c_ = c2 * e
示例:c2=128, e=0.5 → c_=64
"""
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)
"""
三卷积结构:
cv1:1×1 卷积,输入 c1 → 输出 c_
cv2:1×1 卷积,输入 c1 → 输出 c_
cv3:1×1 卷积,输入 2*c_ → 输出 c2
"""
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, k=((1, 1), (3, 3)), e=1.0) for _ in range(n)))
"""
Bottleneck 序列:
创建 n 个 Bottleneck 块
关键结构:每个 Bottleneck 包含:
1×1 卷积(降维)
3×3 卷积(特征提取)
参数:
输入/输出通道:c_
卷积核大小:k=((1,1),(3,3))
扩展因子:e=1.0(无压缩)
"""
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""Forward pass through the CSP bottleneck with 3 convolutions."""
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
"""
前向传播流程:
输入 x 同时送入两个分支:
分支1:cv1 → m(Bottleneck 序列)
分支2:cv2(直通路径)
拼接两个分支结果:torch.cat(..., dim=1)
通过 cv3 融合特征
"""
结构图
C2PSA模块
代码
class C2PSA(nn.Module):
"""
C2PSA module with attention mechanism for enhanced feature extraction and processing.
This module implements a convolutional block with attention mechanisms to enhance feature extraction and processing
capabilities. It includes a series of PSABlock modules for self-attention and feed-forward operations.
Attributes:
c (int): Number of hidden channels.
cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c.
m (nn.Sequential): Sequential container of PSABlock modules for attention and feed-forward operations.
Methods:
forward: Performs a forward pass through the C2PSA module, applying attention and feed-forward operations.
Notes:
This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.
Examples:
>>> c2psa = C2PSA(c1=256, c2=256, n=3, e=0.5)
>>> input_tensor = torch.randn(1, 256, 64, 64)
>>> output_tensor = c2psa(input_tensor)
"""
def __init__(self, c1: int, c2: int, n: int = 1, e: float = 0.5):
"""
Initialize C2PSA module.
Args:
c1 (int): Input channels.
c2 (int): Output channels.
n (int): Number of PSABlock modules.
e (float): Expansion ratio.
"""
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.m = nn.Sequential(*(PSABlock(self.c, attn_ratio=0.5, num_heads=self.c // 64) for _ in range(n)))
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Process the input tensor through a series of PSA blocks.
Args:
x (torch.Tensor): Input tensor.
Returns:
(torch.Tensor): Output tensor after processing.
"""
a, b = self.cv1(x).split((self.c, self.c), dim=1)
b = self.m(b)
return self.cv2(torch.cat((a, b), 1))
解析
C2PSA
模块(增强型注意力特征提取模块)
C2PSA
是一个结合了通道分割和自注意力机制的高级特征提取模块,旨在提升模型对重要特征的关注能力。
class C2PSA(nn.Module):
"""
C2PSA module with attention mechanism for enhanced feature extraction and processing.
This module implements a convolutional block with attention mechanisms to enhance feature extraction and processing
capabilities. It includes a series of PSABlock modules for self-attention and feed-forward operations.
Attributes:
c (int): Number of hidden channels.
cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c.
m (nn.Sequential): Sequential container of PSABlock modules for attention and feed-forward operations.
Methods:
forward: Performs a forward pass through the C2PSA module, applying attention and feed-forward operations.
Notes:
This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.
Examples:
>>> c2psa = C2PSA(c1=256, c2=256, n=3, e=0.5)
>>> input_tensor = torch.randn(1, 256, 64, 64)
>>> output_tensor = c2psa(input_tensor)
"""
"""
C2PSA 是一种具有注意力机制的模块,可增强特征提取和处理能力。
该模块实现了一个带有注意力机制的卷积块,以提升特征提取和处理能力。它包含多个 PSABlock 模块,用于自注意力和前馈操作。
属性:
- c (int): 隐藏通道数。
- cv1 (Conv): 1x1 卷积层,将输入通道数减少到 2c。
- cv2 (Conv): 1x1 卷积层,将输出通道数减少到 c。
- m (nn.Sequential): 包含 PSABlock 模块的序列容器,用于执行注意力和前馈操作。
方法:
- forward: 执行 C2PSA 模块的前向传播,应用注意力和前馈操作。
说明:
此模块与 PSA 模块基本相同,但经过重构以允许堆叠更多的 PSABlock 模块。
"""
def __init__(self, c1: int, c2: int, n: int = 1, e: float = 0.5):
"""
Initialize C2PSA module.
Args:
c1 (int): Input channels.
c2 (int): Output channels.
n (int): Number of PSABlock modules.
e (float): Expansion ratio.
"""
"""
核心功能:通过注意力机制增强特征提取能力
设计理念:结合通道分割和自注意力机制
创新点:可堆叠多个 PSABlock 模块
"""
super().__init__()
assert c1 == c2 # 输入输出通道必须相同
"""
确保模块不改变特征图通道数
设计目标:特征增强而非维度变换
"""
self.c = int(c1 * e) #隐藏通道数 #示例:c1=256, e=0.5 → c=128
self.cv1 = Conv(c1, 2 * self.c, 1, 1)
self.cv2 = Conv(2 * self.c, c1, 1)
self.m = nn.Sequential(*(PSABlock(self.c, attn_ratio=0.5, num_heads=self.c // 64) for _ in range(n)))
"""
PSABlock 参数:
self.c:输入通道数(注意力分支)
attn_ratio=0.5:注意力键值对压缩率
num_heads=self.c // 64:自适应头数计算
"""
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Process the input tensor through a series of PSA blocks.
Args:
x (torch.Tensor): Input tensor.
Returns:
(torch.Tensor): Output tensor after processing.
"""
a, b = self.cv1(x).split((self.c, self.c), dim=1) # 通道分割
b = self.m(b) # 注意力分支处理
return self.cv2(torch.cat((a, b), 1)) # 分支融合
通道操作卷积:
卷积层 | 功能 | 输入→输出 | 说明 |
---|---|---|---|
cv1 |
通道扩展 | c1 → 2*c |
分割为两个独立分支 |
cv2 |
通道压缩 | 2*c → c1 |
融合分支结果 |
设计优势分析
双路特征处理:
分支A:保留原始特征(信息完整性)
分支B:注意力增强特征(关键信息聚焦)
融合优势:兼顾基础特征与增强特征
自适应注意力头:
num_heads = self.c // 64 # 自动计算头数
示例:
c=128 → 128//64=2头
确保每头有足够通道维度(≥64)
注意力效率优化:
attn_ratio=0.5
压缩键值对维度减少注意力计算量约50%
公式:$ \text{FLOPs} \propto (\text{dim} \times \text{attn_ratio})^2 $
可扩展架构:
通过
n
参数堆叠多个 PSABlock深度增强特征提取能力
数值示例
假设输入:
c1=c2=256, n=3, e=0.5
特征图尺寸:64×64
计算过程:
隐藏通道:
c = 256*0.5 = 128
通道分割:
分支A:128通道(原始)
分支B:128通道(处理)
注意力处理:
PSABlock输入:128通道
注意力键值对:128*0.5=64通道
注意力头数:128//64=2
输出融合:128+128=256通道 → 压缩回256通道
结构图
PSABlock模块
代码
class PSABlock(nn.Module):
"""
PSABlock class implementing a Position-Sensitive Attention block for neural networks.
This class encapsulates the functionality for applying multi-head attention and feed-forward neural network layers
with optional shortcut connections.
Attributes:
attn (Attention): Multi-head attention module.
ffn (nn.Sequential): Feed-forward neural network module.
add (bool): Flag indicating whether to add shortcut connections.
Methods:
forward: Performs a forward pass through the PSABlock, applying attention and feed-forward layers.
Examples:
Create a PSABlock and perform a forward pass
>>> psablock = PSABlock(c=128, attn_ratio=0.5, num_heads=4, shortcut=True)
>>> input_tensor = torch.randn(1, 128, 32, 32)
>>> output_tensor = psablock(input_tensor)
"""
def __init__(self, c: int, attn_ratio: float = 0.5, num_heads: int = 4, shortcut: bool = True) -> None:
"""
Initialize the PSABlock.
Args:
c (int): Input and output channels.
attn_ratio (float): Attention ratio for key dimension.
num_heads (int): Number of attention heads.
shortcut (bool): Whether to use shortcut connections.
"""
super().__init__()
self.attn = Attention(c, attn_ratio=attn_ratio, num_heads=num_heads)
self.ffn = nn.Sequential(Conv(c, c * 2, 1), Conv(c * 2, c, 1, act=False))
self.add = shortcut
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Execute a forward pass through PSABlock.
Args:
x (torch.Tensor): Input tensor.
Returns:
(torch.Tensor): Output tensor after attention and feed-forward processing.
"""
x = x + self.attn(x) if self.add else self.attn(x)
x = x + self.ffn(x) if self.add else self.ffn(x)
return x
解析
PSABlock
模块(位置敏感注意力块)
PSABlock
是一个结合了多头自注意力和前馈网络的特征增强模块,专为卷积神经网络设计
核心功能:通过位置敏感注意力和前馈网络增强特征
设计理念:Transformer 思想在 CNN 中的高效实现 (个人:???????有吗,还不清楚)
创新点:空间位置感知的注意力机制
初始化方法详解
class PSABlock(nn.Module):
"""
PSABlock class implementing a Position-Sensitive Attention block for neural networks.
This class encapsulates the functionality for applying multi-head attention and feed-forward neural network layers
with optional shortcut connections.
Attributes:
attn (Attention): Multi-head attention module.
ffn (nn.Sequential): Feed-forward neural network module.
add (bool): Flag indicating whether to add shortcut connections.
Methods:
forward: Performs a forward pass through the PSABlock, applying attention and feed-forward layers.
Examples:
Create a PSABlock and perform a forward pass
>>> psablock = PSABlock(c=128, attn_ratio=0.5, num_heads=4, shortcut=True)
>>> input_tensor = torch.randn(1, 128, 32, 32)
>>> output_tensor = psablock(input_tensor)
"""
"""
PSABlock 是一种用于神经网络的 Position-Sensitive Attention(位置敏感注意力)模块。
该类封装了多头注意力机制和前馈神经网络层的功能,并可选择性地添加快捷连接。
属性:
attn (Attention):多头注意力模块。
ffn (nn.Sequential):前馈神经网络模块。
add (bool):指示是否添加快捷连接的标志。
方法:
forward:执行 PSABlock 的前向传播,应用注意力和前馈层。
示例:
创建一个 PSABlock 并执行前向传播。
"""
def __init__(self, c: int, attn_ratio: float = 0.5, num_heads: int = 4, shortcut: bool = True) -> None:
"""
Initialize the PSABlock.
Args:
c (int): Input and output channels.
attn_ratio (float): Attention ratio for key dimension.
num_heads (int): Number of attention heads.
shortcut (bool): Whether to use shortcut connections.
"""
super().__init__()
self.attn = Attention(c, attn_ratio=attn_ratio, num_heads=num_heads)
"""
注意力模块
输入通道:c
attn_ratio:键值对维度压缩率(默认0.5)
num_heads:多头注意力头数(默认4)
"""
self.ffn = nn.Sequential(Conv(c, c * 2, 1), Conv(c * 2, c, 1, act=False))
"""
前馈网络 (ffn):
经典"扩展-压缩"结构
无激活函数的最终层(保持残差连接兼容性)
"""
self.add = shortcut # 是否使用残差连接
"""
默认启用残差连接
可通过参数关闭
"""
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Execute a forward pass through PSABlock.
Args:
x (torch.Tensor): Input tensor.
Returns:
(torch.Tensor): Output tensor after attention and feed-forward processing.
"""
x = x + self.attn(x) if self.add else self.attn(x)
x = x + self.ffn(x) if self.add else self.ffn(x)
return x
数值示例
假设输入:
c=128, attn_ratio=0.5, num_heads=4
特征图尺寸:32×32
计算过程:
注意力模块:
键值对维度:128×0.5=64
每头维度:64//4=16
QKV总通道:64×3=192
位置编码:
输出通道:4(对应头数)
3×3卷积保持空间维度
FFN模块:
扩展:128→256
压缩:256→128
性能特点
操作 | 计算复杂度 | 参数量 | 特征增强效果 |
---|---|---|---|
标准卷积 | O(k²·c²) | 高 | 中等 |
PSABlock | O(h·d·(H·W)²) | 中等 | 高 |
全局注意力 | O((H·W)²·c) | 高 | 极高 |
结构图
Attention模块
代码
class Attention(nn.Module):
"""
Attention module that performs self-attention on the input tensor.
Args:
dim (int): The input tensor dimension.
num_heads (int): The number of attention heads.
attn_ratio (float): The ratio of the attention key dimension to the head dimension.
Attributes:
num_heads (int): The number of attention heads.
head_dim (int): The dimension of each attention head.
key_dim (int): The dimension of the attention key.
scale (float): The scaling factor for the attention scores.
qkv (Conv): Convolutional layer for computing the query, key, and value.
proj (Conv): Convolutional layer for projecting the attended values.
pe (Conv): Convolutional layer for positional encoding.
"""
def __init__(self, dim: int, num_heads: int = 8, attn_ratio: float = 0.5):
"""
Initialize multi-head attention module.
Args:
dim (int): Input dimension.
num_heads (int): Number of attention heads.
attn_ratio (float): Attention ratio for key dimension.
"""
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
nh_kd = self.key_dim * num_heads
h = dim + nh_kd * 2
self.qkv = Conv(dim, h, 1, act=False)
self.proj = Conv(dim, dim, 1, act=False)
self.pe = Conv(dim, dim, 3, 1, g=dim, act=False)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Forward pass of the Attention module.
Args:
x (torch.Tensor): The input tensor.
Returns:
(torch.Tensor): The output tensor after self-attention.
"""
B, C, H, W = x.shape
N = H * W
qkv = self.qkv(x)
q, k, v = qkv.view(B, self.num_heads, self.key_dim * 2 + self.head_dim, N).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, C, H, W) + self.pe(v.reshape(B, C, H, W))
x = self.proj(x)
return x
解析
Attention
模块(位置感知多头自注意力)
这个模块实现了空间感知的多头自注意力机制,专为视觉任务优化。
核心功能:实现位置敏感的自注意力机制
设计理念:将Transformer的自注意力适配到CNN特征图
创新点:显式位置编码与高效维度管理
class Attention(nn.Module):
"""
Attention module that performs self-attention on the input tensor.
Args:
dim (int): The input tensor dimension.
num_heads (int): The number of attention heads.
attn_ratio (float): The ratio of the attention key dimension to the head dimension.
Attributes:
num_heads (int): The number of attention heads.
head_dim (int): The dimension of each attention head.
key_dim (int): The dimension of the attention key.
scale (float): The scaling factor for the attention scores.
qkv (Conv): Convolutional layer for computing the query, key, and value.
proj (Conv): Convolutional layer for projecting the attended values.
pe (Conv): Convolutional layer for positional encoding.
"""
"""
对输入张量执行自注意力操作的注意力模块。
参数:
dim (int):输入张量的维度。
num_heads (int):注意力头的数量。
attn_ratio (float):注意力键维度与头维度的比率。
属性:
num_heads (int):注意力头的数量。
head_dim (int):每个注意力头的维度。
key_dim (int):注意力键的维度。
scale (float):注意力分数的缩放因子。
qkv (Conv):用于计算查询、键和值的卷积层。
proj (Conv):用于投影注意力值的卷积层。
pe (Conv):位置编码的卷积层。
"""
def __init__(self, dim: int, num_heads: int = 8, attn_ratio: float = 0.5):
"""
Initialize multi-head attention module.
Args:
dim (int): Input dimension.
num_heads (int): Number of attention heads.
attn_ratio (float): Attention ratio for key dimension.
"""
super().__init__()
# 1. 头数配置
self.num_heads = num_heads
self.head_dim = dim // num_heads
# 2. 键维度计算
self.key_dim = int(self.head_dim * attn_ratio)
self.scale = self.key_dim**-0.5 # 注意力分数缩放因子
# 3. QKV投影层
nh_kd = self.key_dim * num_heads
h = dim + nh_kd * 2 # QKV总维度
self.qkv = Conv(dim, h, 1, act=False)
# 4. 输出投影
self.proj = Conv(dim, dim, 1, act=False)
# 5. 位置编码
self.pe = Conv(dim, dim, 3, 1, g=dim, act=False)
关键参数说明:
头维度计算:
head_dim = dim // num_heads
:确保可整除示例:
dim=512, num_heads=8 → head_dim=64
键维度压缩:
key_dim = head_dim * attn_ratio
默认
attn_ratio=0.5
减少50%键维度目的:降低注意力计算复杂度
QKV投影:
总通道
h = dim + 2*(key_dim*num_heads)
分解:
dim
(查询) +nh_kd
(键) +nh_kd
(值)使用1×1卷积生成QKV
位置编码:
3×3深度可分离卷积 (
g=dim
)保持输入输出维度相同
捕获局部位置关系
def forward(self, x: torch.Tensor) -> torch.Tensor:
# 1. 获取输入形状
B, C, H, W = x.shape
N = H * W # 总空间位置数
# 2. 生成QKV
qkv = self.qkv(x) # [B, h, H, W]
# 3. 重塑并分割QKV
qkv = qkv.view(B, self.num_heads, self.key_dim*2+self.head_dim, N)
q, k, v = qkv.split([self.key_dim, self.key_dim, self.head_dim], dim=2)
# 4. 计算注意力分数
attn = (q.transpose(-2, -1) @ k) * self.scale
attn = attn.softmax(dim=-1)
# 5. 应用注意力权重
x = (v @ attn.transpose(-2, -1)).view(B, C, H, W)
# 6. 添加位置编码
x = x + self.pe(v.reshape(B, C, H, W))
# 7. 输出投影
return self.proj(x)
关键步骤详解
1. QKV生成与分割
源码
qkv = self.qkv(x)
q, k, v = qkv.view(B, self.num_heads, self.key_dim * 2 + self.head_dim, N).split(
[self.key_dim, self.key_dim, self.head_dim], dim=2
)
拆解
qkv = self.qkv(x) # [B, dim+2*num_heads*key_dim, H, W]
qkv = qkv.view(B, self.num_heads, 2*self.key_dim+self.head_dim, N)
q, k, v = split([key_dim, key_dim, head_dim], dim=2)
查询(Q):
[B, num_heads, key_dim, N]
键(K):
[B, num_heads, key_dim, N]
值(V):
[B, num_heads, head_dim, N]
维度差异:值使用完整头维度,键使用压缩维度
个人:没看到是什么意思??
2. 注意力分数计算
attn = (q.transpose(-2, -1) @ k) * self.scale
attn = attn.softmax(dim=-1)
矩阵乘法:$ \text{attn} = \text{Softmax}(\frac{Q^T K}{\sqrt{d_k}}) $
转置操作:
q.transpose(-2,-1)
交换空间和特征维度缩放因子:
scale = 1/sqrt(key_dim)
防止梯度消失
3. 注意力加权
x = (v @ attn.transpose(-2, -1)).view(B, C, H, W)
数学表示:$ \text{Output} = V \times \text{attn}^T $
维度变换:
[B, num_heads, head_dim, N] → [B, C, H, W]
特征重组:将多头输出拼接回原始通道维度
4. 位置编码融合
x = x + self.pe(v.reshape(B, C, H, W))
位置编码来源:基于值张量
v
的空间特征深度可分离卷积:3×3卷积独立处理每个通道
残差连接:增强位置信息,保持原始特征
设计优势分析
空间位置感知:
显式位置编码(3×3卷积)
捕获局部空间关系
增强平移不变性
高效注意力计算:
键维度压缩 (
attn_ratio
) 减少计算量示例:当
attn_ratio=0.5
时减少50%计算
维度优化策略:
组件 | 维度 | 压缩比 | 作用 |
---|---|---|---|
键(K) | key_dim |
attn_ratio |
降低注意力计算成本 |
值(V) | head_dim |
1.0 | 保持特征表达能力 |
查询(Q) | head_dim |
1.0 | 保持查询精度 |
多头并行:
独立学习不同的特征子空间
头数自动适配:num_heads = dim // 64
平衡并行效率与表示能力
数值示例
假设输入:
dim=256, num_heads=4, attn_ratio=0.5
特征图尺寸:32×32
计算过程:
头维度:
head_dim=256//4=64
键维度:
key_dim=64*0.5=32
QKV投影:
总通道:256 + 2*(4*32) = 256 + 256 = 512
分割:Q(128), K(128), V(256)
注意力矩阵:32×32=1024 位置 → 1024×1024 矩阵
计算节省:标准注意力FLOPs vs 本方案 ≈ 2:1
与传统注意力的对比
特性 | 标准自注意力 | 位置感知注意力 |
---|---|---|
位置编码 | 正弦/学习嵌入 | 卷积特征提取 |
计算复杂度 | O(N²·C) | O(N²·key_dim·H) |
空间感知 | 弱 | 强(局部上下文) |
参数量 | 高 | 优化(键维度压缩) |
实现难度 | 高 | 中(CNN兼容) |
结构图
head部分:
DWConv(深度可分离卷积模块)
代码
class DWConv(Conv):
"""Depth-wise convolution module."""
def __init__(self, c1, c2, k=1, s=1, d=1, act=True):
"""
Initialize depth-wise convolution with given parameters.
Args:
c1 (int): Number of input channels.
c2 (int): Number of output channels.
k (int): Kernel size.
s (int): Stride.
d (int): Dilation.
act (bool | nn.Module): Activation function.
"""
super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act)
解析
DWConv
是一个高效的深度可分离卷积模块,继承自标准的 Conv
类,专为轻量化神经网络设计。
参数详解
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
c1 |
int | - | 输入通道数 |
c2 |
int | - | 输出通道数 |
k |
int | 1 | 卷积核大小 |
s |
int | 1 | 卷积步长 |
d |
int | 1 | 空洞率(膨胀卷积) |
act |
bool/Module | True | 激活函数(支持自定义) |
核心创新点:分组数 g
的动态计算
g=math.gcd(c1, c2) # 计算输入输出通道的最大公约数
与传统深度卷积的区别
特性 | 传统深度卷积 | DWConv |
---|---|---|
分组数 | 固定 g=c1 |
动态 g=GCD(c1,c2) |
通道约束 | 要求 c2 是 c1 的倍数 |
无限制 |
灵活性 | 低 | 高 |
参数效率 | 中等 | 高 |
结构图
Detect
与 YOLOEDetect
的区别
这两个类都是 YOLO 目标检测模型的检测头,但 YOLOEDetect
在 Detect
的基础上进行了重大扩展,引入了文本引导的语义理解能力。
1. 核心功能差异
特性 | Detect | YOLOEDetect |
---|---|---|
基本功能 | 标准目标检测 | 文本引导的目标检测 |
语义理解 | 无 | 支持文本嵌入 |
多模态支持 | 纯视觉 | 视觉+文本融合 |
创新点 | 传统检测头 | 提示工程增强检测 |
2.结构图对比
3.性能特点(ai生成,不一定准确)
指标 | Detect | YOLOEDetect |
---|---|---|
推理速度 | 快 (基准) | 慢15-20% (文本融合) |
内存占用 | 低 | 高30-40% |
检测精度 (已知类别) | 高 | 相当 |
检测精度 (新类别) | 低 | 高50-70% |
模型灵活性 | 固定类别 | 动态类别支持 |
4.设计哲学差异
方面 | Detect | YOLOEDetect |
---|---|---|
核心目标 | 高效定位与分类 | 语义理解与开放识别 |
类别表示 | 静态one-hot向量 | 动态文本嵌入 |
扩展性 | 有限 | 支持多模态提示 |
创新方向 | 工程优化 | 认知智能 |
参考资料
1.deepseek
2.YOLOv11一文弄懂 | YOLOv11网络结构解读 、yolov11.yaml配置文件详细解读与说明、模型训练参数详细解析 | 通俗易懂!入门必看系列!-CSDN博客(一般,内容与v8一样,大多为复制)
3.(snu77)
(1)总目录
YOLOv11改进有效涨点专栏目录 | 含卷积、主干、注意力机制、Neck、检测头、损失函数、二次创新C2PSA/C3k2等各种网络结构改进-CSDN博客
(2)
YOLOv11 | 一文带你深入理解ultralytics最新作品yolov11的创新 | 训练、推理、验证、导出 (附网络结构图)-CSDN博客博主主要讲解了 v11与v8的改进,不同之处
主要改进点
1将c2f模块替换为c3k2模块
2在SPPF后添加C2PSA层
3将检测头内部的卷积换成深度可分离卷积
4参数变化(网络深度,宽度增加(原因:v11参数量减少,为了弥补精度,所以增加参数))
4.
【YOLOv11改进- 原理解析】 YOLO11 架构解析 以及代码库关键代码逐行解析_yolov11框架-CSDN博客
记录
yolo核心文件
个人
目前yaml文件解析到这里,后续还有其他工作要完成,后面边做边学,完成比完美更重要。