# RF-DETR

RF-DETR is Roboflow's transformer-based real-time object detection and segmentation model. You can run inference against COCO-pretrained RF-DETR checkpoints through the [Serverless Hosted API](/deploy/serverless-hosted-api-v2.md), or self-host using [Roboflow Inference](https://inference.roboflow.com/).

## Code samples

The samples below run RF-DETR through the [Serverless Hosted API](/deploy/serverless-hosted-api-v2.md) and visualize results with [supervision](https://supervision.roboflow.com/). Pass [Roboflow's API Key](https://app.roboflow.com/settings/api) via the `API_KEY` env variable.

### Object detection

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

```python
import os
import urllib.request

import cv2
import supervision as sv
from inference_sdk import InferenceHTTPClient

image_url = "https://storage.googleapis.com/com-roboflow-marketing/notebooks/examples/cars-highway.png"
image_path = "cars-highway.png"
urllib.request.urlretrieve(image_url, image_path)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.getenv("API_KEY"),
)
result = client.infer(image_path, model_id="rfdetr-base")

image = cv2.imread(image_path)
detections = sv.Detections.from_inference(result)

box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()

annotated = box_annotator.annotate(scene=image.copy(), detections=detections)
annotated = label_annotator.annotate(scene=annotated, detections=detections)

cv2.imwrite("cars-highway-annotated.png", annotated)
```

<figure><img src="/files/1IzLvvLBBSUAXad1685k" alt=""><figcaption></figcaption></figure>

### Instance segmentation

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

```python
import os
import urllib.request

import cv2
import supervision as sv
from inference_sdk import InferenceHTTPClient

image_url = "https://storage.googleapis.com/com-roboflow-marketing/notebooks/examples/bicycle.png"
image_path = "bicycle.png"
urllib.request.urlretrieve(image_url, image_path)

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.getenv("API_KEY"),
)
result = client.infer(image_path, model_id="rfdetr-seg-preview")

image = cv2.imread(image_path)
detections = sv.Detections.from_inference(result)

mask_annotator = sv.MaskAnnotator()
label_annotator = sv.LabelAnnotator()

annotated = mask_annotator.annotate(scene=image.copy(), detections=detections)
annotated = label_annotator.annotate(scene=annotated, detections=detections)

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

<figure><img src="/files/KqjVgQqtVxDOxSDoQMff" alt=""><figcaption></figcaption></figure>

## Default COCO-trained models

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

| Alias              | Task                  | Input Size |
| ------------------ | --------------------- | ---------- |
| rfdetr-base        | Object Detection      | 560x560    |
| rfdetr-nano        | Object Detection      | 384x384    |
| rfdetr-small       | Object Detection      | 512x512    |
| rfdetr-medium      | Object Detection      | 576x576    |
| rfdetr-large       | Object Detection      | 704x704    |
| rfdetr-xlarge      | Object Detection      | 700x700    |
| rfdetr-2xlarge     | Object Detection      | 880x880    |
| rfdetr-seg-preview | Instance Segmentation | 560x560    |
| rfdetr-seg-nano    | Instance Segmentation | 312x312    |
| rfdetr-seg-small   | Instance Segmentation | 384x384    |
| rfdetr-seg-medium  | Instance Segmentation | 432x432    |
| rfdetr-seg-large   | Instance Segmentation | 504x504    |
| rfdetr-seg-xlarge  | Instance Segmentation | 624x624    |
| rfdetr-seg-2xlarge | Instance Segmentation | 768x768    |

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

* `https://serverless.roboflow.com` for the Serverless Hosted API.
* `http://localhost:9001` for a local [Inference](https://inference.roboflow.com/) server.
* Your [Dedicated Deployment](/deploy/dedicated-deployments.md) URL for a private endpoint.
  {% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.roboflow.com/deploy/supported-models/rf-detr.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
