SAFER: Sharpness Aware layer-selective Finetuning for Enhanced Robustness in vision transformers

Bhavna Gopal, Huanrui Yang, Mark Horton, Yiran Chen

TL;DR

Vision Transformers (ViTs) are powerful but vulnerable to adversarial attacks, often suffering from overfitting during robust training. This paper introduces SAFER, a novel layer-selective fine-tuning approach that identifies and fine-tunes only the most critical ViT layers using sharpness-aware minimization, significantly boosting both clean and adversarial accuracy without compromising convergence.

What Problem Does This Paper Address?

Vision Transformers (ViTs) have revolutionized computer vision, but their widespread adoption is hampered by their susceptibility to adversarial attacks – small, imperceptible perturbations to input images that can drastically alter a model's predictions. While adversarial training (AT) is the go-to defense, ViTs' complex architectures and large parameter counts exacerbate a common problem: adversarial overfitting. This means the model becomes overly specialized to the specific adversarial examples seen during training, leading to a decline in performance on clean, unperturbed data and potentially limiting overall robustness. Existing methods for identifying "critical" layers, often developed for Convolutional Neural Networks (CNNs), don't translate well to ViTs due to their distinct architectural properties. This paper aims to bridge this gap by proposing a ViT-specific method to mitigate adversarial overfitting by intelligently fine-tuning only the most vulnerable layers.

Methods

The core of SAFER lies in its two-pronged approach: identifying critical layers and then applying a targeted fine-tuning strategy.

  1. Critical Layer Identification: The paper leverages the concept of "sharpness" from Sharpness-Aware Minimization (SAM) [9]. SAM posits that a model's generalization gap (difference between training and test performance) is related to the "sharpness" of its loss landscape. A sharper landscape indicates poorer generalization. SAFER adapts this by measuring the sharpness of the loss landscape for each individual layer of the ViT. This is achieved by freezing all other layers and fine-tuning the target layer with adversarial training. The sharpness of a layer is then quantified using a metric derived from SAM's principles. Layers with higher sharpness are deemed more susceptible to overfitting.
  2. Layer-Selective Fine-tuning: Once the top-K (approximately 5%) most susceptible layers are identified, SAFER applies a modified SAM objective only to these selected layers. The rest of the model's layers are frozen. This selective fine-tuning aims to smooth the loss landscape of these critical layers, thereby reducing overfitting and improving robustness without the computational burden and convergence issues of fine-tuning the entire model. The process is iterative, with layer selection being re-evaluated periodically to adapt to evolving model states. SAFER is also designed to be compatible with Parameter-Efficient Fine-Tuning (PEFT) methods like LoRA and DORA.

Datasets

The experiments were conducted on three standard image classification datasets:

  • CIFAR-10: A dataset of 60,000 color images in 32x32 resolution, with 10 classes.
  • CIFAR-100: Similar to CIFAR-10 but with 100 classes.
  • Imagenette: A 10-class subset of ImageNet-1K, chosen for its clear separation between training and validation sets.

Key Insights and Ideas

  • ViTs have "critical" layers for robustness: Just like CNNs, specific layers in ViTs disproportionately contribute to adversarial vulnerability and overfitting. Identifying and targeting these layers is key.
  • Layer sharpness as an overfitting indicator: The paper successfully adapts the sharpness concept from SAM to pinpoint layers most prone to adversarial overfitting, offering a more granular approach than global SAM.
  • Selective fine-tuning is more efficient and effective: Instead of fine-tuning the entire ViT, focusing only on the critical layers with SAM significantly improves robustness and clean accuracy while avoiding convergence issues.
  • SAFER is adaptable: The method integrates seamlessly with existing adversarial training techniques and Parameter-Efficient Fine-Tuning (PEFT) methods, demonstrating its broad applicability.
  • Gradient masking is not an issue: The black-box evaluation results show that SAFER does not rely on gradient masking for its robustness improvements, indicating genuine defense.

Key Results

  • Significant Accuracy Gains: SAFER consistently improves both clean and adversarial accuracies across various ViT architectures (DeiT, ViT, ConViT, Swin Transformers) and datasets. Typical improvements are around 5%, with some cases reaching up to 20% gains in adversarial accuracy.
  • Outperforms Baselines: SAFER demonstrates superior performance compared to standard PGD-AT (with SGD or SAM) and other layer-selective methods like CLAT and RiFT, especially on ViT architectures.
  • Robustness Against AutoAttack: Models trained with SAFER retain robustness against Auto Attacks, a comprehensive ensemble of diverse attacks, even when trained solely on PGD attacks, suggesting a more generalizable defense.
  • Minimal Overhead: The computational overhead for identifying critical layers is negligible, adding only about 0.2% to the total training time. SAFER fine-tuning epochs are only ~3% slower than standard SGD training.
  • Effective with PEFT: SAFER enhances robustness when used with PEFT methods like LoRA and DORA, making it suitable for scenarios where full model fine-tuning is not feasible.

Limitations and Future Work

  • Hyperparameter Sensitivity: The paper acknowledges that the number of epochs for initial adversarial training and the frequency of layer selection are SAFER hyperparameters that require tuning.
  • Theoretical Understanding: While SAFER demonstrates empirical success, the paper points out that open questions remain regarding why certain layers become prone to overfitting and how to identify them with even greater precision.
  • Deeper Theoretical Exploration: The authors suggest that a deeper theoretical exploration into the causes of overfitting in ViTs and the mechanisms behind SAFER's effectiveness would be valuable future work.

Summary updated

BibTeX
@inproceedings{gopal2025safer,
  author = {Bhavna Gopal and Huanrui Yang and Mark Horton and Yiran Chen},
  title = {{SAFER: Sharpness Aware layer-selective Finetuning for Enhanced Robustness in vision transformers}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2025},
}
Figure