Skip to content

Ultralytics Docs: Using YOLOv8 with SAHI for Sliced Inference

Welcome to the Ultralytics documentation on how to use YOLOv8 with SAHI (Slicing Aided Hyper Inference). This comprehensive guide aims to furnish you with all the essential knowledge you'll need to implement SAHI alongside YOLOv8. We'll deep-dive into what SAHI is, why sliced inference is critical for large-scale applications, and how to integrate these functionalities with YOLOv8 for enhanced object detection performance.

SAHI Sliced Inference Overview

Introduction to SAHI

SAHI (Slicing Aided Hyper Inference) is an innovative library designed to optimize object detection algorithms for large-scale and high-resolution imagery. Its core functionality lies in partitioning images into manageable slices, running object detection on each slice, and then stitching the results back together. SAHI is compatible with a range of object detection models, including the YOLO series, thereby offering flexibility while ensuring optimized use of computational resources.

Key Features of SAHI

  • Seamless Integration: SAHI integrates effortlessly with YOLO models, meaning you can start slicing and detecting without a lot of code modification.
  • Resource Efficiency: By breaking down large images into smaller parts, SAHI optimizes the memory usage, allowing you to run high-quality detection on hardware with limited resources.
  • High Accuracy: SAHI maintains the detection accuracy by employing smart algorithms to merge overlapping detection boxes during the stitching process.

What is Sliced Inference?

Sliced Inference refers to the practice of subdividing a large or high-resolution image into smaller segments (slices), conducting object detection on these slices, and then recompiling the slices to reconstruct the object locations on the original image. This technique is invaluable in scenarios where computational resources are limited or when working with extremely high-resolution images that could otherwise lead to memory issues.

Benefits of Sliced Inference

  • Reduced Computational Burden: Smaller image slices are faster to process, and they consume less memory, enabling smoother operation on lower-end hardware.

  • Preserved Detection Quality: Since each slice is treated independently, there is no reduction in the quality of object detection, provided the slices are large enough to capture the objects of interest.

  • Enhanced Scalability: The technique allows for object detection to be more easily scaled across different sizes and resolutions of images, making it ideal for a wide range of applications from satellite imagery to medical diagnostics.

YOLOv8 without SAHI YOLOv8 with SAHI
YOLOv8 without SAHI YOLOv8 with SAHI

Installation and Preparation


To get started, install the latest versions of SAHI and Ultralytics:

pip install -U ultralytics sahi

Import Modules and Download Resources

Here's how to import the necessary modules and download a YOLOv8 model and some test images:

from sahi.utils.yolov8 import download_yolov8s_model
from sahi import AutoDetectionModel
from import read_image
from sahi.utils.file import download_from_url
from sahi.predict import get_prediction, get_sliced_prediction, predict
from pathlib import Path
from IPython.display import Image

# Download YOLOv8 model
yolov8_model_path = "models/"

# Download test images
download_from_url('', 'demo_data/small-vehicles1.jpeg')
download_from_url('', 'demo_data/terrain2.png')

Standard Inference with YOLOv8

Instantiate the Model

You can instantiate a YOLOv8 model for object detection like this:

detection_model = AutoDetectionModel.from_pretrained(
    device="cpu",  # or 'cuda:0'

Perform Standard Prediction

Perform standard inference using an image path or a numpy image.

# With an image path
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)

# With a numpy image
result = get_prediction(read_image("demo_data/small-vehicles1.jpeg"), detection_model)

Visualize Results

Export and visualize the predicted bounding boxes and masks:


Sliced Inference with YOLOv8

Perform sliced inference by specifying the slice dimensions and overlap ratios:

result = get_sliced_prediction(

Handling Prediction Results

SAHI provides a PredictionResult object, which can be converted into various annotation formats:

# Access the object prediction list
object_prediction_list = result.object_prediction_list

# Convert to COCO annotation, COCO prediction, imantics, and fiftyone formats

Batch Prediction

For batch prediction on a directory of images:

    model_device="cpu",  # or 'cuda:0'

That's it! Now you're equipped to use YOLOv8 with SAHI for both standard and sliced inference.

Citations and Acknowledgments

If you use SAHI in your research or development work, please cite the original SAHI paper and acknowledge the authors:

  title={Slicing Aided Hyper Inference and Fine-tuning for Small Object Detection},
  author={Akyon, Fatih Cagatay and Altinuc, Sinan Onur and Temizel, Alptekin},
  journal={2022 IEEE International Conference on Image Processing (ICIP)},

We extend our thanks to the SAHI research group for creating and maintaining this invaluable resource for the computer vision community. For more information about SAHI and its creators, visit the SAHI GitHub repository.

Created 2023-09-05, Updated 2023-09-06
Authors: glenn-jocher (2)