Towards Understanding and Improving Adversarial Robustness of Vision Transformers

Samyak Jain, Tanima DuttaCVPR 2024 · DOI

Abstract

Vision Transformers (ViTs) are increasingly favored over Convolutional Neural Networks (CNNs), yet their adversarial robustness remains poorly understood. This paper, presented at CVPR 2024, identifies that internal softmax operations in ViTs cause floating-point underflow, leading to "gradient masking." This phenomenon creates a deceptive appearance of robustness. We propose the Adaptive Attention Scaling (AAS) attack to bypass this masking and introduce AAS-Adversarial Training (AAS-AT) to build truly resilient models.

The Problem: Gradient Masking in ViTs

Adversarial attacks involve tiny, imperceptible changes to an image that cause misclassification. While ViTs often appear robust, the authors reveal this is frequently due to numerical errors rather than architectural strength.

In ViT attention blocks, the softmax function can receive inputs with a very large range. When these "pre-softmax" values are large, smaller values are rounded to zero due to floating-point underflow. This "flattens" the gradient, making it nearly impossible for standard optimization-based attacks (like PGD) to find effective adversarial directions. This "gradient masking" hides the model's true vulnerabilities.

Figure 1: Visualization of how large input scales in ViT attention blocks lead to gradient masking and how scaling can overcome this effect.

Methodology

The paper introduces two main components to expose and fix these vulnerabilities:

1. Adaptive Attention Scaling (AAS) Attack

Instead of manual scaling, the AAS attack uses gradient-based optimization to find the optimal scaling factor ($\lambda$) for every attention block. The researchers found that standard cross-entropy loss fails to optimize these factors effectively. Instead, they maximize the LPIPS (Learned Perceptual Image Patch Similarity) distance between original and perturbed features, generating "perceptually aligned" gradients that bypass the masking effect.

2. AAS-Adversarial Training (AAS-AT)

The authors integrate the AAS attack into the adversarial training loop (specifically the TRADES framework). By training models against these "unmasked" and stronger gradients, the resulting ViTs develop genuine robustness that does not depend on numerical precision errors.

Key Results

The effectiveness of AAS and AAS-AT was validated on CIFAR-10, CIFAR-100, and ImageNet-100.

  • Stronger Attacks: Incorporating AAS into state-of-the-art attacks like GAMA increased attack strength by up to 2.9% on CIFAR-100, proving that standard evaluations underestimate ViT vulnerability.
  • Superior Defense: AAS-AT consistently outperformed existing defense methods. On CIFAR-10, it improved robust accuracy by 1.3% to 3.5% across various attack types.
  • AutoAttack Performance: Against the gold-standard "AutoAttack," AAS-AT improved robustness by approximately 3.47% compared to standard PGD-Adversarial Training.
Figure 2: Gain in robust accuracy (%) achieved by AAS-Adversarial Training compared to other scaling techniques across multiple attack methods.

Conclusion

This work demonstrates that the perceived robustness of Vision Transformers is often an artifact of numerical underflow in the attention mechanism. By using Adaptive Attention Scaling, researchers can more accurately evaluate ViT security and train models that are truly resilient to adversarial threats. Future work may investigate similar gradient masking issues in other Transformer-based models, such as Large Language Models (LLMs).

Summary updated

BibTeX
@inproceedings{jain2024towards,
  author = {Samyak Jain and Tanima Dutta},
  title = {{Towards Understanding and Improving Adversarial Robustness of Vision Transformers}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2024},
  doi = {10.1109/cvpr52733.2024.02336},
}
Figure