This module provides high-accuracy object detection on large images by slicing images into overlapping tiles, running inference on each slice, and merging predictions with intelligent NMS/NMM. Designed for simplicity and performance following PixelFlow principles with comprehensive edge case handling.

Classes

  • SlicedInference - Performs sliced inference on large images for improved small object detection.

Functions

  • auto_slice_size - Automatically calculate optimal slice dimensions based on image properties.

SlicedInference

Performs sliced inference on large images for improved small object detection. The main challenge in sliced inference is accurately merging predictions from overlapping slices. This implementation handles critical edge cases including objects on slice boundaries, nested objects, and partial detections at slice edges using intelligent NMS/NMM algorithms with configurable thresholds.

Example

Example
import cv2
import pixelflow as pf
from ultralytics import YOLO

# Setup model and sliced inference
model = YOLO("yolo11n.pt")
slicer = pf.SlicedInference(slice_height=640, slice_width=640, overlap_ratio_h=0.2)

# Define detector function
def detector_func(image):

auto_slice_size

Automatically calculate optimal slice dimensions based on image properties. Computes slice dimensions that maintain the image’s aspect ratio while targeting a specific slice size. Ensures slices are neither too small (minimum 320px) nor larger than the original image dimensions.

Function Signature

auto_slice_size(
    image_height: int,
    image_width: int,
    target_size: int = 640
) -> Any

Parameters

image_height
int
required
Height of the input image in pixels
image_width
int
required
Width of the input image in pixels
target_size
int
default:"640"
Target dimension for the larger side of each slice. Will be adjusted to maintain aspect ratio. Default is 640.

Returns

result
Any
Tuple[int, int]: Optimal (slice_height, slice_width) dimensions that maintain aspect ratio and respect size constraints.

Example

Example
import pixelflow as pf

# Square image - both dimensions equal target_size
h, w = pf.auto_slice_size(2000, 2000, target_size=640)
print(f"Square image slices: {h}x{w}")