文件最后提交记录最后更新时间
适配RT-DETRv2模型 Co-authored-by: sbu-art<sulu520365@163.com> # message auto-generated for no-merge-commit merge: !7553 merge feat/rtdetrv2 into master 适配RT-DETRv2模型 Created-by: sulu_ Commit-by: sbu-art Merged-by: ascend-robot Description: ## Motivation 适配RT-DETRv2模型。 ## Modification 适配RT-DETRv2模型。 ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75536 天前
适配RT-DETRv2模型 Co-authored-by: sbu-art<sulu520365@163.com> # message auto-generated for no-merge-commit merge: !7553 merge feat/rtdetrv2 into master 适配RT-DETRv2模型 Created-by: sulu_ Commit-by: sbu-art Merged-by: ascend-robot Description: ## Motivation 适配RT-DETRv2模型。 ## Modification 适配RT-DETRv2模型。 ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75536 天前
适配RT-DETRv2模型 Co-authored-by: sbu-art<sulu520365@163.com> # message auto-generated for no-merge-commit merge: !7553 merge feat/rtdetrv2 into master 适配RT-DETRv2模型 Created-by: sulu_ Commit-by: sbu-art Merged-by: ascend-robot Description: ## Motivation 适配RT-DETRv2模型。 ## Modification 适配RT-DETRv2模型。 ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75536 天前
适配RT-DETRv2模型 Co-authored-by: sbu-art<sulu520365@163.com> # message auto-generated for no-merge-commit merge: !7553 merge feat/rtdetrv2 into master 适配RT-DETRv2模型 Created-by: sulu_ Commit-by: sbu-art Merged-by: ascend-robot Description: ## Motivation 适配RT-DETRv2模型。 ## Modification 适配RT-DETRv2模型。 ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75536 天前
适配RT-DETRv2模型 Co-authored-by: sbu-art<sulu520365@163.com> # message auto-generated for no-merge-commit merge: !7553 merge feat/rtdetrv2 into master 适配RT-DETRv2模型 Created-by: sulu_ Commit-by: sbu-art Merged-by: ascend-robot Description: ## Motivation 适配RT-DETRv2模型。 ## Modification 适配RT-DETRv2模型。 ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [x] The new code needs to comply with the Clean Code specification. - [x] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [x] CLA has been signed and all committers have signed the CLA in this PR. - [x] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75536 天前
README.md

RT-DETRv2模型-推理指导

概述

RT-DETR(Real-Time Detection Transformer)是实时端到端目标检测模型,RT-DETRv2 在 RT-DETR 的基础上进一步优化了训练策略和检测性能。当前目录提供 Ascend 推理侧所需的补丁、PyTorch 转 ONNX/OM 脚本、OM 单图推理脚本以及 COCO 精度评估脚本,适用于 RT-DETRv2 模型的 Ascend 离线推理部署。

  • 参考论文:

    • Zhao, Yian, et al. "DETRs Beat YOLOs on Real-time Object Detection."
    • Lv, Wenyu, et al. "RT-DETRv2: Improved Baseline with Bag-of-Freebies for Real-Time Detection Transformer."
  • 参考实现:

    url=https://github.com/lyuwenyu/RT-DETR
    branch=main
    commit_id=3d8f87ca74ed715595aa832d99bdd84a19d51567
    model_name=RT-DETRv2
    

    适配昇腾 AI 处理器的实现:

    url=https://gitcode.com/ascend/ModelZoo-PyTorch
    branch=master
    code_path=ACL_PyTorch/built-in/cv/RT-DETRv2
    
    
    
    

输入输出数据

  • 输入数据

    输入数据 数据类型 大小 数据排布格式
    images RGB_FP32 1 x 3 x 640 x 640 NCHW
    orig_target_sizes INT64 1 x 2 ND
  • 输出数据

    输出数据 大小 数据类型 数据排布格式
    labels 1 x 300 INT64 ND
    boxes 1 x 300 x 4 FLOAT32 ND
    scores 1 x 300 FLOAT32 ND

推理环境准备[所有版本]

  • 该模型需要以下插件与驱动。

    表 1 版本配套表

配套 版本 环境准备指导
CANN 8.5.0 Pytorch框架推理环境准备
Python 3.12 -
PyTorch 2.9.0 -
torch_npu 2.9.0 -
  • 支持的硬件类型:Ascend910B

快速上手

获取源码

  1. 获取开源代码仓。

    在根目录下,执行如下命令。
    git clone https://github.com/lyuwenyu/RT-DETR.git
    cd RT-DETR
    git checkout 3d8f87ca74ed715595aa832d99bdd84a19d51567
    
  2. 获取源码。

    上传源码包到RT-DETR/rtdetrv2_pytorch目录并解压
    ├── rtdetrv2_pytorch
    │   ├── RT-DETRv2
    │      ├── patch
    │      │   └── npuv1.patch          # 推荐使用的 NPU 适配补丁
    │      ├── eval_om_coco.py          # OM 模型 COCO 精度评估脚本
    │      ├── export_om.py             # PyTorch->ONNX->OM 转换脚本
    │      ├── infer_om.py              # OM 单图推理脚本
    │      └── README.md                # 使用说明
    
    cd RT-DETR/rtdetrv2_pytorch/RT-DETRv2
    git apply ./patch/npuv1.patch
    cd ..
    

    说明:

    1. npuv1.patch 为推荐补丁,覆盖当前推理适配所需修改。
  3. 安装依赖。

    安装ais_bench推理工具: https://gitee.com/ascend/tools/tree/master/ais-bench_workload/tool/ais_bench

    pip3 install aclruntime-0.0.2-cp312-cp312-linux_aarch64.whl
    pip3 install ais_bench-0.0.2-py3-none-any.whl
    pip3 install torch==2.9.0 torchvision
    pip3 install onnx onnxsim opencv-python pillow pycocotools faster_coco_eval tensorboard matplotlib
    

