ON SINGLE TEST SAMPLES
Year: 2022

Abstract:
We strive to learn a model from a set of source domains that generalizes well to unseen target domains. The main challenge in such a domain generalization scenario is the unavailability of any target domain data during training, resulting in the learned model not being explicitly adapted to the unseen target domains. We propose learning to generalize across domains on single test samples. We leverage a meta-learning paradigm to learn our model to acquire the ability of adaptation with single samples at training time so as to further adapt itself to each single test sample at test time. We formulate the adaptation to the single test sample as a variational Bayesian inference problem, which incorporates the test sample as a conditional into the generation of model parameters. The adaptation to each test sample requires only one feed-forward computation at test time without any fine-tuning or self-supervised training on additional data from the unseen domains. Extensive ablation studies demonstrate that our model learns the ability to adapt models to each single sample by mimicking domain shifts during training. Further, our model achieves at least comparable – and often better – performance than state-of-the-art methods on multiple benchmarks for domain generalization 1.

heading: 1 INTRODUCTION
text: Despite their widespread adoption and success in academia and industry alike, deep convolutional neural networks suffer from a fundamental flaw: they have insufficient generalizability to test data that is out of their training distributions (Recht et al., 2019). Improving the generalization of machine learning methods therefore remains a challenging problem (Moreno-Torres et al., 2012; Recht et al., 2019; Krueger et al., 2021). To deal with the distribution shift, domain adaptation (Saenko et al., 2010; Long et al., 2015; Lu et al., 2020; Li et al., 2021) and domain generalization (Muandet et al., 2013; Li et al., 2017; 2020) have been extensively investigated. In domain adaptation, the key assumption is that the target data, either labeled or unlabeled, is accessible during training, which allows the model to be adapted to the target domains. However, this assumption does not necessarily hold in realistic scenarios where the target domain data is not available. By contrast, domain generalization strives to learn a model on source domains that can generalize well to unseen target domains without any access to the target domain data during training. We aim for domain generalization.
Previous domain generalization approaches have successfully explored domain-invariant learning (Muandet et al., 2013; Chattopadhyay et al., 2020) or domain augmentation (Shankar et al., 2018; Zhou et al., 2020a) to handle the domain shift between the source and target domains. However, since those models are trained on source domains, there will always be an “adaptivity gap” when applying them to target domains without further adaptation (Dubey et al., 2021). Thus, it is necessary to train models that are able to further adapt to target domains without ever using target data during training.
Recently, further adapting a pre-trained model using target domain data by fine-tuning or selfsupervised training at test time has shown effectiveness in improving performance on domain generalization (Sun et al., 2020; Wang et al., 2021). To achieve proper adaptation to the target domain, these methods typically rely on extra fine-tuning operations on target data or extra networks (Dubey et al., 2021) at test time. Although some of these methods can achieve adaptation with a single test
1 Code available: https://github.com/zzzx1224/SingleSampleGeneralization-ICLR2022
sample (D’Innocente et al., 2019; Sun et al., 2020; Banerjee et al., 2021), batches of target data are required for good performance. This prevents these methods from effectively adapting to the target domain when provided with very few target samples or exposed to test samples from multiple target domains without domain identifiers. Different from these works, we aim to learn the ability to adapt to a single sample such that the model can further adapt to each test sample from any target domain.
In this paper, we propose learning to generalize on single test samples for domain generalization. Since a single sample from the target distribution cannot inform much about the whole distribution, we consider each target sample a domain by itself. Correspondingly, we propose that each target sample adjusts the trained model in its own way. This avoids the difficulty of using the limited information available to adapt the model to the entire target domain distribution. An illustration of the method is shown in Figure 1. Using meta-learning (Li et al., 2017), we train our model to
acquire the ability to adapt with each single sample on source domains such that it can then adapt to each individual sample in the target domain to handle the domain shift at test time. Unlike previous methods, our model does not need to be fine-tuned using target data or train extra networks when generalized to the unseen target domains. Once it is trained on source domains, our model is able to adapt to each single target sample with just a feed-forward computation at test time, without the need for any extra target data. Note that not a single target sample is seen during training, which does not forgo the strict source and target separation setting of domain generalization. Moreover, in contrast to previous test-time adaptation methods (Sun et al., 2020; Wang et al., 2021; Dubey et al., 2021), our method does not need to re-adapt the model to each target domain. Our model effortlessly handles test samples from multiple domains without knowing their domain identifiers.
To be specific, we build our model under the meta-learning framework and formulate the single sample generalization as a variational inference problem. In the training stage, we divide the source domains into several meta-source domains and a meta-target domain and explore the adaptive model by incorporating the information of the meta-target sample into the generation of the model parameters. For any given meta-target sample, we propose a variational distribution generated by this sample and the meta-source data to approximate the model distribution obtained by the meta-target data. In the test phase, the adapted models for the samples from the real target domains are generated on the fly by the variational model distribution. The random splits of the meta-source and meta-target domains expose the model to domain shifts and mimic the real generalization process from source domains to the target domain. Thus, the model is endowed with the ability to adapt to any unseen sample by end-to-end training with the source data only. By doing so, our method does not need to introduce any extra target data or fine-tuning operations on the target domain. A comparison of the training/test-time settings among different domain generalization (and domain adaptation) methods is summarized in Table 1.
We demonstrate the effectiveness of our single sample generalization by conducting experiments on commonly-used benchmarks for domain generalization. Our ablation studies under different domain generalization settings demonstrate the advantages of our method in comparison to other recent adaptive methods.

