Domain-Conditioned Transformer for Fully Test-time Adaptation

Yushun Tang, Shuoshuo Chen, Jiyuan Jia, Yi Zhang, Zhihai He · DOI

Introduction

In real-world deployments, machine learning models frequently encounter "domain shift," where the data seen during inference (the target domain) differs significantly from the data used during training (the source domain). This discrepancy—often caused by environmental factors like weather, sensor noise, or stylistic changes—can lead to catastrophic performance degradation.

Fully Test-time Adaptation (TTA) has emerged as a critical paradigm to address this. Unlike standard domain adaptation, TTA requires the model to adapt to new data online, batch-by-batch, without access to the original training set. While previous research focused heavily on Convolutional Neural Networks (CNNs), the adaptation of Vision Transformers (ViTs) presents unique challenges. This paper introduces the Domain-Conditioned Transformer (DCT), a novel approach designed to restore the representational power of ViTs under distribution shift.

The Problem: Distorted Attention Profiles

The authors identify a specific failure mode in Vision Transformers: when exposed to out-of-distribution data, the model's self-attention profiles become distorted. In a healthy model, attention is typically focused on relevant local features or specific objects. However, under domain shift, the "attention distance" increases—the model's focus spreads out or shifts toward irrelevant, domain-specific noise. This distortion prevents the Transformer from extracting the semantic features necessary for accurate classification.

The Domain-Conditioned Transformer (DCT)

To counteract these distortions, the proposed DCT architecture introduces a mechanism for layer-by-layer feature "cleaning." The core components include:

1. Domain Conditioners

The researchers inject three learnable vectors—$c_q$, $c_k$, and $c_v$—into the Query, Key, and Value components of the self-attention mechanism. These vectors act as filters that capture and neutralize domain-specific perturbations.

2. Dynamic Conditioner Generation

Rather than using static parameters, the DCT employs a lightweight Domain Conditioner Generator. This generator takes the class token (CLS) as input. Because the class token naturally aggregates global information about the image, it serves as an effective "domain sensor," providing the context needed to generate conditioners tailored to the specific corruption present in the input.

3. Layer-wise Refinement

As data progresses through the network, the conditioners work iteratively. Early layers focus on identifying the domain noise, while deeper layers gradually recover the original self-attention profiles. Visualization via t-SNE confirms this: images initially clustered by corruption type (e.g., all snowy images grouped together) are gradually re-clustered by their actual semantic class as they reach the final layers.

Test-Time Optimization

During the inference phase, the model does not update its backbone weights. Instead, it optimizes only the conditioner generators and the existing LayerNorm parameters. The optimization process utilizes two key strategies:

  • Reliable Entropy Minimization: The model minimizes the uncertainty of its predictions but filters out high-uncertainty samples that might provide noisy gradients.
  • Sharpness-Aware Minimization (SAM): This technique ensures the model converges to a "flat" minimum in the loss landscape, which is empirically linked to better generalization and robustness.

Experimental Results

The DCT was evaluated against several state-of-the-art TTA methods (such as TENT and SAR) across multiple benchmarks:

  • ImageNet-C: On this benchmark of 15 different image corruptions, DCT achieved an average accuracy of 66.3%, a 2.7% improvement over the previous best-performing method.
  • Small Batch Robustness: Many TTA methods fail when the batch size is reduced to one (BS=1). DCT remains highly effective in these settings, outperforming competitors by 1.6%, making it suitable for real-time edge computing.
  • Cross-Domain Generalization: On the Office-Home and ImageNet-R datasets, DCT consistently outperformed source models and existing adaptation techniques, demonstrating its versatility across various types of distribution shifts (from digital corruptions to artistic renditions).

Conclusion

The Domain-Conditioned Transformer represents a significant step forward for robust Vision Transformers. By leveraging the class token to generate dynamic conditioners, the model effectively "unlearns" domain noise in real-time. While the reliance on Sharpness-Aware Minimization introduces some computational overhead, the minimal parameter increase (~1.7M) and superior performance under small batch sizes make DCT a compelling solution for deploying Transformers in unpredictable, real-world environments.

Summary updated

BibTeX
@inproceedings{tang2024domain,
  author = {Yushun Tang and Shuoshuo Chen and Jiyuan Jia and Yi Zhang and Zhihai He},
  title = {{Domain-Conditioned Transformer for Fully Test-time Adaptation}},
  booktitle = {ACM Multimedia},
  year = {2024},
  url = {https://openreview.net/forum?id=UNv6WBb6ZT},
}
Figure