Principle:Obss Sahi Prediction Merging
| Knowledge Sources | |
|---|---|
| Domains | Object_Detection, Computer_Vision, Postprocessing |
| Last Updated | 2026-02-08 12:00 GMT |
Overview
A family of algorithms that resolve duplicate and overlapping detections produced by sliced inference by suppressing or merging predictions based on spatial overlap metrics.
Description
When an image is sliced with overlap, the same object may be detected in multiple adjacent slices. After coordinate remapping, these duplicate detections must be resolved. Prediction merging addresses this through overlap-based algorithms.
SAHI provides four postprocessing strategies:
Non-Maximum Suppression (NMS): The classic approach. For each set of overlapping predictions above a match threshold, keep only the highest-confidence prediction and discard the rest. Simple and fast, but discards potentially valid predictions.
Non-Maximum Merging (NMM): Instead of discarding overlapping predictions, merge them by computing a weighted combination of their bounding boxes. This produces more accurate localization when multiple slices partially detect the same object.
Greedy Non-Maximum Merging (GreedyNMM): A greedy variant of NMM that processes predictions in descending confidence order and merges each prediction with all overlapping predictions in a single pass. This is the default algorithm in SAHI and provides the best balance of speed and accuracy.
LSNMS: An experimental option using the lsnms library for large-scale NMS with locality-sensitive hashing.
All algorithms support two overlap metrics: IoU (Intersection over Union) and IoS (Intersection over Smaller area). IoS is more suitable when objects of very different sizes overlap.
Usage
Use prediction merging as the fifth step in the SAHI sliced inference pipeline, after collecting all per-slice and full-image detections. Choose the algorithm based on your needs:
- GreedyNMM (default): Best for most sliced inference scenarios
- NMS: When you want simple suppression without merging
- NMM: When precise localization matters and objects are detected in many slices
- LSNMS: For extremely large numbers of predictions (experimental)
Theoretical Basis
IoU and IoS Metrics
Greedy NMM Algorithm
# Pseudocode for Greedy NMM
def greedy_nmm(predictions, threshold, metric="IOU"):
sorted_preds = sort_by_confidence(predictions, descending=True)
keep_to_merge = {}
suppressed = set()
for pred in sorted_preds:
if pred in suppressed:
continue
merge_list = []
for candidate in spatial_neighbors(pred): # STRtree query
if candidate in suppressed or candidate.score > pred.score:
continue
if overlap(pred, candidate, metric) >= threshold:
merge_list.append(candidate)
suppressed.add(candidate)
keep_to_merge[pred] = merge_list
# Merge step: combine bounding boxes
for keep, merge_list in keep_to_merge.items():
for to_merge in merge_list:
keep.bbox = weighted_merge(keep.bbox, to_merge.bbox)
return list(keep_to_merge.keys())
SAHI uses Shapely STRtree (Sort-Tile-Recursive tree) for efficient spatial indexing, reducing the pairwise comparison from O(n^2) to approximately O(n log n) for the spatial query phase.