mcpField
T
tao-train-rtdetr

tao-train-rtdetr

github.com/promptingcompany数据与数据库039 次安装17 周前更新
代码仓库

关于 tao-train-rtdetr

tao-train-rtdetr 是用于 TAO 的 RT-DETR 目标检测技能,面向模型训练、评估和部署流程。它提供数据集要求、配置与操作指引,并支持蒸馏、量化及 TensorRT 部署操作。

主要功能

  1. 01RT-DETR 2D 目标检测
  2. 02训练、评估与推理操作
  3. 03支持蒸馏和量化
  4. 04TensorRT 引擎生成与部署
  5. 05多 GPU 与多节点训练

适用场景

  1. 01使用 COCO 格式数据集训练检测模型
  2. 02评估模型或对图像执行推理
  3. 03通过蒸馏或量化优化模型
  4. 04生成 TensorRT 引擎用于部署

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/promptingcompany/nv-skills --skill tao-train-rtdetr

安装后在 Claude Code 中输入/tao-train-rtdetr确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

BENCHMARK.md
skill-card.md
skill.oms.sig
evals/
evals.json
references/
skill_info.yaml
spec_template_deploy_evaluate.yaml
spec_template_deploy_gen_trt_engine.yaml
spec_template_deploy_inference.yaml
spec_template_distill.yaml
spec_template_evaluate.yaml
spec_template_export.yaml
spec_template_gen_trt_engine.yaml
spec_template_inference.yaml
spec_template_quantize.yaml
spec_template_train.yaml
tao-deploy-rtdetr.md
tao-deploy-rtdetr.skill_info.yaml
schemas/
distill.schema.json
evaluate.schema.json
export.schema.json
gen_trt_engine.schema.json
inference.schema.json
manifest.json
quantize.schema.json
train.schema.json

详细介绍

RT-DETR

RT-DETR(实时目标检测 Transformer)用于 2D 目标检测。专为实时推理设计,兼具出色的准确率。支持蒸馏和量化,以优化部署。

设置 model.pretrained_backbone_path 以指定主干网络权重,或设置 train.pretrained_model_path 以指定完整模型。

对于 TAO Deploy TensorRT 操作(gen_trt_engine、TensorRT evaluate 和 TensorRT inference),请先阅读 references/tao-deploy-rtdetr.md。部署规范模板位于此技能的 references/ 文件夹中,文件名以 spec_template_deploy_*.yaml 为前缀。

数据类架构

生成的 TAO Core 架构打包在 schemas/<action>.schema.json 中,schemas/manifest.json 列出了可用操作。每个生成的架构还会根据架构顶层的 default 字段生成 references/spec_template_<action>.yaml。AutoML 是否启用由模型层在 references/skill_info.yaml 中通过 automl_enabled 声明。要运行 AutoML,还需要 schemas/train.schema.json 和 references/spec_template_train.yaml 存在且可解析。使用打包的训练架构获取 automl_default_parameters、automl_disabled_parameters、默认值、最小值/最大值边界、枚举值、选项权重、数学条件、依赖项和热门参数。不要假定运行时存在 ~/tao-core;维护者会在打包技能库前重新生成架构/模板。

训练操作策略

此模型在模型层启用了 AutoML。处理任何训练阶段请求之前,请阅读 references/skill_info.yaml,并根据显式的 automl_policy 值或用户的工作流请求确定本次运行的策略。将“turn off AutoML”“disable AutoML”“no HPO”或“plain training”等表述视为仅对本次运行设置 automl_policy: off;否则默认为 auto。当 automl_policy: auto、automl_enabled: true,且已打包 schemas/train.schema.json 和 references/spec_template_train.yaml 时,默认通过 tao-skill-bank:tao-run-automl 执行训练操作,并传入此模型的 skill_dir。保留工作流/应用对数据集、规范、输出目录、GPU/平台设置、父检查点和 automl_policy 的覆盖设置。仅当 automl_policy: off 或缺少已打包的训练架构/模板时,才直接训练模型;如果缺少架构,请报告 AutoML 已启用,但在生成架构之前无法为此模型运行。

evaluate、inference、export 和部署流程等非训练操作仍由此模型技能处理。每次运行的 automl_policy 覆盖设置不会更改模型元数据。

训练要求

  • 数据集类型: object_detection
  • 格式: coco、coco_raw
  • 监控指标: val_mAP50
各操作的数据集要求
操作规格键来源文件列表?
蒸馏dataset.train_data_sourcestrain_datasetsimage_dir: images.tar.gz, json_file: annotations.json是
蒸馏dataset.val_data_sourceseval_datasetimage_dir: images.tar.gz, json_file: annotations.json否
评估dataset.test_data_sourceseval_datasetimage_dir: images.tar.gz, json_file: annotations.json否
生成 TRT 引擎gen_trt_engine.tensorrt.calibration.cal_image_dircalibration_datasetimages.tar.gz是
推理dataset.infer_data_sourcesinference_datasetimage_dir: images.tar.gz, classmap: label_map.txt否
量化dataset.train_data_sourcestrain_datasetsimage_dir: images.tar.gz, json_file: annotations.json是
量化dataset.val_data_sourceseval_datasetimage_dir: images.tar.gz, json_file: annotations.json否
量化dataset.quant_calibration_data_sourcestrain_datasetsimage_dir: images.tar.gz, json_file: annotations.json否
训练dataset.train_data_sourcestrain_datasetsimage_dir: images.tar.gz, json_file: annotations.json是
训练dataset.val_data_sourceseval_datasetimage_dir: images.tar.gz, json_file: annotations.json否
典型规格覆盖项