heading: 2 RELATED WORK
text: Domain adaptation To handle the domain shift between source and target domains, considerable efforts have been devoted to developing methods for domain adaptation (Long et al., 2015; Lu et al., 2020; Hoffman et al., 2018; Kumar et al., 2010; Tzeng et al., 2017; Luo et al., 2019). However, the assumption that target data is accessible during training is often invalid. Recently, some even more challenging settings were proposed in domain adaptation, e.g., few-shot (Motiian et al., 2017a) and one-shot domain adaptation (Dong & Xing, 2018; Luo et al., 2020), but there are still a few or one target sample available during training. In this work, we focus on the more strict setting, domain generalization, that assumes not a single target sample is accessible during training. We construct our model under a meta-learning framework to learn the ability of adaptation to single samples across source domains. At test time, no more learning is needed and we generate an adapted model for each individual target sample by only a forward pass, without any extra fine-tuning operation.
Domain generalization The problem of domain generalization was introduced by Blanchard et al. (2011), and formally posed by Muandet et al. (2013). Muandet et al. (2013) and Ghifary et al. (2016) both considered domain-invariant learning by minimizing the dissimilarity of features across source domains. Following these works, many methods explored domain invariant learning for domain generalization (Motiian et al., 2017b; Rojas-Carulla et al., 2018; Li et al., 2018b; Seo et al., 2020; Zhao et al., 2020). Alternatively, augmenting the source domain data has been explored to simulate more domain shift during training (Shankar et al., 2018; Volpi et al., 2018; Zhou et al., 2020a; Qiao et al., 2020; Zhou et al., 2020b). Our method tackles domain shift by learning to adapt to each unseen target sample, which is orthogonal to domain-invariant and domain augmentation methods.
Domain meta-learning Meta-learning methods have also been studied for domain generalization (Balaji et al., 2018; Du et al., 2020; Xiao et al., 2021; Du et al., 2021). Li et al. (2018a) introduced the model agnostic meta-learning (Finn et al., 2017) into domain generalization. Dou et al. (2019) further enhanced the method with global and local regularization. We follow the experimental settings of previous methods (Du et al., 2020; Xiao et al., 2021) that split source domains into meta-source and meta-target domains. By mimicking the adaptation process at training time, our model learns to learn the adaptable classifiers from meta-source domains and the meta-target sample.
Test-time adaptation Recently, several methods have proposed to adapt a model trained on source data to unseen target data at test time (D’Innocente et al., 2019; Sun et al., 2020; Wang et al., 2021; Dubey et al., 2021). Sun et al. (2020) trained their model on source domains with both supervised and self-supervised objective functions, and then fine-tuned it on target data with only the self-supervised one. Wang et al. (2021) further improved this method by removing the self-supervised module, while fine-tuning the model with an entropy minimization loss. Differently, Dubey et al. (2021) trained a domain embedding network with unsupervised source data, which is utilized to generate the target domain embedding by hundreds of unlabeled target samples to adapt the model at test time, without fine-tuning or further training. These adaptive methods all need extra steps, e.g., fine-tuning the model or training embedding networks, and batches of target samples for good adaptation. In contrast, our method learns to adapt to each target sample using just its own information without any extra target samples and fine-tuning operations. The adaptation is achieved by generating the model for each target sample with only one forward pass. Another recent work utilized test-time augmentation to improve robustness at test time (Chai et al., 2021). While it is of interest to adapt a test-time sample to source domains with such test-time augmentations, our method aims to adapt the model trained by source domains to each target sample.

