Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 105 additions & 3 deletions docs/learn/export.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
---
description: Export RF-DETR models to ONNX, TensorRT, TFLite, ExecuTorch, and native CoreML (FP32/FP16/INT8) for high-performance inference on GPUs, mobile, and edge devices.
description: Export RF-DETR models to ONNX, TensorRT, TFLite, ExecuTorch, native CoreML and OpenVINO IR (FP32/FP16/INT8) for high-performance inference on GPUs, mobile, and edge devices.
---

# Export RF-DETR Model

!!! tip "Key Takeaways"

- Export to ONNX for cross-platform inference with ONNX Runtime, OpenVINO, or TensorRT
- Export to OpenVINO IR for optimized inference on CPU (x86, ARM), GPU (Intel integrated & discrete GPU) and AI accelerators (Intel NPU)
- Export to TFLite (FP32, FP16, INT8) for mobile and edge deployment
- TensorRT conversion delivers lowest latency on NVIDIA GPUs (2.3 ms for Nano)
- INT8 quantization requires calibration data from your dataset for accurate results
- Custom input resolutions supported (must be divisible by `patch_size × num_windows`, which varies by model variant)
- Export to ExecuTorch for on-device PyTorch inference (XNNPACK, CoreML, QNN)
- Export directly to native CoreML (`.mlpackage`) for Xcode / Apple-platform deployment — see [Native CoreML Export](#native-coreml-export-mlpackage)

RF-DETR supports exporting models to ONNX, TFLite, ExecuTorch, and native CoreML formats, enabling deployment across a wide range of inference frameworks, edge devices, and hardware accelerators.
RF-DETR supports exporting models to ONNX, TFLite, ExecuTorch, native CoreML and OpenVINO IR formats, enabling deployment across a wide range of inference frameworks, edge devices, and hardware accelerators.

## Installation

Expand All @@ -24,6 +25,9 @@ Install the export dependencies you need:
# ONNX export only
pip install "rfdetr[onnx]"

# OpenVINO IR export
pip install "rfdetr[openvino]"

# TFLite export
pip install "rfdetr[tflite]"

Expand Down Expand Up @@ -67,7 +71,7 @@ The `export()` method accepts several parameters to customize the export process
| Parameter | Default | Description |
| ------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `output_dir` | `"output"` | Directory where the exported model will be saved. |
| `format` | `"onnx"` | Export format: `"onnx"`, `"tflite"`, `"tensorrt"` (alias: `"trt"`), `"executorch"`, or `"coreml"`. |
| `format` | `"onnx"` | Export format: `"onnx"`, `"tflite"`, `"tensorrt"` (alias: `"trt"`), `"executorch"`, `"openvino"` or `"coreml"`. |
| `quantization` | `None` | TFLite quantization mode: `None`/`"fp32"`, `"fp16"`, or `"int8"`. Only used when `format="tflite"`. |
| `calibration_data` | `None` | Calibration data for TFLite export. Image directory, `.npy` file path, NumPy array, or `None`. See [TFLite Export](#tflite-export). |
| `max_images` | `100` | Maximum number of images to load from a calibration directory for TFLite INT8 quantization. Ignored for other calibration data formats. |
Expand Down Expand Up @@ -464,6 +468,104 @@ boxes = interpreter.get_tensor(boxes_detail["index"])
labels = interpreter.get_tensor(labels_detail["index"])
```

## OpenVINO IR Export

OpenVINO IR (Intermediate Representation) is a proprietary model format used by the OpenVINO Toolkit to optimize and deploy deep learning models.

### Prerequisites

```bash
pip install "rfdetr[openvino]"
```

### Basic OpenVINO Export

=== "Object Detection"

```python
from rfdetr import RFDETRMedium

model = RFDETRMedium(pretrain_weights="<path/to/checkpoint.pth>")

model.export(format="openvino", output_dir="output")
```

=== "Image Segmentation"

```python
from rfdetr import RFDETRSegMedium

model = RFDETRSegMedium(pretrain_weights="<path/to/checkpoint.pth>")

model.export(format="openvino", output_dir="output")
```

This produces two files (named after the model's variant):

- `output/<model-variant>.xml` - The model structure (Intermediate Representation)
- `output/<model-variant>.bin` - The model weights

### OpenVINO Export with Custom Resolution

```python
from rfdetr import RFDETRMedium

model = RFDETRMedium(pretrain_weights="<path/to/checkpoint.pth>")

model.export(format="openvino", shape=(608, 608))
```

### OpenVINO Inference Example

```python
import numpy as np
from PIL import Image
from rfdetr.export._openvino.inference import OpenVINOInference

# Load the exported model
model = OpenVINOInference("output/rfdetr-medium.xml")

# Prepare input image (NCHW format, ImageNet normalized)
image = Image.open("image.jpg").convert("RGB").resize((576, 576))
image_array = np.array(image).astype(np.float32) / 255.0

# Apply ImageNet normalization
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
image_array = (image_array - mean) / std

# Convert to NCHW format
image_array = np.transpose(image_array, (2, 0, 1))
image_array = np.expand_dims(image_array, axis=0)

# Run inference
outputs = model(image_array)
boxes, labels = outputs
```

### Benchmark OpenVINO Model

Use OpenVINO's `benchmark_app` tool to measure performance:

```bash
benchmark_app -m output/rfdetr-medium.xml -data_shape [1,3,576,576]
```

### OpenVINO Model Outputs

The exported OpenVINO IR model produces the following outputs:

- **Object Detection Models**:

- Output 0: Bounding boxes `[batch, 300, 4]` (x, y, w, h in normalized coordinates)
- Output 1: Class logits `[batch, 300, num_classes]`

- **Segmentation Models**:

- Output 0: Bounding boxes `[batch, 300, 4]`
- Output 1: Class logits `[batch, 300, num_classes]`
- Output 2: Instance masks (if segmentation head is present)

## ExecuTorch Export

!!! warning "Experimental — Use with Caution"
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ coreml = [
# cause is understood/fixed upstream; don't treat it as a guaranteed-deterministic fix.
"torch<2.12",
]
openvino = [
"openvino>=2024.0", # Direct PyTorch -> OpenVINO IR conversion
]
loggers = [
"tensorboard>=2.13.0",
"protobuf>=3.20.0", # Cap <4.0.0 removed — tensorflow>=2.16.0 (required by [tflite]) needs protobuf>=3.20.3 and is incompatible with <4.0.0 (see #1041)
Expand Down
Loading
Loading