In Search of Lost Domain Generalization
Ishaan Gulrajani, David Lopez-Paz
Introduction
Machine learning systems often fail to generalize out-of-distribution, crashing in spectacular ways when tested outside the domain of training examples (Torralba and Efros 2011). The overreliance of learning systems on the training distribution manifests widely. For instance, self-driving car systems struggle to perform under conditions different to those of training, including variations in light (Dai and Van Gool 2018), weather (Volk et al. 2019), and object poses (Alcorn et al. 2019). As another example, systems trained on medical data collected in one hospital do not generalize to other health centers (Castro et al. 2019; AlBadawy et al. 2018; Perone et al. 2019; Heaven 2020). Arjovsky et al. 2019 suggest that failing to generalize out-of-distribution is failing to capture the causal factors of variation in data, clinging instead to easier-to-fit spurious correlations, which are prone to change from training to testing domains. Examples of spurious correlations commonly absorbed by learning machines include racial biases (Stock and Cisse 2018), texture statistics (Geirhos et al. 2018), and object backgrounds (Beery et al. 2018). Alas, the capricious behaviour of machine learning systems out-of-distribution is a roadblock to their deployment in critical applications.
Aware of this problem, the research community has spent significant effort during the last decade to develop algorithms able to generalize out-of-distribution. In particular, the literature in domain generalization assumes access to multiple datasets during training, each of them containing examples about the same task, but collected under a different domain or environment (Blanchard et al. 2011; Muandet et al. 2013). The goal of domain generalization algorithms is to incorporate the invariances across these training datasets into a classifier, in hopes that such invariances also hold in novel test domains. Different domain generalization solutions assume different types of invariances and propose algorithms to estimate them from data.
Despite the enormous importance of domain generalization, the literature is scattered: a plethora of different algorithms appear yearly, and these are evaluated under different datasets and model selection criteria. Borrowing from the success of standard computer vision benchmarks such as ImageNet (Russakovsky et al. 2015), the purpose of this work is to perform a standardized, rigorous comparison of domain generalization algorithms. In particular, we ask: how useful are domain generalization algorithms in realistic settings? Towards answering this question, we first study model selection criteria for domain generalization methods, resulting in the recommendation:
A domain generalization algorithm should be responsible for specifying a model selection method.
We then carefully implement nine domain generalization algorithms on seven multi-domain datasets and three model selection criteria, leading us to the conclusion reflected in Tables 1 and 4:
When equipped with modern neural network architectures and data augmentation techniques, empirical risk minimization achieves state-of-the-art performance in domain generalization.
As a result of our research, we release DomainBed, a framework to streamline rigorous and reproducible experimentation in domain generalization. Using DomainBed, adding a new algorithm or dataset is a matter of a few lines of code; a single command runs all the experiments, performs all the model selections, and auto-generates all the tables included in this work. Moreover, our motivation is to keep DomainBed alive, welcoming pull requests from our fellow colleagues to update the available algorithms, datasets, model selection criteria, and result tables.
Section 2 kicks off our exposition with a review of the domain generalization setup. Section 3 discusses the difficulties of model selection in domain generalization and makes recommendations for a path forward. Section 4 introduces DomainBed, describing the algorithms and datasets contained in the initial release. Section 5 discusses the experimental results of running the entire DomainBed suite; these illustrate the strength of ERM and the importance of model selection criteria. Finally, Section 6 offers our view on future research directions in domain generalization. Our Appendices review one hundred articles spanning a decade of research in this topic, collecting the experimental performance of over thirty published algorithms.
The problem of domain generalization
Domain generalization differs from unsupervised domain adaptation. In the latter, it is assumed that unlabeled data from the test domain is available during training (Pan and Yang 2009; Patel et al. 2015; Wilson and Cook 2018). Table 2 compares different machine learning setups to highlight the nature of domain generalization problems. The causality literature refers to domain generalization as learning from multiple environments (Peters et al. 2016; Arjovsky et al. 2019). Although challenging, domain generalization is the best approximation to real prediction problems, where unforeseen distributional discrepancies between training and testing data are surely expected.
Model selection as part of the learning problem
Here we discuss issues surrounding model selection (choosing hyperparameters, training checkpoints, architecture variants) in domain generalization and make specific recommendations for a path forward. Because we lack access to a validation set identically distributed to the test data, model selection in domain generalization is not as straightforward as in supervised learning. Some works adopt heuristic strategies whose behavior is not well-studied, while others simply omit a description of how to choose hyperparameters. This leaves open the possibility that hyperparameters were chosen using the test data, which is not methodologically sound. Differences in results arising from inconsistent tuning practices may be misattributed to the algorithms under study, complicating fair assessments.
We believe that much of the confusion surrounding model selection in domain generalization arises from treating it as a question of experimental design. In reality, selecting hyperparameters is a learning problem at least as hard as fitting the model (inasmuch as we may interpret any model parameter as a hyperparameter). Like all learning problems, model selection requires assumptions about how the test data relates to the training data. Different domain generalization algorithms make different assumptions, and it is not clear a priori what assumptions are correct, or how these assumptions influence the model selection criterion. Indeed, choosing reasonable assumptions is at the heart of domain generalization research. Therefore, a domain generalization algorithm without a strategy to choose its hyperparameters remains incomplete.
A domain generalization algorithm should be responsible for specifying a model selection method.
While algorithms without well-justified model selection methods are incomplete, they may be useful as stepping-stones in a research agenda. In this case, instead of using an ad-hoc model selection method, we can evaluate incomplete algorithms by considering an oracle model selection method, where we select hyperparameters on the test domain. Of course, it is important that we avoid invalid comparisons between oracle results and baselines tuned without an oracle method. Also, unless we restrict access to the test domain data somehow, we risk obtaining meaningless results. For instance, we could just train on such test domain data using supervised learning.
Researchers should disclaim any oracle-selection results as such and specify policies to limit access to the test domain.
Having made broad recommendations, we review and justify three methods for model selection in domain generalization, often used but rarely discerned.
We split each training domain into training and validation subsets. Then, we pool the validation subsets of each training domain to create an overall validation set. Finally, we choose the model maximizing the accuracy on the overall validation set.
This strategy assumes that the training and test examples follow similar distributions. For example, Ben-David et al. 2010 bound the test domain error of a classifier by the training domain error, plus a divergence measure between the training and test domains.
Leave-one-domain-out cross-validation
This strategy assumes that training and test domains are drawn from a meta-distribution over domains, and that our goal is to maximize the expected performance under this meta-distribution.
Test-domain validation set (oracle)
We choose the model maximizing the accuracy on a validation set that follows the distribution of the test domain. Following our earlier recommendation to limit test domain access, we allow queries per algorithm (one query per choice of hyperparameters in our random search). This means that we do not allow early stopping based on the validation set. Instead, we train all models for the same fixed number of steps and consider only the final checkpoint. Recall that we do not consider this a valid benchmarking methodology, since it requires access to the test domain. Oracle-selection results can be either optimistic, because we access the test distribution, or pessimistic, because the query limit reduces the number of considered hyperparameter combinations.
As an alternative to limiting the number of queries, we could borrow tools from differential privacy, previously applied to enable multiple re-uses of validation sets in standard supervised learning (Dwork et al. 2015). In a nutshell, differential privacy tools add Laplace noise to the accuracy statistic of the algorithm before reporting it to the practitioner.
2 Considerations from the literature
Some references in prior work discuss additional strategies to choose hyperparemeters in domain generalization problems. For instance, Krueger et al. 2020 suggest choosing hyperparameters to maximize the performance across all domains of an external dataset. The validity of this strategy depends on the relatedness between datasets. Albuquerque et al. 2019 suggest performing model selection based on the loss function (which often incorporates an algorithm-specific regularizer), and D’Innocente and Caputo 2018 derive an strategy specific to their algorithm.
DomainBed: A PyTorch testbed for domain generalization
At the heart of our large scale experimentation is DomainBed, a PyTorch (Paszke et al. 2019) testbed to streamline reproducible and rigorous research in domain generalization:
https://github.com/facebookresearch/DomainBed (coming soon)
The initial release comprises nine algorithms, seven datasets, and three model selection methods (described in Section 3), as well as the infrastructure to run all the experiments and generate all the LaTeX tables below with a single command. DomainBed is a living project: we expect to update the above repository with new results, algorithms, and datasets. Contributions via pull requests from fellow researchers are welcome. Adding a new algorithm or dataset to DomainBed is a matter of a few lines of code (see Appendix E for an example).
DomainBed includes downloaders and loaders for seven multi-domain image classification tasks: Colored MNIST (Arjovsky et al. 2019), Rotated MNIST (Ghifary et al. 2015), PACS (Li et al. 2017), VLCS (Fang et al. 2013), Office-Home (Venkateswara et al. 2017), Terra Incognita (Beery et al. 2018), and DomainNet (Peng et al. 2019). We list and show example images from each dataset in Table 3, and provide their full details in Appendix C.
The datasets differ in many ways but two are particularly important. The first difference is between synthetic and real datasets. In Rotated MNIST and Colored MNIST, domains are synthetically constructed such that we know what features will generalize a priori, so using too much prior knowledge (e.g. by augmenting with rotations) is off-limits, whereas the other datasets contain domains arising from natural processes, making it sensible to use prior knowledge. The second difference is about what changes across domains. On one hand, in datasets other than Colored MNIST, the domain changes the distribution of images, but likely bears no information about the true image-to-label mapping. On the other hand, in Colored MNIST, the domain influences the true image-to-label mapping, biasing algorithms that try to estimate this function directly.
2 Algorithms
The initial release of DomainBed includes implementations of nine baseline algorithms:
Empirical Risk Minimization (ERM, Vapnik 1998) minimizes the sum of errors across domains and examples.
Group Distributionally Robust Optimization (DRO, Sagawa et al. 2019) performs ERM while increasing the importance of domains with larger errors.
Inter-domain Mixup (Mixup, Xu et al. 2019; Yan et al. 2020; Wang et al. 2020) performs ERM on linear interpolations of examples from random pairs of domains and their labels.
Meta-Learning for Domain Generalization (MLDG, Li et al. 2018a) leverages MAML (Finn et al. 2017) to meta-learn how to generalize across domains.
Different variants of the popular algorithm of Ganin et al. 2016 to learn features with distributions matching across domains:
Domain-Adversarial Neural Networks (DANN, Ganin et al. 2016) employ an adversarial network to match feature distributions.
Class-conditional DANN (C-DANN, Li et al. 2018d) is a variant of DANN matching the conditional distributions across domains, for all labels .
CORAL (Sun and Saenko 2016) matches the mean and covariance of feature distributions.
MMD (Li et al. 2018b) matches the MMD (Gretton et al. 2012) of feature distributions.
Invariant Risk Minimization (IRM (Arjovsky et al. 2019)) learns a feature representation such that the optimal linear classifier on top of that representation matches across domains.
Appendix D describes the network architectures and hyperparameter search spaces for all algorithms.
3 Implementation choices for realistic evaluation
Our goal is a realistic evaluation of domain generalization algorithms. To that end, we make several implementation choices which depart from prior work, explained below.
Most prior work on VLCS and PACS borrows features from or finetune ResNet-18 models (He et al. 2016). Since larger ResNets are known to generalize better, we opt to finetune ResNet-50 models for all datasets except Rotated MNIST and Colored MNIST, where we use a smaller CNN architecture (see Appendix D).
Data augmentation
Data augmentation is a standard ingredient to train image classification models. In domain generalization, data augmentation can play an especially important role when augmentations can approximate some of the variations between domains. Therefore, for all non-MNIST datasets, we train using the following data augmentations: crops of random size and aspect ratio, resizing to pixels, random horizontal flips, random color jitter, grayscaling the image with 10% probability, and normalization using the ImageNet channel means and standard deviations. For MNIST datasets, we use no data augmentation.
Using all available data
In Rotated MNIST, whereas the usual version of the dataset constructs all domains from the same set of 1000 digits, we divide all the MNIST digits evenly among domains. We deviate from standard practice for two reasons: we believe that using the same digits across training and test domains amounts to leaking test data, and we believe that artificially restricting the available training domain data complicates the task in an unrealistic way.
Experiments
We run experiments for all algorithms (Section 4.2), datasets (Section 4.1), and model selection criteria (Section 3) shipped in DomainBed. We consider all configurations of a dataset where we hide one domain for testing and train on the remaining ones.
For each algorithm and test environment, we conduct a random search (Bergstra and Bengio 2012) of 20 trials over the hyperparameter distribution (see Appendix D). We use each model selection method from Section 3 to select amongst the 20 models from the random search. We split the data from each domain into 80% and 20% splits. We use the larger splits for training and final evaluation, and the smaller splits to select hyperparameters.
Standard error bars
While some domain generalization literature reports error bars across seeds, randomness arising from model selection is often ignored. While this is acceptable if the goal is a best-versus-best comparison, it prohibits nuanced analyses. For instance, does method A outperform method B only because random search for A got lucky? We therefore repeat our entire study three times making every random choice anew: hyperparameters, weight initializations, and dataset splits. Every number we report is a mean over these repetitions, together with their estimated standard error.
This experimental protocol amounts to training a total of 45,900 neural networks.
1 Results
Table 4 summarizes the results of our experiments. For each dataset and model, we average the best results (according to each model selection criterion) across test domains. We then report the average of this number across three independent runs of the entire sweep, and its corresponding standard error. For results per dataset and domain, we refer the reader to Appendix B. We draw three main conclusions from our results:
Table 1 summarizes this result when model selection is performed using a training domain validation set. What is responsible for this strong performance? We suspect four factors: a bigger network architecture (ResNet-50), strong data augmentations, careful hyperparameter tuning and, in Rotated MNIST, using the full training data to construct our domains (instead of using a 1000-image subset). While we are not first to use any these techniques alone, we may be first to combine all of them. Interestingly, these results suggest standard techniques to improve in-distribution generalization are very effective at improving out-of-distribution generalization. Our result does not refute prior work: it is possible that with similar techniques, some competing methods may improve upon ERM. Rather, our results highlight the importance of comparing domain generalization algorithms to strong and realistic baselines. Incorporating novel algorithms into DomainBed is an easy way to do so. For an extensive review of results published in the literature about more than thirty algorithms, we refer the reader to Appendix A.5.
When all conditions are equal, no algorithm outperforms ERM by a significant margin
We observe this result in Table 4, obtained from running from scratch every combination of dataset, algorithm, and model selection criteria included in DomainBed. Given any model selection criterion, no method improves upon the average performance of ERM in more than one point. We do not claim that any of these algorithms cannot possibly improve upon ERM, but getting substantial domain generalization improvements over ERM on these datasets proved challenging.
Model selection methods matter
We observe that model selection with a training domain validation set outperforms leave-one-domain-out cross-validation across multiple datasets and algorithms. This does not mean that using a training domain validation set is the right way to tune hyperparameters. After all, it did not enable any algorithm to significantly outperform the ERM baseline. Moreover, the stronger performance of oracle-selection (+2%) suggests possible headroom for improvement.
Outlook
We have conducted an extensive empirical evaluation of domain generalization algorithms. Our results led to two major conclusions. First, empirical risk minimization achieves state-of-the-art performance when compared to eight popular domain generalization alternatives, also improving upon all the numbers previously reported in the literature. Second, model selection has a significant effect on domain generalization, and it should be regarded as an integral part of any proposed method. We conclude with a series of mini-discussions that answer some questions, but raise even more.
While conducting our experiments, we became aware of the power of data augmentation. Zhang et al. 2019 show that strong data augmentation can improve out-of-distribution generalization while not impacting in-distribution generalization. We think of data augmentation as feature removal: the more we augment a training example, the more invariant we make our predictor with respect to the applied transformations. If the practitioner is lucky and performs the data augmentations that cancel the spurious correlations varying from domain to domain, then out-of-distribution performance should improve. Given a particular domain generalization problem, what sort of data augmentation pipelines should we implement?
Is this as good as it gets?
We question whether domain generalization is expected in the considered datasets. Why do we assume a neural network should be able to classify cartoons, given only photorealistic training data? In the case of Rotated MNIST, do truly rotation-invariant features discriminative of the digit class exist? Are those features expressible by a neural network? Even in the presence of correct model selection, is the out-of-distribution performance of modern ERM implementations as good as it gets? Or is it simply as bad as every other alternative? How can we establish upper-bounds on what performance is achievable out-of-distribution via domain generalization techniques?
Are these the right datasets?
Some of the datasets considered in the domain-generalization literature do not reflect realistic situations. In reality, if one wanted to classify cartoons, the easiest option would be to collect a small labeled dataset of cartoons. Should we consider more realistic, impactful tasks for better research in domain generalization? Attractive alternatives include medical imaging in different hospitals and self-driving cars in different cities.
It is all about (untestable) assumptions
Every time we use ERM, we assume that training and testing examples are drawn from the same distribution. Also every time, this is an untestable assumption. The same applies for domain generalization: each algorithm assumes a different (untestable) type of invariance across domains. Therefore, the performance of a domain generalization algorithm depends on the problem at hand, and only time can tell if we have made a good choice. This is akin to the generalization of a scientific theory such as Newton’s gravitation, which cannot be proved but has so far resisted falsification. We believe there is promise in algorithms with self-adaptation capabilities during test time.
Benchmarking and the rules of the game
While limiting the use of modern techniques cheapens experiments, it also distorts them from more realistic scenarios, which is the focus of our study. Our view is that benchmark designers should balance these factors to promote a set of rules of the game that are not only well-defined, but realistic and well-motivated. Synthetic datasets are helpful tools, but we must not lose sight of the goal, which is artificial intelligence able to generalize in the real world. In words of Marcel Proust:
Perhaps the immobility of the things that surround us is forced upon them by our conviction that they are themselves, and not anything else, and by the immobility of our conceptions of them.
Broader impact
Current machine learning systems fail capriciously when facing novel distributions of examples. This unreliability hinders the application of machine learning systems in critical applications such as transportation, security, and healthcare. Here we strive to find robust machine learning models that discard spurious correlations, as we expect invariant patterns to generalize out-of-distribution. This should lead to fairer, safer, and more reliable machine learning systems. But with great powers comes great responsibility: researchers in domain generalization must adhere to the strictest standards of model selection and evaluation. We hope that our results and the release of DomainBed are some small steps in this direction, and we look forward to collaborate with fellow researchers to streamline reproducible and rigorous research towards true generalization power.
References
Appendix A A decade of literature on domain generalization
In this section, we provide an exhaustive literature review on a decade of domain generalization research. The following classifies domain generalization algorithms according into four strategies to learn invariant predictors: learning invariant features, sharing parameters, meta-learning, or performing data augmentation.
Muandet et al. 2013 use kernel methods to find a feature transformation that (i) minimizes the distance between transformed feature distributions across domains, and (ii) does not destroy any of the information between the original features and the targets. In their pioneering work, Ganin et al. 2016 propose Domain Adversarial Neural Networks (DANN), a domain adaptation technique which uses generative adversarial networks (GANs, Goodfellow et al. 2014), to learn a feature representation that matches across training domains. Akuzawa et al. 2019 extend DANN by considering cases where there exists an statistical dependence between the domain and the class label variables. Albuquerque et al. 2019 extend DANN by considering one-versus-all adversaries that try to predict to which training domain does each of the examples belong to. Li et al. 2018b employ GANs and the maximum mean discrepancy criteria [Gretton et al. 2012] to align feature distributions across domains. Matsuura and Harada 2019 leverages clustering techniques to learn domain-invariant features even when the separation between training domains is not given. Li et al. 2018c, Li et al. 2018d learns a feature transformation such that the conditional distributions match for all training domains and label values . Shankar et al. 2018 use a domain classifier to construct adversarial examples for a label classifier, and use a label classifier to construct adversarial examples for the domain classifier. This results in a label classifier with better domain generalization. Li et al. 2019a train a robust feature extractor and classifier. The robustness comes from (i) asking the feature extractor to produce features such that a classifier trained on domain can classify instances for domain , and (ii) asking the classifier to predict labels on domain using features produced by a feature extractor trained on domain . Li et al. 2020 adopt a lifelong learning strategy to attack the problem of domain generalization. Motiian et al. 2017 learn a feature representation such that (i) examples from different domains but the same class are close, (ii) examples from different domains and classes are far, and (iii) training examples can be correctly classified. Ilse et al. 2019 train a variational autoencoder [Kingma and Welling 2014] where the bottleneck representation factorizes knowledge about domain, class label, and residual variations in the input space. Fang et al. 2013 learn a structural SVM metric such that the neighborhood of each example contains examples from the same category and all training domains. The algorithms of Sun and Saenko 2016, Sun et al. 2016, Rahman et al. 2019a match the feature covariance (second order statistics) across training domains at some level of representation. The algorithms of Ghifary et al. 2016, Hu et al. 2019 use kernel-based multivariate component analysis to minimize the mismatch between training domains while maximizing class separability.
Although popular, learning domain-invariant features has received some criticism [Zhao et al. 2019, Johansson et al. 2019]. Some alternatives exist, as we review next. Peters et al. 2016, Rojas-Carulla et al. 2018 considered that one should search for features that lead to the same optimal classifier across training domains. In their pioneering work, Peters et al. 2016 linked this type of invariance to the causal structure of data, and provided a basic algorithm to learn invariant linear models, based on feature selection. Arjovsky et al. 2019 extend the previous to general gradient-based models, including neural networks, in their Invariant Risk Minimization (IRM) principle. Teney et al. 2020 build on IRM to learn a feature transformation that minimizes the relative variance of classifier weights across training datasets. The authors apply their method to reduce the learning of spurious correlations in Visual Question Answering (VQA) tasks. Ahuja et al. 2020 analyze IRM under a game-theoretic perspective to develop an alternative algorithm. Krueger et al. 2020 propose an approximation to the IRM problem consisting in reducing the variance of error averages across domains. Bouvier et al. 2019 attack the same problem as IRM by re-weighting data samples.
A.2 Sharing parameters
Blanchard et al. 2011 build classifiers , where is a kernel mean embedding [Muandet et al. 2017] that summarizes the dataset associated to the example . Since the distributional identity of test instances is unknown, these embeddings are estimated using single test examples at test time. See Blanchard et al. 2017, Deshmukh et al. 2019 for theoretical results on this family of algorithms. Khosla et al. 2012 learn one max-margin linear classifier per domain , from which they distill their final, invariant predictor . Ghifary et al. 2015 use a multitask autoencoder to learn invariances across domains. To achieve this, the authors assume that each training dataset contains the same examples; for instance, photographs about the same objects under different views. Mancini et al. 2018b train a deep neural network with one set of dedicated batch-normalization layers [Ioffe and Szegedy 2015] per training dataset. Then, a softmax domain classifier predicts how to linearly-combine the batch-normalization layers at test time. Similarly, Mancini et al. 2018a learn a softmax domain classifier used to linearly-combine domain-specific predictors at test time. D’Innocente and Caputo 2018 explore more sophisticated ways of aggregating domain-specific predictors. Li et al. 2017 extends Khosla et al. 2012 to deep neural networks by extending each of their parameter tensors with one additional dimension, indexed by the training domains, and set to a neutral value to predict domain-agnostic test examples. Ding and Fu 2017 implement parameter-tying and low-rank reconstruction losses to learn a predictor that relies on common knowledge across training domains. Hu et al. 2016, Sagawa et al. 2019 weight the importance of the minibatches of the training distributions proportional to their error.
A.3 Meta-learning
Li et al. 2018a employ Model-Agnostic Meta-Learning, or MAML [Finn et al. 2017], to build a predictor that learns how to adapt fast between training domains. Dou et al. 2019 use a similar MAML strategy, together with two regularizers that encourage features from different domains to respect inter-class relationships, and be compactly clustered by class labels. Li et al. 2019b extend the MAML meta-learning strategy to instances of domain generalization where the categories vary from domain to domain. Balaji et al. 2018 use MAML to meta-learn a regularizer encouraging the model trained on one domain to perform well on another domain.
A.4 Augmenting data
Data augmentation is an effective strategy to address domain generalization [Zhang et al. 2019]. Unfortunately, how to design efficient data augmentation routines depends on the type of data at hand, and demands a significant amount of work from human experts. Xu et al. 2019, Yan et al. 2020, Wang et al. 2020 use mixup [Zhang et al. 2018] to blend examples from the different training distributions. Carlucci et al. 2019a constructs an auxiliary classification task aimed at solving jigsaw puzzles of image patches. The authors show that this self-supervised learning task learns features that improve domain generalization. Albuquerque et al. 2020 introduce the self-supervised task of predicting responses to Gabor filter banks, in order to learn more transferrable features. Wang et al. 2019 remove textural information from images to improve domain generalization. Volpi et al. 2018 show that training with adversarial data augmentation on a single domain is sufficient to improve domain generalization. Nam et al. 2019, Asadi et al. 2019 promote representations of data that ignore texture and focus on shape. Rahman et al. 2019b, Zhou et al. 2020, Carlucci et al. 2019a are three alternatives that use GANs to augment the data available during training time.
A.5 Previous state-of-the-art numbers
Table 5 compiles the best out-of-distribution test accuracies reported across a decade of domain generalization research.
Appendix B Domain generalization accuracies per algorithm, dataset, and domain
B.2 Rotated MNIST
B.3 VLCS
B.4 PACS
B.5 Office-Home
B.6 TerraIncognita
B.7 DomainNet
Appendix C Dataset details
DomainBed includes downloaders and loaders for seven multi-domain image classification tasks:
Colored MNIST [Arjovsky et al. 2019] is a variant of the MNIST handwritten digit classification dataset [LeCun 1998]. Domain contains a disjoint set of digits colored either red or blue. The label is a noisy function of the digit and color, such that color bears correlation with the label and the digit bears correlation 0.75 with the label. This dataset contains examples of dimension and classes.
Rotated MNIST [Ghifary et al. 2015] is a variant of MNIST where domain 0, 15, 30, 45, 60, 75 contains digits rotated by degrees. Our dataset contains examples of dimension and classes.
PACS [Li et al. 2017] comprises four domains art, cartoons, photos, sketches . This dataset contains examples of dimension and classes.
VLCS [Fang et al. 2013] comprises photographic domains Caltech101, LabelMe, SUN09, VOC2007 . This dataset contains examples of dimension and classes.
Office-Home [Venkateswara et al. 2017] includes domains art, clipart, product, real . This dataset contains examples of dimension and classes.
Terra Incognita [Beery et al. 2018] contains photographs of wild animals taken by camera traps at locations . Our version of this dataset contains examples of dimension and classes.
DomainNet [Peng et al. 2019] has six domains clipart, infograph, painting, quickdraw, real, sketch . This dataset contains examples of size and classes.
For all datasets, we first pool the raw training, validation, and testing images together. For each random seed, we then instantiate random training, validation, and testing splits.
Appendix D Model architectures, hyperparameter spaces, and other training details
In this section we describe the model architectures and hyperparameter search spaces used in our experiments.
We list the neural network architecture used for each dataset in Table 6 and specify the details of our MNIST network in 7.
For the architecture “Resnet-50”, we replace the final (softmax) layer of a ResNet50 pretrained on ImageNet and fine-tune. Observing that batch normalization interferes with domain generalization algorithms (as different minibatches follow different distributions), we freeze all batch normalization layers before fine-tuning. We insert a dropout layer before the final linear layer.
D.2 Hyperparameters
We list all hyperparameters, their default values, and the search distribution for each hyperparameter in our random hyperparameter sweeps, in Table 8.
D.3 Other training details
We optimize all models using Adam [Kingma and Ba 2015].
Appendix E Adding new datasets and algorithms to our framework
In their basic form, algorithms are classes that implement a method .update(minibatches) and a method .predict(x). The update method receives a list of minibatches, one minibatch per training domain, and each minibatch containing a number of input-output pairs. For example, to implement group DRO [Sagawa et al. 2019, Algorithm 1], we simply write the following in algorithms.py:
⬇ class DRO(ERM): def __init__(self, input_shape, num_classes, num_domains, hparams): super().__init__(input_shape, num_classes, num_domains, hparams) self.register_buffer("q", torch.Tensor()) def update(self, minibatches): device = "cuda" if minibatches.is_cuda else "cpu" if not len(self.q): self.q = torch.ones(len(minibatches)).to(device) losses = torch.zeros(len(minibatches)).to(device) for m in range(len(minibatches)): x, y = minibatches[m] losses[m] = F.cross_entropy(self.predict(x), y) self.q[m] *= (self.hparams["dro_eta"] * losses[m].data).exp() self.q /= self.q.sum() loss = torch.dot(losses, self.q) / len(minibatches) self.optimizer.zero_grad() loss.backward() self.optimizer.step() return {’loss’: loss.item()}
By inheriting from ERM, this new class has access to a default classifier .network, optimizer .optimizer, and prediction method .predict(x). Finally, we should tell DomainBed about the hyperparameters of this new algorithm. To do so, add the following line to the function _hparams from hparams_registry.py:
⬇ hparams[’dro_eta’] = (1e-2, 10**random_state.uniform(-3, -1))
To add a new image classification dataset to DomainBed, arrange your image files as /path/MyDataset/domain/class/image.jpg. Then, append to datasets.py:
⬇ class MyDataset(MultipleEnvironmentImageFolder): N_STEPS = 2500 CHECKPOINT_FREQ = 300 def __init__(self, root, test_envs=None): self.dir = os.path.join(root, "MyDataset/") super(MyDataset, self).__init__(self.dir)
In the previous, N_STEPS determines the number of gradient updates an algorithm should perform to learn this dataset. The variable CHECKPOINT_FREQ determines the number of gradient steps an algorithm should wait before reporting its performance in all domains.
We are now ready to launch an experiment with our new algorithm and dataset:
⬇ python train.py --model DRO --dataset MyDataset --data_dir /path --test\_envs 1 \ --output_dir /path/to/logs_files --hparams ’{"dro_eta":␣0.2}’
Finally, we can run a fully automated sweep on all datasets, algorithms, test domains, and model selection criteria by simply invoking python sweep.py. After adapting the file sweep.py to the computing infrastructure at hand, this single command automatically generates all the result tables that we report in this manuscript.
By extending the method .update(minibatches, unlabeled) to accept a minibatch of unlabeled examples from the test domain, we can immediately use DomainBed as a framework to perform experimentation on unsupervised domain adaptation algorithms.