heading: 3 METHODOLOGY
text: In domain generalization, several domains are defined as different data distributions in the joint space X × Y , where X and Y denote the input space and the label space. The domains are divided into non-overlapping source domains, collectively denoted as S and the target domains T . During training, only data from source domains is accessible. Data from target domains is never seen. Our goal is to learn a model on the source domains only that generalizes well to the target domains. As
we focus on homogeneous domain generalization (Zhou et al., 2021) in this work, all domains share the same label space.
Since the target data is inaccessible during training, we design a meta-learning framework with episodic training (Li et al., 2019; Balaji et al., 2018) that endows the model with the capability of learning to generalize on single test samples from any unseen domain. To this end, according to the precise domain annotations (if available) or image clusters, we divide the source domain(s) S into several meta-source domains S ′ and a meta-target domain T ′ in each iteration to expose the model to domain shifts. After training, the learned model is applied to the target domain T for evaluation. In this case, the model is exposed to different domain shifts to learn the ability to handle it. After training, the learned model is applied to the target domain T for evaluation. Variational learning We develop our model under the probabilistic framework. Assume we have a single sample xt′ from the meta-target domain T ′, and we would like to predict its class label yt′ . We consider the conditional predictive log-likelihood log p(yt′ |xt′ , T ′). By incorporating the model θt′ into the prediction distribution, we have
log p(yt′ |xt′ , T ′) = log ∫ p(yt′ |xt′ ,θt′)p(θt′ |T ′)dθt′ , (1)
where we condition the prediction on T ′ to leverage information from the meta-target data and (xt′ ,yt′) denotes the input-output pair of the meta-target sample. We refer to p(θt′ |T ′) as the meta-prior distribution since it is conditioned on the meta-target data.
However, in practice, the labeled data from the target domain is inaccessible, leading to an intractable distribution p(θt|T ) during inference. To address the problem, we propose to approximate the model function p(θt|T ) with the accessible source data by the variational inference approach, which provides a framework to approximate intractable distributions via optimization. By training under the meta-learning framework, our method will learn the ability to approximate the model functions of unseen target domains by source domain data.
Variational domain generalization We introduce a variational distribution q(θt′ |S ′) to approximate the true posterior over the model parameters θ. By incorporating q(θt′ |S ′) into eq. (1), we derive a lower bound of the conditional predictive log-likelihood:
log p(yt′ |xt′ , T ′) = log ∫ p(yt′ |xt′ ,θt′)p(θt′ |T ′)dθt′
≥ Eq(θt′ )[log p(yt′ |xt′ ,θt′)]− DKL[q(θt′ |S ′)||p(θt′ |T ′)].
(2)
The introduced Kullback-Leibler (KL) divergence term is minimized to reduce the distance between q(θt′ |S ′) and p(θt′ |T ′), which encourages the model θ inferred by q(θt′ |S ′) from meta-source domains to be generalizable to the meta-target domain.
Although eq. (2) reduces the difference between the variational distributions q(θt′ |S ′) and p(θt′ |T ′), q(θt′ |S ′) still has limited capacity of handling the meta-target samples due to “adaptivity gap” (Dubey et al., 2021). Since only little information about the meta-target domain T ′ and the meta-target sample xt′ is available in S ′, there is no guarantee that the variational distribution q(θt′ |S ′) will be adapted to the meta-target domain T ′. To reduce the “adaptivity gap”, the model needs to acquire the ability to effectively use the target information. Thus, we further propose to learn to adapt the model θt′ by taking more information of the meta-target data into account.
Single sample generalization In domain generalization, especially for real applications, when given data from an unseen target domain, the only accessible information of the target data is from the target sample, without any annotations. We therefore propose to utilize the information contained in the given target sample to construct the adapted model. Instead of utilizing q(θt′ |S ′) as the variational distribution, we incorporate the meta-target sample xt′ into the variational distribution q(θt′ |xt′ ,S ′) to approximate the true posterior distribution. Given an unlabeled sample xt′ from the meta-target domain T ′, we have a new evidence lower bound (ELBO) as follows:
log p(yt′ |xt′ , T ′) = log ∫ p(yt′ |xt′ ,θt′)p(θt′ |T ′)dθt′
≥ Eq(θt′ )[log p(yt′ |xt′ ,θt′)]− DKL[q(θt′ |xt′ ,S ′)||p(θt′ |T ′)].
(3)
By conditioning on the given sample xt′ and the source data S ′, the adapted variational distribution q(θt′ |xt′ ,S ′) contains both the categorical information provided by the meta-source data and the
domain information given by the meta-target sample. This to a large extent guarantees the inferred model to be discriminative and adapted to the given meta-target domain sample. Moreover, the KL divergence in eq. (3) further acts as a regularizer to guide the variational distribution to be similar to the parameter distribution p(θt′ |T ′) obtained by the meta-target data, which is assumed to be the most suitable parameter for the meta-target sample xt′ . This makes the model better adapted to xt′
Based on the meta-learning framework and eq. (3), our model learns the ability to adapt the metasource model to each meta-target instance across different domain shifts. The learned ability is exploited to handle domain shifts at test time by adapting the source model to each target instance. During inference, our method directly uses the variational distributions q(θt|xt,S) as the adapted model generated by the information of the given target sample xt and the data from all source domains S . Thus, the method is able to generalize to each unseen target sample without the requirement of any extra data from the target domains or fine-tuning operations. The derivations of eq. (2) and eq. (3), as well as more discussions on their tightness are provided in Appendix A.
Learning to adapt classifiers The variational inference framework in eq. (3) indicates that we can learn to adapt all model parameters θ to the target sample with its own information. This could, however, be computationally infeasible due to the large number of parameters in the network. Thus, for computational efficiency and feasibility we utilize the features from the feature extractor as the sample-specific information to construct the adapted classifier, which contains much fewer parameters.
To do so, we divide the model θ into a feature extractor ϕ and a classifier w. The feature extractor is shared across domains, while the classifier is trained to be adapted to each single sample by eq. (3). Both the meta-prior distribution p(wt′ |T ′) and the variational posterior distribution q(wt′ |xt′ ,S ′) of the classifier wt′ are generated by amortized inference using the amortization technique (Kingma & Welling, 2013; Gershman & Goodman, 2014; Gordon et al., 2019; Shen et al., 2021). The amortization technique provides a natural way to generate model parameters by feature representations, which is one appropriate solution for our method to incorporate the target feature into the construction of the adapted variational posterior distribution. Moreover, the amortization networks can be trained and evaluated together with the model, without introducing extra training steps and fine-tuning operations to achieve the adaptation ability. Specifically, we use the center features of samples in each class from the meta-target domain and meta-source domains as the representatives of T ′ and S ′. We generate the meta-prior distribution p(wt′ |T ′) by taking the center features of T ′ as input to the amortized inference network θa. For the variational distribution q(wt′ |xt′ ,S ′), the input to the amortized inference network is the concatenation of the center features of the meta-source domain S ′ with the features of the target sample xt′ for adaptation. The amortized inference network θa is shared for the inference of both the meta-prior and posterior distributions.
However, the direct concatenation operation is not necessarily optimal as the input for the amortized inference network. We propose to generate the parameter distribution using the meta-source domains and then adapt it with the given meta-target sample. To do so, we first generate a classifier distribution p(ws′ |S ′) using the center features of the meta-source data S ′. After that, we take p(ws′ |S ′) as a prior and combine it with the meta-target sample to estimate the variational posterior, as follows:
q(wt′ |xt′ ,S ′) = ∫ p(wt′ |xt′ ,ws′)p(ws′ |S ′)dws′ . (4)
This establishes a hierarchical variational inference of the posterior distribution. We show in our experiments that the hierarchical inference yields better results than the direct concatenation. In practice, we approximate the integral by:
q(wt′ |xt′ ,S ′) = L∑
ℓ=1
p(wt′ |xt′ ,w(ℓ)s′ ), (5)
where w(ℓ)s′ ∼ p(ws′ |S ′) and L is the number of Monte Carlo samples. The negative KL divergence term in eq. (3) tries to close the gap between the variational adaptive distribution q(wt′ |xt′ ,S ′) and the meta-prior distribution p(wt′ |T ′), aiming to make the variational distribution adapt better to the target data. However, there is no guarantee that the classifier produced by the meta-prior is discriminative enough for classification. To address this issue, we introduce an intermediate supervision based on the meta-prior distribution to make it as predictive as possible, by maximizing:
Ep(wt′ |T ′) [ log p(yt′ |xt′ ,wt′) ] . (6)
This is also implemented as a cross-entropy loss using Monte Carlo sampling from the meta-prior. An illustration of our architecture is provided in Figure 2. We also provide an algorithm in Appendix B.
Empirical objective function By incorporating eq. (5) and eq. (6) into eq. (3), we obtain the final empirical objective function:
L= M∑
m=1
log p(yt′ |xt′ ,w(m)t′ ) + N∑
n=1
log p(yt′ |xt′ ,w(n)t′ )− DKL( L∑
ℓ=1
p(wt′ |xt′ ,w(ℓ)s′ )||p(wt′ |T ′))
(7) where we use Monte Carlo sampling: w(m)t′ ∼ ∑L ℓ=1 q(wt′ |xt′ ,w (ℓ) s′ ) and w (n) t′ ∼ p(wt′ |T ′).
As illustrated in Figure 3, at training time (a), we train the model episodically on meta-source domains S ′ and a meta-target domain T ′. At inference time (b), given a single sample xt from the target domain T , we use this sample to adapt the ws generated by the source data. The adaptation is achieved by generating the adapted classifiers wt for each target sample with only one forward pass using the learned amortization inference network θa.

