本文介绍
为提升 YOLOv8 框架对全局以来关系的捕捉能力,本文借鉴 CVPR2025 EfficientViM 所提出的EfficientViMBlock模块改进YOLOv8的C2f模块。 EfficientViM基于状态空间模型(SSM)设计了新颖的HSM-SSD结构,从而实现在保证计算效率的前提下高效捕捉全局依赖关系。具体来说,HSM-SSD通过对压缩后的隐藏状态执行通道混合,再配合所提出的多阶段隐藏状态融合策略,获得了较优的推理吞吐量和模型精度。实验结果如下(本文通过VOC数据验证算法性能,epoch为100,batchsize为32,imagesize为640*640):
Model | mAP50-95 | mAP50 | run time (h) | params (M) | interence time (ms) |
---|---|---|---|---|---|
YOLOv8 | 0.549 | 0.760 | 1.051 | 3.01 | 0.2+0.3(postprocess) |
YOLO11 | 0.553 | 0.757 | 1.142 | 2.59 | 0.2+0.3(postprocess) |
yolov8_C2f-EfficientViM | 0.521 | 0.740 | 1.081 | 2.81 | 0.2+0.3(postprocess) |
重要声明:本文改进后代码可能只是并不适用于我所使用的数据集,对于其他数据集可能存在有效性。
本文改进是为了降低最新研究进展至YOLO的代码迁移难度,从而为对最新研究感兴趣的同学提供参考。
代码迁移
重点内容
步骤一:迁移代码
ultralytics框架的模块代码主要放在ultralytics/nn
文件夹下,此处为了与官方代码进行区分,可以新增一个extra_modules
文件夹,然后将我们的代码添加进入。
具体代码如下:
import torch
import torch.nn as nn
__all__ = ['EfficientViMBlock']
class LayerNorm1D(nn.Module):
"""LayerNorm for channels of 1D tensor(B C L)"""
def __init__(self, num_channels, eps=1e-5, affine=True):
super(LayerNorm1D, self).__init__()
self.num_channels = num_channels
self.eps = eps
self.affine = affine
if self.affine:
self.weight = nn.Parameter(torch.ones(1, num_channels, 1))
self.bias = nn.Parameter(torch.zeros(1, num_channels, 1))
else:
self.register_parameter('weight', None)
self.register_parameter('bias', None)
def forward(self, x):
mean = x.mean(dim=1, keepdim=True) # (B, 1, H, W)
var = x.var(dim=1, keepdim=True, unbiased=False) # (B, 1, H, W)
x_normalized = (x - mean) / torch.sqrt(var + self.eps) # (B, C, H, W)
if self.affine:
x_normalized = x_normalized * self.weight + self.bias
return x_normalized
class ConvLayer2D(nn.Module):
def __init__(self, in_dim, out_dim, kernel_size=3, stride=1, padding=0, dilation=1, groups=1, norm=nn.BatchNorm2d, act_layer=nn.ReLU, bn_weight_init=1):
super(ConvLayer2D, self).__init__()
self.conv = nn.Conv2d(
in_dim,
out_dim,
kernel_size=(kernel_size, kernel_size),
stride=(stride, stride),
padding=(padding, padding),
dilation=(dilation, dilation),
groups=groups,
bias=False
)
self.norm = norm(num_features=out_dim) if norm else None
self.act = act_layer() if act_layer else None
if self.norm:
torch.nn.init.constant_(self.norm.weight, bn_weight_init)
torch.nn.init.constant_(self.norm.bias, 0)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.conv(x)
if self.norm:
x = self.norm(x)
if self.act:
x = self.act(x)
return x
class ConvLayer1D(nn.Module):
def __init__(self, in_dim, out_dim, kernel_size=3, stride=1, padding=0, dilation=1, groups=1, norm=nn.BatchNorm1d, act_layer=nn.ReLU, bn_weight_init=1):
super(ConvLayer1D, self).__init__()
self.conv = nn.Conv1d(
in_dim,
out_dim,
kernel_size=kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups,
bias=False
)
self.norm = norm(num_features=out_dim) if norm else None
self.act = act_layer() if act_layer else None
if self.norm:
torch.nn.init.constant_(self.norm.weight, bn_weight_init)
torch.nn.init.constant_(self.norm.bias, 0)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.conv(x)
if self.norm:
x = self.norm(x)
if self.act:
x = self.act(x)
return x
class FFN(nn.Module):
def __init__(self, in_dim, dim):
super().__init__()
self.fc1 = ConvLayer2D(in_dim, dim, 1)
self.fc2 = ConvLayer2D(dim, in_dim, 1, act_layer=None, bn_weight_init=0)
def forward(self, x):
x = self.fc2(self.fc1(x))
return x
class HSMSSD(nn.Module):
def __init__(self, d_model, ssd_expand=1, A_init_range=(1, 16), state_dim = 64):
super().__init__()
self.ssd_expand = ssd_expand
self.d_inner = int(self.ssd_expand * d_model)
self.state_dim = state_dim
self.BCdt_proj = ConvLayer1D(d_model, 3*state_dim, 1, norm=None, act_layer=None)
conv_dim = self.state_dim*3
self.dw = ConvLayer2D(conv_dim, conv_dim, 3,1,1, groups=conv_dim, norm=None, act_layer=None, bn_weight_init=0)
self.hz_proj = ConvLayer1D(d_model, 2*self.d_inner, 1, norm=None, act_layer=None)
self.out_proj = ConvLayer1D(self.d_inner, d_model, 1, norm=None, act_layer=None, bn_weight_init=0)
A = torch.empty(self.state_dim, dtype=torch.float32).uniform_(*A_init_range)
self.A = torch.nn.Parameter(A)
self.act = nn.SiLU()
self.D = nn.Parameter(torch.ones(1))
self.D._no_weight_decay = True
def forward(self, x, H, W):
batch, _, L= x.shape
# H = int(math.sqrt(L))
BCdt = self.dw(self.BCdt_proj(x).view(batch,-1, H, W)).flatten(2)
B,C,dt = torch.split(BCdt, [self.state_dim, self.state_dim, self.state_dim], dim=1)
A = (dt + self.A.view(1,-1,1)).softmax(-1)
AB = (A * B)
h = x @ AB.transpose(-2,-1)
h, z = torch.split(self.hz_proj(h), [self.d_inner, self.d_inner], dim=1)
h = self.out_proj(h * self.act(z.clone())+ h * self.D)
y = h @ C # B C N, B C L -> B C L
y = y.view(batch,-1,H,W).contiguous()# + x * self.D # B C H W
return y, h
class EfficientViMBlock(nn.Module):
def __init__(self, dim, mlp_ratio=4., ssd_expand=1, state_dim=32):
super().__init__()
self.dim = dim
self.mlp_ratio = mlp_ratio
self.mixer = HSMSSD(d_model=dim, ssd_expand=ssd_expand,state_dim=state_dim)
self.norm = LayerNorm1D(dim)
self.dwconv1 = ConvLayer2D(dim, dim, 3, padding=1, groups=dim, bn_weight_init=0, act_layer = None)
self.dwconv2 = ConvLayer2D(dim, dim, 3, padding=1, groups=dim, bn_weight_init=0, act_layer = None)
self.ffn = FFN(in_dim=dim, dim=int(dim * mlp_ratio))
#LayerScale
self.alpha = nn.Parameter(1e-4 * torch.ones(4,dim), requires_grad=True)
def forward(self, x):
alpha = torch.sigmoid(self.alpha).view(4,-1,1,1)
# DWconv1
x = (1-alpha[0]) * x + alpha[0] * self.dwconv1(x)
# HSM-SSD
x_prev = x
x, h = self.mixer(self.norm(x.flatten(2)), *(x_prev.shape[2:]))
x = (1-alpha[1]) * x_prev + alpha[1] * x
# DWConv2
x = (1-alpha[2]) * x + alpha[2] * self.dwconv2(x)
# FFN
x = (1-alpha[3]) * x + alpha[3] * self.ffn(x)
return x
if __name__ == "__main__":
inputs = torch.randn(1, 3, 224, 224)
downsample = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1)
efficientvim = EfficientViMBlock(64)
outputs = efficientvim(downsample(inputs))
print(outputs.shape)
步骤二:创建模块并导入
为了与之前所定义的C2f改进模块对齐,本文通过对上述代码简单改写,实现下面内容。此时需要在当前目录新建一个block.py
文件用以统一管理自定义的C2f模块(当然也可以直接在ultralytics/nn/modules/block.py
中直接添加)。内容如下:
import torch
import torch.nn as nn
from ..modules import C2f
from .efficientvim import EfficientViMBlock
class C2f_EfficientViM(C2f):
def __init__(self, c1, c2, n = 1, shortcut = False, g = 1, e = 0.5):
super().__init__(c1, c2, n, shortcut, g, e)
self.m = nn.ModuleList(EfficientViMBlock(self.c) for _ in range(n))
添加完成之后需要新增一个__init__.py
文件,将添加的模块导入到__init__.py
文件中,这样在调用的时候就可以直接使用from extra_modules import *
。__init__.py
文件需要撰写以下内容:
from .block import C2f_EfficientViM
具体目录结构如下图所示:
nn/
└── extra_modules/
├── __init__.py
├── block.py
└── efficientvim.py
步骤三:修改tasks.py
文件
首先在tasks.py
文件中添加以下内容:
from ultralytics.nn.extra_modules import *
然后找到parse_model()
函数,在函数查找如下内容:
if m in base_modules:
c1, c2 = ch[f], args[0]
if c2 != nc: # if c2 not equal to number of classes (i.e. for Classify() output)
c2 = make_divisible(min(c2, max_channels) * width, 8)
使用较老ultralytics版本的同学,此处可能不是
base_modules
,而是相关的模块的字典集合,此时直接添加到集合即可;若不是就找到base_modules
所指向的集合进行添加,添加方式如下:
base_modules = frozenset(
{
Classify, Conv, ConvTranspose, GhostConv, Bottleneck, GhostBottleneck,
SPP, SPPF, C2fPSA, C2PSA, DWConv, Focus, BottleneckCSP, C1, C2, C2f, C3k2,
RepNCSPELAN4, ELAN1, ADown, AConv, SPPELAN, C2fAttn, C3, C3TR, C3Ghost,
torch.nn.ConvTranspose2d, DWConvTranspose2d, C3x, RepC3, PSA, SCDown, C2fCIB,
A2C2f,
# 自定义模块
C2f_EfficientViM,
}
)
其次找到parse_model()
函数,在函数查找如下内容:
if m in repeat_modules:
args.insert(2, n) # number of repeats
n = 1
与base_modules
同理,具体添加方式如下:
repeat_modules = frozenset( # modules with 'repeat' arguments
{
BottleneckCSP, C1, C2, C2f, C3k2, C2fAttn, C3, C3TR, C3Ghost, C3x, RepC3,
C2fPSA, C2fCIB, C2PSA, A2C2f,
# 自定义模块
C2f_EfficientViM,
}
)
步骤四:修改配置文件
在相应位置添加如下代码即可。
# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.33, 0.25, 1024] # YOLOv8n summary: 129 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPS
s: [0.33, 0.50, 1024] # YOLOv8s summary: 129 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPS
m: [0.67, 0.75, 768] # YOLOv8m summary: 169 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPS
l: [1.00, 1.00, 512] # YOLOv8l summary: 209 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPS
x: [1.00, 1.25, 512] # YOLOv8x summary: 209 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPS
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, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 6, C2f_EfficientViM, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 3, C2f_EfficientViM, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
# YOLOv8.0n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 3, C2f, [512]] # 12
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-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)
- [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)