Improving the Behaviour of Vision Transformers with Token-Consistent Stochastic Layers

Nikola Popović, Danda Pani Paudel, Thomas Probst, Luc Van Gool

TL;DR

Researchers have developed a way to make Vision Transformers (ViTs) more reliable, robust, and private by injecting "token-consistent" noise into their internal layers. By ensuring the same random scaling is applied to every visual "token" simultaneously, the network learns to prioritize the underlying structure of an image over raw pixel values, significantly improving performance in adversarial defense and data privacy with negligible impact on accuracy.

What Problem Does This Paper Address?

Vision Transformers (ViTs) have become a dominant force in computer vision, but they share common weaknesses with many deep learning models:

  1. Overconfidence: They often provide high-certainty predictions even when they are wrong (poor calibration).
  2. Vulnerability: Small, invisible perturbations to an image (adversarial attacks) can easily fool them.
  3. Privacy Leaks: In collaborative environments where features are shared (like mobile apps sending data to a server), it is often possible for an attacker to "reconstruct" the original sensitive input image from those intermediate features.

While adding randomness (stochasticity) is a known fix for some of these issues, standard methods like Dropout often degrade a model's accuracy or fail to provide strong enough protection. The authors seek a form of randomness that "strengthens" the model without breaking its ability to recognize objects.

Methods

The authors introduce Token-consistent Stochastic Layers.

In a standard ViT, an image is broken into "tokens" (small patches). These tokens pass through Multi-Layer Perceptron (MLP) blocks. The authors insert a stochastic operation inside these blocks.

The Key Innovation: Instead of applying random noise to every single value independently (like standard Dropout), they sample one random vector for an entire block and apply it to all tokens identically.

  • Mathematical Intuition: This operation is a "homeomorphism"—a shape-preserving transformation. Because every token is scaled by the same random factor, the topological relationship between the tokens remains the same, even though their individual values are scrambled.
  • Implementation: They use a square diagonal matrix $A$ sampled from a uniform distribution $U(1-\Delta, 1+\Delta)$. This is inserted into the MLP after the first fully connected layer.
  • Efficiency: This requires no architectural changes and no additional learnable parameters. A brief 30-epoch fine-tuning session is enough to adapt a pre-trained ViT to this new "noisy" environment.

Datasets

The primary training and evaluation were conducted on:

  • ImageNet-1k: The gold standard for image classification (1.2M training images).
  • Transfer Learning Suite: 12 diverse datasets including CIFAR-10/100, Flowers, Pets, and SUN397 to test if the "noisy" features still work on new tasks.
  • CIFAR-10/100: Used specifically for evaluating privacy in collaborative learning setups.

Key Insights and Ideas

  • Topology over Values: By forcing the network to deal with consistent scaling noise, the authors hypothesize that the model stops "memorizing" specific feature values and starts relying on the "General Topology" of the data.
  • The "Aha" Moment on Consistency: The authors found that being consistent across tokens is actually a "stronger" form of noise than independent noise. If noise is independent (like Dropout), the network can essentially "average it out" across tokens. When it's consistent, the network has to learn to be truly invariant to that scaling.
  • Privacy through Obfuscation: Because the noise is applied during inference, an attacker trying to reconstruct an image from a stolen feature map faces a "moving target." The same image produces different feature maps every time, making reconstruction significantly more difficult.

Key Results

  • Accuracy: On ImageNet, the DeiT-S model maintained a high accuracy (~79.6%) even with moderate noise. Even at extreme noise levels ($\Delta = 1.0$), accuracy only dipped slightly while other benefits skyrocketed.
  • Calibration: The Expected Calibration Error (ECE) dropped from 0.026 (standard) to 0.010 (theirs), meaning the model's confidence scores much more accurately reflected its actual likelihood of being correct.
  • Adversarial Robustness: When combined with adversarial training, their method achieved a 49.32% accuracy under a PGD-10 attack, significantly outperforming the standard adversarial training baseline of 42.33%.
  • Privacy: In reconstruction attacks, the error (L1 loss) for an attacker trying to recover the original image was nearly double that of a deterministic network, effectively "blurring" sensitive details like faces in the feature space.

Limitations and Future Work

  • Fine-tuning Required: While the method doesn't need new parameters, it does require the model to be fine-tuned to handle the noise. It isn't a "plug-and-play" solution for a frozen, pre-trained model.
  • Not "Certifiable": While the empirical robustness is high, the authors note it does not yet meet the mathematical standards of "certifiable" defense, meaning a clever enough future attack might still find a way through.
  • Subgroup Analysis: The authors acknowledge they haven't yet studied if this noise injection affects different subgroups of data (e.g., demographic bias) differently, which is a critical step for ethical deployment.

Summary updated

Abstract

We introduce token-consistent stochastic layers in vision transformers, without causing any severe drop in performance. The added stochasticity improves network calibration, robustness and strengthens privacy. We use linear layers with token-consistent stochastic parameters inside the multilayer perceptron blocks, without altering the architecture of the transformer. The stochastic parameters are sampled from the uniform distribution, both during training and inference. The applied linear operations preserve the topological structure, formed by the set of tokens passing through the shared multilayer perceptron. This operation encourages the learning of the recognition task to rely on the topological structures of the tokens, instead of their values, which in turn offers the desired robustness and privacy of the visual features. The effectiveness of the token-consistent stochasticity is demonstrated on three different applications, namely, network calibration, adversarial robustness, and feature privacy, by boosting the performance of the respective established baselines.
BibTeX
@misc{popovic2021improving,
  author = {Nikola Popović and Danda Pani Paudel and Thomas Probst and Luc Van Gool},
  title = {{Improving the Behaviour of Vision Transformers with Token-Consistent Stochastic Layers}},
  year = {2021},
  eprint = {2112.15111},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2112.15111},
}

Loading related content...

Figure