SAHI (Slicing Aided Hyper Inference)

repository·main·Indexed 26 days ago

https://github.com/obss/sahi

A lightweight vision library for large-scale object detection and instance segmentation. SAHI enables 'sliced inference' to detect small objects in high-resolution images by breaking them into smaller patches. It supports integration with frameworks including Ultralytics (YOLO), MMDetection, HuggingFace, and TorchVision, and provides CLI tools for prediction, COCO dataset manipulation, and evaluation.

Tokens
22.3K
Snippets
55
Records
118
Agent score
88%

What's inside SAHI

  1. Overview of SAHI (Slicing Aided Hyper Inference)

    main

    SAHI is an open-source vision library designed for large-scale object detection and instance segmentation, specifically optimized for detecting small objects. It uses a slicing-aided methodology that allows users to improve detection accuracy without requiring additional fine-tuning of the underlying model.

    SAHI is compatible with a wide range of popular object detectors, including:

    • Ultralytics (YOLOv8, YOLO11, YOLO26)
    • HuggingFace Transformers (detection and segmentation)
    • RT-DETR, TorchVision, MMDetection, Detectron2
    • YOLOv5, YOLOE, YOLO-World, and Roboflow RF-DETR
  2. Understand the Sliced Inference workflow

    main

    SAHI implements sliced inference to detect small objects in large images (e.g., 4K drone or satellite photos) that would otherwise be lost during standard model resizing. The workflow follows three steps:

    1. Slice: The image is divided into a grid of overlapping tiles. This ensures objects at tile boundaries are captured in full in at least one patch.
    2. Detect: The detector runs on every tile. By default, SAHI also runs the detector on the full image (perform_standard_pred=True) to catch large objects that might be split across tiles.
    3. Merge: Tile-level predictions are mapped back to full-image coordinates. Overlapping detections are merged or suppressed using post-processing algorithms.
  3. Explore SAHI Notebooks and Demos for various frameworks

    main

    SAHI provides specialized Google Colab notebooks for performing sliced inference with several popular computer vision frameworks. You can find ready-to-run demos for:

    • Ultralytics (YOLO series): YOLO26, YOLO11, YOLO11-OBB, YOLOv5, YOLOX, and YOLOE.
    • Detection Frameworks: RT-DETR, RT-DETR v2, GroundingDINO, MMDetection, Detectron2, and TorchVision.
    • Other Integrations: Roboflow / RF-DETR and HuggingFace models.

    For YOLOX, a live demo is also available on HuggingFace Spaces.

  4. Integrate Detectron2 models with SAHI

    main

    Use Facebook's Detectron2 models for detection and instance segmentation using model_type="detectron2". Requires config_path.

    pip install detectron2
    from sahi import AutoDetectionModel
    from sahi.predict import get_sliced_prediction
    
    detection_model = AutoDetectionModel.from_pretrained(
        model_type="detectron2",
        model_path="path/to/model_final.pth",
        config_path="path/to/config.yaml",
        confidence_threshold=0.25,
        device="cuda:0",
    )
    
    result = get_sliced_prediction(
        "image.jpg",
        detection_model,
        slice_height=512,
        slice_width=512,
    )
  5. Integrate YOLOv5 models with SAHI

    main

    Load classic YOLOv5 models using the yolov5 pip package and model_type="yolov5".

    pip install yolov5
    from sahi import AutoDetectionModel
    from sahi.predict import get_sliced_prediction
    
    detection_model = AutoDetectionModel.from_pretrained(
        model_type="yolov5",
        model_path="yolov5s.pt",
        confidence_threshold=0.25,
        device="cuda:0",
    )
    
    result = get_sliced_prediction(
        "image.jpg",
        detection_model,
        slice_height=512,
        slice_width=512,
    )
  6. Use Roboflow models with SAHI

    main
    SAHI supports Roboflow models for both object detection and instance segmentation tasks via the sahi.models.roboflow.RoboflowModel class. This allows you to leverage Roboflow's hosted models within SAHI's slicing inference pipeline.
  7. Install SAHI with specific detection frameworks

    main

    Depending on your model requirements, you may need to install additional frameworks alongside SAHI.

    Ultralytics (YOLO):

    pip install ultralytics>=8.3.161

    HuggingFace (Transformers):

    pip install transformers>=4.49.0 timm

    YOLOv5:

    pip install yolov5==7.0.14 sahi==0.12.1

    MMDetection (mmdet):

    pip install mim
    mim install mmdet==3.3.0

    Roboflow (Inference):

    pip install inference>=0.51.5 rfdetr>=1.6.2
  8. Integrate YOLO-World (Zero-Shot) models with SAHI

    main

    Use YOLO-World for open-vocabulary detection (detecting objects by text description) using model_type="yolo-world".

    from sahi import AutoDetectionModel
    from sahi.predict import get_sliced_prediction
    
    detection_model = AutoDetectionModel.from_pretrained(
        model_type="yolo-world",
        model_path="yolov8s-worldv2.pt",
        confidence_threshold=0.1,
        device="cuda:0",
    )
    
    result = get_sliced_prediction(
        "image.jpg",
        detection_model,
        slice_height=512,
        slice_width=512,
    )
  9. Configure SAHI Postprocessing Backends

    main

    SAHI supports three interchangeable backends for postprocessing (NMS, NMM) depending on your hardware and dependencies:

    • numpy: Best for CPU-only environments with small/medium prediction counts. No extra dependencies required.
    • numba: Best for CPU with large prediction counts. Requires pip install numba. Note: there is a ~1s JIT warmup on the first call.
    • torchvision: Best for CUDA GPU availability; fastest for large batches. Requires pip install torch torchvision and a CUDA-enabled environment.

    By default, SAHI uses auto-detection, prioritizing torchvision (if CUDA is present), then numba, then numpy.

    from sahi.postprocess.backends import get_postprocess_backend
    
    # Check which backend was resolved (triggers auto-detection)
    print(get_postprocess_backend())  # Returns "auto" until first postprocessing call