In standard Transformer architectures, the cross-attention mechanism acts as a feature aggregator. A set of queries "looks" at a set of keys (pixels or image patches) and pulls information from them based on similarity. While powerful, this process often results in "distributed" representations where information from a single object is smeared across multiple queries, or a single query captures fragments of many unrelated objects.
A growing body of research suggests that we can achieve more precise, object-centric representations by reframing cross-attention as a clustering problem. By slightly modifying the mathematics of attention, researchers have transformed the Transformer decoder into an iterative solver that partitions an image into discrete, meaningful entities.
The Shift from Aggregation to Competition
The core intuition behind this shift lies in how we normalize attention weights. In standard attention, we apply a softmax function across the spatial dimension (the keys). This ensures that for every query, the weights assigned to all pixels sum to one. However, this does not prevent multiple queries from focusing on the exact same pixel.
In Object-Centric Learning with Slot Attention, Francesco Locatello and colleagues introduced a "competitive" attention mechanism. By normalizing the attention weights over the query dimension instead of the key dimension, they forced the queries (or "slots") to compete for pixels. If one slot strongly claims a particular pixel, other slots are mathematically discouraged from taking it.
This simple change transforms attention into a differentiable clustering assignment. As James Chenhao Liang notes in CLUSTSEG: Clustering for Universal Segmentation, shifting the softmax to the query dimension aligns the Transformer mathematically with the Expectation-Maximization (EM) algorithm, where pixels are "assigned" to the most likely cluster center.
Iterative Refinement: k-means and EM
If queries are cluster centers, then the Transformer decoder's layers can be viewed as iterations of a clustering algorithm. This perspective was formalized in k-means Mask Transformer (kMaX-DeepLab) by Qihang Yu, Huiyu Wang, and their team. They replaced the standard spatial softmax with a cluster-wise argmax, directly mimicking the hard assignment step of the classic k-means algorithm.
The process follows a recurring two-step cycle:
- Assignment (E-step): Each pixel is assigned to its nearest query center based on feature affinity.
- Update (M-step): Each query center is updated by pooling the features of the pixels assigned to it.
This iterative loop, also explored in CMT-DeepLab, allows the model to progressively refine object boundaries. In the first layer, a query might represent a vague blob; by the final layer, it has "gravitated" toward the precise center of an object, having aggregated all relevant pixel features into a holistic representation.
Universality through Clustering
One of the most compelling arguments for this approach is its generality. Traditional vision models often require different "heads" or architectures for different tasks—one for classifying the whole image, another for detecting bounding boxes, and a third for pixel-level segmentation.
By treating every vision task as a clustering problem, models like ClusterFormer can use a single, unified architecture. In this framework:
- Image Classification is clustering where only one or a few global clusters are needed.
- Instance Segmentation is clustering where each cluster corresponds to an individual object.
- Superpixel Generation is clustering where pixels are grouped into small, local, homogeneous regions.
As James Chenhao Liang demonstrates in ClusterFormer, this clustering-based attention is not only more explainable—since we can clearly see which pixels "belong" to which query—but also more efficient. In early layers with high spatial resolution, performing softmax over a small number of queries is computationally cheaper than performing it over thousands of pixels, providing a path toward more scalable and universal visual learners.