DETRs Beat YOLOs on Real-time Object Detection

October 19, 2023 · View on GitHub

最新动态

  • 发布RT-DETR-R50和RT-DETR-R101的代码和预训练模型
  • 发布RT-DETR-L和RT-DETR-X的代码和预训练模型
  • 发布RT-DETR-R50-m模型(scale模型的范例)
  • 发布RT-DETR-R34模型
  • 发布RT-DETR-R18模型
  • 发布RT-DETR-Swin和RT-DETR-FocalNet模型
  • 发布RTDETR Obj365预训练模型
  • 发布RT-DETR-H的代码和预训练模型

简介

RT-DETR是第一个实时端到端目标检测器。具体而言,我们设计了一个高效的混合编码器,通过解耦尺度内交互和跨尺度融合来高效处理多尺度特征,并提出了IoU感知的查询选择机制,以优化解码器查询的初始化。此外,RT-DETR支持通过使用不同的解码器层来灵活调整推理速度,而不需要重新训练,这有助于实时目标检测器的实际应用。RT-DETR-L在COCO val2017上实现了53.0%的AP,在T4 GPU上实现了114FPS,RT-DETR-X实现了54.8%的AP和74FPS,RT-DETR-H实现了56.3%的AP和40FPS,在速度和精度方面都优于相同规模的所有YOLO检测器。RT-DETR-R50实现了53.1%的AP和108FPS,RT-DETR-R101实现了54.3%的AP和74FPS,在精度上超过了全部使用相同骨干网络的DETR检测器。 若要了解更多细节,请参考我们的论文paper.

基础模型

ModelEpochBackboneInput shapeAPvalAP^{val}AP50valAP^{val}_{50}Params(M)FLOPs(G)T4 TensorRT FP16(FPS)Pretrained Modelconfig
RT-DETR-R186xResNet-1864046.563.82060217downloadconfig
RT-DETR-R346xResNet-3464048.966.83192161downloadconfig
RT-DETR-R50-m6xResNet-5064051.369.636100145downloadconfig
RT-DETR-R506xResNet-5064053.171.342136108downloadconfig
RT-DETR-R1016xResNet-10164054.372.77625974downloadconfig
RT-DETR-L6xHGNetv264053.071.632110114downloadconfig
RT-DETR-X6xHGNetv264054.873.16723474downloadconfig
RT-DETR-H6xHGNetv264056.374.812349040downloadconfig

高精度模型

ModelEpochbackboneinput shapeAPvalAP^{val}AP50valAP^{val}_{50}Pretrained Modelconfig
RT-DETR-Swin3xSwin_L_38464056.273.5downloadconfig
RT-DETR-FocalNet3xFocalNet_L_38464056.974.3downloadconfig

Objects365预训练模型

ModelEpochDatasetInput shapeAPvalAP^{val}AP50valAP^{val}_{50}T4 TensorRT FP16(FPS)WeightLogs
RT-DETR-R181xObjects36564022.931.2-downloadlog
RT-DETR-R185xCOCO + Objects36564049.266.6217downloadlog
RT-DETR-R501xObjects36564035.146.2-downloadlog
RT-DETR-R502xCOCO + Objects36564055.373.4108downloadlog
RT-DETR-R1011xObjects36564036.848.3-downloadlog
RT-DETR-R1012xCOCO + Objects36564056.274.574downloadlog

Notes:

  • COCO + Objects365 代表使用Objects365预训练权重,在COCO上finetune的结果

注意事项:

  • RT-DETR 基础模型均使用4个GPU训练。
  • RT-DETR 在COCO train2017上训练,并在val2017上评估。
  • 高精度模型RT-DETR-Swin和RT-DETR-FocalNet使用8个GPU训练,显存需求较高。

快速开始

依赖包:
  • PaddlePaddle >= 2.4.1
安装
训练&评估
  • 单卡GPU上训练:
# training on single-GPU
export CUDA_VISIBLE_DEVICES=0
python tools/train.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml --eval
  • 多卡GPU上训练:
# training on multi-GPU
export CUDA_VISIBLE_DEVICES=0,1,2,3
python -m paddle.distributed.launch --gpus 0,1,2,3 tools/train.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml --fleet --eval
  • 评估:
python tools/eval.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml \
              -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_r50vd_6x_coco.pdparams
  • 测试:
python tools/infer.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml \
              -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_r50vd_6x_coco.pdparams \
              --infer_img=./demo/000000570688.jpg

详情请参考快速开始文档.

部署

1. 导出模型
cd PaddleDetection
python tools/export_model.py -c configs/rtdetr/rtdetr_r50vd_6x_coco.yml \
              -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_r50vd_6x_coco.pdparams trt=True \
              --output_dir=output_inference
2. 转换模型至ONNX
pip install onnx==1.13.0
pip install paddle2onnx==1.0.5
  • 转换模型:
paddle2onnx --model_dir=./output_inference/rtdetr_r50vd_6x_coco/ \
            --model_filename model.pdmodel  \
            --params_filename model.pdiparams \
            --opset_version 16 \
            --save_file rtdetr_r50vd_6x_coco.onnx
3. 转换成TensorRT(可选)
  • 确保TensorRT的版本>=8.5.1
  • TRT推理可以参考RT-DETR的部分代码或者其他网络资源
trtexec --onnx=./rtdetr_r50vd_6x_coco.onnx \
        --workspace=4096 \
        --shapes=image:1x3x640x640 \
        --saveEngine=rtdetr_r50vd_6x_coco.trt \
        --avgRuns=100 \
        --fp16

量化压缩

详细步骤请参考:RT-DETR自动化量化压缩

模型Base mAPACT量化mAPTRT-FP32TRT-FP16TRT-INT8配置文件量化模型
RT-DETR-R5053.153.032.05ms9.12ms6.96msconfigModel
RT-DETR-R10154.354.154.13ms12.68ms9.20msconfigModel
RT-DETR-HGNetv2-L53.052.926.16ms8.54ms6.65msconfigModel
RT-DETR-HGNetv2-X54.854.649.22ms12.50ms9.24msconfigModel

其他

1. 参数量和计算量统计 可以使用以下代码片段实现参数量和计算量的统计
import paddle
from ppdet.core.workspace import load_config, merge_config
from ppdet.core.workspace import create

cfg_path = './configs/rtdetr/rtdetr_r50vd_6x_coco.yml'
cfg = load_config(cfg_path)
model = create(cfg.architecture)

blob = {
    'image': paddle.randn([1, 3, 640, 640]),
    'im_shape': paddle.to_tensor([[640, 640]]),
    'scale_factor': paddle.to_tensor([[1., 1.]])
}
paddle.flops(model, None, blob, custom_ops=None, print_detail=False)
2. YOLOs端到端速度测速
  • 可以参考RT-DETR benchmark部分或者其他网络资源

引用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}
}