heading: 4 EXPERIMENTS
text: We conduct our experiments on four widely used benchmarks for domain generalization: PACS (Li et al., 2017), Office-Home (Venkateswara et al., 2017), rotated MNIST, and Fashion-MNIST (Piratla et al., 2020). The details of the datasets and our implementations are provided in Appendix C.
Benefit of single sample generalization We first investigate the effectiveness of our single sample generalization on the PACS dataset. To demonstrate the benefits of all components of our method, we consider four settings: (i) the “variational amortized classifier” with eq. (2) as the objective function, (ii) our single sample generalization method without hierarchical architecture (eq. 5), (iii) single sample generalization without supervision on the meta-prior distribution (eq. 6), and (iv) the entire single sample generalization method, with the objective function in eq. (7). Results based on the ResNet-18 backbone are shown in Table 2.
By comparing rows 1 and 2 of Table 2, it is clear that our single sample generalization brings an improvement in accuracy over the invariant amortized classifier, especially on the “art-painting” and “cartoon” domains. These results demonstrate the benefits of introducing the information of the given target sample into the model generation. The comparison also demonstrates that our model learns the capability to adapt to the target sample using its own information. Comparing the last two rows shows the benefits of the intermediate supervision in eq. (6), which provides a more discriminative
meta-prior distribution during training to learn more adapted variational distribution, especially on the “art-painting”, “cartoon” and “sketch” domains. Results in rows 2 and 4 of Table 2 show that the hierarchical architecture for the variational adaptive classifier q(wt′) provides a more adapted model for each target sample and achieves the best performance on all domains.
Importance of appropriate domain shift during training To show the importance of mimicking an appropriate domain shift during training, we conduct experiments with different divisions of source domains during training. As shown in Table 3, when we randomly split images into source domains (row 1), the domain shift between them is too small to mimic the domain shift between source and target domains. Thus, the performance are not so good, especially on “art-painting” and “sketch”. When we split the source domains with image clusters (row 2) and precise domain annotations (row 3), the performance is improved, demonstrating the importance of constructing appropriate domain shift during training. By better mimicking domain shifts during training, our method achieves better performance. Importantly, the experiments also show that our method can improve the performance without the need to have multiple domains and annotations by a simple unsupervised domain split method, e.g., clustering. The conclusion is also demonstrated in the single-source domain experiments that are provided in Table 11, Appendix D.
Ablation of meta-learning setting To show the importance of the meta-learning framework, we also implement a non-meta-learning version of our method. We directly generate the adapted classifiers by features from all source domains and the feature representations of the single sample. The results are shown in Table 4. Without the meta-learning framework, the model has difficulty to learn the ability to adapt to unseen domains and tends to overfit to the source domains. Thus the performance on all domains is worse than the meta-learning-based model.
Influence of backbone To show the influence of backbone models on the single sample generalization, we further compare our proposal with a baseline empirical risk minimization (ERM) method on PACS by varying the backbone model size. As shown in Table 5, our method achieves more obvious performance gains with larger backbone models compared to the ERM baseline. We explain this by our use of an amortized inference network that generates the classifier by the source domain features and the target sample feature. Implying an increase in backbone capacity has a direct effect on our classifier capacity, enabling better adaptation to the target sample.
Visualization of single sample generalization To illustrate the benefit of incorporating the test sample into classifier generation, we visualize the the target features and the classifiers of both the
Single sample generalization vs. Tent2 Next we conduct an experiment on rotated-MNIST to compare our method with Tent by Wang et al. (2021), which is a recent test-time adaptation method that fine-tunes the model with batches of target samples. We follow the setting in (Piratla et al., 2020) and generalize seven domains by rotating the samples by 0◦ to 90◦ in intervals of 15◦. To make the target domains more different from each other, we use images rotated by 0◦, 45◦ and 90◦ as target domains, 15◦, 30◦, 60◦ and 75◦ as source domains. For fair comparisons, we train the base model of Tent using data from all source domains, which is the same amount of train and validation data as in our method. Then, the trained base model is adapted by the target samples as in the original paper of Tent. The other settings are the same as our methods. As shown in Figure 5, we design two settings for Tent for the comparisons: “single target domain” (left) and “multiple target domains” (right). In the “single target domain” setting, we adapt the base model to each of the target domains separately using Tent and take the average accuracy of different domains. For “multiple target domains”, Tent is required to adapt to all three target domains jointly without the domain identifiers. Since our method is able to adapt to each sample, the performance is consistent under both settings.
As expected, Tent achieves good adaptation for large batch sizes (e.g., 128) from a single target domain. However, when there are only very few samples, and ultimately just one sample, the adaptation of Tent starts to suffer, and may even hurt accuracy. Tent, like other test-time adaptive domain generalization methods, relies on a batch of samples to adapt to the target domain. In other words, the test samples have known domain identifiers. However, in practical settings, the test samples might come from different domains and the domain identifiers may be inaccessible. Thus, we also explore the effectiveness under the “multiple target domains” setting, as shown in Figure 5 (right). In this scenario, the adaptability of test-time adaptation methods like Tent, starts to decline, even with more samples. In contrast, our method learns the capability to adapt to each sample from any target domain, overcoming the lack of target samples while being robust under multiple target domains. We also compare our method with Tent on PACS and Office-Home. The results are shown in Table 6. We perform Tent with a baseline model trained on all source domain data, which is then fine-tuned by 128 target samples for 100 iterations. The conclusion is the same as on rotated MNIST. Our method achieves better performance. One reason can be that our model
2 All Tent results are obtained by running the author-provided code accompanying (Wang et al., 2021).
adapts the model to each individual target sample rather than the entire target domain. Thus, different adaptations are conducted on different target samples. Alternatives are sensitive to the chosen batch of samples, which is not necessarily generalizable to the entire domain, especially when the batch is small. Another reason is our choice for meta-learning. By mimicking the adaptation process under the meta-learning framework, the model learns the ability to adapt to a single target sample. Without meta-learning, as shown in Table 4, the performance drops.
Comparisons with state of the art Finally, we compare our method with the state-of-the-art on four datasets. The results on PACS and Office-Home are reported in Table 6. Our method achieves good performance with both the ResNet-18 and ResNet-50 backbones. Compared with others, our method shows larger advantages with ResNet-50 than ResNet-18 on both PACS and Office-Home. This is reasonable since we generate the adaptive model using the features of source and single target samples, which contain more information with ResNet-50, leading to more adapted models. This is also demonstrated in Table 5. Results per domain, as well as the results on rotated-MNIST and Fashion-MNIST are provided in Appendix D.1, with similar results and conclusions.

heading: 5 CONCLUSION
text: We propose learning to generalize by single test samples for domain generalization. Our model is able to adapt to any unseen target sample by leveraging the information of the given sample to generate the model. We formulate the adaptation process as a variational inference problem and train the model under the meta-learning framework. After training on the source data, our method can adapt to each sample from the unseen target domains without any fine-tuning operations or extra target data. Ablation studies and further experiments on several benchmarks show the effectiveness of our method.

heading: ACKNOWLEDGMENT
text: This work is financially supported by the Inception Institute of Artificial Intelligence, the University of Amsterdam and the allowance Top consortia for Knowledge and Innovation (TKIs) from the Netherlands Ministry of Economic Affairs and Climate Policy.
ETHICS STATEMENT
With the capability to handle the domain shift without the accessibility of target data during training and fine-tuning operations, our method has potential positive impacts in the applications that often face unseen domains in practice, e.g., medical imaging and automatic driving. Accordingly, our method would also potentially face some negative social impacts accompanying with applications, e.g., lack of fairness with the model trained by incomplete data and the privacy of patients in medical imaging.
REPRODUCIBILITY STATEMENT
To reproduce our method, the details of the method and loss functions are provided in Section 3. We also provide the derivations and discussions about the evidence lower bound used in our method in Appendix A. The datasets used in this paper and the detailed implementations of the method are reported in Appendix C. An illustration of the architecture is shown in Figure 2. We also provide a link to the source code of our method in the Abstract.

