rtdetr
Ultralytics-style API, 100% Apache-2.0. RT-DETR object detection you can ship
inside a product: train with PyTorch, run with OpenVINO, and keep the workflow
your team already knows — model.train(...), model.predict(...),
results[0].boxes.xyxy.
Every line of the network, loss, trainer, validator and exporter is an original implementation. No Ultralytics code, no AGPL weights, no license surprises.
pip install rtdetr # inference (numpy, opencv, openvino, pyyaml)
pip install "rtdetr[train]" # + training (torch, torchvision, scipy, onnx)
from rtdetr import RTDETR
model = RTDETR("rtdetr-r18") # weights from the mirror (see "Pretrained weights")
results = model("bus.jpg", conf=0.5) # list[Results]
r = results[0]
r.boxes.xyxy, r.boxes.conf, r.boxes.cls # plain numpy
r.names[int(r.boxes.cls[0])] # "person"
r.plot(); r.save(); r.show()
model = RTDETR("rtdetr-r18")
model.train(data="data.yaml", epochs=100, imgsz=640, batch=8, device=0)
metrics = model.val(data="data.yaml") # metrics.box.map50 / .box.map
model.export(format="openvino", half=True) # IR + labels.txt, ready to deploy
Why this exists
Ultralytics YOLO is excellent and its API is muscle memory for a lot of people —
but the code and weights are AGPL-3.0, which rules them out of most shipped
products. rtdetr keeps the ergonomics and drops the license problem.
Predicting
Any source a YOLO user would expect:
| Source | Example |
|---|---|
| image | model("bus.jpg") |
| glob | model("frames/*.jpg") |
| folder | model("dataset/images") |
| list file | model("images.txt") |
| URL | model("https://example.com/bus.jpg") |
| video | model("clip.mp4") |
| stream | model("rtsp://camera/live") |
| webcam | model(0) |
| array | model(numpy_bgr) / model(pil_image) |
| list | model(["a.jpg", "b.jpg"]) |
for r in model.predict("clip.mp4", conf=0.4, stream=True): # generator, O(1) memory
print(r.boxes.xyxyn)
model.predict("bus.jpg", save=True) # writes runs/detect/predict/bus.jpg
model.track("clip.mp4") # IoU tracker -> r.boxes.id
The log line reads the way you expect:
image 1/1 bus.jpg: 640x640 4 persons, 1 bus, 12.3ms
Results
| Attribute | What you get |
|---|---|
r.boxes.xyxy |
(N, 4) pixel corners |
r.boxes.xywh |
(N, 4) pixel centre + size |
r.boxes.xyxyn / r.boxes.xywhn |
the same, normalized 0..1 |
r.boxes.conf / r.boxes.cls |
(N,) scores and class indices |
r.boxes.id |
track ids after model.track(...), else None |
r.names |
{0: "person", ...} |
r.plot() |
annotated BGR ndarray |
r.save() / r.show() |
write / display it |
r.summary() |
detections as JSON-ready dicts |
r.speed |
{"preprocess": ms, "inference": ms, "postprocess": ms} |
Training
YOLO-format labels — the same data.yaml and images/ + labels/*.txt layout:
path: /data/cans
train: images/train
val: images/val
names:
0: can
1: bottle
model = RTDETR("rtdetr-r18")
best = model.train(
data="data.yaml", epochs=100, imgsz=640, batch=8,
device=0, workers=4, project="runs", name="train",
resume=False, patience=50, lr0=1e-4, seed=0,
)
# runs/train/weights/best.pt (last.pt too — resume=True picks the run back up)
AdamW with a 10× lower LR on the backbone, linear warmup into cosine decay, AMP
on CUDA, mAP50-95 after every epoch, early stop on patience.
Exporting and deploying
model = RTDETR("runs/train/weights/best.pt")
xml = model.export(format="openvino", half=True, imgsz=640)
Writes best.xml + best.bin, plus labels.txt (one class name per line)
next to the IR — that is how downstream runtimes discover class names.
The exported graph emits probabilities, not logits. Anything that decodes it
must not apply a sigmoid a second time; this package checks the score range and
skips it (rtdetr/predictor.py).
CLI
Same key=value shape as the yolo command:
rtdetr predict model=rtdetr-r18 source=bus.jpg conf=0.5
rtdetr train model=rtdetr-r18 data=data.yaml epochs=100
rtdetr val model=best.pt data=data.yaml
rtdetr export model=best.pt format=openvino half=true
rtdetr track model=best.pt source=clip.mp4
Pretrained weights
RTDETR("rtdetr-r18") downloads the IR for that name on first use and caches it
in ~/.rtdetr/ ($RTDETR_HOME to move it, $RTDETR_ASSETS_URL to point at an
internal mirror — handy for air-gapped sites). Known names: rtdetr-r18,
rtdetr-r34, rtdetr-r50; each mirror entry is <name>/<name>.xml, .bin,
.pt and labels.txt.
The weights are the original RT-DETR COCO checkpoints, which their authors
release under Apache-2.0. This package's network matches that reference layout
module for module, so they load with strict=True — no remapping and no
re-training. Building the whole mirror takes a few CPU-minutes:
python tools/build_mirror.py --out mirror # r18 + r34 + r50, .pt + IR + labels
huggingface-cli upload leeyunjai/rtdetr mirror . --repo-type=model
Until that upload happens,
RTDETR("rtdetr-r18")raises aModelNotFoundErrornaming the ways forward — it never silently falls back to an untrained network. Point$RTDETR_ASSETS_URLat any host serving the same layout to use it now.
For a single checkpoint without the IR:
python tools/convert_official.py --variant r18 --out rtdetr-r18.pt
Design notes
- Backbone PResNet-vd 18/34/50 — three-conv stem, average-pooled shortcuts.
- Encoder AIFI transformer on the top level + CCFF (CSPRepLayer) FPN/PAN fusion.
- Decoder two-stage: dense heads pick the top-300 encoder tokens as queries, 3/4/6 layers refine them with multi-scale deformable cross-attention.
- Loss Hungarian matching, varifocal + L1 + GIoU, over every decoder layer and the encoder's own proposals.
- Validation COCO-style mAP50 / mAP50-95, 101-point interpolation, no pycocotools dependency.
- Inference OpenVINO, plain resize (no letterbox) to match training.
The network definition under rtdetr/nn/ is adapted from the RT-DETR reference
implementation (lyuwenyu/RT-DETR,
Apache-2.0) precisely so its released weights load here unchanged; see
NOTICE. Everything around it — packaging, API, trainer, validator,
exporter, predictor, CLI — is this project's own. Nothing here derives from an
AGPL-licensed project.
Deformable attention is the grid_sample formulation, so the export path stays
plain ONNX (opset 16+) → OpenVINO; a test pins it to within 1e-4 of eager
PyTorch, and the assembled model matches the reference implementation to ~1e-5.
Development
pip install -e ".[train,dev]"
pytest -q # the parity checklist lives in tests/
ruff check .
Releasing
Push a v* tag and GitHub Actions builds and uploads to PyPI:
git tag v0.1.0
git push origin v0.1.0
.github/workflows/publish.yml uses PyPI trusted publishing — no API token
lives in the repo. One-time setup on PyPI (Publishing → add a pending publisher):
| Field | Value |
|---|---|
| PyPI project | rtdetr |
| Owner | leeyunjai82 |
| Repository | rtdetr |
| Workflow | publish.yml |
| Environment | pypi |
The job refuses to publish when the tag and pyproject.toml version disagree, so
bump the version in the same commit you tag.
한국어 빠른 시작
Ultralytics YOLO와 사용법이 같지만 라이선스는 Apache-2.0입니다. 제품에 넣어도 문제없습니다.
from rtdetr import RTDETR
model = RTDETR("rtdetr-r18") # 미러에서 가중치 다운로드 (아래 "Pretrained weights" 참고)
results = model("bus.jpg", conf=0.5) # list[Results]
results[0].boxes.xyxy # numpy 배열
results[0].save() # 결과 이미지 저장
model.train(data="data.yaml", epochs=100) # YOLO 형식 라벨 그대로
model.val(data="data.yaml").box.map50 # mAP50
model.export(format="openvino", half=True) # IR + labels.txt
가중치 캐시는 ~/.rtdetr/, 사내 미러는 RTDETR_ASSETS_URL 환경변수로 지정합니다.
License
Apache-2.0. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rtdetr-0.2.0.tar.gz.
File metadata
- Download URL: rtdetr-0.2.0.tar.gz
- Upload date:
- Size: 54.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c772d53015e1ad205ae707957b4e4b5d5b6495f9691ac132127d6a641e63a89e
|
|
| MD5 |
198f5c4e41e5c9dd3338f07a62c04420
|
|
| BLAKE2b-256 |
f5f976f5ba8d498b85e3fe356685b53869a132d05ae3f83b9a366015c4282c21
|
Provenance
The following attestation bundles were made for rtdetr-0.2.0.tar.gz:
Publisher:
publish.yml on leeyunjai82/rtdetr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rtdetr-0.2.0.tar.gz -
Subject digest:
c772d53015e1ad205ae707957b4e4b5d5b6495f9691ac132127d6a641e63a89e - Sigstore transparency entry: 2583204704
- Sigstore integration time:
-
Permalink:
leeyunjai82/rtdetr@5ecad06ca9025147ff142a7399c9d7f1bd94c1e6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/leeyunjai82
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5ecad06ca9025147ff142a7399c9d7f1bd94c1e6 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file rtdetr-0.2.0-py3-none-any.whl.
File metadata
- Download URL: rtdetr-0.2.0-py3-none-any.whl
- Upload date:
- Size: 52.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23110fa40ef36bea48a812665f805fa4bacb4111505d185d167fc1f8619f6d1a
|
|
| MD5 |
43069492789ffe473a6ee994c297b566
|
|
| BLAKE2b-256 |
7f7878c7d35e32b3033a4ae5b77c545fec6c6cfc4ff3cc36d8b715854d878ee6
|
Provenance
The following attestation bundles were made for rtdetr-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on leeyunjai82/rtdetr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rtdetr-0.2.0-py3-none-any.whl -
Subject digest:
23110fa40ef36bea48a812665f805fa4bacb4111505d185d167fc1f8619f6d1a - Sigstore transparency entry: 2583204713
- Sigstore integration time:
-
Permalink:
leeyunjai82/rtdetr@5ecad06ca9025147ff142a7399c9d7f1bd94c1e6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/leeyunjai82
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5ecad06ca9025147ff142a7399c9d7f1bd94c1e6 -
Trigger Event:
workflow_dispatch
-
Statement type: