本文介绍
为提升 YOLOv8 在目标检测任务中的特征表达能力,本文借鉴 PRCV2024(Oral) MAF-YOLO 所提出的尺度融合方式MAFPN改进YOLOv8的Neck部分。 在MAFPN中,设计了表面辅助融合(SAF)模块,以将Backbone网络的输出与Neck网络相结合,保留适量的浅层信息以促进后续学习。同时,高级辅助融合(AAF)模块深植于Neck网络内,向输出层传递更为丰富的梯度信息。 实验结果如下(本文通过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_MAFPN-C2f | 0.560 | 0.770 | 1.133 | 2.99 | 0.3+0.3(postprocess) |
重要声明:本文改进后代码可能只是并不适用于我所使用的数据集,对于其他数据集可能存在有效性。
本文改进是为了降低最新研究进展至YOLO的代码迁移难度,从而为对最新研究感兴趣的同学提供参考。
代码迁移
重点内容
步骤一:迁移代码
ultralytics框架的模块代码主要放在ultralytics/nn
文件夹下,此处为了与官方代码进行区分,可以新增一个extra_modules
文件夹,然后将我们的代码添加进入。
具体代码如下:
import torch
import torch.nn as nn
import torch.nn.functional as F
from itertools import repeat
import collections.abc
# from ..modules import Conv
class Conv(nn.Module):
'''Normal Conv with SiLU activation'''
def __init__(self, in_channels, out_channels, kernel_size = 1, stride = 1, groups=1, bias=False):
super().__init__()
padding = kernel_size // 2
self.conv = nn.Conv2d(
in_channels,
out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
groups=groups,
bias=bias,
)
self.bn = nn.BatchNorm2d(out_channels)
self.act = nn.SiLU()
def forward(self, x):
return self.act(self.bn(self.conv(x)))
def forward_fuse(self, x):
return self.act(self.conv(x))
def _ntuple(n):
def parse(x):
if isinstance(x, collections.abc.Iterable) and not isinstance(x, str):
return tuple(x)
return tuple(repeat(x, n))
return parse
to_1tuple = _ntuple(1)
to_2tuple = _ntuple(2)
to_3tuple = _ntuple(3)
to_4tuple = _ntuple(4)
to_ntuple = _ntuple
def get_conv2d_uni(in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias,
attempt_use_lk_impl=True):
kernel_size = to_2tuple(kernel_size)
if padding is None:
padding = (kernel_size[0] // 2, kernel_size[1] // 2)
else:
padding = to_2tuple(padding)
need_large_impl = kernel_size[0] == kernel_size[1] and kernel_size[0] > 5 and padding == (kernel_size[0] // 2, kernel_size[1] // 2)
# if attempt_use_lk_impl and need_large_impl:
# print('---------------- trying to import iGEMM implementation for large-kernel conv')
# try:
# from depthwise_conv2d_implicit_gemm import DepthWiseConv2dImplicitGEMM
# print('---------------- found iGEMM implementation ')
# except:
# DepthWiseConv2dImplicitGEMM = None
# print('---------------- found no iGEMM. use original conv. follow https://github.com/AILab-CVC/UniRepLKNet to install it.')
# if DepthWiseConv2dImplicitGEMM is not None and need_large_impl and in_channels == out_channels \
# and out_channels == groups and stride == 1 and dilation == 1:
# print(f'===== iGEMM Efficient Conv Impl, channels {in_channels}, kernel size {kernel_size} =====')
# return DepthWiseConv2dImplicitGEMM(in_channels, kernel_size, bias=bias)
return nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, groups=groups, bias=bias)
def get_bn(channels):
return nn.BatchNorm2d(channels)
def fuse_bn(conv, bn):
kernel = conv.weight
running_mean = bn.running_mean
running_var = bn.running_var
gamma = bn.weight
beta = bn.bias
eps = bn.eps
std = (running_var + eps).sqrt()
t = (gamma / std).reshape(-1, 1, 1, 1)
return kernel * t, beta - running_mean * gamma / std
def convert_dilated_to_nondilated(kernel, dilate_rate):
identity_kernel = torch.ones((1, 1, 1, 1), dtype=kernel.dtype, device =kernel.device )
if kernel.size(1) == 1:
# This is a DW kernel
dilated = F.conv_transpose2d(kernel, identity_kernel, stride=dilate_rate)
return dilated
else:
# This is a dense or group-wise (but not DW) kernel
slices = []
for i in range(kernel.size(1)):
dilated = F.conv_transpose2d(kernel[:,i:i+1,:,:], identity_kernel, stride=dilate_rate)
slices.append(dilated)
return torch.cat(slices, dim=1)
def merge_dilated_into_large_kernel(large_kernel, dilated_kernel, dilated_r):
large_k = large_kernel.size(2)
dilated_k = dilated_kernel.size(2)
equivalent_kernel_size = dilated_r * (dilated_k - 1) + 1
equivalent_kernel = convert_dilated_to_nondilated(dilated_kernel, dilated_r)
rows_to_pad = large_k // 2 - equivalent_kernel_size // 2
merged_kernel = large_kernel + F.pad(equivalent_kernel, [rows_to_pad] * 4)
return merged_kernel
class DilatedReparamBlock(nn.Module):
"""
Dilated Reparam Block proposed in UniRepLKNet (https://github.com/AILab-CVC/UniRepLKNet)
We assume the inputs to this block are (N, C, H, W)
"""
def __init__(self, channels, kernel_size, deploy, use_sync_bn=False, attempt_use_lk_impl=True):
super().__init__()
self.lk_origin = get_conv2d_uni(channels, channels, kernel_size, stride=1,
padding=kernel_size//2, dilation=1, groups=channels, bias=deploy,
)
self.attempt_use_lk_impl = attempt_use_lk_impl
# Default settings. We did not tune them carefully. Different settings may work better.
# if kernel_size == 17:
# self.kernel_sizes = [5, 9, 3, 3, 3]
# self.dilates = [1, 2, 4, 5, 7]
# elif kernel_size == 15:
# self.kernel_sizes = [5, 7, 3, 3, 3]
# self.dilates = [1, 2, 3, 5, 7]
# elif kernel_size == 13:
# self.kernel_sizes = [5, 7, 3, 3, 3]
# self.dilates = [1, 2, 3, 4, 5]
# elif kernel_size == 11:
# self.kernel_sizes = [5, 5, 3, 3, 3]
# self.dilates = [1, 2, 3, 4, 5]
# elif kernel_size == 9:
# self.kernel_sizes = [5, 5, 3, 3]
# self.dilates = [1, 2, 3, 4]
# elif kernel_size == 7:
# self.kernel_sizes = [5, 3, 3, 3]
# self.dilates = [1, 1, 2, 3]
# elif kernel_size == 5:
# self.kernel_sizes = [3, 3, 1]
# self.dilates = [1, 2, 1]
# elif kernel_size == 3:
# self.kernel_sizes = [3, 1]
# self.dilates = [1, 1]
if kernel_size == 17:
self.kernel_sizes = [5, 9, 3, 3, 3]
self.dilates = [1, 2, 4, 5, 7]
elif kernel_size == 15:
self.kernel_sizes = [5, 7, 3, 3, 3]
self.dilates = [1, 2, 3, 5, 7]
elif kernel_size == 13:
self.kernel_sizes = [5, 7, 3, 3, 3]
self.dilates = [1, 2, 3, 4, 5]
elif kernel_size == 11:
self.kernel_sizes = [5, 5, 3, 3, 3]
self.dilates = [1, 2, 3, 4, 5]
elif kernel_size == 9:
self.kernel_sizes = [7, 5, 3]
self.dilates = [1, 1, 1]
elif kernel_size == 7:
self.kernel_sizes = [5, 3]
self.dilates = [1, 1]
elif kernel_size == 5:
self.kernel_sizes = [3, 1]
self.dilates = [1, 1]
elif kernel_size == 3:
self.kernel_sizes = [3, 1]
self.dilates = [1, 1]
else:
raise ValueError('Dilated Reparam Block requires kernel_size >= 5')
if not deploy:
self.origin_bn = get_bn(channels)
for k, r in zip(self.kernel_sizes, self.dilates):
self.__setattr__('dil_conv_k{}_{}'.format(k, r),
nn.Conv2d(in_channels=channels, out_channels=channels, kernel_size=k, stride=1,
padding=(r * (k - 1) + 1) // 2, dilation=r, groups=channels,
bias=False))
self.__setattr__('dil_bn_k{}_{}'.format(k, r), get_bn(channels))
def forward(self, x):
if not hasattr(self, 'origin_bn'): # deploy mode
return self.lk_origin(x)
out = self.origin_bn(self.lk_origin(x))
for k, r in zip(self.kernel_sizes, self.dilates):
conv = self.__getattr__('dil_conv_k{}_{}'.format(k, r))
bn = self.__getattr__('dil_bn_k{}_{}'.format(k, r))
out = out + bn(conv(x))
return out
def merge_dilated_branches(self):
if hasattr(self, 'origin_bn'):
origin_k, origin_b = fuse_bn(self.lk_origin, self.origin_bn)
for k, r in zip(self.kernel_sizes, self.dilates):
conv = self.__getattr__('dil_conv_k{}_{}'.format(k, r))
bn = self.__getattr__('dil_bn_k{}_{}'.format(k, r))
branch_k, branch_b = fuse_bn(conv, bn)
origin_k = merge_dilated_into_large_kernel(origin_k, branch_k, r)
origin_b += branch_b
merged_conv = get_conv2d_uni(origin_k.size(0), origin_k.size(0), origin_k.size(2), stride=1,
padding=origin_k.size(2)//2, dilation=1, groups=origin_k.size(0), bias=True,
attempt_use_lk_impl=self.attempt_use_lk_impl)
merged_conv.weight.data = origin_k
merged_conv.bias.data = origin_b
self.lk_origin = merged_conv
self.__delattr__('origin_bn')
for k, r in zip(self.kernel_sizes, self.dilates):
self.__delattr__('dil_conv_k{}_{}'.format(k, r))
self.__delattr__('dil_bn_k{}_{}'.format(k, r))
class UniRepLKNetBlock(nn.Module):
def __init__(self,
dim,
kernel_size,
deploy=False,
attempt_use_lk_impl=True):
super().__init__()
if deploy:
print('------------------------------- Note: deploy mode')
if kernel_size == 0:
self.dwconv = nn.Identity()
elif kernel_size >= 3:
self.dwconv = DilatedReparamBlock(dim, kernel_size, deploy=deploy,
attempt_use_lk_impl=attempt_use_lk_impl)
else:
assert kernel_size in [3]
self.dwconv = get_conv2d_uni(dim, dim, kernel_size=kernel_size, stride=1, padding=kernel_size // 2,
dilation=1, groups=dim, bias=deploy,
attempt_use_lk_impl=attempt_use_lk_impl)
if deploy or kernel_size == 0:
self.norm = nn.Identity()
else:
self.norm = get_bn(dim)
def forward(self, inputs):
out = self.norm(self.dwconv(inputs))
return out
class DepthBottleneckUni(nn.Module):
def __init__(self,
in_channels,
out_channels,
shortcut=True,
kersize = 5,
expansion_depth = 1,
small_kersize = 3,
use_depthwise=True):
super(DepthBottleneckUni, self).__init__()
mid_channel = int(in_channels * expansion_depth)
self.conv1 = Conv(in_channels, mid_channel, 1)
self.shortcut = shortcut
if use_depthwise:
self.conv2 = UniRepLKNetBlock(mid_channel, kernel_size=kersize)
self.act = nn.SiLU()
self.one_conv = Conv(mid_channel,out_channels,kernel_size = 1)
else:
self.conv2 = Conv(out_channels, out_channels, 3, 1)
def forward(self, x):
y = self.conv1(x)
y = self.act(self.conv2(y))
# y = self.conv2(y)
y = self.one_conv(y)
return y
class RepHDW(nn.Module):
def __init__(self, in_channels, out_channels, depth=1, shortcut = True, expansion = 0.5, kersize = 5,depth_expansion = 1,small_kersize = 3,use_depthwise = True):
super(RepHDW, self).__init__()
c1 = int(out_channels * expansion) * 2
c_ = int(out_channels * expansion)
self.c_ = c_
self.conv1 = Conv(in_channels, c1, 1, 1)
self.m = nn.ModuleList(DepthBottleneckUni(self.c_, self.c_, shortcut,kersize,depth_expansion,small_kersize,use_depthwise) for _ in range(depth))
self.conv2 = Conv(c_ * (depth+2), out_channels, 1, 1)
def forward(self,x):
x = self.conv1(x)
x_out = list(x.split((self.c_, self.c_), 1))
for conv in self.m:
y = conv(x_out[-1])
x_out.append(y)
y_out = torch.cat(x_out, axis=1)
y_out = self.conv2(y_out)
return y_out
步骤二:创建模块并导入
此时需要在当前目录新增一个__init__.py
文件,将添加的模块导入到__init__.py
文件中,这样在调用的时候就可以直接使用from extra_modules import *
。__init__.py
文件需要撰写以下内容:
from .maf_yolo import RepHDW
具体目录结构如下图所示:
nn/
└── extra_modules/
├── __init__.py
└── maf_yolo.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,
# 自定义模块
RepHDW,
}
)
其次找到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,
# 自定义模块
RepHDW,
}
)
步骤四:修改配置文件
在相应位置添加如下代码即可。
# 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: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
m: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPs
l: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
x: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 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, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 6, C2f, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 3, C2f, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
# YOLOv8.0n head
head:
- [6, 1, Conv, [256, 3, 2]] # 10-P5/32
- [[-1, 9], 1, Concat, [1]] # 11
- [-1, 1, RepHDW, [512, False, 0.5, 9, 3]] # 12-P5/32
- [-1, 1, nn.Upsample, [None, 2, "nearest"]] # 13-P4/16
- [4, 1, Conv, [128, 3, 2]] # 14-P4/16
- [[-1, -2, 6], 1, Concat, [1]] # 15
- [-1, 1, RepHDW, [512, False, 0.5, 7, 3]] # 16-P4/16
- [-1, 1, nn.Upsample, [None, 2, "nearest"]] # 17-P3/8
- [2, 1, Conv, [64, 3, 2]] # 18-P3/8
- [[-1, -2, 4], 1, Concat, [1]] # 19
- [-1, 1, RepHDW, [256, False, 0.5, 5, 3]] # 20-P3/8
- [[17, -1], 1, Concat, [1]] # 21
- [-1, 1, RepHDW, [256, False, 0.5, 5, 3]] # 22-P3/8
- [20, 1, Conv, [256, 3, 2]] # 23-P4/16
- [22, 1, Conv, [256, 3, 2]] # 24-P4/16
- [[-1, -2, 16, 13], 1, Concat, [1]] # 25-P4/16
- [-1, 1, RepHDW, [512, False, 0.5, 7, 3]] # 26-P4/16
- [16, 1, Conv, [512, 3, 2]] # 27-P5/32
- [26, 1, Conv, [512, 3, 2]] # 28-P5/32
- [[-1, -2, 12], 1, Concat, [1]] # 29-P5/32
- [-1, 1, RepHDW, [512, False, 0.5, 9, 3]] # 30-P5/32
- [[22, 26, 30], 1, Detect, [nc]] # Detect(P3, P4, P5)