现在利用YOLOv8作为基础模型并在其上进行改进是比较热门的一个放论文的方向,但随着相关文章数量的增多,期刊对于改进方法的创新度要求也逐渐增高,简单的添加模块发论文也变得比较困难,本文将YOLOv8中的C2f模块与EMA注意力机制结合形成新的模块(本文命名为C2f_EMA,也可用其他名字),可以提高改进的创新度。
下面是EMA注意力机制的论文链接:
https://arxiv.org/abs/2305.13563v1
EMA注意力机制
在各种计算机视觉任务中,通道或空间注意机制对于产生更多可识别的特征表示具有显着的有效性。然而,通过通道降维来建模跨通道关系可能会对提取深度视觉表征带来副作用。
文章提出了一种新型的高效多尺度注意力(EMA)模块。为了保留每个通道上的信息和减少计算开销,将部分通道重构为批处理维度,并将通道维度分组为多个子特征,使空间语义特征在每个特征组内均匀分布。
总结一下相比以前的注意力机制最大的改进就是在将通道分成多个子通道的同时保持张量的大小不变,即将部分通道重构为批处理维度,例如原来的张量维度为[B,C,H,W],其中B为批处理 大小,C为通道数,H为高度,W为宽度,重构之后变为[B*G,C//G,H,W],张量大小均为B*C*H*W,从而减少了信息的丢失。
需要查看代码的朋友可以点击这里:https://github.com/YOLOonMe/EMA-attention-module
结合C2f模块
本文将EMA注意力机制加入到到C2f模块中的第一个卷积层之后,对输入特征权重进行动态分配,根据输入的实际内容来决定关注的重点,在引入少量的计算开销下提高模型的精度和泛化性。
下图分别为改进前后的模块结构:
改进后的代码如下:
import torch
from torch import nn
def autopad(k, p=None, d=1): # kernel, padding, dilation
"""Pad to 'same' shape outputs."""
if d > 1:
k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k] # actual kernel-size
if p is None:
p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
return p
class Bottleneck(nn.Module):
"""Standard bottleneck."""
def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5):
"""Initializes a bottleneck module with given input/output channels, shortcut option, group, kernels, and
expansion.
"""
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):
"""'forward()' applies the YOLO FPN to input data."""
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
class Conv(nn.Module):
"""Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)."""
default_act = nn.SiLU() # default activation
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
"""Initialize Conv layer with given arguments including activation."""
super().__init__()
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
self.bn = nn.BatchNorm2d(c2)
self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
def forward(self, x):
"""Apply convolution, batch normalization and activation to input tensor."""
return self.act(self.bn(self.conv(x)))
def forward_fuse(self, x):
"""Perform transposed convolution of 2D data."""
return self.act(self.conv(x))
class EMA(nn.Module):
def __init__(self, channels, factor=8):
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, kernel_size=1, stride=1, padding=0)
self.conv3x3 = nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size=3, stride=1, padding=1)
def forward(self, x):
b, c, h, w = x.size()
group_x = x.reshape(b * self.groups, -1, h, w) # b*g,c//g,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) # b*g, c//g, hw
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) # b*g, c//g, hw
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)
# class C2f_EMA(nn.Module):
# """Faster Implementation of CSP Bottleneck with 2 convolutions."""
#
# def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5):
# """Initialize CSP bottleneck layer with two convolutions with arguments ch_in, ch_out, number, shortcut, groups,
# expansion.
# """
# super().__init__()
# self.c = int(c2 * e) # hidden channels
# self.cv1 = Conv(c1, 2 * self.c, 1, 1)
# self.EMA = EMA(self.c, factor=8)
# self.cv2 = Conv((2 + n) * self.c, c2, 1) # optional act=FReLU(c2)
# self.m = nn.ModuleList(Bottleneck(self.c, self.c, shortcut, g, k=((3, 3), (3, 3)), e=1.0) for _ in range(n))
#
# def forward(self, x):
# """Forward pass through C2f layer."""
# y = list(self.cv1(x).chunk(2, 1))
# y[-1] = self.EMA(y[-1])
# y.extend(m(y[-1]) for m in self.m)
# return self.cv2(torch.cat(y, 1))
#
# def forward_split(self, x):
# """Forward pass using split() instead of chunk()."""
# y = list(self.cv1(x).split((self.c, self.c), 1))
# y[-1] = self.EMA(y[-1])
# y.extend(m(y[-1]) for m in self.m)
# return self.cv2(torch.cat(y, 1))
class C2f_EMA(nn.Module):
"""Faster Implementation of CSP Bottleneck with 2 convolutions."""
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5):
"""Initialize CSP bottleneck layer with two convolutions with arguments ch_in, ch_out, number, shortcut, groups,
expansion.
"""
super().__init__()
self.c = int(c2 * e) # hidden channels
self.cv1 = Conv(c1, 2 * self.c, 1, 1)
self.EMA = EMA(self.c * 2, factor=8)
self.cv2 = Conv((2 + n) * self.c, c2, 1) # optional act=FReLU(c2)
self.m = nn.ModuleList(Bottleneck(self.c, self.c, shortcut, g, k=((3, 3), (3, 3)), e=1.0) for _ in range(n))
def forward(self, x):
"""Forward pass through C2f layer."""
y = list(self.EMA(self.cv1(x)).chunk(2, 1))
y.extend(m(y[-1]) for m in self.m)
return self.cv2(torch.cat(y, 1))
def forward_split(self, x):
"""Forward pass using split() instead of chunk()."""
y = list(self.EMA(self.cv1(x)).split((self.c, self.c), 1))
y.extend(m(y[-1]) for m in self.m)
return self.cv2(torch.cat(y, 1))
上面代码中注释的部分是第二种改进方法,如下图:
改进步骤
在ultralytics根目录下创建一个文件夹,这里我命名为mymodel,将上面的代码放在此文件夹中并命名为C2f_EMA。
然后打开ultralytics/nn/tasks.py,在顶部进行注册
from mymodel.C2f_EMA import C2f_EMA
再从task.py中找到parse_model函数,在其中的如下位置末尾添加C2f_EMA即可。
if m in (
Classify,
Conv,
ConvTranspose,
GhostConv,
Bottleneck,
GhostBottleneck,
SPP,
SPPF,
DWConv,
Focus,
BottleneckCSP,
C1,
C2,
C2f,
C3,
C3TR,
C3Ghost,
nn.ConvTranspose2d,
DWConvTranspose2d,
C3x,
SCDown,
C2fCIB,
PSA,
RepC3,
C2f_EMA,
):
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)
args = [c1, c2, *args[1:]]
最后在根目录创建myyaml文件夹并在其中创建C2f_EMA.yaml配置文件,将下面代码放在里面,这里是替换了neck部分的C2f,同理也可以替换Backbone部分的,感兴趣的朋友可以尝试一下:
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# 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:
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 3, C2f_EMA, [512]] # 12
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 3, C2f_EMA, [256]] # 15 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 12], 1, Concat, [1]] # cat head P4
- [-1, 3, C2f_EMA, [512]] # 18 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 9], 1, Concat, [1]] # cat head P5
- [-1, 3, C2f_EMA, [1024]] # 21 (P5/32-large)
- [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)
打印出来的结果如下图,这里用YOLOv8s为基础模型,可以看到仅引入了少量的参数量和计算复杂度,如果用模型宽度更小的YOLOv8n的话参数量还可以降低,大家可以拿自己的数据集实验一下。