PaCa-ViT: Learning Patch-to-Cluster Attention in Vision Transformers

Ryan Grainger, Thomas Paniagua, Xi Song, Naresh Cuntoor, Mun Wai Lee, Tianfu Wu

TL;DR

PaCa-ViT introduces Patch-to-Cluster (PaCa) attention, a novel mechanism that replaces standard patch-to-patch self-attention with attention directed toward a small set of learned semantic clusters. This shift reduces computational complexity from quadratic to linear and bridges the semantic gap in visual tokenization, leading to state-of-the-art performance and built-in interpretability across major vision benchmarks.

What Problem Does This Paper Address?

Vision Transformers (ViTs) have revolutionized computer vision by treating image patches as "visual tokens," similar to words in natural language processing. However, they face three significant hurdles:

  1. Quadratic Complexity: Standard self-attention requires every patch to interact with every other patch. As image resolution increases, the computational cost grows quadratically ($O(N^2)$), making high-resolution tasks like object detection and semantic segmentation extremely expensive.
  2. The Semantic Gap: Unlike words, which have inherent meaning, raw image patches are just arbitrary sub-grids. There is a "semantic gap" between these low-level patches and the high-level concepts the model needs to learn.
  3. Interpretability: Understanding why a ViT makes a specific prediction usually requires complex post-hoc explanation methods.

Methods

The authors propose Patch-to-Cluster (PaCa) attention to solve these issues simultaneously. Instead of attending to all other patches, each patch (Query) attends to a predefined small number of $M$ latent "visual tokens" or clusters (Keys and Values).

Comparison of vanilla patch-to-patch attention vs. the proposed Patch-to-Cluster attention.

Key Innovations:

  • Linear Complexity: By setting $M$ to a small constant (e.g., 100), the complexity becomes $O(N \times M)$, which is linear with respect to the number of patches $N$.
  • Onsite Clustering: The model learns to assign patches to clusters end-to-end. This can be done block-wise (per Transformer block) or stage-wise (shared across a stage). The authors found stage-wise clustering to be more efficient and effective.
  • Clustering Architectures: They explored two ways to generate cluster assignments: using a lightweight CNN (to capture local spatial context) or a Multi-Layer Perceptron (MLP) (for a domain-agnostic approach).
  • External Clustering Teacher: To improve early-stage learning, they experimented with a lightweight "teacher" CNN that provides cluster assignments to the ViT, helping it focus on high-frequency information.
  • PaCa Segmentation Head: A specialized, lightweight head for semantic segmentation that uses the PaCa mechanism to directly supervise cluster assignments using ground-truth class labels.
Architectural comparison between spatial-reduction attention and the proposed PaCa module.

Datasets

The models were trained and evaluated on three standard computer vision benchmarks:

  • ImageNet-1k: For large-scale image classification (1.28M training images, 1,000 classes).
  • MS-COCO 2017: For object detection and instance segmentation.
  • MIT-ADE20k: For dense semantic segmentation (150 semantic categories).

Key Insights and Ideas

  • Joint Learning: The model induces a "clustering-for-attention" and "attention-for-clustering" loop. Better clusters lead to better attention, and the attention mechanism helps refine what constitutes a meaningful cluster.
  • Meaningful Visual Tokens: By forcing the model to attend to clusters, it naturally learns to group semantically related parts of an image (e.g., all parts of a dog) into a single latent token, bridging the semantic gap.
  • Built-in Interpretability: Because the cluster assignments are explicit, they can be visualized as heatmaps. This provides a "forward explainer"—you can see exactly which parts of the image the model is grouping together without needing extra tools.
  • Efficiency at Scale: While the overhead of clustering makes PaCa slightly slower on small $224 \times 224$ images, its linear scaling makes it significantly more efficient for the high-resolution images used in detection and segmentation.

Key Results

PaCa-ViT consistently outperformed strong baselines like Swin Transformers and PVTv2:

  • Classification: PaCa-Tiny (12.2M parameters) achieved 80.9% Top-1 accuracy on ImageNet-1k, surpassing PVTv2-B1 (14.0M parameters) by 2.2%.
  • Semantic Segmentation: On MIT-ADE20k, the PaCa-Base model reached 50.39 mIoU, outperforming the much larger PVTv2-B5 (48.7 mIoU) while being significantly more computationally efficient.
  • Object Detection: On MS-COCO, PaCa models showed superior performance (e.g., PaCa-Small achieving 46.4 $AP^b$) compared to similar-sized Swin and PVT variants.

Limitations and Future Work

  • Redundancy: Qualitative visualizations showed that some learned clusters are redundant or cluttered. The authors suggest that future work could use diversity-promoting losses (like Barlow Twins) to encourage more distinct clusters.
  • Small Image Overhead: For low-resolution inputs, the extra clustering step can slightly outweigh the savings from linear attention. The true benefits of PaCa shine as resolution increases.
  • Future Directions: The authors propose exploring instance-sensitive cluster masks to filter out redundant clusters on the fly and further refining the "teacher-student" distillation approach for large foundation models.

Summary updated

BibTeX
@inproceedings{grainger2023paca,
  author = {Ryan Grainger and Thomas Paniagua and Xi Song and Naresh Cuntoor and Mun Wai Lee and Tianfu Wu},
  title = {{PaCa-ViT: Learning Patch-to-Cluster Attention in Vision Transformers}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2023},
  doi = {10.1109/cvpr52729.2023.01781},
}
Figure