A Layer Selection Approach to Test Time Adaptation

Sabyasachi Sahoo, Mostafa ElAraby, Jonas Ngnawé, Yann Batiste Pequignot, Frédéric Precioso, Christian Gagné

Introduction

When deploying deep learning models in real-world scenarios, they frequently encounter distribution shifts—data that differs significantly from the training set. Test Time Adaptation (TTA) aims to mitigate this by updating a pretrained model on the fly using unlabeled target data during inference. However, standard TTA methods typically update all model parameters, a strategy that can lead to performance collapse or catastrophic forgetting of source knowledge.

The Challenge of Global Adaptation

Research indicates that adapting the wrong layers can be detrimental, sometimes resulting in performance worse than the original non-adapted model. Crucially, the "optimal" layer for adaptation is not static; it varies depending on the specific data shift and the unsupervised loss function employed. The primary challenge lies in dynamically identifying which layers to adapt at test time without access to labeled data.

Gradient-Aligned Layer Adaptation (GALA)

To address these issues, this paper introduces GALA (Gradient-Aligned Layer Adaptation). GALA evaluates each layer individually for every incoming sample to determine whether an update should be applied.

Gradient Alignment Criterion

The core of GALA is a cosine similarity criterion. As a model adapts, gradients can become noisy. GALA calculates the cosine similarity between a layer's proposed gradient update for the current sample ($u_i$) and the layer's total parameter displacement ($TD_{i-1}$), representing the sum of all previous updates.

Gradient alignment criterion for update selection.

If the angle $\alpha_i$ between these vectors is too wide, indicating that the new update contradicts the established learning trajectory, GALA masks the update. Only updates that exceed a predefined alignment threshold are applied.

Reset Windows

While the cosine criterion ensures stability, a strict adherence could prevent the model from adapting to genuine changes in the loss landscape. GALA incorporates a "reset window" mechanism that periodically resets the anchor point used for calculating total displacement.

Dynamic layer adaptation and reset windows in GALA.

This periodic reset allows the model to pivot its trajectory when necessary, dynamically switching between "Adapt" and "Freeze" states for different layers as the data stream evolves. This granularity can be applied at the block or individual layer level, with finer control typically yielding superior results.

Experimental Evaluation

GALA was evaluated across several rigorous benchmarks:

  • Domainbed: Assessing large, single-distribution shifts using datasets like PACS, VLCS, TerraIncognita, and Office-Home.
  • Continual TTA: Evaluating multi-domain, continuous shifts on CIFAR10C and CIFAR100C.
  • Tiny-Domainbed: A custom subset used for "oracle" ablation studies to compare GALA against brute-force layer selection.

Key Insights and Results

  • Contextual Receptiveness: The study confirms there is no universal "best layer" to fine-tune; receptiveness depends on the shift and the loss function (e.g., SHOT or Pseudo-Labeling).
  • Prevention of Forgetting: By filtering out misaligned gradients, GALA prevents the destruction of source domain knowledge that often plagues "All layers" adaptation. It strikes a balance between learning new features and retaining pretrained ones.
  • Performance Gains: On Domainbed, GALA outperformed the standard "All layers" approach by over 5%. In the more challenging Continual TTA setting, GALA achieved a massive ~65% improvement over the "All layers" baseline.
  • Robustness: GALA remains effective even in extreme single-sample adaptation scenarios (batch size = 1), where other methods typically fail.

Conclusion

GALA provides a robust, unsupervised method for selective layer adaptation. By treating conservative updating as a form of regularization, it mimics the performance of an informed "oracle" and significantly improves the reliability of models undergoing test-time adaptation. Future work may focus on making the alignment thresholds and window sizes fully adaptive to the incoming data stream.

Summary updated

BibTeX
@inproceedings{sahoo2025layer,
  author = {Sabyasachi Sahoo and Mostafa ElAraby and Jonas Ngnawé and Yann Batiste Pequignot and Frédéric Precioso and Christian Gagné},
  title = {{A Layer Selection Approach to Test Time Adaptation}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i19.34229},
}
Figure