How rt-detrs/rt-detrv4 works
Purpose and entry points
RT-DETRv4 is a PyTorch real-time object-detection training and deployment repository. Its primary path is train.py, driven by hierarchical YAML configurations such as configs/rtv4/rtv4_hgnetv2_s_coco.yml. The same framework can reproduce RT-DETRv2, D-FINE, and DEIM model families through their config sets.
Configuration and object assembly
The configuration/workspace layer resolves YAML inheritance, runtime settings, and named component construction. This separates experiment definitions from Python implementations and permits model, dataset, optimizer, and solver substitution.
• engine/core/yaml_config.py
• engine/core/workspace.py
• configs/runtime.yml
Data ingestion and evaluation boundary
Detection data enters through configurable PyTorch dataloaders, COCO/VOC dataset adapters, augmentations, and batch collation. COCO-format custom datasets are the main supported custom-data boundary; evaluators calculate detection metrics from model predictions.
• engine/data/dataloader.py
• engine/data/dataset/coco_dataset.py
• engine/data/transforms/
Detector architecture
The RT-DETRv4 path composes a visual backbone, hybrid encoder, transformer-style decoder, matching/criterion loss, and prediction postprocessing. Backbones include HGNetv2 and ResNet/CSP variants; selected configuration determines the concrete graph.
• engine/backbone/hgnetv2.py
• engine/rtv4/rtv4.py
• engine/rtv4/hybrid_encoder.py
Foundation-model distillation
A central RT-DETRv4-specific training stage loads an external DINOv2 or DINOv3 vision foundation model as teacher, producing representations used by the student detector’s loss path. Teacher source and checkpoint locations are supplied by config; DINOv3 code/weights are external prerequisites rather than vendored infrastructure.
• engine/rtv4/dinov3_teacher.py
• engine/rtv4/dinov2_teacher.py
• configs/rtv4/rtv4_hgnetv2_l_coco.yml
Training, distributed execution, and optimization
train.py delegates task lifecycle to detection solver/engine classes: distributed initialization, train/eval loops, checkpoint restore, AMP, EMA, schedulers, and optimizer updates. Runtime assumes CUDA-capable PyTorch; documented execution uses torchrun across GPUs.
• train.py
• engine/solver/det_solver.py
• engine/optim/amp.py
Inference and deployment outputs
Trained checkpoints can run through native PyTorch inference or be exported to ONNX, then consumed by ONNX Runtime, OpenVINO, or TensorRT. TensorRT engines are also benchmarked for latency; these are downstream execution targets, not required for core training.
• tools/deployment/export_onnx.py
• tools/inference/torch_inf.py
• tools/inference/trt_inf.py
Main entry points: det_solver.py Detection solver · workspace.py Component workspace · yaml_config.py YAML resolver · rtv4.py RT-DETRv4 detector
Components 19
Configuration
- YAML resolver · Resolves inherited experiment settings.
engine/core/yaml_config.py - Component workspace · Builds named model, data, and solver components.
engine/core/workspace.py - Runtime defaults
configs/runtime.yml - RT-DETRv4 experiment · Selects the detector, teacher, and training recipe.
configs/rtv4/rtv4_hgnetv2_l_coco.yml
Data & Evaluation
- Detection dataloader
engine/data/dataloader.py - COCO/VOC adapters · COCO format is the primary custom-data interface.
engine/data/dataset/coco_dataset.py - Augmentations
Detector & Distillation
- RT-DETRv4 detector · Composes backbone, encoder, decoder, loss, and postprocessing.
engine/rtv4/rtv4.py - Visual backbone · HGNetv2 and other configured backbone variants.
engine/backbone/hgnetv2.py - Hybrid encoder
engine/rtv4/hybrid_encoder.py - DINO foundation teacher · Configured DINOv2/DINOv3 checkpoints provide distillation representations.
engine/rtv4/dinov3_teacher.py
Training Runtime
- Training entry point
train.py - Detection solver · Owns distributed train/eval lifecycle and checkpointing.
engine/solver/det_solver.py - AMP execution
engine/optim/amp.py - Trained checkpoint
Inference & Deployment
- PyTorch inference
tools/inference/torch_inf.py - ONNX export
tools/deployment/export_onnx.py - ONNX deployment targets · ONNX Runtime, OpenVINO, or TensorRT consume exported models.
- TensorRT inference · Runs and benchmarks TensorRT engines.
tools/inference/trt_inf.py
Connections 22
- Runtime defaults → YAML resolver: default settings
- RT-DETRv4 experiment → YAML resolver: experiment recipe
- YAML resolver → Component workspace: resolved definitions
- Training entry point → YAML resolver: loads config
- Component workspace → Detection dataloader: constructs
- Component workspace → RT-DETRv4 detector: constructs
- Component workspace → DINO foundation teacher: constructs
- Component workspace → Detection solver: constructs
- COCO/VOC adapters → Augmentations: samples
- Augmentations → Detection dataloader: batches
- Training entry point → Detection solver: runs
- Detection dataloader → Detection solver: training/eval batches
- Visual backbone → Hybrid encoder: visual features
- Hybrid encoder → RT-DETRv4 detector: encoded features
- DINO foundation teacher → RT-DETRv4 detector: distillation representations
- RT-DETRv4 detector → Detection solver: predictions and losses
- AMP execution → Detection solver: mixed precision
- Detection solver → Trained checkpoint: saves/restores
- Trained checkpoint → PyTorch inference: loads
- Trained checkpoint → ONNX export: exports
- ONNX export → ONNX deployment targets: ONNX model
- ONNX deployment targets → TensorRT inference: TensorRT engine