Skip to main content

NMS & Soft-NMS: Removing Duplicate Detections

Understanding Non-Maximum Suppression algorithms for object detection post-processing, from greedy NMS to soft variants

Overview

Non-Maximum Suppression (NMS) is a critical post-processing step in object detection that removes duplicate detections for the same object. When a detector like YOLO, Faster R-CNN, or SSD processes an image, it often produces multiple overlapping bounding boxes for a single object. NMS filters these redundant predictions, keeping only the most confident detection.

While standard "greedy" NMS uses a hard threshold—completely removing any box with high overlap—this can accidentally eliminate valid detections of nearby objects. Soft-NMS addresses this by gradually reducing confidence scores instead of removing boxes outright, preserving detections for objects standing close together.

Key Concepts

Intersection over Union (IoU)

The ratio of overlap area to total union area between two boxes. IoU = 1 means identical boxes; IoU = 0 means no overlap.

Confidence Score

Each detection has a confidence score (0-1) indicating how certain the model is about the presence and classification of an object.

Greedy Selection

NMS is greedy—it always picks the highest-confidence box first, then removes competitors. This order matters!

Hard Threshold Problem

Standard NMS uses a binary decision: IoU ≥ threshold means removal. This cliff effect can eliminate valid nearby objects.

Score Decay

Soft-NMS replaces removal with decay—overlapping boxes have their scores reduced proportionally to overlap, not set to zero.

Class-Specific Suppression

NMS is typically applied per-class, so a 'person' box won't suppress a 'car' box even if they overlap significantly.

Understanding IoU

Before diving into suppression algorithms, you need to understand IoU (Intersection over Union)—the metric used to measure how much two boxes overlap.

Drag either box, or focus it and use the arrow keys, to see how IoU changes with overlap. The IoU > 0.5 band under the figure starts at 0.5, whereas the list below it uses 0.7.

IoU values determine whether two boxes are considered duplicates:

  • IoU > 0.7: Almost certainly the same object
  • IoU 0.3-0.7: Ambiguous—could be same object or neighbors
  • IoU < 0.3: Probably different objects

Greedy NMS Algorithm

The standard NMS algorithm is deceptively simple but has important implications.

Press Play, or use Previous and Next, to step through greedy NMS on six detections; moving the IoU threshold slider returns the run to its first step.

How It Works

1

Sort by Confidence

Arrange all detections in descending order by confidence score. The most confident prediction will be processed first.

2

Select Maximum

Take the highest-confidence remaining box as a 'keeper'. This box is guaranteed to survive.

3

Compute Overlap

Calculate IoU between the keeper and every remaining box. High IoU indicates potential duplicates.

4

Suppress Overlapping

Remove any box with IoU ≥ threshold (typically 0.5). These are considered duplicates of the keeper.

5

Repeat

Continue with the remaining boxes until none are left. Each iteration picks one keeper.

The Problem with Hard Thresholds

Standard NMS works well for isolated objects, but struggles with crowded scenes. Consider two people standing close together—their detection boxes will naturally overlap. If IoU exceeds the threshold, the lower-confidence detection is completely removed, even though it represents a valid, separate person.

Move the Nt and σ sliders to see how Hard NMS and the two Soft-NMS variants rescore the same overlapping detections. The table shows final scores after iterative selection and rescoring; the diagrams summarize Person 2’s result. The IoU column compares each box with Person 1. Scores below 0.3 are filtered from the output.

Soft-NMS: A Gentler Approach

Soft-NMS (Bodla et al., 2017) replaces the hard removal with gradual score decay:

Gaussian Decay: Smooth, continuous reduction based on IoU squared. Works well for most cases but requires tuning the σ parameter.

Linear Decay: Simpler linear reduction above threshold. More predictable behavior but has a discontinuity at the threshold.

The key insight: Instead of asking "is this a duplicate? yes/no", Soft-NMS asks "how much should I trust this detection given the overlap?"

NMS Variants for Different Scenarios

Different detection scenarios call for different suppression strategies.

Choose a method to see its formula, strengths and limitations, and select a scenario to mark it; the scenario cards only mark the selection and do not change the method shown. The performance table lists fixed ratings, not benchmark results.

Real-World Applications

Standard Object Detection

General images with well-separated objects

Use Greedy NMS with IoU threshold 0.5

Crowded Scenes

Dense pedestrian crowds, shelf products, parking lots

Use Soft-NMS (Gaussian) with σ = 0.5

Autonomous Driving

Occluded vehicles and pedestrians in traffic

Use DIoU-NMS for center-aware suppression

Real-time Video

60+ FPS requirements for video analytics

Use Matrix NMS for parallel GPU processing

Medical Imaging

Precise lesion localization in X-rays/CT scans

Use Weighted NMS for accurate box coordinates

Small Object Detection

Aerial imagery, satellite photos, microscopy

Consider a higher IoU threshold to preserve overlapping neighbors, then validate the duplicate trade-off

Advantages & Limitations

Advantages

  • ✓Essential for removing duplicate detections
  • ✓Simple to implement and understand
  • ✓Works well for most standard detection tasks
  • ✓Soft-NMS preserves valid nearby detections
  • ✓Class-specific processing prevents cross-category errors
  • ✓Many optimized implementations available (GPU, CUDA)

Limitations

  • ×Greedy nature can cause suboptimal selections
  • ×Hard threshold creates cliff effect for borderline cases
  • ×Sequential processing limits parallelization
  • ×Sensitive to IoU threshold choice
  • ×May require different settings per object class
  • ×Cannot recover from suppressing valid detections

Best Practices

  • Start with Greedy NMS: Use standard NMS with IoU=0.5 as baseline. Only switch to variants if you identify specific problems.
  • Tune Threshold Per Task: A lower threshold suppresses duplicates more aggressively but can remove nearby objects. A higher threshold preserves neighbors but can leave duplicates; validate on your data.
  • Use Soft-NMS for Crowds: Switch to Soft-NMS when detecting objects that naturally cluster (pedestrians, products, cells).
  • Apply Per-Class: Always run NMS separately for each object class to prevent cross-category suppression.
  • Consider Detection Speed: Use Matrix NMS for real-time applications. Standard NMS is fine for offline processing.
  • Validate on Edge Cases: Test specifically on crowded scenes and occluded objects where NMS has the most impact.

Choosing Your IoU Threshold

The IoU threshold is the most important hyperparameter for NMS:

ThresholdEffectBest For
0.3Aggressive suppressionWhen duplicate suppression is the priority
0.5Balanced (default)General object detection
0.7Conservative suppressionWhen preserving overlapping neighbors is the priority

Key Formulas

Standard NMS:

text
s_i = 0  if IoU(M, b_i) ≥ N_t

Soft-NMS (Gaussian):

text
s_i = s_i × exp(−IoU²/σ)

Soft-NMS (Linear):

text
s_i = s_i × (1 − IoU)  if IoU ≥ N_t

DIoU-NMS:

text
DIoU = IoU − d²/c²

Where d is center distance and c is diagonal of enclosing box.

Further Reading

If you found this explanation helpful, consider sharing it with others.

Mastodon