Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Roboflow Rf detr RFDETR Init

From Leeroopedia


Knowledge Sources
Domains Object_Detection, Transfer_Learning
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for constructing and initializing RF-DETR models with pretrained weights.

Description

RFDETR.__init__ orchestrates model creation by building a ModelConfig, downloading pretrained weights via download_pretrain_weights, and creating a Model instance. The Model.__init__ method handles architecture construction via build_model, weight loading from checkpoints, class count mismatch handling, and optional LoRA injection on the backbone encoder.

Usage

Used automatically when instantiating any RFDETR size variant class. Also called internally during fine-tuned model loading.

Code Reference

Source Location

  • Repository: rf-detr
  • File: rfdetr/detr.py
  • Lines: L63-76 (RFDETR.__init__)
  • File: rfdetr/main.py
  • Lines: L83-92 (download_pretrain_weights), L94-166 (Model.__init__)

Signature

class RFDETR:
    def __init__(self, **kwargs):
        """
        Initialize RF-DETR model.

        Builds ModelConfig from kwargs, downloads pretrained weights
        if needed, and constructs Model instance.
        """
        self.model_config = self.get_model_config(**kwargs)
        self.maybe_download_pretrain_weights()
        self.model = self.get_model(self.model_config)
        self.callbacks = defaultdict(list)
        ...

class Model:
    def __init__(self, **kwargs):
        """
        Build model architecture and load pretrained weights.

        Args (via kwargs):
            encoder: str - backbone encoder name
            hidden_dim: int - transformer hidden dimension
            dec_layers: int - number of decoder layers
            pretrain_weights: Optional[str] - weight file path or hosted key
            device: str - compute device
            num_classes: int - number of object classes
            backbone_lora: bool - apply LoRA to backbone
        """
        ...

def download_pretrain_weights(pretrain_weights: str, redownload: bool = False) -> None:
    """Download pretrained weights from hosted URLs if not present locally."""
    ...

Import

from rfdetr import RFDETRBase  # or any size variant
from rfdetr.main import Model, download_pretrain_weights

I/O Contract

Inputs

Name Type Required Description
pretrain_weights Optional[str] No Weight file key (e.g. "rf-detr-base.pth") or local path
device str No Compute device ("cuda", "cpu", "mps"), default auto-detected
encoder str No Backbone encoder name (set by ModelConfig)
hidden_dim int No Transformer hidden dimension (set by ModelConfig)
dec_layers int No Number of decoder layers (set by ModelConfig)
num_classes int No Number of object classes (default: 90)
backbone_lora bool No Whether to apply LoRA to backbone (default: False)

Outputs

Name Type Description
model.model LWDETR (nn.Module) The neural network model on device
model.postprocess PostProcess Post-processing module for converting predictions to detections
model.device torch.device The compute device
model.resolution int Input image resolution

Usage Examples

Default Initialization

from rfdetr import RFDETRBase

# Automatically downloads rf-detr-base.pth and loads weights
model = RFDETRBase()

Custom Weights

from rfdetr import RFDETRBase

# Load fine-tuned weights
model = RFDETRBase(pretrain_weights="output/checkpoint_best_total.pth")

With LoRA Backbone

from rfdetr.main import Model

# Model with LoRA applied to backbone for efficient fine-tuning
model = Model(
    encoder="dinov2_windowed_small",
    hidden_dim=256,
    dec_layers=3,
    pretrain_weights="rf-detr-base.pth",
    backbone_lora=True,
)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment