NMS & Soft-NMS: Removing Duplicate Detections

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

Best viewed on desktop for optimal interactive experience

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.

Interactive IoU Calculator

Intersection over Union (IoU)

Drag the boxes to see how IoU changes with overlap

Ground Truth (A)Prediction (B)
IoU Value
25.7%
IoU = Intersection / Union
= 4900 / 19100
Area A12000 px²
Area B12000 px²
Intersection4900 px²
Union19100 px²
IoU < 0.3

Boxes are mostly separate. Both predictions likely refer to different objects.

0.3 ≤ IoU ≤ 0.5

Partial overlap. Could be same object or neighbors. NMS threshold usually here.

IoU > 0.5

High overlap. Almost certainly the same object. Lower-confidence box should be suppressed.

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:

NMS Step-by-Step Animation

Non-Maximum Suppression (NMS)

Watch the greedy algorithm remove duplicate detections step by step

IoU Threshold:50%
Step 1 of 14Sort by Confidence
95%88%82%79%71%65%
Kept
Suppressed
Comparing
Remaining
Sort by Confidence

Sort all detection boxes by confidence score (descending)

0
Kept
0
Suppressed
#095%
#188%
#282%
#379%
#471%
#565%

Key Insight: NMS is greedy — it always keeps the highest-confidence box first, then removes overlapping boxes. This can accidentally suppress valid detections of nearby objects!

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.

Hard NMS vs Soft-NMS Comparison

Hard NMS vs Soft-NMS

Compare how different suppression strategies handle overlapping detections

IoU Threshold (Nt):0.50
σ (Gaussian):0.5
Score Decay Functions
IoU with highest-confidence boxScore multiplier00.250.50.75100.51NtHard NMSSoft (Gaussian)Soft (Linear)
Example: Two People Standing Close Together
DetectionOriginalIoUHard NMSSoft (Gaussian)Soft (Linear)
Person 1 (highest)0.920.92 ✓0.92 ✓0.92 ✓
Person 2 (valid!)(nearby object)0.850.290.850.72✓ Kept0.85✓ Kept
Duplicate0.780.830.000.200.13
Hard NMS
KeptREMOVED

Valid detection lost due to binary decision

Soft-NMS (Gaussian)
0.920.72Reduced

Score decays smoothly with overlap

Soft-NMS (Linear)
0.920.85Reduced

Linear penalty above threshold

Hard NMS
si = 0   if IoU ≥ Nt
Soft-NMS (Gaussian)
si = si × e−IoU²/σ
Soft-NMS (Linear)
si = si × (1 − IoU) if IoU ≥ Nt

Key Insight: Soft-NMS preserves nearby valid detections by decaying scores instead of removing boxes entirely. This is especially useful for crowded scenes with overlapping objects!

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:

NMS Variants Comparison

NMS Variants Comparison

Different algorithms for different detection challenges

Greedy NMS

Original algorithm. Simple and fast, but uses hard threshold.

s_i = 0 if IoU(M, b_i) ≥ N_t
Best for: General object detection with well-separated objects

Advantages

  • +Very fast (O(n log n))
  • +Simple to implement
  • +Well-understood behavior

Limitations

  • Hard threshold problem
  • May suppress valid detections
  • Sensitive to IoU threshold

Choose the Right NMS for Your Scenario

Performance Characteristics

AlgorithmSpeedAccuracyCrowded ScenesGPU-Friendly
Greedy NMS●●●●●●●●○○●●○○○●●●○○
Soft-NMS●●●○○●●●●○●●●●○●●●○○
DIoU-NMS●●●○○●●●●○●●●○○●●●○○
Weighted NMS●●○○○●●●●●●●●●○●●○○○
Matrix NMS●●●●●●●●○○●●●○○●●●●●

Practical Advice: Start with Greedy NMS for baseline. Switch to Soft-NMS for crowded scenes. Use Matrix NMS when speed is critical. Consider DIoU-NMS for occluded objects.

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

Use lower IoU threshold (0.3) to avoid over-suppression

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: Lower threshold (0.3) for small objects or dense scenes. Higher threshold (0.7) when duplicates are problematic.
  • 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 suppressionSmall objects, sparse scenes
0.5Balanced (default)General object detection
0.7Conservative suppressionCrowded scenes, when duplicates are rare

Key Formulas

Standard NMS:

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

Soft-NMS (Gaussian):

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

Soft-NMS (Linear):

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

DIoU-NMS:

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