每项操作都必须覆盖数据源——Agent 必须根据上方“各操作的数据集要求”表构建数据源路径,并将其包含在 spec_overrides 中。

S3_TRAIN = "s3://bucket/data/train"
S3_EVAL = "s3://bucket/data/eval"

训练(必需的数据源):

{
    "train.num_epochs": 10,
    "train.checkpoint_interval": 10,
    "train.validation_interval": 10,
    "train.num_gpus": 1,
    "dataset.num_classes": "<num_classes> + 1",
    "dataset.train_data_sources": [{"image_dir": f"{S3_TRAIN}/images.tar.gz", "json_file": f"{S3_TRAIN}/annotations.json"}],
    "dataset.val_data_sources": {"image_dir": f"{S3_EVAL}/images.tar.gz", "json_file": f"{S3_EVAL}/annotations.json"},
}

评估(必需的数据源):

{
    "dataset.num_classes": "<num_classes> + 1",
    "dataset.test_data_sources": {"image_dir": f"{S3_EVAL}/images.tar.gz", "json_file": f"{S3_EVAL}/annotations.json"},
}

导出:

{
    "dataset.num_classes": "<num_classes> + 1",
    "export.input_height": 640,
    "export.input_width": 640,
}

量化(必需的数据源):

{
    "dataset.num_classes": "<num_classes> + 1",
    "quantize.layers": [
        {
            "module_name": "*",
            "weights": {
                "dtype": "float8_e4m3fn"
            },
            "activations": {
                "dtype": "float8_e4m3fn"
            }
        }
    ],
    "dataset.train_data_sources": [{"image_dir": f"{S3_TRAIN}/images.tar.gz", "json_file": f"{S3_TRAIN}/annotations.json"}],
    "dataset.val_data_sources": {"image_dir": f"{S3_EVAL}/images.tar.gz", "json_file": f"{S3_EVAL}/annotations.json"},
    "dataset.quant_calibration_data_sources": {"image_dir": f"{S3_TRAIN}/images.tar.gz", "json_file": f"{S3_TRAIN}/annotations.json"},
}

生成 TRT 引擎(必需的数据源):

{
    "gen_trt_engine.tensorrt.data_type": "FP16",
    "gen_trt_engine.tensorrt.calibration.cal_image_dir": [f"{S3_TRAIN}/images.tar.gz"],
}

推理(必需的数据源):

{
    "dataset.num_classes": "<num_classes> + 1",
    "dataset.infer_data_sources": {"image_dir": f"{S3_EVAL}/images.tar.gz", "classmap": f"{S3_EVAL}/label_map.txt"},
}

蒸馏(必需的数据源):

{
    "dataset.train_data_sources": [{"image_dir": f"{S3_TRAIN}/images.tar.gz", "json_file": f"{S3_TRAIN}/annotations.json"}],
    "dataset.val_data_sources": {"image_dir": f"{S3_EVAL}/images.tar.gz", "json_file": f"{S3_EVAL}/annotations.json"},
}

评估数据集

可选。如果提供,则会在每个检查点提供验证集 mAP。

重要参数

  • dataset.num_classes:类别数量。默认值为 80(MSCOCO 的 80 个类别)。必须与数据集标注相符。
  • model.backbone:默认值为 resnet_50。支持:ResNet 变体、ConvNeXt、FAN、EfficientViT。RT-DETR 针对实时性能进行了优化,使用更轻量的骨干网络。
  • train.optim.lr:学习率。默认值为 1e-4(低于 DINO 的 2e-4)。lr_backbone 默认值为 1e-5。
  • dataset.augmentation.train_spatial_size:训练输入尺寸。默认值为 [640, 640]。小于 DINO 的多尺度输入(最高 1333)。这是 RT-DETR 实现高速运行的关键。
  • model.num_feature_levels:默认值为 3(DINO 为 4)。return_interm_indices 为 [1,2,3]。
  • train.enable_ema:指数移动平均。默认值为 False。启用后可能使收敛更平稳。
  • dataset.remap_mscoco_category:默认值为 False。仅当使用原始 MSCOCO 数据集且需要将类别 ID 从 91 映射至 80 时,才设为 True。

多 GPU / 多节点

启动方式: torchrun(LIGHTNING_EXCLUDED_NETWORK)。入口点运行 torchrun --nnodes=N --nproc-per-node=M train.py,而非普通的 python。

