A Generalized Loss Function for Crowd Counting and Localization

Jia Wan, Ziquan Liu, Antoni B. Chan

Introduction

Crowd counting and localization are critical tasks in computer vision, typically addressed by predicting a "density map"—a heatmap where the integral represents the total count. Traditionally, these models are trained using a pixel-wise L2 loss against ground-truth maps generated by blurring dot annotations with fixed Gaussian kernels. However, this standard approach suffers from location insensitivity and relies on heuristic representations that do not account for varying scene perspectives.

This paper introduces a generalized loss function based on Unbalanced Optimal Transport (UOT). By treating the discrepancy between predicted density maps and ground-truth dot annotations as a transport problem, the authors provide a framework that naturally encourages sparse, accurate predictions and unifies existing loss functions as special cases.

The Unbalanced Optimal Transport Framework

In standard Optimal Transport (OT), one calculates the minimum "work" required to move mass from one distribution to another. A significant limitation of standard OT is the requirement for both distributions to have identical total mass. In crowd counting, predicted counts rarely match ground truth exactly during training.

Visualization of the transport plan and resulting density distributions, showing how mass is moved to match ground truth points.

Unbalanced OT addresses this by allowing for mass destruction or creation, penalizing count mismatches while finding the optimal spatial alignment. The proposed loss function decomposes into four key terms:

  1. Transport Loss: Directs predicted density toward the nearest annotations.
  2. Entropic Regularization: Maintains smoothness to prevent density from collapsing into single pixels.
  3. Pixel-wise Loss: Measures the difference between the prediction and a map constructed from the transport plan.
  4. Point-wise Loss: Ensures every ground-truth annotation is accounted for.

Unifying L2 and Bayesian Losses

A major contribution of this work is the mathematical proof that traditional L2 loss and Bayesian Loss (BL) are actually suboptimal special cases of the UOT framework. In these traditional methods, the transport matrices are "frozen" or predefined. By allowing the transport matrix to be optimized dynamically during training, the UOT loss learns a superior intermediate representation.

Comparison of (a) L2 loss, (b) Bayesian loss, and (c) the proposed UOT loss, illustrating how UOT generalizes previous approaches by optimizing the transport plan.

Perspective-Guided Cost and Sparsity

To handle the depth of a scene, the authors incorporate a perspective-guided cost function. By increasing the transport cost for regions further from the camera (typically higher in the image), the model is forced to produce more compact and distinct density points for crowded, distant areas.

Because the UOT loss pushes density toward specific points, the resulting maps are significantly sharper and sparser than traditional Gaussian heatmaps. This makes localization—identifying the exact coordinates of individuals—a natural byproduct of the counting task, rather than a separate post-processing challenge.

Experimental Results

The UOT loss achieved state-of-the-art (SOTA) performance across several major benchmarks:

  • NWPU-Crowd: Achieved a Mean Absolute Error (MAE) of 79.3, a significant improvement over the previous best of 88.4.
  • UCF-QNRF: Dominated localization metrics with an F-measure of 0.763, outperforming specialized detection models like Faster R-CNN in dense environments.
  • Versatility: The loss consistently improved performance across various backbones, including VGG19, CSRNet, and MCNN.

Conclusion

The Unbalanced Optimal Transport framework provides a theoretically grounded and high-performing alternative to traditional crowd-counting losses. By generalizing L2 and Bayesian losses, it removes the need for heuristic Gaussian kernel selection and provides a unified path for both counting and localization. While computationally more intensive due to Sinkhorn iterations, its robustness and accuracy make it a powerful tool for dense object counting tasks.

Summary updated

BibTeX
@inproceedings{wan2021generalized,
  author = {Jia Wan and Ziquan Liu and Antoni B. Chan},
  title = {{A Generalized Loss Function for Crowd Counting and Localization}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2021},
  doi = {10.1109/cvpr46437.2021.00201},
}
Figure