Computer Vision Lab Algorithms Bipartite (Hungarian) matching
Bipartite (Hungarian) matching
Finding a minimum-cost one-to-one assignment between the predictions and the ground-truth objects, so no object is matched with two predictions.
This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.
Mathematical formula
\hat\sigma = \arg\min_{\sigma\in\mathfrak S_N}\sum_{i} \mathcal L_{match}\big(y_i, \hat y_{\sigma(i)}\big)Explanation
A cost matrix is built between each ground-truth object and each prediction (class probability + L1 distance + GIoU for the boxes), and the Hungarian algorithm solves it in cubic time. Unmatched predictions are trained to predict “no object”. So the model learns by itself not to duplicate, and NMS becomes unnecessary.
Detailed explanation
Intuition
When the model outputs a fixed set of N predictions and the image contains M\le N objects, we need to decide which prediction is responsible for which object before computing the loss. Bipartite matching chooses the one-to-one assignment with the lowest total cost; the rest are labelled “no object”. And because each object has only one prediction, duplicate predictions are penalised, so the model learns not to duplicate.
How it works step by step
- Build a cost matrix
C_{ij}between the objectiand the predictionj. - Solve the assignment problem with the Hungarian algorithm (Kuhn–Munkres).
- Compute the loss on the matched pairs, and a “no object” loss for the rest.
Mathematical formulation
C_{ij} = -\hat p_j(c_i) + \lambda_{L1}\lVert b_i - \hat b_j\rVert_1 + \lambda_{giou}\big(1 - \mathrm{GIoU}(b_i,\hat b_j)\big)\mathrm{GIoU}(A,B) = \mathrm{IoU}(A,B) - \frac{|C\setminus(A\cup B)|}{|C|}where C the smallest box enclosing the two; so the loss keeps a gradient even when the two boxes do not overlap.
Computational complexity
O(N^3) for the matching, which is small in practice (N=100–300) and computed on the CPU.
Advantages
- Dispenses with NMS and anchors.
- A general framework for any “set prediction”.
Limitations
- The instability of the assignment early in training slows convergence; methods such as denoising training (DN-DETR) address it.
When do you use it?
In DETR and its family, and Mask2Former. scipy.optimize.linear_sum_assignment is a ready-made implementation.
Code example (PyTorch)
from scipy.optimize import linear_sum_assignment
rows, cols = linear_sum_assignment(cost_matrix) # lowest total costApplications
DETR, RT-DETR, Mask2Former and all “set prediction” models.
Limitations
The assignment may oscillate between epochs early in training, slowing convergence; its cost is cubic in the number of queries.
References
- N. Carion, F. Massa, G. Synnaeve, N. Usunier, A. Kirillov, S. Zagoruyko (2020).End-to-End Object Detection with Transformers. arXiv:2005.12872 (ECCV 2020). doi:10.48550/arXiv.2005.12872
