Implementation:Roboflow Rf detr RFDETR Export
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Deployment, Model_Export |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for exporting RF-DETR models to ONNX format provided by the RF-DETR library.
Description
RFDETR.export() delegates to Model.export() which prepares the model, generates sample input tensors via make_infer_image(), and calls export_onnx() to invoke torch.onnx.export with appropriate input/output names and opset version. The export supports backbone-only export, custom input shapes, and optional post-export simplification.
Usage
Call model.export() on any initialized RFDETR model to produce an ONNX file.
Code Reference
Source Location
- Repository: rf-detr
- File: rfdetr/detr.py
- Lines: L129-135 (RFDETR.export)
- File: rfdetr/main.py
- Lines: L554-624 (Model.export)
- File: rfdetr/deploy/export.py
- Lines: L64-86 (export_onnx)
Signature
class RFDETR:
def export(self, **kwargs) -> None:
"""Export your model to an ONNX file."""
class Model:
def export(
self,
output_dir: str = "output",
infer_dir: str = None,
simplify: bool = False,
backbone_only: bool = False,
opset_version: int = 17,
verbose: bool = True,
force: bool = False,
shape: Optional[Tuple[int, int]] = None,
batch_size: int = 1,
**kwargs,
) -> None:
"""Export the trained model to ONNX format."""
def export_onnx(
output_dir: str,
model: nn.Module,
input_names: List[str],
input_tensors: torch.Tensor,
output_names: List[str],
dynamic_axes: Optional[Dict],
backbone_only: bool = False,
verbose: bool = True,
opset_version: int = 17,
) -> str:
"""Export model to ONNX using torch.onnx.export. Returns output file path."""
Import
from rfdetr import RFDETRBase
# Internal: from rfdetr.deploy.export import export_onnx
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| output_dir | str | No | Directory for ONNX file (default: "output") |
| opset_version | int | No | ONNX opset version (default: 17) |
| simplify | bool | No | Run simplification after export (default: False) |
| backbone_only | bool | No | Export only the backbone (default: False) |
| shape | Optional[Tuple[int,int]] | No | Input shape; defaults to model resolution |
| batch_size | int | No | Batch size for export (default: 1) |
Outputs
| Name | Type | Description |
|---|---|---|
| ONNX file | File | {output_dir}/inference_model.onnx with inputs ['input'] and outputs ['dets', 'labels'] |
Usage Examples
Basic Export
from rfdetr import RFDETRBase
model = RFDETRBase()
model.export(output_dir="./onnx_output")
# Produces: ./onnx_output/inference_model.onnx
Export with Simplification
from rfdetr import RFDETRBase
model = RFDETRBase(pretrain_weights="output/checkpoint_best_total.pth")
model.export(
output_dir="./onnx_output",
simplify=True,
opset_version=17,
)
# Produces: ./onnx_output/inference_model.onnx
# ./onnx_output/inference_model.sim.onnx
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment