### YOLOv11 改进
YOLOv11引入了一系列重要的架构改进,旨在提升模型的速度和准确性。这些改进包括但不限于更高效的特征提取模块、优化的颈部结构以及更强壮的头部设计[^1]。
#### C3K2 结构
C3K2 是一种新型瓶颈层设计,在YOLOv11中得到了应用。该结构通过组合卷积操作与跨阶段部分连接(Cross Stage Partial Connections),实现了参数量的有效减少的同时保持甚至提升了性能表现。具体来说,C3K2 使用两个分支来处理输入数据流:一条路径执行标准卷积运算;另一条则采用轻量化策略,仅保留关键通道的信息传递。最终两条路径的结果会被融合起来形成输出。
```python
class C3K2(nn.Module):
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
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) # act=FReLU(c2)
self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])
def forward(self, x):
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))
```
#### MobileNetV4 架构及其增强
MobileNetV4 继承并发展了前代版本的核心理念——即利用深度可分离卷积降低计算成本。在此基础上进行了多项创新:
- **新的激活函数**:采用了更加灵活有效的激活机制。
- **自适应分辨率调整**:根据不同场景需求动态改变网络内部各层的感受野大小。
- **渐进式学习框架**:允许模型逐步增加复杂度以更好地捕捉不同尺度的目标对象特性。
```python
import torch.nn as nn
def make_divisible(v, divisor=8, min_value=None):
if min_value is None:
min_value = divisor
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_v < 0.9 * v:
new_v += divisor
return new_v
class SEBlock(nn.Module):
"""Squeeze-and-Excitation block with Swish."""
def __init__(self, channel, reduction=16):
super(SEBlock, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, make_divisible(channel // reduction)),
SiLU(),
nn.Linear(make_divisible(channel // reduction), channel),
Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
# Example usage within a larger model definition...
```