Skip to content

RTDETR Model

sahi.models.rtdetr

RT-DETR detection model wrapper for SAHI.

Provides integration with Ultralytics RT-DETR real-time detection transformer models.

Classes

RTDetrDetectionModel

RTDetrDetectionModel(
    *args: object,
    fuse: bool = False,
    task: str | None = None,
    **kwargs: object,
)

Bases: UltralyticsDetectionModel

RT-DETR object detection model.

Wraps Ultralytics RT-DETR for real-time detection inference.

Source code in sahi/models/ultralytics.py
def __init__(self, *args: object, fuse: bool = False, task: str | None = None, **kwargs: object) -> None:
    """Initialize the Ultralytics detection model.

    Accepts all arguments from ``DetectionModel.__init__`` plus the
    following keyword arguments.

    Args:
        *args: Variable length argument list passed to DetectionModel.
        fuse: If True, fuse Conv2d and BatchNorm2d layers for faster
            inference. Default: False.
        task: Ultralytics task type (e.g. ``"detect"``, ``"segment"``,
            ``"obb"``). When None, the task is inferred from the model.
            Default: None.
        **kwargs: Arbitrary keyword arguments passed to DetectionModel.
    """
    self.fuse: bool = fuse
    self.task: str | None = task
    existing_packages = getattr(self, "required_packages", None) or []
    self.required_packages = [*list(existing_packages), "ultralytics"]
    super().__init__(*args, **kwargs)  # type: ignore[misc, arg-type]
Methods:
load_model
load_model() -> None

Detection model is initialized and set to self.model.

Source code in sahi/models/rtdetr.py
def load_model(self) -> None:
    """Detection model is initialized and set to self.model."""
    from ultralytics import RTDETR

    try:
        model_source = self.model_path or "rtdetr-l.pt"
        model = RTDETR(model_source)
        model.to(self.device)
        self.set_model(model)
    except Exception as e:
        raise TypeError("model_path is not a valid rtdetr model path: ", e)