> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/models/supported-models/rf-detr.md).

# RF-DETR

RF-DETR is a real-time object detection and instance segmentation model from Roboflow, with keypoint detection and semantic segmentation variants. Train it on Roboflow, run it through the Serverless C

## RF-DETR Object Detection

RF-DETR is Roboflow's transformer-based real-time detection model. Run inference against COCO-pretrained object detection checkpoints through the [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), or self-host using [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

### RF-DETR Object Detection API

The steps below run RF-DETR through the [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api) and visualize results with [supervision](https://supervision.roboflow.com/).

{% stepper %}
{% step %}
**Get your API Key**

Create a Roboflow account, find your key on the [Roboflow API settings page](https://app.roboflow.com/settings/api) and make it available to your shell:

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}
**Install the dependencies**

These two packages call the model and draw its results:

```bash
pip install -U inference-sdk supervision
```

{% endstep %}

{% step %}
**Run the model**

Run `rfdetr-small` on a sample image and annotate boxes and labels:

```python
import os
import cv2
import supervision as sv
from inference_sdk import InferenceHTTPClient, InferenceConfiguration

image_url = "https://media.roboflow.com/quickstart/traffic.jpg"
image = sv.load_image_from_url(image_url)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
).configure(InferenceConfiguration(api_key_transport="header"))
result = client.infer(image, model_id="rfdetr-small")

detections = sv.Detections.from_inference(result)

annotated = sv.BoxAnnotator().annotate(image.copy(), detections)
annotated = sv.LabelAnnotator().annotate(annotated, detections)

cv2.imwrite("traffic-annotated.png", annotated)
```

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-ccf613f155ad9e3e50d2520e5d45bbd68dff5bde%2Frf-detr-detection.webp?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

{% hint style="info" %}
Set `api_url` to match your deployment target:

* `https://serverless.roboflow.com` for the Serverless Cloud API.
* `http://localhost:9001` for a local [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) server.
* Your [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) URL for a private endpoint.
  {% endhint %}

### RF-DETR Object Detection models and benchmarks

Pass any of these aliases as the `model_id` when running inference. The SDK resolves each alias to the underlying Roboflow project version.

<table data-search="false"><thead><tr><th>Alias</th><th>Input Size</th><th>mAP50-95</th><th>ONNX latency (ms)*</th><th>TensorRT FP16 (ms)*</th></tr></thead><tbody><tr><td><code>rfdetr-nano</code></td><td>384x384</td><td>48.4</td><td>9.7</td><td>6.2</td></tr><tr><td><code>rfdetr-small</code></td><td>512x512</td><td>53.0</td><td>12.9</td><td>8.3</td></tr><tr><td><code>rfdetr-medium</code></td><td>576x576</td><td>54.7</td><td>16.3</td><td>9.5</td></tr><tr><td><code>rfdetr-large</code></td><td>704x704</td><td>56.5</td><td>25.6</td><td>11.6</td></tr><tr><td><code>rfdetr-xlarge</code></td><td>700x700</td><td>58.6</td><td>41.6</td><td>14.9</td></tr><tr><td><code>rfdetr-2xlarge</code></td><td>880x880</td><td>60.1</td><td>53.4</td><td>21.7</td></tr></tbody></table>

## RF-DETR Instance Segmentation

RF-DETR also provides instance segmentation checkpoints that predict masks alongside boxes. Run them through the [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), or self-host using [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

### RF-DETR Instance Segmentation API

Set your API key and install the dependencies as shown above, then run a segmentation checkpoint and draw its masks:

```python
import os
import cv2
import supervision as sv
from inference_sdk import InferenceHTTPClient

image_url = "https://media.roboflow.com/quickstart/traffic.jpg"
image = sv.load_image_from_url(image_url)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)
result = client.infer(image, model_id="rfdetr-seg-preview")

detections = sv.Detections.from_inference(result)

annotated = sv.MaskAnnotator().annotate(image.copy(), detections)
annotated = sv.LabelAnnotator().annotate(annotated, detections)

cv2.imwrite("traffic-annotated.png", annotated)
```

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-ef481717e6be56837c022e17751f42458d9f288d%2Frf-detr-segmentation.webp?alt=media" alt=""><figcaption></figcaption></figure>

### RF-DETR Instance Segmentation models and benchmarks

Pass any of these aliases as the `model_id` when running inference. The SDK resolves each alias to the underlying Roboflow project version. Figures are mask mAP on COCO `val`.

<table data-search="false"><thead><tr><th>Alias</th><th>Input Size</th><th>Mask mAP50-95</th><th>ONNX latency (ms)*</th><th>TensorRT FP16 (ms)*</th></tr></thead><tbody><tr><td><code>rfdetr-seg-nano</code></td><td>312x312</td><td>40.3</td><td>15.8</td><td>10.5</td></tr><tr><td><code>rfdetr-seg-small</code></td><td>384x384</td><td>43.1</td><td>19.3</td><td>11.9</td></tr><tr><td><code>rfdetr-seg-medium</code></td><td>432x432</td><td>45.3</td><td>23.9</td><td>14.1</td></tr><tr><td><code>rfdetr-seg-large</code></td><td>504x504</td><td>47.1</td><td>30.1</td><td>15.4</td></tr><tr><td><code>rfdetr-seg-xlarge</code></td><td>624x624</td><td>48.8</td><td>51.2</td><td>19.1</td></tr><tr><td><code>rfdetr-seg-2xlarge</code></td><td>768x768</td><td>49.9</td><td>89.6</td><td>25.6</td></tr></tbody></table>

## RF-DETR Semantic Segmentation

RF-DETR semantic segmentation assigns a class to each pixel. Regions of the same class share a label rather than being split into separate objects. Pretrained checkpoints cover the 150 ADE20K scene classes (ex: sky, building, road, car, person). Run them through the [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), or self-host using [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

### RF-DETR Semantic Segmentation API

Set your API key and install the dependencies as shown above (supervision 0.32.0 or later), then run a pretrained checkpoint and draw its classes:

```python
import os

import cv2
import supervision as sv
from inference_sdk import InferenceHTTPClient, InferenceConfiguration

image_url = "https://media.roboflow.com/docs/mountain-lake-boats.jpg"
image = sv.load_image_from_url(image_url)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
).configure(InferenceConfiguration(
    api_key_transport="header",
    client_downsizing_disabled=True,
))
result = client.infer(image, model_id="rfdetr-pretrains/rfdetr-sem-large-ade20k")

detections = sv.Detections.from_inference(result)

palette = sv.ColorPalette.from_hex([
    "#22C55E", "#38BDF8", "#8B5CF6", "#F59E0B", "#EC4899",
    "#EF4444", "#2563EB", "#14B8A6", "#A3E635", "#FDE047",
])
annotated = sv.MaskAnnotator(
    color=palette, opacity=0.35, color_lookup=sv.ColorLookup.INDEX
).annotate(image.copy(), detections)
annotated = sv.PolygonAnnotator(color=sv.Color.WHITE, thickness=2).annotate(
    annotated, detections
)
annotated = sv.LabelAnnotator(
    color=palette,
    color_lookup=sv.ColorLookup.INDEX,
    text_position=sv.Position.CENTER_OF_MASS,
    text_scale=0.8,
    smart_position=True,
).annotate(annotated, detections)

cv2.imwrite("lake-annotated.png", annotated)
print(detections.data["class_name"])
```

`sv.Detections.from_inference` turns each class in the result into one detection whose mask covers every pixel of that class, so you can draw it with any mask annotator. Class names come from the result's `class_map`, and class ID 0 (the background) is left out. Keep client-side downsizing disabled so the mask matches the input image size.

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-afa78b6bde2a63ba07fb99e000a029cf0fb7c00e%2Frf-detr-semantic-segmentation.webp?alt=media" alt="Wooden boats on a mountain lake with RF-DETR semantic segmentation predictions: water, boats, a pier, trees, and rocks each in their own color and labeled"><figcaption><p>Pretrained RF-DETR Large ADE20K output from the example above. Photo: <a href="https://www.pexels.com/photo/wooden-boat-and-a-scenic-view-of-a-lake-and-mountains-14211208/">Christian Thöni</a>, <a href="https://www.pexels.com/license/">Pexels License</a>. Cropped.</p></figcaption></figure>

### RF-DETR Semantic Segmentation models and training

Pass any of these model IDs as the `model_id` when running inference. Each one is pretrained on ADE20K.

<table data-search="false"><thead><tr><th>Model ID</th><th>Input Size</th></tr></thead><tbody><tr><td><code>rfdetr-pretrains/rfdetr-sem-nano-ade20k</code></td><td>312x312</td></tr><tr><td><code>rfdetr-pretrains/rfdetr-sem-small-ade20k</code></td><td>384x384</td></tr><tr><td><code>rfdetr-pretrains/rfdetr-sem-medium-ade20k</code></td><td>432x432</td></tr><tr><td><code>rfdetr-pretrains/rfdetr-sem-large-ade20k</code></td><td>500x500</td></tr><tr><td><code>rfdetr-pretrains/rfdetr-sem-xlarge-ade20k</code></td><td>560x560</td></tr><tr><td><code>rfdetr-pretrains/rfdetr-sem-2xlarge-ade20k</code></td><td>880x880</td></tr></tbody></table>

To segment your own classes, create a [Semantic Segmentation project](https://docs.roboflow.com/models/supported-tasks/semantic-segmentation.md#fine-tuning-a-semantic-segmentation-model), then choose RF-DETR and a size in [Custom Training](https://docs.roboflow.com/models/train/train-a-model.md#web-app). You can also use [Neural Architecture Search](https://docs.roboflow.com/models/train/neural-architecture-search.md) to compare models for your accuracy and latency needs. Evaluate semantic segmentation accuracy with [mean Intersection over Union (mIoU)](https://docs.roboflow.com/models/evaluate/evaluate-trained-models.md), rather than the box or mask mAP in the tables above.

## RF-DETR Keypoint Detection

RF-DETR keypoint detection is a preview checkpoint, pretrained on COCO person keypoints. Run it through the [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), or self-host using [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

{% hint style="info" %}
This checkpoint is a preview. Its accuracy and output format can change in later releases.
{% endhint %}

### RF-DETR Keypoint Detection API

Set your API key and install the dependencies as shown above, then run `rfdetr-keypoint-preview` and annotate keypoints:

```python
import os
import cv2
import numpy as np
import supervision as sv
from inference_sdk import InferenceHTTPClient

COCO_KEYPOINTS = [
    "nose", "left_eye", "right_eye", "left_ear", "right_ear",
    "left_shoulder", "right_shoulder", "left_elbow", "right_elbow",
    "left_wrist", "right_wrist", "left_hip", "right_hip",
    "left_knee", "right_knee", "left_ankle", "right_ankle",
]

image_url = "https://media.roboflow.com/notebooks/examples/person-walking.png"
image = sv.load_image_from_url(image_url)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)
result = client.infer(image, model_id="rfdetr-keypoint-preview")

# Each prediction lists only the keypoints the model can see, so place them in
# the 17 COCO slots the skeleton expects and mark the missing ones not visible.
xy, visible = [], []
for prediction in result["predictions"]:
    found = {point["class"]: (point["x"], point["y"]) for point in prediction["keypoints"]}
    xy.append([found.get(name, (0, 0)) for name in COCO_KEYPOINTS])
    visible.append([name in found for name in COCO_KEYPOINTS])

key_points = sv.KeyPoints(xy=np.array(xy, dtype=np.float32), visible=np.array(visible))

annotated = sv.EdgeAnnotator(color=sv.Color.GREEN, thickness=3).annotate(image.copy(), key_points)
annotated = sv.VertexAnnotator(color=sv.Color.RED, radius=5).annotate(annotated, key_points)
cv2.imwrite("person-walking-annotated.png", annotated)
```

<figure><img src="https://3448603185-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fcmo9mhfIjYrvFFy1U7dk%2Fuploads%2Fgit-blob-58cd1b0b1df35e71787197ce73cd5294b67a9a74%2Frf-detr-keypoint.webp?alt=media" alt=""><figcaption></figcaption></figure>

### RF-DETR Keypoint Detection models and benchmarks

Pass the alias as the `model_id` when running inference. Accuracy is COCO `val` AP50-95 scored with object keypoint similarity (OKS), the standard COCO keypoint metric, so it does not compare to the box and mask mAP above.

<table data-search="false"><thead><tr><th>Alias</th><th>Input Size</th><th>Keypoint AP50-95</th><th>Parameters (M)</th><th>Latency (ms)†</th></tr></thead><tbody><tr><td><code>rfdetr-keypoint-preview</code></td><td>576x576</td><td>71.8</td><td>126.4</td><td>9.7</td></tr></tbody></table>

Roboflow does not publish a prebuilt TensorRT engine for this checkpoint yet, so Inference runs it on ONNX Runtime even when you install the `inference-models[trt10]` extra. Reaching the latency in the table means building the engine yourself with the [rfdetr](https://github.com/roboflow/rf-detr) package.

***

† Keypoint accuracy and latency are the figures published in the [RF-DETR benchmarks](https://rfdetr.roboflow.com/latest/#keypoints): latency is TensorRT FP16 on 1x NVIDIA T4 at batch size 1, timing the model only. That is a different GPU and a different pipeline from the tables above, so the two sets of latencies do not compare directly.

\* Latency is measured with [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) on 1x NVIDIA L4, batch size 1, mean of 1,000 inferences (100 warmup). The default `inference-gpu` install runs ONNX on the CUDA execution provider; adding the `inference-models[trt10]` extra selects a prebuilt TensorRT FP16 engine automatically. FP16 matches FP32 accuracy within 0.2 mAP on COCO `val2017`. Accuracy is the published COCO `val` spec (see the [RF-DETR announcement](https://blog.roboflow.com/rf-detr/)).

## Upload Custom Weights

To run an RF-DETR model you trained outside Roboflow, upload its weights to a Project with the [Roboflow CLI](https://docs.roboflow.com/reference/platform/cli/install-and-set-up-the-cli):

```bash
roboflow upload_model -p my-project -v 1 -t rfdetr-medium -m ./output -f checkpoint_best_total.pth
```

Set `-t` to the variant you trained (ex: `rfdetr-small`, `rfdetr-seg-medium`). See [Upload Custom Weights](https://docs.roboflow.com/models/model-weights/upload-custom-weights.md) for the Python SDK and training version requirements.

## License

RF-DETR uses two licenses depending on the model size:

* **Apache 2.0** (Nano, Small, Medium, and Large detection, every segmentation size, and Keypoint): free for commercial use, including self-hosted and closed-source products. No license fee and no need to open-source your code.
* **PML 1.0** (XL and 2XL detection, every semantic segmentation size, and the RF-DETR Pose keypoint model; the Roboflow Platform Model License): commercial use is included when you deploy on Roboflow; commercial self-hosting requires the Enterprise add-on.

See [Model Licenses](https://docs.roboflow.com/models/model-licenses.md) for details.
