RFDETR is the base class for all RF-DETR model variants. You do not instantiate it directly; instead, use a concrete variant such as RFDETRMedium or RFDETRSegLarge. Every variant inherits the methods documented here.
predict()
Run inference on one or more images and return bounding box (or segmentation mask) predictions.
Parameters
A single image or a list of images to run inference on. Accepted types per image:
str— file path or HTTP/HTTPS URLPIL.Image.Image— PIL image in RGB channel ordernp.ndarray— NumPy array in RGB channel order, shape(H, W, 3)torch.Tensor— pre-normalized tensor in[0, 1], shape(C, H, W)
Minimum confidence score for a detection to be included in the output. Detections with a score below this value are discarded.
Optional
(height, width) tuple to override the model’s default inference resolution. Both dimensions must be positive integers divisible by patch_size × num_windows. Defaults to (model.resolution, model.resolution).Backbone patch size used for shape-divisibility validation. Defaults to the value in
model_config.patch_size (typically 14 for large models and 16 for smaller ones). When provided, must match the instantiated model’s patch size exactly.Returns
A single
supervision.Detections when one image is passed, or a list when multiple images are passed.train()
Fine-tune the model on your own dataset using the PyTorch Lightning training stack.
All keyword arguments are forwarded directly to TrainConfig. You can pass them as plain keyword arguments — you do not need to construct a TrainConfig object yourself.
Parameters
All parameters accepted byTrainConfig are valid here. The most commonly used ones are listed below.
Path to your dataset directory. Supports COCO (
_annotations.coco.json) and YOLO (data.yaml) formats. See dataset formats for details.Number of training epochs.
Per-device micro-batch size. Set to
"auto" to let RF-DETR probe available memory and choose the largest safe value.Base learning rate for the decoder and projection heads.
Directory where checkpoints, logs, and the exported training config are written.
Path to a checkpoint file to resume training from. When set, PTL restores optimizer state and epoch count automatically.
Torch-style device string (e.g.
"cpu", "cuda", "cuda:1", "mps"). Mapped to the appropriate PyTorch Lightning accelerator. When omitted, the best available device is selected automatically.export()
Export the trained model to ONNX format.
Parameters
Directory to write the
.onnx file to.ONNX opset version to target.
Static batch size to bake into the ONNX graph.
When
True, exports with a dynamic batch dimension so the ONNX model accepts variable batch sizes at runtime.(height, width) to override the model’s default export resolution. Both dimensions must be divisible by patch_size × num_windows.Export only the backbone (feature extractor), omitting the detection head.
Print export progress to stdout.
ONNX export requires the See the full ONNX export guide for runtime integration examples.
onnx extras. Install with:deploy_to_roboflow()
Upload a trained model to Roboflow for hosted inference via the Roboflow API and Workflows.
Parameters
The slug of the Roboflow workspace to deploy to.
The project ID within the workspace.
The project version number to deploy the model to.
Your Roboflow API key. When omitted, the value of the
ROBOFLOW_API_KEY environment variable is used.from_checkpoint() class method
Load a trained or fine-tuned model from a checkpoint file. The correct model subclass is inferred automatically from the pretrain_weights field stored in the checkpoint.
RFDETR class if you do not know the variant ahead of time:
Parameters
Path to a
.pth checkpoint file produced by model.train().Additional keyword arguments forwarded to the model constructor. Useful for overriding
num_classes or passing device.Returns
An instance of the appropriate
RFDETR subclass loaded from the checkpoint, ready for inference or further training.optimize_for_inference()
Optimize the model for faster inference by creating a copy of the model in eval mode with the detection head exported. Optionally compiles the model with torch.jit.trace for maximum speed.
Parameters
When
True, compiles the optimized model with torch.jit.trace for maximum inference speed. The compiled model is fixed to a specific batch_size and resolution — both must match exactly when calling predict().Batch size to use when compiling the traced model. Only relevant when
compile=True.Floating-point precision for the optimized model.
remove_optimized_model()
Remove a previously created optimized inference model and revert to standard inference.
class_names property
Returns the class names associated with the loaded model.
pretrain_weights or from_checkpoint()), returns the class names embedded in that checkpoint.
Module-level from_checkpoint()
A convenience wrapper that delegates to RFDETR.from_checkpoint(). Identical behaviour, slightly shorter import:
ModelContext
ModelContext is the lightweight model wrapper returned by RFDETR.get_model(). It holds the underlying nn.Module, the post-processor, the device, and the input resolution. You rarely need to interact with it directly, but it is available if you need low-level access.
Attributes
The underlying LWDETR
nn.Module.Post-processing module that converts raw logits and boxes to scored detections.
Device the model lives on.
Square input resolution in pixels (e.g.
576 for RFDETRMedium).Class names loaded from the checkpoint.
None when no custom names are embedded.Related
Model variants
All available detection and segmentation model classes with their default configs.
TrainConfig
Complete reference for every training parameter accepted by
model.train().ONNX export
Export your trained model to ONNX for deployment.
Roboflow deployment
Deploy to Roboflow for hosted inference.