
RT-DETR使用教程: RT-DETR使用教程
RT-DETR改进汇总贴:RT-DETR更新汇总贴
《YOLOv12: Attention-Centric Real-Time Object Detectors》
一、 模块介绍
**论文链接:**https://arxiv.org/abs/2502.12524
代码链接: https://gitcode.com/gh_mirrors/yo/yolov12
论文速览:
长期以来,增强YOLO框架的网络架构一直至关重要,但一直专注于基于cnn的改进,尽管注意力机制在建模能力方面已被证明具有优越性。这是因为基于注意力的模型无法匹配基于cnn的模型的速度。本文提出了一种以注意力为中心的YOLO框架,即YOLOv12,与之前基于cnn的YOLO框架的速度相匹配,同时利用了注意力机制的性能优势。YOLOv12在精度和速度方面超越了所有流行的实时目标检测器。例如,YOLOv12-N在T4 GPU上以1.64ms的推理延迟实现了40.6% mAP,以相当的速度超过了高级的YOLOv10-N / YOLOv11-N 2.1%/1.2% mAP。这种优势可以扩展到其他模型规模。YOLOv12还超越了改善DETR的端到端实时检测器,如RT-DETR /RT-DETRv2: YOLOv12- s比RT-DETR- r18 / RT-DETRv2-r18运行更快42%,仅使用36%的计算和45%的参数。更多的比较见图1。
**总结:本文将其中的R-ELAN思想融入其他模块。**
二、二创融合模块
2.1 相关二创模块及所需参数
该模块可如图加入到RepNCSPELAN4、RepC3与自研等模块中,代码见群文件,所需参数如下。
RepNCSPELAN4-变式模块 所需参数:(c1, c2, c3 , c4, n**)**
CCRI及变式模块 所需参数:( c1, c2 , k, n, lightconv, shortcut, scale, e, act**)**
RepC4及变式模块 所需参数:(c1, c2, n, e**)**
其中,RepNCSPELAN4模块的代码如下:
class RepNCSPELAN4_R_ELAN(nn.Module):
"""CSP-ELAN."""
def __init__(self, c1, c2, c3, c4, n=1):
"""Initializes CSP-ELAN layer with specified channel sizes, repetitions, and convolutions."""
super().__init__()
self.c = c3 // 2
self.cv1 = Conv(c1, self.c, 1, 1)
self.cv2 = nn.Sequential(RepCSP(c3 // 2, c4, n), Conv(c4, c4, 3, 1))
self.cv3 = nn.Sequential(RepCSP(c4, c4, n), Conv(c4, c4, 3, 1))
self.cv4 = Conv(self.c + (2 * c4), c2, 1, 1)
def forward(self, x):
"""Forward pass through RepNCSPELAN4 layer."""
y = [self.cv1(x)]
y.extend((m(y[-1])) for m in [self.cv2, self.cv3])
return self.cv4(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.extend(m(y[-1]) for m in [self.cv2, self.cv3])
return self.cv4(torch.cat(y, 1))
2.2 更改yaml文件 (以自研模型加入为例)
打开更改ultralytics/cfg/models/rt-detr路径下的rtdetr-l.yaml文件,替换原有模块。
# Ultralytics YOLO 🚀, AGPL-3.0 license
# RT-DETR-l object detection model with P3-P5 outputs. For details see https://docs.ultralytics.com/models/rtdetr
# ⭐⭐Powered by https://blog.csdn.net/StopAndGoyyy, 技术指导QQ:2668825911⭐⭐
# Parameters
nc: 80 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n-cls.yaml' will call yolov8-cls.yaml with scale 'n'
# [depth, width, max_channels]
l: [1.00, 1.00, 512]
# n: [ 0.33, 0.25, 1024 ]
# s: [ 0.33, 0.50, 1024 ]
# m: [ 0.67, 0.75, 768 ]
# l: [ 1.00, 1.00, 512 ]
# x: [ 1.00, 1.25, 512 ]
# ⭐⭐Powered by https://blog.csdn.net/StopAndGoyyy, 技术指导QQ:2668825911⭐⭐
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, CCRI, [128, 5, True, False]]
- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
- [-1, 4, CCRI, [256, 3, True, True]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 4, RepNCSPELAN4_R_ELAN, [512, 512, 256, 1]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 2, CCRI, [1024, 3, True, False]]
head:
- [-1, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 9 input_proj.2
- [-1, 1, AIFI, [1024, 8]]
- [-1, 1, Conv, [256, 1, 1]] # 11, Y5, lateral_convs.0
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [6, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 13 input_proj.1
- [[-2, -1], 1, Concat, [1]]
- [-1, 2, RepC4, [256]] # 15, fpn_blocks.0
- [-1, 1, Conv, [256, 1, 1]] # 16, Y4, lateral_convs.1
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [4, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 18 input_proj.0
- [[-2, -1], 1, Concat, [1]] # cat backbone P4
- [-1, 2, RepC4, [256]] # X3 (20), fpn_blocks.1
- [-1, 1, Conv, [256, 3, 2]] # 22, downsample_convs.0
- [[-1, 16], 1, Concat, [1]] # cat Y4
- [-1, 2, RepC4, [256]] # F4 (23), pan_blocks.0
- [-1, 1, Conv, [256, 3, 2]] # 24, downsample_convs.1
- [[-1, 11], 1, Concat, [1]] # cat Y5
- [-1, 2, RepC4, [256]] # F5 (26), pan_blocks.1
- [[20, 23, 26], 1, RTDETRDecoder, [nc]] # Detect(P3, P4, P5)
# ⭐⭐Powered by https://blog.csdn.net/StopAndGoyyy, 技术指导QQ:2668825911⭐⭐
2.2 修改train.py文件
创建Train_RT脚本用于训练。
from ultralytics.models import RTDETR
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
if __name__ == '__main__':
model = RTDETR(model='ultralytics/cfg/models/rt-detr/rtdetr-l.yaml')
# model.load('yolov8n.pt')
model.train(data='./data.yaml', epochs=2, batch=1, device='0', imgsz=640, workers=2, cache=False,
amp=True, mosaic=False, project='runs/train', name='exp')
在train.py脚本中填入修改好的yaml路径,运行即可训。