Sub topic 15
One-to-one matching with the Hungarian algorithm
Prerequisites and the matching problem
This chapter assumes that you know basic Python and NumPy, understand feature maps from convolutional neural networks, and have seen classification and regression losses. Familiarity with object queries in Transformers is helpful but not required.
An object detector produces several predictions, but the image may contain only a few ground-truth objects. During training, the detector must decide which prediction should learn from which object. This process is called label assignment.
Earlier detectors assigned labels to anchors using rules based on Intersection over Union (IoU). DETR treats detection as set prediction instead. It compares all predictions with all ground-truth objects and chooses a one-to-one assignment.
Building a matching cost
Suppose a DETR model produces 100 object queries, while an image contains three objects. Each query predicts:
- A class probability, including a special no-object class.
- A bounding box.
- A confidence that is represented through the class probabilities.
For each prediction and ground-truth object, DETR computes a matching cost. A typical cost combines:
- Classification cost: low when the prediction assigns high probability to the object's class.
- L1 box cost: low when the predicted coordinates are close to the target coordinates.
- Generalized IoU cost: low when the boxes overlap well, including when they do not overlap.
A simplified form is:
cost = classification cost + box cost + overlap cost
The costs form a matrix. Rows represent predictions, and columns represent ground-truth objects.
| Prediction | Car | Person |
|---|---|---|
| Query 0 | 0.9 | 0.2 |
| Query 1 | 0.1 | 0.8 |
| Query 2 | 0.5 | 0.6 |
The smaller number means a better match. The matching algorithm should select Query 0 for the person and Query 1 for the car, because their total cost is 0.2 + 0.1.
Hungarian matching
The Hungarian algorithm, also called the Kuhn–Munkres algorithm, finds the minimum-cost one-to-one assignment. It does not greedily choose the cheapest cell at each step. Instead, it considers the assignment globally, so an early choice cannot leave a poor remaining assignment.
The following example uses SciPy's implementation:
import numpy as np
from scipy.optimize import linear_sum_assignment
# Rows are predictions. Columns are ground-truth objects.
cost = np.array([
[0.9, 0.2],
[0.1, 0.8],
[0.5, 0.6],
])
rows, cols = linear_sum_assignment(cost)
matches = list(zip(rows.tolist(), cols.tolist()))
print(matches)
# [(0, 1), (1, 0)]The algorithm matched Query 0 with the person and Query 1 with the car. Query 2 was left unmatched.
Training unmatched predictions
DETR has more queries than most images have objects. After matching, every unmatched query receives the no-object target. The model therefore learns to produce empty predictions rather than duplicate boxes.
This assignment is recalculated for every training image. The matching itself is not differentiated through. Instead, the selected pairs determine which classification and box losses receive gradients.
One-to-one matching removes the need to decide that several nearby predictions are duplicates. It also removes anchor design and the hand-written IoU thresholds used by many anchor-based detectors. However, the matching cost must balance class accuracy, coordinate accuracy, and overlap. Poor weights can cause the matcher to prefer a box with the right class but the wrong location, or a well-positioned box with the wrong class.
Exercise
A cost matrix has three prediction rows and two ground-truth columns. Why is one prediction left unmatched after Hungarian matching?
Show answer
The detector has more predictions than ground-truth objects. Hungarian matching creates one-to-one pairs, so the remaining prediction is trained as no-object.