规格键描述默认值
train.num_gpus每个节点的 GPU 数量1
train.gpu_idsGPU 设备索引[0]
train.num_nodes节点数量1
train.distributed_strategyddp 或 fsdpddp
  • 明确设置了 CUDA_VISIBLE_DEVICES(与使用 TAO_VISIBLE_DEVICES 的 Lightning 管理模型不同)
  • 使用激活检查点时的 ddp:find_unused_parameters=False
  • 不使用激活检查点时的 ddp:find_unused_parameters=True
  • 支持 fsdp,并强制使用 FP16

多节点环境变量(由编排器设置):

变量用途
WORLD_SIZE节点数量(触发多节点模式)
NODE_RANK当前节点的等级(从 0 开始)
MASTER_ADDR等级为 0 的节点 IP
MASTER_PORT等级为 0 的端口(默认 29500)
NUM_GPU_PER_NODE每个节点的 GPU 数量(默认值:所有可见 GPU)

重要: 如果未设置 RANK,则会将 NODE_RANK 复制到 RANK。这是 torchrun 多节点运行的必要条件。

导出 / TRT 默认值

  • 导出输入:640x640,opset 17
  • TRT 数据类型:FP32、FP16、INT8
  • TRT 工作区:1024 MB
  • TRT max_batch_size:4

完整的 TAO Deploy 参考文档:tao-deploy-rtdetr。

蒸馏

RT-DETR 支持使用教师模型进行知识蒸馏。需要使用 distill 操作,并提供教师模型路径和蒸馏绑定配置。

硬件

至少需要 1 块 GPU,建议使用 2 块 GPU。每块 GPU 需配备 16GB 以上显存(V100 或 A100)。由于输入尺寸较小(640x640)且特征层较少,RT-DETR 比 DINO/GDINO 更节省显存。在小型到中型数据集上,使用单块 GPU 也能很好地训练。

错误模式

CUDA 显存不足:减小 batch_size。640x640 的 RT-DETR 比 1333px 的 DINO 更轻量,但在 16GB GPU 上,batch_size > 8 仍可能导致显存不足。

num_classes 不匹配:RT-DETR 默认值为 80(DINO 的默认值为 91)。确保 dataset.num_classes 与标注类别数一致。

return_interm_indices 与 num_feature_levels:默认值为 [1,2,3] 和 num_feature_levels=3。若更改,二者必须保持一致。

Spec 参数 / 父模型推断

特定于模型的推断映射应放在此 MD 文件中,而不是 config.json 中。生成的运行器应读取本节内容,并在调用 create_job() 前使用 SDK 辅助函数应用这些映射。这与旧版微服务的 infer_params.py 流程一致。

来自 TAO Core rtdetr.config.json 的推断映射:

操作Spec 字段推断函数含义
distilldistill.pretrained_teacher_model_pathparent_model根据父作业结果文件夹推断模型文件
distillencryption_keykey加密密钥
distillresults_diroutput_dir当前作业结果目录
evaluateencryption_keykey加密密钥
evaluateevaluate.checkpointparent_model根据父作业结果文件夹推断模型文件
evaluateevaluate.trt_engineparent_model根据父作业结果文件夹推断模型文件
evaluateresults_diroutput_dir当前作业结果目录
exportencryption_keykey加密密钥
exportexport.checkpointparent_model根据父作业结果文件夹推断模型文件
exportexport.onnx_filecreate_onnx_file输出 ONNX 路径
exportresults_diroutput_dir当前作业结果目录
gen_trt_engineencryption_keykey加密密钥
gen_trt_enginegen_trt_engine.onnx_fileparent_model根据父作业结果文件夹推断模型文件
gen_trt_enginegen_trt_engine.tensorrt.calibration.cal_cache_filecreate_cal_cache校准缓存路径
gen_trt_enginegen_trt_engine.trt_enginecreate_engine_file输出 TensorRT 引擎路径
gen_trt_engineresults_diroutput_dir当前作业结果目录
inferenceencryption_keykey加密密钥
inferenceinference.checkpointparent_model根据父作业结果文件夹推断模型文件
inferenceinference.trt_engineparent_model根据父作业结果文件夹推断模型文件
inferenceresults_diroutput_dir当前作业结果目录
quantizeencryption_keykey加密密钥
quantizequantize.model_pathparent_model根据父作业结果文件夹推断模型文件
quantizeresults_diroutput_dir当前作业结果目录
trainencryption_keykey加密密钥
trainmodel.pretrained_backbone_pathptm_if_no_resume_model不存在恢复检查点时使用的 PTM
trainresults_diroutput_dir当前作业结果目录
traintrain.pretrained_model_pathptm_if_no_resume_model不存在恢复检查点时使用的 PTM
traintrain.resume_training_checkpoint_pathresume_model根据当前作业结果文件夹推断模型文件

对于 parent_model 或 parent_model_folder,请将上游 train/export/AutoML 子作业 ID 作为 parent_job_id 传入。SDK 会列出父结果文件夹、筛选检查点制品,并返回选定的模型文件或文件夹。不要将这些映射加回 config.json,也不要修改生成的运行器脚本来猜测检查点路径。

评论

支持 Markdown