heading: A DERIVATIONS
text: Derivation of variational domain generalization In domain generalization, since the target domain data is inaccessible, the distribution p(θt|T ) is intractable. We leverage the meta-learning setting to mimic the domain generalization process. In particular, we episodically divide the source domains into meta-source domains S ′ and a meta-target domain T ′ and learn the ability to generalize to the target data. Thus, during training, we generate the parameter distribution by the meta-source data S ′ to approximate the distribution p(θt′ |T ′) generated by the meta-target data. Then at inference, the model is able to approximate the distribution p(θt|T ) with the parameters generated by the source data S. To do so, we introduce a variational distribution q(θt′ |S ′) to approximate the true posterior over the model parameter θ. The full derivation of eq. (2) in the main paper is as follows:
log p(yt′ |xt′ , T ′) = log ∫ p(yt′ |xt′ ,θt′)p(θt′ |T ′)dθt′
= log ∫ p(yt′ |xt′ ,θt′)
p(θt′ |T ′) q(θt′ |S ′) q(θt′ |S ′)dθt′
= log [ Eq(θt′ |S′) [ p(yt′ |xt′ ,θt′)
p(θt′ |T ′) q(θt′ |S ′) ]] ≥ Eq(θt′ |S′) [ log [ p(yt′ |xt′ ,θt′)
p(θt′ |T ′) q(θt′ |S ′) ]] = Eq(θt′ |S′)[log p(yt′ |xt′ ,θt′)]− DKL[q(θt′ |S ′)||p(θt′ |T ′)].
(8)
Derivation of single sample generalization Since there is little information available about the target domain or target data in the variational distribution q(θt′ |S ′) in eq. (2), the resultant model is not adaptive to the target domain data. To generate a model adapted to the target sample, we introduce information of the target data, and incorporate the meta-target sample xt′ into the variational distribution q(θt′ |xt′ ,S ′). The detailed derivation is:
log p(yt′ |xt′ , T ′) = log ∫ p(yt′ |xt′ ,θt′)p(θt′ |T ′)dθt′
= log ∫ p(yt′ |xt′ ,θt′)
p(θt′ |T ′) q(θt′ |xt′ ,S ′) q(θt′ |xt′ ,S ′)dθt′
= log [ Eq(θt′ |xt′ ,S′) [ p(yt′ |xt′ ,θt′)
p(θt′ |T ′) q(θt′ |xt′ ,S ′) ]] ≥ Eq(θt′ |xt′ ,S′) [ log [ p(yt′ |xt′ ,θt′)
p(θt′ |T ′) q(θt′ |xt′ ,S ′) ]] = Eq(θt′ |xt′ ,S′))[log p(yt′ |xt′ ,θt′)]− DKL[q(θt′ |xt′ ,S ′)||p(θt′ |T ′)].
(9)
Comparison of the two evidence lower bounds The main difference between eq. (8) and eq. (9) is the variational posterior q(θt′). Compared with eq. (8), our method (eq. 9) introduces the target sample xt′ into the generation of the variational posterior, which therefore achieves more adaptive classifiers for the target sample. Moreover, the tightness of the bound is also related to the variational posterior. The approximation gap between our bound (eq. 3) and the objective log-likelihood corresponds to the KL divergence between the variational posterior q(θt′ |xt′ ,S ′) and the true posterior p(θt′ |xt′ ,yt′ , T ′), as shown in the following equation:
logp(yt′ |xt′ , T ′) = Eq(θt′ )[log p(yt′ |xt′ ,θt′)]− DKL[q(θt′ |xt′ ,S ′)||p(θt′ |T ′)]︸ ︷︷ ︸
ELBO
+ DKL[q(θt′ |xt′ ,S ′)||p(θt′ |xt′ ,yt′ , T ′)]︸ ︷︷ ︸ Approximation Gap . (10)
Compared with eq. (2), whose approximation gap is formulated as DKL[q(θt′ |S ′)||p(θt′ |xt′ ,yt′ , T ′)], our method takes the given sample xt′ as one of the conditions, leading to a tighter lower bound.

heading: B ALGORITHM OF SINGLE SAMPLE GENERALIZATION
text: We describe the the detailed training and test algorithm of our single sample generalization method in Algorithm 1
Algorithm 1 Single-Test-Sample Generalization TRAINING TIME Require: Source domains S = {Ds}Ss=1 each with n sample pairs (xs,ys) Require: Learning rate λ; the number of iterations Niter . Initialize Θ = {ϕ,θa,ψ}. ϕ: ImageNet-pretrained feature extractor; ψ: multiple layer module. for iter in Niter do T ′← Randomly Sample({Ds}Ss=1, t
′); S ′← {Ds}Ss=1 \ T
′; Sample datapoints {(x(k)t′ ,y (k) t′ )} ∼ T ′.
µ w (m) t′ , σ w (m) t′ = fθa(T ′); w (m) t′ ∼ N (µw(m) t′ , σ w (m) t′ ). // Compute meta-prior p(wt′ |T ′) as eq.(6) µws′ , σws′ = fψ(S ′); ws′ ∼ N (µws′ , σws′ ). // Compute p(ws′ |S
′) in eq.(4) µ w (n)
t′ , σ w (n) t′ = fθa(ws′ ,ϕ(x
(k) t′ )); w (n) t′ ∼ N (µw(n) t′ , σ w (n) t′ ). // Compute q(wt′ |x(k)t′ ,S ′) as eq.(5)
L = ∑ k ( M∑ m=1 CrossEntropy ((w(m)t′ · ϕ(x (k) t′ )),y (k) t′ ) + N∑ n=1 CrossEntropy ((w(n)t′ · x (k) t′ ),y (k) t′ )
+ DKL[q(wt′ |x(k)t′ ,S ′)||p(wt′ |T ′)]
) . // Compute loss as eq.(7)
Update parameters: Θ← Θ− λ∇ΘL. // Gradient descent step end for
TEST TIME Require: Source domains S = {Ds}Ss=1; target samples {xt} T t=1 from the target domain T ; trained model with weight Θ = {ϕ,θa,ψ}. µws , σws = fψ(S); ws ∼ N (µws , σws). // Compute p(ws|S) µ w
(n) t
, σ w
(n) t = fθa(ws,ϕ(xt)); w (n) t ∼ N (µw(n)t , σw(n)t ). // Compute q(wt|x (k) t ,S)
return yt = w(n)t · xt.