准备数据集

  1. 获取原始数据集。(解压命令可参考 tar -xvf *.tarunzip *.zip

    本模型精度验证使用 COCO val2017 数据集及其标注文件。请用户自行获取数据集,并上传到服务器目录(如:/home/RT-DETR/rtdetrv2_pytorch/dataset/coco)。

    数据集下载链接:

    • http://images.cocodataset.org/zips/val2017.zip
    • http://images.cocodataset.org/annotations/annotations_trainval2017.zip

    数据集目录结构请参考:

    coco
    ├── annotations
    │   └── instances_val2017.json
    └── val2017
    
  2. 数据预处理。

    当前目录未提供单独的离线预处理脚本,infer_om.pyeval_om_coco.py 会在推理时自动完成如下处理:

    • 将图片缩放到 input_size x input_size
    • 将图像转为 RGB
    • 归一化到 [0, 1]
    • 转换为 NCHW 格式

    若导出 OM 时修改了 input_size,推理和评测时请保持相同尺寸参数。

模型推理

  1. 模型转换。

    使用 PyTorch 将模型权重文件 .pth 转换为 .onnx 文件,再使用 ATC 工具将 .onnx 文件转换为离线推理模型文件 .om

    1. 获取权重文件。

      准备与配置文件匹配的 PyTorch 权重文件,放入 RT-DETR/rtdetrv2_pytorch 目录或可访问路径下。

      下载路径:https://github.com/lyuwenyu/RT-DETR/tree/main/rtdetrv2_pytorch

    2. 导出 ONNX 文件并转换为 OM。

      1. 配置环境变量。

        source /usr/local/Ascend/ascend-toolkit/set_env.sh
        
      2. 进入上游工程目录,执行转换脚本。

        cd RT-DETR/rtdetrv2_pytorch
        python3 ./RT-DETRv2/export_om.py \
          -c configs/rtdetrv2/rtdetrv2_r18vd_120e_coco.yml \
          -r rtdetrv2_r18vd_120e_coco_rerun_48.1.pth \
          -o rtdetrv2_model \
          --soc-version Ascend910B4-1
        
        • 执行命令查看芯片名称(${chip_name})。
        npu-smi info
        

        参数说明:

        • -c:RT-DETRv2 配置文件路径
        • -r:PyTorch 权重文件路径
        • -o:输出 OM 文件名前缀
        • -s:输入尺寸,默认为 640
        • -b:batch size,默认为 1
        • --soc-version:芯片型号,如 Ascend310P3Ascend910B4-1
        • --keep-onnx:保留中间 ONNX 文件(可选)

        执行成功后可获得:

        • rtdetrv2_model.onnx(若保留中间文件)
        • rtdetrv2_model.om
  2. 开始推理验证。

    a. 安装 ais_bench 推理工具。

    本文档“安装依赖”步骤已经包含 ais_bench 安装命令,完成后即可直接调用本目录脚本执行 OM 推理。

    b. 执行推理。

    python3 ./RT-DETRv2/infer_om.py \
      -m rtdetrv2_model.om \
      -i /path/to/image.jpg \
      -o result.jpg \
      -s 640 \
      -t 0.5
    

    参数说明:

    • -m/--model:OM 模型文件路径
    • -d/--data-path:COCO 数据集根目录,脚本默认读取其中的 val2017
    • -i/--image:指定单张图片路径;不指定时默认从 val2017 随机选图
    • -o/--output:结果图保存路径
    • -s/--input-size:输入尺寸,需与导出 OM 时保持一致
    • -t/--threshold:置信度阈值
    • --device:NPU device id

    执行成功后将在当前目录生成带检测框的结果图片。

    c. 性能测试。

    python3 -m ais_bench --model rtdetrv2_model.om --loop 100
    

    d. 精度验证。

    python3 ./RT-DETRv2/eval_om_coco.py \
      --model rtdetrv2_model.om \
      --image-dir ./dataset/coco/val2017 \
      --ann-file ./dataset/coco/annotations/instances_val2017.json \
      --size 640 \
      --output-json predictions.json \
      --zero-based-label
    

    参数说明:

    • --model:OM 模型路径
    • --image-dir:COCO val2017 图片目录
    • --ann-file:COCO 标注文件路径
    • --size:输入尺寸=640,需与导出 OM 时保持一致
    • --output-json:预测结果 JSON 输出路径
    • --zero-based-label:将 0~79 标签映射回 COCO category_id

    当前导出的 RT-DETRv2 部署后处理默认输出 0~79 类别索引,评估 COCO 精度时建议加上 --zero-based-label

模型推理性能&精度

###性能

Model Batch Size Input Size FPS Card
RT-DETRv2 1 640x640 223 Ascend910B4
RT-DETRv2 1 640x640 217 T4 GPU

###精度

Model 数据集 APval AP50val Card
RT-DETRv2 coco val2017 48.0 65.0 Ascend910B4
RT-DETRv2 coco val2017 48.1 65.1 T4 GPU

公网地址说明

类型 说明 公网地址
开源代码仓 RT-DETR 上游源码 https://github.com/lyuwenyu/RT-DETR
数据集 COCO val2017 http://images.cocodataset.org/zips/val2017.zip
数据集 COCO annotations http://images.cocodataset.org/annotations/annotations_trainval2017.zip
推理工具 ais_bench https://gitee.com/ascend/tools/tree/master/ais-bench_workload/tool/ais_bench