百度 RT-DETR:基于视觉 Transformer 的实时对象检测器
概述#
实时检测 Transformer(RT-DETR)由百度开发,是一种端到端对象检测器,能够在保持高准确率的同时实现实时性能。它基于 DETR 构建。DETR 是一种无需 NMS 的Transformer检测框架;RT-DETR 在此基础上加入卷积主干网络和高效混合编码器,实现实时速度。RT-DETR 通过解耦尺度内交互与跨尺度融合,高效处理多尺度特征。该模型适应性很强,无需重新训练即可通过使用不同的解码器层灵活调整推理速度。RT-DETR 在 CUDA 搭配 TensorRT 等加速后端上表现最佳。
观看: 如何使用百度 RT-DETR 进行对象检测 | 使用 Ultralytics 进行推理和基准测试
百度 RT-DETR 概览。 RT-DETR 模型架构图展示了骨干网络的最后三个阶段 {S3, S4, S5},它们作为编码器的输入。高效的混合编码器通过尺度内特征交互(AIFI)和跨尺度特征融合模块(CCFM),将多尺度特征转换为图像特征序列。模型采用 IoU 感知查询选择,从中选出固定数量的图像特征,作为解码器的初始目标查询。最后,带有辅助预测头的解码器迭代优化目标查询,生成边界框和置信度分数(来源)。
主要功能#
- 高效混合编码器:百度 RT-DETR 使用高效混合编码器,通过解耦尺度内交互和跨尺度融合来处理多尺度特征。这种基于 Transformer 的设计降低了计算成本,并支持实时对象检测。
- IoU 感知查询选择:百度 RT-DETR 通过具备 IoU 感知能力的查询选择,改进了对象查询初始化。这使模型能够专注于场景中最相关的对象,从而提高检测准确率。
- 可调节的推理速度: 百度 RT-DETR 无需重新训练,只需使用不同的解码器层即可灵活调整推理速度。这种适应性有助于模型在各种实时目标检测场景中实际应用。
- 无需 NMS 的框架: RT-DETR 基于 DETR,无需进行非极大值抑制后处理,从而简化检测流程,并有望提高效率。
- 无锚框检测: RT-DETR 是一款无锚框检测器,能够简化检测流程,并可能提升模型在不同数据集上的泛化能力。
预训练模型#
Ultralytics Python API 提供不同规模的预训练 PaddlePaddle RT-DETR 模型:
- RT-DETR-L:在 COCO val2017 上达到 53.0% AP,在 T4 GPU 上达到 114 FPS
- RT-DETR-X:在 COCO val2017 上 AP 为 54.8%,在 T4 GPU 上为 74 FPS
百度于 2024 年 7 月发布了 RTDETRv2,进一步改进了原有架构。
使用示例#
以下示例展示了简单的 RT-DETR训练和推理流程。你也可以通过 Ultralytics Platform 使用云 GPU 训练模型。
from ultralytics import RTDETR
# 加载一个在 COCO 上预训练的 RT-DETR-l 模型
model = RTDETR("rtdetr-l.pt")
# 显示模型信息(可选)
model.info()
# 使用 COCO8 示例数据集训练模型 100 个 epoch
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)
# 使用 RT-DETR-l 模型对图像 'bus.jpg' 进行推理
results = model("path/to/bus.jpg")使用 PyTorch 2.0 或更高版本在 CUDA 上训练 RT-DETR 时,设置 deterministic=False。它的可变形注意力使用 F.grid_sample,而该算子没有确定性的 CUDA 反向传播,因此 deterministic=True 无法保证运行可复现,还可能降低训练吞吐量。seed 仍用于控制权重初始化、数据顺序和增强采样。
RT-DETR 预训练权重支持两种推理时设置,可在无需重新训练的情况下降低延迟:
eval_idx:提前停止解码。对于默认的 6 层解码器,请使用从零开始的索引(0–5)。eval_idx=5使用所有层;eval_idx=3使用 4 层。在搭载 TensorRT v10.11 的 T4 GPU 上,使用 4 层后,RT-DETR-L 的性能从 8.0 ms / 52.7 mAP 提升至 7.4 ms / 52.5 mAP。num_queries:减少目标查询数量(默认值:300)。在相同配置下,将数量降至 100,可在 COCO 上达到 7.4 ms / 51.7 mAP。对于每张图像目标数量较少的数据集,mAP 通常下降得更少,但该值应高于每张图像预期目标数量的最大值。
这两种设置都可能降低 mAP——部署前请在自己的数据集上验证这种权衡。
from ultralytics import RTDETR
rtdetr = RTDETR("rtdetr-l.pt")
head = rtdetr.model.model[-1]
# 验证速度与精度之间的权衡后,选择其中一种或同时使用两种设置。
head.decoder.eval_idx = 3 # 使用 6 层解码器中的 4 层。
head.num_queries = 100 # 减少目标查询数量。
results = rtdetr("path/to/image.jpg")
# 导出时使用相同的解码器和查询设置,包括 TensorRT 导出。
rtdetr.export(format="engine", device=0, quantize=16)支持的任务和模式#
此表列出了模型类型、具体的预训练权重、各模型支持的任务,以及支持的模式(训练、验证、预测、导出),支持的模式以 ✅ 表示。
| 模型类型 | 预训练权重 | 支持的任务 | 训练 | 验证 | 推理 | 导出 |
|---|---|---|---|---|---|---|
| RT-DETR 大型版 | rtdetr-l.pt | 目标检测 | ✅ | ✅ | ✅ | ✅ |
| RT-DETR 超大型版 | rtdetr-x.pt | 目标检测 | ✅ | ✅ | ✅ | ✅ |
rtdetr-resnet50.yaml 和 rtdetr-resnet101.yaml 仅以 YAML 架构形式提供。Ultralytics 仅为 rtdetr-l 和 rtdetr-x 发布预训练权重。你可以从 YAML 实例化 ResNet 变体(例如 RTDETR("rtdetr-resnet50.yaml")),并按需训练或微调。
理想应用场景#
RT-DETR 尤其适合同时要求高精度和实时性能的应用:
- 自动驾驶:在自动驾驶系统中实现可靠的环境感知,其中速度和准确率都至关重要。
- 先进机器人技术:使机器人能够在动态环境中完成需要准确对象识别和交互的复杂任务。
- 医学影像:应用于医疗保健领域,在这些场景中,准确检测对象对于诊断至关重要。
- 监控系统:适用于需要实时监控和高检测精度的安防应用。了解安防报警系统。
- 卫星图像分析:适用于需要理解全局背景的高分辨率图像详细分析。阅读卫星图像中的计算机视觉。
引用与致谢#
如果你在研究或开发中使用百度的 RT-DETR,请引用原始论文:
@misc{lv2023detrs,
title={DETRs Beat YOLOs on Real-time Object Detection},
author={Wenyu Lv and Shangliang Xu and Yian Zhao and Guanzhong Wang and Jinman Wei and Cheng Cui and Yuning Du and Qingqing Dang and Yi Liu},
year={2023},
eprint={2304.08069},
archivePrefix={arXiv},
primaryClass={cs.CV}
}引用 RTDETRv2 时,请引用2024 年论文:
@misc{lv2024rtdetrv2,
title={RT-DETRv2: Improved Baseline with Bag-of-Freebies for Real-Time Detection Transformer},
author={Wenyu Lv and Yian Zhao and Qinyao Chang and Kui Huang and Guanzhong Wang and Yi Liu},
year={2024},
eprint={2407.17140},
archivePrefix={arXiv},
primaryClass={cs.CV}
}我们谨向百度和 PaddlePaddle 团队致谢,感谢他们为 计算机视觉 社区创建并维护这项宝贵资源。他们开发了基于 Transformer 的实时目标检测器 RT-DETR,这一点值得高度赞赏。
常见问题#
百度的 RT-DETR(实时检测 Transformer)是一款基于 Transformer 的实时目标检测器。它通过高效的混合编码器解耦尺度内交互和跨尺度融合,从而高效处理多尺度特征。模型采用 IoU 感知的查询选择机制,聚焦于最相关的目标,以提升检测准确率。无需重新训练,只需调整解码器层数即可改变推理速度,因此 RT-DETR 适用于各种实时目标检测场景。
使用 Ultralytics Python API 加载预训练的 PaddlePaddle RT-DETR 模型。例如,加载在 COCO 上预训练的 RT-DETR-l 模型,对其进行微调并运行推理:
示例from ultralytics import RTDETR # 加载一个在 COCO 上预训练的 RT-DETR-l 模型 model = RTDETR("rtdetr-l.pt") # 显示模型信息(可选) model.info() # 使用 COCO8 示例数据集训练模型 100 个 epoch results = model.train(data="coco8.yaml", epochs=100, imgsz=640) # 使用 RT-DETR-l 模型对图像 'bus.jpg' 进行推理 results = model("path/to/bus.jpg")百度的 RT-DETR 将高效混合编码器与 IoU 感知查询选择相结合,在保持高准确率的同时降低了计算成本。它无需重新训练即可通过使用不同的解码器层调整推理速度,提升了灵活性,适用于 CUDA 搭配 TensorRT 等加速后端上的实时应用。与传统的基于 CNN 的检测器相比,Transformer 架构还能捕捉更多全局上下文。
百度的 RT-DETR 无需重新训练,即可通过使用不同的解码器层灵活调整推理速度。这让你可以在实时对象检测任务中平衡速度和准确率:减少解码器层可加快处理速度,但准确率会略有下降;使用所有层则能获得最准确的检测结果。在计算能力各异的设备上部署模型时,这项功能尤其有价值。
不能。对于 RT-DETR,
max_det会限制推理后返回的预测数量,但不会增加解码器生成的目标查询数量。Ultralytics RT-DETR 预训练检查点使用 300 个目标查询,因此即使将max_det设为更大的值,每张图像也无法返回超过 300 个检测结果。如果只需要较少的高置信度预测结果,可以使用
max_det减少返回的检测结果,例如设置max_det=100。如果你的数据集中每张图像可能包含超过 300 个目标,请在模型 YAML 中提高解码器查询数量(nq),并训练自定义 RT-DETR 模型;在训练完成后对预训练检查点更改此值并不能达到同样效果,而且需要重新训练才能学习额外的查询。
评论148
提出问题、分享结果,或帮助其他人。
PhucPhamSy
Hi all, Could you please let me know how to use all parameters in the appendix of the original paper, "https://arxiv.org/pdf/2304.08069"? Thanks!
1
Glenn JocherUltralyticsHey there! 👋
We're really glad you're interested in diving deep into the parameters detailed in the original paper. Specifically, for utilizing parameters as mentioned in the RT-DETR paper ("DETRs Beat YOLOs on Real-time Object Detection"), I recommend focusing on the key components such as the Efficient Hybrid Encoder, IoU-aware Query Selection, and adaptability in Inference Speed adjustments that make RT-DETR efficient.
Details on these components can be extrapolated to parameter usage within your model through our Python API. For instance, when initializing the RT-DETR model or adjusting inference speed, you would consider the different scales of pre-trained models available:
from ultralytics import RTDETR # For a larger model with potentially higher accuracy but lower FPS (Frames Per Second) model = RTDETR('rtdetr-l.pt') # For an extra-large model prioritizing accuracy over speed model_x = RTDETR('rtdetr-x.pt')Adjustments for inference speed and applying IoU-aware query selection can directly correlate with your focus during the
.train()or.predict()processes, especially understanding what goes behind each configuration setting in these functions.Unfortunately, we don't provide a direct mapping for every parameter mentioned in the appendix to code-level argument settings as the implementation abstracts many complexities for user convenience and to maintain the API's simplicity. However, understanding the RT-DETR's key features and translating that understanding into practical API usage is a great way to leverage the power of RT-DETR. 😊
For more tailored guidance, always feel free to ask or refer to our official documentation for specific examples and deeper insights.
Happy coding! 🚀
Murat Raimbekovhey @wba-zzu, thanks for raising this.
quick rundown of where things stand:
resnet50 / resnet101 - the architectures are already shipped as yaml configs:
you can build and train them straight from yaml:
from ultralytics import RTDETR model = RTDETR("rtdetr-resnet50.yaml") model.train(data="coco.yaml", epochs=100, imgsz=640)however, ultralytics does not publish pretrained weights for these. the only official rt-detr checkpoints in the ultralytics/assets releases are
rtdetr-l.ptandrtdetr-x.pt, both of which use the hgnetv2 backbone (not resnet). this is also flagged in the model docs under "architecture-only variants".resnet18 - not shipped. there is no
rtdetr-resnet18.yamlin the repo, so a resnet18 variant would need its own yaml and corresponding modules added.loading external pretrained weights: ultralytics builds the model from yaml, so the state_dict keys follow a pattern like
model.<index>.layer.<...>.conv.weight. weights from another implementation will not load directly - they would need a key-mapping / conversion step to match this layout.hope this helps!
Boao Wang想要将原版RT-DETR中的ResNet50和ResNet18以及ResNet101完全移植到Ultralytics,并且获得其预训练模型
995742853
您好,我想请问一下,在哪里有基于rtdetr-resnet50的预训练pt权重,我知道看到rtdetr-l以及x的预训练权重
Glenn JocherUltralytics目前 RT-DETR 文档 里公开提供的 Ultralytics 预训练权重只有
rtdetr-l.pt和rtdetr-x.pt;如果你找的是 ResNet50 版本,建议查看 Baidu 官方的 RT-DETR PyTorch 权重列表,其中提供的是rtdetr_r50vd_*.pth,不是 Ultralytics 直接发布的rtdetr-resnet50.pt,也欢迎直接在这里继续提问,方便 Ultralytics 团队跟进。 (docs.ultralytics.com)995742853
谢谢您的回复
maitianstudio-arch
How to use RT-DETRv2?
Glenn JocherUltralyticsUltralytics currently exposes the original RT-DETR pretrained weights (
rtdetr-l.pt,rtdetr-x.pt) via theRTDETRAPI; RT-DETRv2 pretrained weights/configs aren’t packaged inultralyticsyet, so if you share what RT-DETRv2 checkpoint you have (Paddle vs PyTorch) and how you’re trying to run it, we can point you to the right conversion/workflow—meanwhile you can use RT-DETR as shown in the RT-DETR docs.from ultralytics import RTDETR model = RTDETR("rtdetr-l.pt") results = model("path/to/image.jpg")
fxc0627
你好,请问RE-DETR模型适用于小目标工业缺陷检测吗?对于小目标检测有什么天然的优势?
wang18382140151-hub
你好,最近deim,dfine等对于rtdetr有了很好的改进,尤其是deim能很好的加速训练的速度,能将其整合进rtdetr中吗?
yuwencool
您好RT-DETR模型的推理支持model.track模式吗?
liyansong123456
RT-DETRv3 has been released. Does Ultralytics have plans to integrate it?
AK993
你好,rt-detr支持在转为rknn格式,并在rk3588上部署吗