YOLOv11改进-模块-引入矩形自校准模块RCM有利于复杂场景(小目标、遮挡等)
本篇文章将介绍一个新的改进机制——矩形自校准模块RCM,并阐述如何将其应用于YOLOv11中,显著提升模型性能。首先,我们将解析RCM的工作原理,RCM通过矩形自校准注意力机制和形状自校准捕捉全局上下文信息,并结合局部细节融合,提升模型对前景物体的建模能力和边界识别精度。为了解决复杂场景中难以处理前景背景分割、精确定位和多尺度物体检测这些问题,我们将RCM(矩形自校准模块)与YOLOv11结合,利用RCM的上下文捕捉与特征增强能力,提升YOLOv11的检测性能。随后,本文将详细讨论如何将RCM引入YOLOv11模型中,优化其目标检测能力。
1. Rectangular Self-Calibration Module (RCM)结构介绍
RCM是专为语义分割等任务设计的上下文增强模块,旨在通过捕捉水平和垂直全局上下文信息,提升模型对前景物体的建模能力。它的核心思想包括以下几个部分:
1. 矩形自校准注意力机制:通过水平和垂直池化操作,生成矩形注意力区域,用以捕捉关键的上下文信息。这些区域通过加权机制使模型更加聚焦前景对象。
2. 形状自校准:通过大核卷积调节矩形注意力区域的形状,使其更贴近前景物体,提升模型的前景定位精度。
3. 局部细节融合:通过深度卷积进一步增强局部特征的细节表示,使得模型在边界识别和小物体检测中表现更好。
2. YOLOv11与RCM的结合
在YOLOv11中,RCM模块可以用于增强目标检测中的空间特征建模,尤其是在处理复杂场景下的前景目标时,通过捕捉水平和垂直的全局上下文,RCM可以帮助YOLOv11更加准确地定位和识别不同尺度的物体。具体可以通过以下几方面来结合:
-
backbone引入:将RCM应用于YOLOv11的特征提取阶段,通过矩形自校准机制,提升网络对前景物体的注意力集中度,使检测更加精准。
-
head引入: 利用RCM的多尺度上下文提取能力,将不同分辨率的特征进行融合,进一步提升YOLOv11在复杂场景下的多目标检测性能。
3. Rectangular Self-Calibration Module (RCM)代码部分
import torch
import torch.nn as nn
from timm.models.layers import DropPath, to_2tuple
# from conv import Conv
# from block import C2f, C3k
from .conv import Conv
from .block import C2f, C3k
class ConvMlp(nn.Module):
""" 使用 1x1 卷积保持空间维度的 MLP
"""
def __init__(
self, in_features, hidden_features=None, out_features=None, act_layer=nn.ReLU,
norm_layer=None, bias=True, drop=0.):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
bias = to_2tuple(bias)
self.fc1 = nn.Conv2d(in_features, hidden_features, kernel_size=1, bias=bias[0])
self.norm = norm_layer(hidden_features) if norm_layer else nn.Identity()
self.act = act_layer()
self.drop = nn.Dropout(drop)
self.fc2 = nn.Conv2d(hidden_features, out_features, kernel_size=1, bias=bias[1])
def forward(self, x):
x = self.fc1(x)
x = self.norm(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
return x
#rectangular self-calibration attention (RCA)
class RCA(nn.Module):
def __init__(self, inp, kernel_size=1, ratio=1, band_kernel_size=11, dw_size=(1, 1), padding=(0, 0), stride=1,
square_kernel_size=2, relu=True):
super(RCA, self).__init__()
self.dwconv_hw = nn.Conv2d(inp, inp, square_kernel_size, padding=square_kernel_size // 2, groups=inp)
self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
self.pool_w = nn.AdaptiveAvgPool2d((1, None))
gc = inp // ratio
self.excite = nn.Sequential(
nn.Conv2d(inp, gc, kernel_size=(1, band_kernel_size), padding=(0, band_kernel_size // 2), groups=gc),
nn.BatchNorm2d(gc),
nn.ReLU(inplace=True),
nn.Conv2d(gc, inp, kernel_size=(band_kernel_size, 1), padding=(band_kernel_size // 2, 0), groups=gc),
nn.Sigmoid()
)
def sge(self, x):
# [N, D, C, 1]
x_h = self.pool_h(x)
x_w = self.pool_w(x)
x_gather = x_h + x_w # .repeat(1,1,1,x_w.shape[-1])
ge = self.excite(x_gather) # [N, 1, C, 1]
return ge
def forward(self, x):
loc = self.dwconv_hw(x)
att = self.sge(x)
out = att * loc
return out
#Rectangular Self-Calibration Module (RCM)
class RCM(nn.Module):
""" MetaNeXtBlock 块
参数:
dim (int): 输入通道数.
drop_path (float): 随机深度率。默认: 0.0
ls_init_value (float): 层级比例初始化值。默认: 1e-6.
"""
def __init__(
self,
dim,
token_mixer=RCA,
norm_layer=nn.BatchNorm2d,
mlp_layer=ConvMlp,
mlp_ratio=2,
act_layer=nn.GELU,
ls_init_value=1e-6,
drop_path=0.,
dw_size=11,
square_kernel_size=3,
ratio=1,
):
super().__init__()
self.token_mixer = token_mixer(dim, band_kernel_size=dw_size, square_kernel_size=square_kernel_size,
ratio=ratio)
self.norm = norm_layer(dim)
self.mlp = mlp_layer(dim, int(mlp_ratio * dim), act_layer=act_layer)
self.gamma = nn.Parameter(ls_init_value * torch.ones(dim)) if ls_init_value else None
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
def forward(self, x):
shortcut = x
x = self.token_mixer(x)
x = self.norm(x)
x = self.mlp(x)
if self.gamma is not None:
x = x.mul(self.gamma.reshape(1, -1, 1, 1))
x = self.drop_path(x) + shortcut
return x
class Bottleneck_RCM(nn.Module):
"""Standard bottleneck."""
def __init__(self, c1, c2, shortcut=True, g=1, k=(3, 3), e=0.5):
"""Initializes a standard bottleneck module with optional shortcut connection and configurable parameters."""
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.cv3 = RCM(c_)
self.add = shortcut and c1 == c2
def forward(self, x):
"""Applies the YOLO FPN to input data."""
# return x + self.cv2(self.cv1(self.cv3(x))) if self.add else self.cv2(self.cv1(self.cv3(x)))
return x + self.cv2(self.cv3(self.cv1(x))) if self.add else self.cv2(self.cv3(self.cv1(x)))
# return x + self.cv2(self.cv3(x)) if self.add else self.cv2(self.cv3(x))
class C3k2_RCM(C2f):
"""Faster Implementation of CSP Bottleneck with 2 convolutions."""
def __init__(self, c1, c2, n=1, c3k=False, e=0.5, g=1, shortcut=True):
"""Initializes the C3k2 module, a faster CSP Bottleneck with 2 convolutions and optional C3k blocks."""
super().__init__(c1, c2, n, shortcut, g, e)
self.m = nn.ModuleList(
C3k(self.c, self.c, 2, shortcut, g) if c3k else Bottleneck_RCM(self.c, self.c, shortcut, g) for _ in range(n)
)
if __name__ == '__main__':
TB = RCM(256)
#创建一个输入张量
batch_size = 8
input_tensor=torch.randn(batch_size, 256, 64, 64 )
#运行模型并打印输入和输出的形状
output_tensor =TB(input_tensor)
print("Input shape:",input_tensor.shape)
print("0utput shape:",output_tensor.shape)
4. 将RCM引入到YOLOv11中
第一: 将下面的核心代码复制到D:\bilibili\model\YOLO11\ultralytics-main\ultralytics\nn路径下,如下图所示。
第二:在task.py中导入RCM包
第三:在task.py中的模型配置部分下面代码
第二改进修改代码的部分
第一改进修改代码的部分
elif m is RCM : args = [ch[f]]
第四:将模型配置文件复制到YOLOV11.YAMY文件中
第一个改进配置文件
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLO11 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=yolo11n.yaml' will call yolo11.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
# YOLO11n 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, 2, C3k2, [256, False, 0.25]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 2, C3k2, [512, False, 0.25]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 2, C3k2, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 2, C3k2, [1024, True]]
- [-1, 1, RCM, []]
- [-1, 1, SPPF, [1024, 5]] # 9
- [-1, 2, C2PSA, [1024]] # 10
# YOLO11n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 2, C3k2, [512, False]] # 13
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 14], 1, Concat, [1]] # cat head P4
- [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 11], 1, Concat, [1]] # cat head P5
- [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)
- [[17, 20, 23], 1, Detect, [nc]] # Detect(P3, P4, P5)
第二个改进配置文件
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLO11 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=yolo11n.yaml' will call yolo11.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
# YOLO11n 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, 2, C3k2, [256, False, 0.25]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 2, C3k2, [512, False, 0.25]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 2, C3k2, [512, True]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 2, C3k2, [1024, True]]
- [-1, 1, SPPF, [1024, 5]] # 9
- [-1, 2, C2PSA, [1024]] # 10
# YOLO11n head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 2, C3k2, [512, False]] # 13
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 2, C3k2_RCM, [256, False]] # 16 (P3/8-small)
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 13], 1, Concat, [1]] # cat head P4
- [-1, 2, C3k2_RCM, [512, False]] # 19 (P4/16-medium)
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 10], 1, Concat, [1]] # cat head P5
- [-1, 2, C3k2_RCM, [1024, True]] # 22 (P5/32-large)
- [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)
第五:运行成功
from ultralytics.models import NAS, RTDETR, SAM, YOLO, FastSAM, YOLOWorld
if __name__=="__main__":
# 使用自己的YOLOv11.yamy文件搭建模型并加载预训练权重训练模型
model = YOLO(r"D:\bilibili\model\YOLO11\ultralytics-main\ultralytics\cfg\models\11\yolo11_RCM.yaml")\
.load(r'D:\bilibili\model\YOLO11\ultralytics-main\yolo11n.pt') # build from YAML and transfer weights
results = model.train(data=r'D:\bilibili\model\ultralytics-main\ultralytics\cfg\datasets\VOC_my.yaml',
epochs=100, imgsz=640, batch=8)