Gramformer: Learning Crowd Counting via Graph-Modulated Transformer

Hui Lin, Zhiheng Ma, Xiaopeng Hong, Qinnan Shangguan, Deyu Meng

Introduction

Crowd counting—the task of estimating the number of individuals in dense, often cluttered images—is a critical component of urban planning, public safety, and traffic management. While early approaches relied heavily on Convolutional Neural Networks (CNNs), the field has recently shifted toward Transformer-based architectures. Transformers offer a global receptive field, allowing the model to capture long-range dependencies across an entire scene. However, this global view introduces a unique challenge: "homogenized attention."

In dense crowd scenarios, many image patches (such as small heads in a distance) appear nearly identical. Standard self-attention mechanisms often converge on a "lazy" solution where every patch produces a similar attention map, effectively recreating a blurry version of the final density map at every layer. This loss of feature diversity leads to reduced accuracy and poor localization. Gramformer (Graph-Modulated Transformer) addresses this by introducing graph-based modulations that force the model to seek out complementary, non-redundant information.

The Challenge of Homogenized Attention

In a typical Transformer, the self-attention mechanism calculates the similarity between patches to determine feature weights. In crowd counting, where the visual signal is highly repetitive, this similarity-based approach causes the attention maps to collapse into a uniform state. Instead of learning the nuanced spatial relationships required to distinguish individual heads from complex backgrounds (like foliage or architectural textures), the model produces "blobby" density maps that lack precision.

Methodology

Gramformer introduces three primary innovations to the standard Transformer block to combat homogenization and improve counting accuracy.

1. Attention Graph Modulation (Anti-Similarity)

To prevent the model from focusing only on similar, redundant features, Gramformer employs an Edge Weight Regression (EWR) network. This component identifies dissimilarities between patches and modulates the attention mechanism in an "anti-similarity" fashion. By forcing the model to attend to complementary information, Gramformer ensures that the final count is derived from a diverse set of feature "votes," rather than a single, repetitive signal.

2. Feature-based Centrality Encoding

The model treats image features as nodes in a k-Nearest Neighbor (k-NN) graph. For each patch, it calculates a "centrality index" based on its in-degree—essentially measuring how many other patches are similar to it. These indices are used to bias the input features, helping the model dynamically identify which regions are most representative or structurally important. This centrality is re-computed at every layer, allowing the model to refine its focus as features become more abstract.

3. Geometric Edge Regularization

Crowd scenes are heavily influenced by camera perspective; individuals appear smaller as they move toward the horizon. Gramformer incorporates a geometric prior into its loss function based on the principle of horizontal consistency. Since people at the same vertical height in an image typically share the same scale, the model is penalized if its semantic content varies too drastically along the horizontal axis. This regularization helps the model handle scale variations without requiring explicit perspective maps.

Experimental Results

Gramformer was evaluated on four major benchmarks: UCF-QNRF, NWPU-CROWD, ShanghaiTech Part A, and JHU-CROWD++.

Quantitative Performance

The model achieved state-of-the-art (SOTA) results, particularly on high-density and diverse datasets:

  • UCF-QNRF: Achieved a Mean Absolute Error (MAE) of 76.7, surpassing previous leading models like MAN and STEERER.
  • NWPU-CROWD: Reached an MAE of 72.5, establishing a significant lead over other Transformer-based methods on this large-scale dataset.
  • Efficiency: Despite its graph-based components, Gramformer is relatively efficient. With 29 million parameters, it is significantly more lightweight than a standard ViT-B (86 million parameters), making it more viable for practical deployment.

Qualitative Improvements

Visual analysis of the density maps produced by Gramformer reveals significantly sharper localization compared to vanilla Transformers. The model successfully distinguishes between dense crowds and background textures that often trigger false positives in other models, such as tree leaves or building facades. The resulting density maps provide distinct points for individuals rather than broad, overlapping clusters.

Conclusion

Gramformer demonstrates that the key to improving Transformer performance in repetitive visual tasks is not more similarity, but controlled diversity. By using graph-modulated attention to break the cycle of homogenization and incorporating geometric priors, the model achieves superior accuracy and clarity in crowd counting. The authors suggest that this graph-modulation strategy could be extended to other domains characterized by repeating elements, such as agricultural monitoring or medical cell counting.

Summary updated

BibTeX
@inproceedings{lin2024gramformer,
  author = {Hui Lin and Zhiheng Ma and Xiaopeng Hong and Qinnan Shangguan and Deyu Meng},
  title = {{Gramformer: Learning Crowd Counting via Graph-Modulated Transformer}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2024},
  doi = {10.1609/aaai.v38i4.28126},
}
Figure