Skip to content

Roboflow Model

sahi.models.roboflow

Roboflow detection model wrapper for SAHI.

Provides integration with Roboflow's inference SDK for object detection and instance segmentation models.

Classes

RoboflowDetectionModel

RoboflowDetectionModel(
    model: object | None = None,
    model_path: str | None = None,
    config_path: str | None = None,
    device: str | None = None,
    mask_threshold: float = 0.5,
    confidence_threshold: float = 0.3,
    category_mapping: dict | None = None,
    category_remapping: dict | None = None,
    load_at_init: bool = True,
    image_size: int | None = None,
    api_key: str | None = None,
)

Bases: DetectionModel

Roboflow object detection model.

Supports both Roboflow Universe models (API-based) and local RF-DETR models.

Initialize the RoboflowDetectionModel with the given parameters.

Parameters:

Name Type Description Default
model
object | None

object Either a Roboflow model string identifier or an RF-DETR model class.

None
api_key
str | None

str Roboflow API key for authentication.

None
model_path
str | None

str Path for the instance segmentation model weight

None
config_path
str | None

str Path for the mmdetection instance segmentation model config file

None
device
str | None

Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc.

None
mask_threshold
float

float Value to threshold mask pixels, should be between 0 and 1

0.5
confidence_threshold
float

float All predictions with score < confidence_threshold will be discarded

0.3
category_mapping
dict | None

dict: str to str Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"}

None
category_remapping
dict | None

dict: str to int Remap category ids based on category names, after performing inference e.g. {"car": 3}

None
load_at_init
bool

bool If True, automatically loads the model at initialization

True
image_size
int | None

int Inference input size.

None
Source code in sahi/models/roboflow.py
def __init__(
    self,
    model: object | None = None,
    model_path: str | None = None,
    config_path: str | None = None,
    device: str | None = None,
    mask_threshold: float = 0.5,
    confidence_threshold: float = 0.3,
    category_mapping: dict | None = None,
    category_remapping: dict | None = None,
    load_at_init: bool = True,
    image_size: int | None = None,
    api_key: str | None = None,
) -> None:
    """Initialize the RoboflowDetectionModel with the given parameters.

    Args:
        model: object
            Either a Roboflow model string identifier or an RF-DETR model class.
        api_key: str
            Roboflow API key for authentication.
        model_path: str
            Path for the instance segmentation model weight
        config_path: str
            Path for the mmdetection instance segmentation model config file
        device: Torch device, "cpu", "mps", "cuda", "cuda:0", "cuda:1", etc.
        mask_threshold: float
            Value to threshold mask pixels, should be between 0 and 1
        confidence_threshold: float
            All predictions with score < confidence_threshold will be discarded
        category_mapping: dict: str to str
            Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"}
        category_remapping: dict: str to int
            Remap category ids based on category names, after performing inference e.g. {"car": 3}
        load_at_init: bool
            If True, automatically loads the model at initialization
        image_size: int
            Inference input size.
    """
    # A string is a Roboflow Universe model id, except when it exactly names an
    # RF-DETR class, which selects a local model and never contacts the API.
    self._use_universe = bool(model) and isinstance(model, str) and model not in RFDETR_MODEL_NAMES
    self._model = model
    self._device = device
    self._api_key = api_key

    if self._use_universe:
        existing_packages = getattr(self, "required_packages", None) or []
        self.required_packages = [*list(existing_packages), "inference"]
    else:
        existing_packages = getattr(self, "required_packages", None) or []
        self.required_packages = [*list(existing_packages), "rfdetr"]

    super().__init__(
        model=model,
        model_path=model_path,
        config_path=config_path,
        device=device,
        mask_threshold=mask_threshold,
        confidence_threshold=confidence_threshold,
        category_mapping=category_mapping,
        category_remapping=category_remapping,
        load_at_init=False,
        image_size=image_size,
    )

    if load_at_init:
        self.load_model()
Attributes
has_mask property
has_mask: bool

Returns if model output contains segmentation mask.

Methods:
set_model
set_model(model: Any, **kwargs: Any) -> None

Set the detection model.

Parameters:

Name Type Description Default
model Any

Any Loaded model.

required
**kwargs Any

Additional keyword arguments.

{}
Source code in sahi/models/roboflow.py
def set_model(self, model: Any, **kwargs: Any) -> None:
    """Set the detection model.

    Args:
        model: Any
            Loaded model.
        **kwargs: Additional keyword arguments.
    """
    self.model = model
load_model
load_model() -> None

Load detection model from Roboflow.

This function initializes detection model and sets to self.model. Uses self.model_path, self.config_path, and self.device.

Source code in sahi/models/roboflow.py
def load_model(self) -> None:
    """Load detection model from Roboflow.

    This function initializes detection model and sets to self.model.
    Uses self.model_path, self.config_path, and self.device.
    """
    if self._use_universe:
        from inference import get_model
        from inference.core.env import API_KEY
        from inference.core.exceptions import RoboflowAPINotAuthorizedError

        api_key = self._api_key or API_KEY

        try:
            model = get_model(self._model, api_key=api_key)
        except RoboflowAPINotAuthorizedError as e:
            raise ValueError(
                "Authorization failed. Please pass a valid API key with "
                "the `api_key` parameter or set the `ROBOFLOW_API_KEY` environment variable."
            ) from e

        assert model.task_type in ["object-detection", "instance-segmentation"], (
            "Roboflow model must be an object detection model or an instance segmentation model."
        )

    else:
        import rfdetr.detr

        model, model_path = self._model, self.model_path
        model_names = RFDETR_MODEL_NAMES
        model_types = tuple(getattr(rfdetr.detr, name) for name in RFDETR_MODEL_NAMES)

        # Accept the class name as a string so local models work without importing rfdetr.
        if isinstance(model, str) and model in model_names:
            model = getattr(rfdetr.detr, model)

        if hasattr(model, "__name__") and model.__name__ in model_names:
            model_params = dict(
                device=self._device,
                num_classes=len(self.category_mapping.keys()) if self.category_mapping else None,
            )
            if model_path:
                model_params["pretrain_weights"] = model_path
                if self.image_size:
                    model_params["resolution"] = int(self.image_size)

            model = model(**model_params)  # type: ignore[operator]
        elif isinstance(model, model_types):
            model = model
        else:
            raise ValueError(
                f"Could not resolve a local RF-DETR model from {self._model!r}. Pass `model` as one of "
                f"{model_names} (the class, an instance, or its name as a string) together with "
                "`model_path` for local weights. Note that any other string is treated as a Roboflow "
                "Universe model id and requires an API key."
            )

    self.set_model(model)
perform_inference
perform_inference(image: ndarray) -> None

Run inference on image and store predictions.

Parameters:

Name Type Description Default
image ndarray

np.ndarray A numpy array that contains the image to be predicted.

required
Source code in sahi/models/roboflow.py
def perform_inference(
    self,
    image: np.ndarray,
) -> None:
    """Run inference on image and store predictions.

    Args:
        image: np.ndarray
            A numpy array that contains the image to be predicted.
    """
    if self._use_universe:
        self._original_predictions = self.model.infer(image, confidence=self.confidence_threshold)
    else:
        self._original_predictions = [self.model.predict(image, threshold=self.confidence_threshold)]

Functions: