TransMix: Attend to Mix for Vision Transformers

Jieneng Chen, Shuyang Sun, Ju He, Philip H. S. Torr, Alan L. Yuille, Song Bai

Introduction

Vision Transformers (ViTs) have revolutionized computer vision but often require heavy regularization to overcome their lack of inductive biases, such as translation invariance. While data augmentation techniques like Mixup and CutMix are standard, they rely on a flawed assumption: that the importance of an image region is strictly proportional to its area. TransMix addresses this by using the model's own self-attention maps to dynamically weight labels during the mixing process.

The Limitation of Area-Based Mixing

In standard CutMix, if 30% of Image A is replaced by a patch from Image B, the resulting label is a fixed 70/30 blend. However, if the 30% patch contains only background while the remaining 70% of Image A contains the primary object, the area-based label becomes misleading. This "label-input gap" forces the model to learn from noisy supervision, potentially hindering performance and generalization.

The TransMix Method: Leveraging Spatial Intelligence

TransMix bridges this gap by leveraging the inherent spatial intelligence of ViTs. Instead of using arbitrary area ratios, it utilizes the multi-head class attention map from the final transformer block to calculate the mixing proportion $\lambda$.

TransMix uses attention maps to calculate the label mixing ratio, ensuring the ground truth reflects the most salient parts of the image.

The process is straightforward:

  1. Input Mixing: A random region from Image B is pasted into Image A, similar to CutMix.
  2. Attention Extraction: During the forward pass, the model generates a class attention map ($A$) indicating which patches are most relevant for classification.
  3. Dynamic Labeling: The label weight for Image B is determined by the sum of attention values within the pasted patch's coordinates.

Because the attention map is already a part of the standard ViT forward pass, TransMix adds zero extra parameters and introduces negligible computational overhead (FLOPs).

Qualitative Impact on Label Assignment

By using attention, TransMix "corrects" labels on the fly. If a model focuses heavily on a newly introduced patch, that label receives more weight; if the patch is ignored as background, the original label remains dominant. This ensures that the model is not penalized for correctly identifying the most discriminative features in a mixed image.

Qualitative comparison showing how TransMix adjusts labels based on visual saliency compared to traditional area-based assignments.

Key Results and Robustness

TransMix consistently improves performance across various architectures and tasks:

  • Classification Gains: It improves ImageNet-1k top-1 accuracy by approximately 0.9% for models like DeiT-S and XCiT-L. In head-to-head comparisons, TransMix (80.7%) outperformed more complex methods like Puzzle-Mix (79.8%) and Attentive-CutMix (77.5%).
  • Downstream Transferability: Pre-training with TransMix enhances performance in semantic segmentation (Pascal Context) and object detection (COCO), improving mIoU by up to 0.9%.
  • Enhanced Robustness: TransMix-trained models are significantly more resilient to occlusion and spatial shuffling. Even when 70% of image information is lost, TransMix models maintain significantly higher accuracy than vanilla ViTs.

Conclusion

TransMix is an "embarrassingly simple" yet powerful refinement of data augmentation for Vision Transformers. By aligning the label space with the model's visual focus, it provides a more accurate supervisory signal, leading to better generalization, robustness, and transferability without the need for external saliency models or additional parameters.

Summary updated

Abstract

Mixup-based augmentation has been found to be effective for generalizing models during training, especially for Vision Transformers (ViTs) since they can easily overfit. However, previous mixup-based methods have an underlying prior knowledge that the linearly interpolated ratio of targets should be kept the same as the ratio proposed in input interpolation. This may lead to a strange phenomenon that sometimes there is no valid object in the mixed image due to the random process in augmentation but there is still response in the label space. To bridge such gap between the input and label spaces, we propose TransMix, which mixes labels based on the attention maps of Vision Transformers. The confidence of the label will be larger if the corresponding input image is weighted higher by the attention map. TransMix is embarrassingly simple and can be implemented in just a few lines of code without introducing any extra parameters and FLOPs to ViT-based models. Experimental results show that our method can consistently improve various ViT-based models at scales on ImageNet classification. After pre-trained with TransMix on ImageNet, the ViT-based models also demonstrate better transferability to semantic segmentation, object detection and instance segmentation. TransMix also exhibits to be more robust when evaluating on 4 different benchmarks. Code is publicly available at https://github.com/Beckschen/TransMix.
BibTeX
@inproceedings{chen2022transmix,
  author = {Jieneng Chen and Shuyang Sun and Ju He and Philip H. S. Torr and Alan L. Yuille and Song Bai},
  title = {{TransMix: Attend to Mix for Vision Transformers}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2022},
  doi = {10.1109/cvpr52688.2022.01182},
}
Figure