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:Obss Sahi AutoDetectionModel From Pretrained

From Leeroopedia


Knowledge Sources
Domains Object_Detection, Computer_Vision, Model_Management
Last Updated 2026-02-08 12:00 GMT

Overview

Concrete factory method for instantiating framework-specific detection models through a unified interface provided by the SAHI library.

Description

AutoDetectionModel.from_pretrained() is a static factory method that maps a model_type string (e.g. "ultralytics", "huggingface", "mmdet") to the appropriate DetectionModel subclass. It supports 10 detection frameworks: Ultralytics (YOLOv8/v11), YOLOv5, YOLO-World, YOLOE, RT-DETR, Detectron2, MMDetection, HuggingFace, TorchVision, and Roboflow.

The method dynamically imports the framework-specific model class, instantiates it with the provided parameters, and optionally loads the model weights immediately (when load_at_init=True, the default).

Usage

Import and call this method as the first step in any SAHI inference workflow. Use it whenever you need to load a detection model for sliced or full-image inference without coupling your code to a specific detection framework.

Code Reference

Source Location

  • Repository: sahi
  • File: sahi/auto_model.py
  • Lines: L24-89

Signature

class AutoDetectionModel:
    @staticmethod
    def from_pretrained(
        model_type: str,
        model_path: str | None = None,
        model: Any | 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,
        **kwargs,
    ) -> DetectionModel:
        """Loads a DetectionModel from given path.

        Args:
            model_type: Name of the detection framework
                ("ultralytics", "huggingface", "mmdet", "yolov5",
                 "detectron2", "torchvision", "roboflow", "rtdetr",
                 "yolo-world", "yoloe")
            model_path: Path to model weights file
            model: A pre-initialized model instance
            config_path: Path to config file (for mmdet/detectron2)
            device: "cpu" or "cuda:0"
            mask_threshold: Threshold for mask pixels (0-1)
            confidence_threshold: Minimum score to keep predictions
            category_mapping: Dict mapping category id to name
            category_remapping: Dict remapping category names to new ids
            load_at_init: Auto-load model on creation (default True)
            image_size: Inference input size
        """

Import

from sahi import AutoDetectionModel

I/O Contract

Inputs

Name Type Required Description
model_type str Yes Detection framework name (e.g. "ultralytics", "huggingface")
model_path str No Path to model weights file (e.g. "yolov8n.pt")
model Any No Pre-initialized model instance (alternative to model_path)
config_path str No Path to model config (required for mmdet, detectron2)
device str No Torch device string ("cpu", "cuda:0"); auto-selected if None
mask_threshold float No Threshold for mask pixel values (default 0.5)
confidence_threshold float No Minimum prediction score to retain (default 0.3)
category_mapping dict No Maps category id strings to name strings
category_remapping dict No Remaps category names to new integer ids post-inference
load_at_init bool No Whether to load model immediately (default True)
image_size int No Inference input resolution

Outputs

Name Type Description
return DetectionModel Initialized framework-specific detection model instance ready for inference

Usage Examples

Basic: Ultralytics YOLOv8

from sahi import AutoDetectionModel

# Load a YOLOv8 nano model on GPU
detection_model = AutoDetectionModel.from_pretrained(
    model_type="ultralytics",
    model_path="yolov8n.pt",
    confidence_threshold=0.3,
    device="cuda:0",
)

HuggingFace Transformers Model

from sahi import AutoDetectionModel

detection_model = AutoDetectionModel.from_pretrained(
    model_type="huggingface",
    model_path="facebook/detr-resnet-50",
    confidence_threshold=0.5,
    device="cuda:0",
    image_size=800,
)

MMDetection with Config

from sahi import AutoDetectionModel

detection_model = AutoDetectionModel.from_pretrained(
    model_type="mmdet",
    model_path="checkpoints/faster_rcnn_r50_fpn_1x_coco.pth",
    config_path="configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py",
    confidence_threshold=0.25,
    device="cuda:0",
)

Related Pages

Implements Principle

Requires Environment

Page Connections

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