Abstract
Machine learning models frequently encounter performance degradation when deployed on data that differs from their training distribution, a phenomenon known as domain shift. Test-Time Adaptation (TTA) aims to mitigate this by updating models during inference. However, traditional TTA methods often rely on computationally expensive gradient updates and suffer from catastrophic forgetting of source knowledge. This paper explores AdaNPC (Adaptive Non-Parametric Classifier), a novel framework that replaces standard parametric classifiers with a k-Nearest Neighbor (kNN) memory bank. By appending high-confidence test samples to this memory bank, AdaNPC achieves rapid, gradient-free adaptation while maintaining stability across successive domain shifts. Our results demonstrate that AdaNPC significantly outperforms existing TTA methods in both accuracy and computational efficiency across multiple benchmarks.
Introduction
In real-world deployments, the assumption that training (source) and testing (target) data are independent and identically distributed (i.i.d.) rarely holds. Domain shift—caused by changes in lighting, weather, sensor noise, or geographic location—can lead to catastrophic failures in static models. To address this, researchers have developed two primary paradigms: Domain Generalization (DG), which seeks to build robust models a priori, and Test-Time Adaptation (TTA), which allows models to adjust to specific target data on-the-fly.
Despite their promise, current TTA methods face two significant hurdles. First, they are computationally intensive, typically requiring backpropagation for every new batch of test samples, which limits their utility in real-time or resource-constrained environments. Second, they are prone to "domain forgetting"; as the model parameters are tuned to the target domain, the model's performance on the original source domain (or previously encountered target domains) often collapses. AdaNPC addresses these challenges by shifting from a parametric to a non-parametric classification strategy.
Methodology: The AdaNPC Framework
AdaNPC introduces a fundamental shift in model architecture by replacing the final linear layer of a neural network with a kNN-based memory bank. The framework consists of three distinct phases: training, inference, and adaptation.
1. Training with kNN Loss
Unlike standard models trained with cross-entropy loss, AdaNPC is trained using a kNN-based loss function. This objective encourages the feature encoder to map samples into a high-dimensional space where instances of the same class form tight, well-separated clusters. This structured feature space is essential for the subsequent kNN retrieval process.
2. The Memory Bank and Inference
Following training, the features and labels of the source dataset are stored in a memory bank. When a test sample is encountered, the encoder extracts its feature representation. The model then identifies the $k$ nearest neighbors within the memory bank using cosine similarity. The final prediction is determined by a weighted vote among these neighbors. To ensure scalability, AdaNPC utilizes efficient vector search libraries like Faiss, allowing the kNN search to remain nearly as fast as a standard linear layer forward pass.
3. Gradient-Free Adaptation
Adaptation in AdaNPC is remarkably simple: if the model's prediction for a test sample exceeds a predefined confidence threshold, the sample's feature vector and its predicted label (pseudo-label) are appended to the memory bank. This allows the model to "expand" its knowledge base with target-specific information without modifying the underlying encoder weights. Because no gradients are calculated, the process is inherently stable and avoids the pitfalls of weight-based forgetting.
Theoretical Motivation
The authors provide a theoretical justification for AdaNPC based on the reduction of distribution divergence. By utilizing a local neighborhood for classification rather than a global parametric boundary, the model effectively filters out source data that is irrelevant to the specific target sample. Mathematically, this approach reduces the Wasserstein distance between the source and target distributions, leading to more robust generalization in the presence of shift.
Experimental Results
AdaNPC was evaluated against state-of-the-art TTA and DG methods across several benchmarks, including PACS, VLCS, DomainNet, and corruption datasets like ImageNet-C.
Accuracy and Robustness
On the Rotated MNIST benchmark, which tests adaptation to a long sequence of evolving domains, AdaNPC achieved an accuracy 50% higher than leading TTA methods such as Tent and T3A. While competing methods saw their performance degrade as they adapted to successive domains, AdaNPC's performance remained stable, demonstrating its unique ability to handle "continual" TTA without catastrophic forgetting.
Computational Efficiency
One of the most significant advantages of AdaNPC is its speed. On ImageNet, AdaNPC recorded an inference latency of approximately 22ms per image. In contrast, gradient-based TTA methods often required over 270ms per image. This order-of-magnitude improvement makes AdaNPC a viable candidate for real-time applications on edge devices.
Zero Forgetting
In successive adaptation tests, where a model is evaluated on the source domain after adapting to multiple target domains, AdaNPC maintained its original source accuracy. In some instances, the addition of diverse target samples to the memory bank even slightly improved the model's performance on the original source data, a feat rarely achieved by parametric models.
Discussion and Limitations
While AdaNPC offers significant advantages, it is not without trade-offs. The primary concern is the memory footprint. Storing feature vectors for large datasets like ImageNet requires significant RAM (approximately 2.4GB). While manageable for modern servers, this may pose challenges for low-power embedded systems. Additionally, the model is sensitive to pseudo-label noise; if a high-confidence but incorrect prediction is added to the memory bank, it could potentially degrade future predictions. Future research may focus on memory pruning techniques and more robust filtering mechanisms to mitigate these risks.
Conclusion
AdaNPC represents a significant departure from traditional Test-Time Adaptation by leveraging the flexibility of non-parametric classifiers. By treating adaptation as a memory expansion problem rather than a parameter optimization problem, it achieves state-of-the-art performance, superior computational efficiency, and total immunity to catastrophic forgetting. As models are increasingly deployed in dynamic, unpredictable environments, the principles of gradient-free, expandable knowledge demonstrated by AdaNPC provide a promising path forward for robust machine learning.
Summary updated