heading: C DATASETS AND IMPLEMENTATION DETAILS
text: Datasets PACS (Li et al., 2017) is a widely used dataset consisting of 9,991 images of seven classes from four domains, i.e., photo, art-painting, cartoon and sketch. We use the same training and validation split as in (Li et al., 2017) and follow the “leave-one-out” protocol from (Li et al., 2017; 2019; Carlucci et al., 2019). Office-Home (Venkateswara et al., 2017) has 15,500 images of 65 categories. The images are also from four domains, i.e., art, clipart, product and real-world. We use the same experimental protocol as for PACS. Rotated MNIST and Fashion-MNIST are utilized in Piratla et al. (2020). We follow their recommended settings and randomly select a subset of 2,000 images from MNIST and 10,000 images from Fashion-MNIST. The subset of images is rotated by 0◦ through 90◦ in intervals of 15◦, creating seven domains. We use the subsets with rotation angles from 15◦ to 75◦ as the source domains, and images rotated by 0◦ and 90◦ as the target domains. For comparison, we evaluate our method on both in-distribution and out-of-distribution data.
Implementation details We evaluate our method on PACS with both ResNet-18 and ResNet-50 (He et al., 2016) pretrained on ImageNet (Deng et al., 2009) as the backbone. During training, we use Adam optimization and train the model for 10,000 iterations. The learning rate of the backbone is set to 0.00005 for ResNet-18 and 0.00001 for ResNet-50, while the learning rate of the network for generating the classifier is set to 0.0001 consistently. The batch size is 128. To generate the classifier, in each iteration we select 10 samples from each category in each meta-source domain for generating the center features S ′, and the same number of samples from the meta-target domain for T ′. The model with the highest validation accuracy is utilized for evaluation on the target domain. Most of the experimental settings and hyperparameters on Office-Home are the same as on PACS. Since there are more categories in Office-Home, the number of samples per category per domain is set to 5 to fit the memory footprint. The learning rate of the backbone is set to 0.00001 for both ResNet-18
and ResNet-50. For fair comparisons, we evaluate the rotated MNIST and Fashion-MNIST with ResNet-18, following (Piratla et al., 2020). The other experimental settings are also the same as PACS. We train all models on an NVIDIA Tesla V100 GPU. The detailed information about other hyperparameters are summarized in Table 7.

heading: D EXTRA EXPERIMENTS
text: D.1 DETAILED COMPARISONS WITH STATE OF THE ART
PACS In Table 8, we conduct experiments with both ResNet-18 and ResNet-50 backbones. Our method achieves good performance using the ResNet-18 backbone. For each individual domain, we are competitive with the state of the art and slightly better on the “cartoon” domain when using ResNet-18. On the “art-painting” and “cartoon” domains with ResNet-50, our method also achieves good performance. Although it delivers competitive, or sometimes even better, performance on most domains, our method is not as good on the “sketch” domain. One reason might be that the images and features from this domain carry less information than other domains, leading to less adaptive models.
Office-Home As shown in Table 9, our method again achieves good overall performance with both the ResNet-18 and ResNet-50 backbones. With ResNet-18 as the backbone, we outperform other methods by a good margin on the “clipart” domain, while delivering competitive performance on the other domains. When utilizing ResNet-50 as the backbone, our method achieves good performance on all four domains.
Rotated MNIST and Fashion-MNIST. On rotated MNIST and Fashion-MNIST, for fair comparison, we use ResNet-18 as the backbone following (Piratla et al., 2020) and evaluate the method on both in-distribution and out-of-distribution sets. Our method achieves best performance on both datasets as
shown in Table 10, especially for the out-of-distribution setting. Moreover, our method demonstrates less performance drop from in-distribution to out-of-distribution data in comparison to other methods.
D.2 SINGLE SOURCE DOMAIN GENERALIZATION
As our method learns the adaptation ability by mimicking the domain shift during training, it requires at least two source domains to support the meta-learning scheme, which is a limitation of the method.
We conduct an experiment that uses SVHN as the single source domain and MNIST as the target. We generate source domains by two simple methods: random image split and clustering of images (K-means). The results in Table 11 show that our method performs slightly better with larger domain shift simulated in the single source domain. To learn better adaptation ability, auxiliary methods are needed to generate larger domain shifts, for example by domain augmentation techniques like Qiao et al. (2020).
E VISUALIZATIONS
More comparisons with baseline To further demonstrate the effectiveness of our method, we provide more visualizations of the classifier parameters and feature representations from different domains in Figure. 6. The left column shows the visualizations of the “variational amortized classifier” (baseline), and the three columns on the right show our results. The same conclusion can be drawn here as in the main paper. Our method is able to generate adapted classifiers for each test sample, providing better predictions than the fixed classifiers in the baseline method.
Failure cases To gain insights into our method, we also provide some failure cases in Figure 7. Our method gets confused when samples have objects of different categories in the same image, as shown in the first row. Although failing in these cases, our model provides correct predictions for the other objects in the image. In the visualizations, the classifier with the same category of the labeled object is also close to the feature, which demonstrates the effectiveness of our method. In the middle two rows, our method struggles with the samples that contain multiple objects or complex backgrounds.
Table 11: Experiments on single source domain generalization. We use SVHN as the source domain and MNIST as the target. Our method performs slightly better with larger domain shift simulated in the single source domain.
Method SVHN → MNIST Baseline 82.05 ±0.34 Random split by images 82.45 ±0.70 Split by cluster 83.01 ±0.36
Ph ot o
Baseline Single sample generalization
Ar t-p
ai nt in g
Ca rt oo n Sk et ch
Classifiers: Elephant Dog Giraffe Guitar Horse House Person
A dog sample A giraffe sample A horse sample
A dog sample A horse sample A person sample
A giraffe sample A person sample An elephant sample
A house sample An elephant sample A guitar sample
Figure 6: Visualization of single sample generalization on PACS. We plot the test samples and classifiers in the same 2D plane. Different shapes denote different categories. Samples are in red, classifiers in blue. The baseline method produces the same classifier for all samples from each domain, while our method generates the classifier adapted to each target sample. The two methods use the same test samples from each domain. The test sample is shown in the left-top corner of each sub-figure. The visualization shows why our model performs better.
A possible reason is that to take full advantage of the given sample, we utilize multi-level features of the target sample to generate the adapted classifier. However, the complex background and multiple objects in the image bring too much noisy information, leading to less adapted classifiers for the given sample. A solution can be extracting information of the target sample selectively to reduce noise in the features. Moreover, in the “sketch” domain, as shown in the last row, the samples usually have less information than other domains. This makes our method more sensitive to the given information. When there is noisy information, e.g., the chair in the left figure, or object with unobvious features, e.g., the elephant in the right figure, the method fails to generate proper classifiers for the sample.

