L EARNING TO G ENERALIZE ACROSS D OMAINS ON S INGLE T EST S AMPLES
A BSTRACT
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 .
I NTRODUCTION
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
[Table]
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.
single target feature together define new classifier prototypes, which are more adapted to the target sample.
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.
R ELATED W ORK
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.
Muandet et al.
and Ghifary et al.
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.
M ETHODOLOGY
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 x t ′ from the meta-target domain T ′ , and we would like to predict its class label y t ′ .
We consider the conditional predictive log-likelihood log p ( y t ′ | x t ′ , T ′ ) .
By incorporating the model θ t ′ into the prediction distribution, we have
[Equation]
(1) where we condition the prediction on T ′ to leverage information from the meta-target data and ( x t ′ , y t ′ )
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:
[Equation]
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 x t ′ 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 x t ′ into the variational distribution q ( θ t ′ | x t ′ , S ′ ) to approximate the true posterior distribution.
Given an unlabeled sample x t ′ from the meta-target domain T ′ , we have a new evidence lower bound (ELBO) as follows:
[Equation]
By conditioning on the given sample x t ′ and the source data S ′ , the adapted variational distribution q ( θ t ′ | x t ′ , 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 x t ′ .
This makes the model better adapted to x t ′
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 | x t , S ) as the adapted model generated by the information of the given target sample x t 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.
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.
Both the meta-prior distribution p ( w t ′ |T ′ ) and the variational posterior distribution q ( w t ′ | x t ′ , S ′ ) of the classifier w t ′ 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 ( w t ′ |T ′ ) by taking the center features of T ′ as input to the amortized inference network θ a .
For the variational distribution q ( w t ′ | x t ′ , 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 x t ′ 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 ( w s ′ |S ′ ) using the center features of the meta-source data S ′ .
After that, we take p ( w s ′ |S ′ ) as a prior and combine it with the meta-target sample to estimate the variational posterior, as follows:
[Equation]
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:
[Equation]
where w ( ℓ ) s ′ ∼ p ( w s ′
|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 ( w t ′ | x t ′ , S ′ ) and the meta-prior distribution p ( w t ′ |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:
[Equation]
This is also implemented as a cross-entropy loss using Monte Carlo sampling from the meta-prior.
Empirical objective function By incorporating eq.
(5) and eq.
(6) into eq.
(3), we obtain the final empirical objective function:
[Equation]
where we use Monte Carlo sampling: w
[Equation]
m
[Equation]
As illustrated in Figure 2, 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 x t from the target domain T , we use this sample to adapt the w s generated by the source data.
The adaptation is achieved by generating the adapted classifiers w t for each target sample with only one forward pass using the learned amortization inference network θ a .
The detailed architecture is provided in Figure 3.
We also provide an algorithm in Appendix B.
E XPERIMENTS
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.
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
[Table] [Table]
[Table]
meta-learning framework (second row)
, the method performs better on all domains.
[Table]
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 ( w t ′ ) 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
[Table]
“variational amortized classifier” (baseline) and our single sample generalization method in Figure 4.
We treat the vectors of different categories in the classifier as prototypes with the same dimension as the test features and map the classifier vectors and features into the same 2D space.
As shown in the left figure (baseline), the classifier parameters of the “invariant amortized classifier” are trained on source domains and fixed for all target samples.
In contrast, as shown in the three figures on the right, our method generates specific, adapted classifiers for each test target sample.
For instance, the test sample ( ) has the shortest distance to the adapted classifier of its own class ( ), producing the correct predictions.
More visualizations, including failure cases, are provided in Appendix E.
Single sample generalization vs. Tent 2 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
[Figure]
A cc u r a c y A cc u r a c y
[Table]
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.
C ONCLUSION
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.
A CKNOWLEDGMENT
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.
E THICS S TATEMENT
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.
R EPRODUCIBILITY S TATEMENT
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 3.
We also provide a link to the source code of our method in the Abstract.
A D ERIVATIONS
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:
[Equation]
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
[Equation]
we introduce information of the target data
[Equation]
and incorporate the meta-target sample x t ′ into the variational distribution q ( θ t ′ | x t ′ , S ′ ) .
The detailed derivation is:
[Equation]
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 x t ′ 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 ′ | x t ′ , S ′ ) and the true posterior p ( θ t ′ | x t ′ , y t ′ , T ′ ) , as shown in the following equation:
[Equation]
Compared with eq.
(2), whose approximation gap is formulated as D KL [ q ( θ t ′
|S ′ )
|| p ( θ t ′ | x t ′ , y t ′ , T ′ )] , our method takes the given sample x t ′ as one of the conditions, leading to a tighter lower bound.
B A LGORITHM OF S INGLE S AMPLE G ENERALIZATION
We describe the the detailed training and test algorithm of our single sample generalization method in
Algorithm
TRAINING TIME
[Table]
[Equation]
[Table]
[Equation]
[Table]
[Equation]
[Table]
[Equation]
[Table]
Sample
[Table]
[Equation]
[Table]
[Equation]
[Table]
[Equation]
[Table]
[Equation]
+ D KL [ q ( w t ′ | x ( k ) t ′ , S ′ )
|| p ( w t ′ |T ′ )] (cid:17) . //
Compute loss as eq.(7)
Update parameters: Θ ← Θ − λ ∇ Θ L . //
Gradient descent step end for
TEST TIME
Require : Source domains S
=
{ D s } Ss =1 ; target samples { x t } Tt =1 from the target domain T ; trained model with weight Θ = { ϕ , θ a , ψ } .
µ w s , σ w s = f ψ ( S ) ; w s ∼ N ( µ w s , σ w s ) . //
Compute p ( w s |S ) µ w ( n ) t , σ w ( n ) t = f θ a ( w s , ϕ ( x t )) ; w ( n ) t ∼ N ( µ w ( n ) t , σ w ( n ) t ) . //
Compute q ( w t | x ( k ) t , S ) return y t = w ( n ) t · x t .
C D ATASETS AND I MPLEMENTATION D ETAILS
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 ofcategories.
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.
We follow their recommended settings and randomly select a subset of 2, 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- (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-
[Table] [Table]
and ResNet-50.
For fair comparisons
[Table]
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.
D E XTRA E XPERIMENTS D. D ETAILED 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
[Table]
In-distribution performance is evaluated on the test sets of MNIST and Fashion-MNIST with rotation angles of 15 ◦ , 30 ◦ , 45 ◦ , 60 ◦ and 75 ◦ , while the out-of-distribution performance is evaluated on test sets with angles of 0 ◦ and 90 ◦ .
We achieve the best performance on both the in-distribution and out-of-distribution test sets.
[Table]
Dou et al
[Table] [Table]
Piratla et al
[Table] [Table]
This paper
[Table]
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. S INGLE 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.
E V ISUALIZATIONS
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.
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]
A r t - p a i n t i n g
C a r t oo n
S k e t c h
[Table]
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.
[Figure]
F M ORE COMPARISONS WITH T ENT
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,
[Figure]
Multiple target domains
Number of samples Number of samples
A cc u r a c y A cc u r a c y
Number of samples
Number of samples Number of samples
Number of samples
A cc u r a c y A cc u r a c y
A cc u r a c y A cc u r a c y
[Figure]