Slicing Aided Hyper Inference (SAHI) is a technique to improve small object detection performance with computer vision models. SAHI cuts an image into smaller images then runs inference on each smaller image. Predictions are then aggregated back together.
In this guide, we are going to walk through how to use SAHI with
to improve your ability to detect small objects with a vision model.
1. Install supervision
2. Load a model
3. Run inference using the sv.InferenceSlicer object
Without further ado, let's get started!
First, install the supervision pip package:
pip install supervision
Once you have installed supervision, you are ready to load your data and start writing logic to filter detections.
First, we are going to load a model for use in running inference. For this guide, we will use a
model. We will then define a function that will run inference on an image and load the data into an sv.Detections object.
Let's load our model then define a function that, given an image, will run inference:
import supervision as sv from ultralytics import YOLO dataset = sv.DetectionDataset.from_yolo(...) model = YOLO(...) def callback(image: np.ndarray) -> sv.Detections: result = model(image) return sv.Detections.from_ultralytics(result)
Replace the ... with the response object from your model.
The sv.InferenceSlicer object takes a callback function that returns an sv.Detections object. The slicer divides a provided image into smaller parts, runs inference on each, then combines the results into a single sv.Detections object. We can process the Detections object using supervision to accomplish tasks like plotting bounding boxes and filtering predictions.
slicer = sv.InferenceSlicer(callback=callback) detections = slicer(image=image) prediction_num = len(sliced_detections.xyxy) box_annotator = sv.BoxAnnotator() annotated_frame = box_annotator.annotate( scene=image.copy(), detections=detections, labels=labels ) sv.plot_image(image=annotated_frame, size=(16, 16))
The above code uses SAHI to process an image then plots the results from inference on an image. This image is then displayed.
supervision provides an extensive range of functionalities for working with computer vision models. With supervision, you can:
1. Process and filter detections and segmentation masks from a range of popular models (YOLOv5, Ultralytics YOLOv8, MMDetection, and more).
2. Process and filter classifications.
3. Compute confusion matrices.
And more! To learn about the full range of functionality in supervision, check out the supervision documentation.