关于 tao-train-rtdetr
tao-train-rtdetr 是用于 TAO 的 RT-DETR 目标检测技能,面向模型训练、评估和部署流程。它提供数据集要求、配置与操作指引,并支持蒸馏、量化及 TensorRT 部署操作。
主要功能
- 01RT-DETR 2D 目标检测
- 02训练、评估与推理操作
- 03支持蒸馏和量化
- 04TensorRT 引擎生成与部署
- 05多 GPU 与多节点训练
适用场景
- 01使用 COCO 格式数据集训练检测模型
- 02评估模型或对图像执行推理
- 03通过蒸馏或量化优化模型
- 04生成 TensorRT 引擎用于部署
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ promptingcompany/ nv-skills --skill tao-train-rtdetr安装后在 Claude Code 中输入/tao-train-rtdetr确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
BENCHMARK.mdskill-card.mdskill.oms.sigevals/evals.jsonreferences/skill_info.yamlspec_template_deploy_evaluate.yamlspec_template_deploy_gen_trt_engine.yamlspec_template_deploy_inference.yamlspec_template_distill.yamlspec_template_evaluate.yamlspec_template_export.yamlspec_template_gen_trt_engine.yamlspec_template_inference.yamlspec_template_quantize.yamlspec_template_train.yamltao-deploy-rtdetr.mdtao-deploy-rtdetr.skill_info.yamlschemas/distill.schema.jsonevaluate.schema.jsonexport.schema.jsongen_trt_engine.schema.jsoninference.schema.jsonmanifest.jsonquantize.schema.jsontrain.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_sources | train_datasets | image_dir: images.tar.gz, json_file: annotations.json | 是 |
| 蒸馏 | dataset.val_data_sources | eval_dataset | image_dir: images.tar.gz, json_file: annotations.json | 否 |
| 评估 | dataset.test_data_sources | eval_dataset | image_dir: images.tar.gz, json_file: annotations.json | 否 |
| 生成 TRT 引擎 | gen_trt_engine.tensorrt.calibration.cal_image_dir | calibration_dataset | images.tar.gz | 是 |
| 推理 | dataset.infer_data_sources | inference_dataset | image_dir: images.tar.gz, classmap: label_map.txt | 否 |
| 量化 | dataset.train_data_sources | train_datasets | image_dir: images.tar.gz, json_file: annotations.json | 是 |
| 量化 | dataset.val_data_sources | eval_dataset | image_dir: images.tar.gz, json_file: annotations.json | 否 |
| 量化 | dataset.quant_calibration_data_sources | train_datasets | image_dir: images.tar.gz, json_file: annotations.json | 否 |
| 训练 | dataset.train_data_sources | train_datasets | image_dir: images.tar.gz, json_file: annotations.json | 是 |
| 训练 | dataset.val_data_sources | eval_dataset | image_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_ids | GPU 设备索引 | [0] |
train.num_nodes | 节点数量 | 1 |
train.distributed_strategy | ddp 或 fsdp | ddp |
- 明确设置了
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 字段 | 推断函数 | 含义 |
|---|---|---|---|
| distill | distill.pretrained_teacher_model_path | parent_model | 根据父作业结果文件夹推断模型文件 |
| distill | encryption_key | key | 加密密钥 |
| distill | results_dir | output_dir | 当前作业结果目录 |
| evaluate | encryption_key | key | 加密密钥 |
| evaluate | evaluate.checkpoint | parent_model | 根据父作业结果文件夹推断模型文件 |
| evaluate | evaluate.trt_engine | parent_model | 根据父作业结果文件夹推断模型文件 |
| evaluate | results_dir | output_dir | 当前作业结果目录 |
| export | encryption_key | key | 加密密钥 |
| export | export.checkpoint | parent_model | 根据父作业结果文件夹推断模型文件 |
| export | export.onnx_file | create_onnx_file | 输出 ONNX 路径 |
| export | results_dir | output_dir | 当前作业结果目录 |
| gen_trt_engine | encryption_key | key | 加密密钥 |
| gen_trt_engine | gen_trt_engine.onnx_file | parent_model | 根据父作业结果文件夹推断模型文件 |
| gen_trt_engine | gen_trt_engine.tensorrt.calibration.cal_cache_file | create_cal_cache | 校准缓存路径 |
| gen_trt_engine | gen_trt_engine.trt_engine | create_engine_file | 输出 TensorRT 引擎路径 |
| gen_trt_engine | results_dir | output_dir | 当前作业结果目录 |
| inference | encryption_key | key | 加密密钥 |
| inference | inference.checkpoint | parent_model | 根据父作业结果文件夹推断模型文件 |
| inference | inference.trt_engine | parent_model | 根据父作业结果文件夹推断模型文件 |
| inference | results_dir | output_dir | 当前作业结果目录 |
| quantize | encryption_key | key | 加密密钥 |
| quantize | quantize.model_path | parent_model | 根据父作业结果文件夹推断模型文件 |
| quantize | results_dir | output_dir | 当前作业结果目录 |
| train | encryption_key | key | 加密密钥 |
| train | model.pretrained_backbone_path | ptm_if_no_resume_model | 不存在恢复检查点时使用的 PTM |
| train | results_dir | output_dir | 当前作业结果目录 |
| train | train.pretrained_model_path | ptm_if_no_resume_model | 不存在恢复检查点时使用的 PTM |
| train | train.resume_training_checkpoint_path | resume_model | 根据当前作业结果文件夹推断模型文件 |
对于 parent_model 或 parent_model_folder,请将上游 train/export/AutoML 子作业 ID 作为 parent_job_id 传入。SDK 会列出父结果文件夹、筛选检查点制品,并返回选定的模型文件或文件夹。不要将这些映射加回 config.json,也不要修改生成的运行器脚本来猜测检查点路径。