1、自创可扩展卷积模块
可扩展卷积与一般卷积的区别是,可以得到不同感受野的大小。这里使用三分支不同的可扩展卷积,对特征图进行提取,得到感受野不同的特征图,有助于图中的小目标信息提取
class RFBconv(nn.Module):
def __init__(self, c1):
super(RFBconv, self).__init__()
# 第一条分支
c2 = c1
self.branch1 = nn.Sequential(
Conv(c1,c2,1,1),
nn.Conv2d(c2, c2, kernel_size=3, dilation=1, padding=1)
)
# 第二条分支
self.branch2 = nn.Sequential(
Conv(c1, c2, 1,1),
nn.Conv2d(c2, c2, kernel_size=3, dilation=1, padding=1),
nn.Conv2d(c2, c2, kernel_size=3, dilation=3, padding=3)
)
# 第三条分支
self.branch3 = nn.Sequential(
Conv(c1, c2, 1,1),
nn.Conv2d(c2, c2, kernel_size=5, dilation=1, padding=2),
nn.Conv2d(c2, c2, kernel_size=3, dilation=5, padding=5)
)
# 1x1卷积用于通道数变换
self.conv1x1 = Conv(3 * c2, c2, 1)
def forward(self, x):
# 分别对三条分支进行前向传播
out1 = self.branch1(x)
out2 = self.branch2(x)
out3 = self.branch3(x)
# 将三个分支的结果连接在一起
concatenated = torch.cat((out1, out2, out3), dim=1)
# 使用1x1卷积进行通道数变换
result = self.conv1x1(concatenated)
result = result + x
return result
2、EMA注意力机制
EMA是CBAM注意力的改进,比CBAM注意力更好
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)
3、配置文件
# 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: 20 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
# YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
# l: [1.00, 1.00, 512] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
# s: [0.33, 0.50, 1024]
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.0s head
head:
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [-1,1,EMA,[]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 3, C2f, [512]] # 13
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [-1,1,EMA,[]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 3, C2f, [256]] # 17 (P3/8-small)
- [-1, 1, nn.Upsample, [None, 2, 'nearest']]
- [-1,1,EMA,[]]
- [[-1, 2], 1, Concat, [1]] # cat backbone P3
- [-1, 3, C2f, [128]] # 18 (P2/4-xsmall)
- [-1,1,RFBconv,[]] # 22
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 17], 1, Concat, [1]] # cat head P4
- [-1, 3, C2f, [256]] # 21 (P3/8-small)
- [-1,1,RFBconv,[]] # 26
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 13], 1, Concat, [1]] # cat head P5
- [-1, 3, C2f, [512]] # 29 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 9], 1, Concat, [1]] # cat head P5
- [-1, 3, C2f, [1024]] # 32 (P5/32-large)
- [[22, 26, 29, 32], 1, Detect, [nc]] # Detect(P2, P3, P4, P5)