heading: F MORE COMPARISONS WITH TENT
text: We provide more detailed comparisons with Tent Wang et al. (2021) in Figure 8 to further show the effectiveness of our method. The three rows in Figure 8 show the accuracy of different settings on 0◦, 45◦, and 90◦ target domain, respectively. The mean accuracy is shown in Figure 5 in the main paper. The same as in the main paper, the left column shows results under the “single target domain” setting, while the right column shows the “multiple target domains” setting. In the “single target domain” setting, Tent is utilized to adapt the base model trained on source domains to each target domain independently. By contrast, in the “multiple target domains” setting, the base model is adapted and evaluated on three target domains jointly, without the domain identifiers. The base model is adapted by Tent using different number of samples, e.g., 1, 32, 128, with different optimization steps, e.g., 1, 10, 100. The same conclusion can be drawn here as in the main paper. Compared with Tent, our method adapts the model to each target sample individually by a single and efficient feed-forward pass. Thus, our performance is independent to the settings and optimization steps.
Specifically, as shown in the left column, Tent achieves better performance with both larger numbers of samples and fine-tuning steps, especially on 0◦ and 90◦ domains. However, with fewer target samples being available, and ultimately just one sample, the adaptation of Tent starts to suffer, or even hurt the accuracy with more optimization steps. When adapted to three target domains jointly, as shown in the right column, the adapted performance is similar with the “single target domain”
settings with few target samples. With more target samples, the performance degrades, worse than the left column. Accuracy on 0◦ domain improves slightly, while on 45◦ and 90◦ domain even drops as the adaptation is affected by samples from other domains. In contrast, since our method adapts the model to each target sample individually, we achieve consistently good performance regardless of how many domains the test samples cover. This demonstrates the benefit of our single sample generalization.

Review summaries:
- The paper describes a method for domain generalization that performs test-time adaptation using a single test example at a time (as opposed to a transductive setting used in other works where a whole batch of test examples are used). - The method is cast as a meta learning task. The training data is split into meta-training/test domains to mimic the adaptation process during training.

This paper studied the problem of single test sample generalization. Its goal is to adapt a pre-trained model to unseen target domains without extra fine-tuning. The paper formulated the single test sample generalization problem as a variational inference problem and proposed a meta-learning framework. To bypass extra fine-tuning, a single test sample was used as a conditional to generate model parameters. Experiments and ablation studies on common-used benchmarks demonstrate the effectiveness of the proposed method. 

The paper proposes an approach to ensure a model trained on a set of source domains generalizes well to an unseen target domain based on a single unlabeled target sample. Unlike the standard domain generalization (DG) setting, where there is no scope for adaptation in the target domain, and domain adaptation (DA), where the model has access to source and unlabeled target data during training, the paper falls somewhere in between where no access to target data is assumed during training and a single unlabeled target sample is allowed for quick adaptation. In principle, the proposed setting is somewhat similar to source-free domain adaptation with the distinction that only one sample (and not the entire target dataset) and one quick adaptation pass on the data is allowed. The authors leverage a meta-learning paradigm to mimic domain shift by defining meta-source and meta-target domains within the source domain vocabulary (has also been considered in prior DG settings). The approach adopted by the authors relies on modeling a conditional distribution over the model parameters given a meta-target domain sample and source domain representative features. The authors set this up within a variational inference framework that allows them to explicitly parameterize this conditional distribution to infer model parameters in a target domain. The authors further explore the utility of different instantiations and alternative formulations of their objective and show that their proposed version has the best performance. When compared with prior DG and test-time adaptive methods, the proposed approach leads to competitive or improved performance. Further ablations demonstrate the utility of the proposed approach over other test-time adaptive approaches in terms of number of target samples available for unsupervised adaptation.

The paper deals with the problem of domain generalization, specifically performing inference on a single test example.  The key idea is to use meta-learning to train the model on the source data -- in the training stage, the source data is divided into meta-source and meta-target, and the model is trained to adapt to the meta-target domain.  This mimics domain shift within the source data.  The method is seamlessly transferred to the target domain, where inference is performed on a single test example, without finetuning model parameters or using additional networks.