SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Zhuofan Ying $^{1,2}$  Peter Hase $^{1}$  Mohit Bansal $^{1}$
$^{1}$ UNC Chapel Hill  $^{2}$ Columbia University
{zfying, peter, mbansal}@cs.unc.edu
Biological vision systems make adaptive use of context to recognize objects in new settings with novel contexts as well as occluded or blurry objects in familiar settings [3, 35]. In this paper, we investigate how vision models adaptively use context for out-of-distribution (OOD) generalization and leverage our analysis results to improve model OOD generalization. First, we formulate two distinct OOD settings where the contexts are either irrelevant (BACKGROUND-INVARIANCE) or beneficial (OBJECT-DISAMBIGUATION), reflecting the diverse contextual challenges faced in biological vision. We then analyze model performance in these two different OOD settings and demonstrate that models that excel in one setting tend to struggle in the other. Notably, prior works on learning causal features improve on one setting but hurt in the other. This underscores the importance of generalizing across both OOD settings, as this ability is crucial for both human cognition and robust AI systems. Next, to better understand the model properties contributing to OOD generalization, we use representational geometry analysis and our own probing methods to examine a population of models, and we discover that those with more factorized representations and appropriate feature weighting are more successful in handling BACKGROUND-INVARIANCE and OBJECT-DISAMBIGUATION tests. We further validate these findings through causal intervention, manipulating representation factorization and feature weighting to demonstrate their causal effect on performance. These results show that interpretability-based model metrics can predict OOD generalization and are causally connected to model generalization. Motivated by our analysis results, we propose new data augmentation methods aimed at enhancing model generalization. The proposed methods outperform strong baselines, yielding improvements in both in-distribution and OOD tests. We conclude that, in order to replicate the generalization abilities of biological vision, computer vision models must have factorized object vs. background representations and appropriately weight both kinds of features.<sup>1</sup>
Humans can recognize objects even when the objects appear in novel settings like new viewpoints, lighting, and background conditions [6, 17]. This kind of generalization has been the subject of many studies in computer vision, where past methods have optimized models to avoid relying on "spurious correlations" (such as the object's background) in order to encourage generalization [46, 2, 38]. However, human vision can also generalize to settings where objects are occluded, blurred, or otherwise distorted, as illustrated by the blurred keyboard in Fig. 1. In fact, research in psychology shows that humans accomplish this task largely by relying on background information, which helps disambiguate the identity of the foreground object [24, 53, 39, 23].
1Our code is available at: https://github.com/zfying/AdaptiveContext
(1) Model Properties
(2) Adaptive Use of Context
Figure 1: Feature factorization and appropriate feature weighting support Adaptive Contextual Perception. Humans can flexibly generalize to object settings where the context is irrelevant (2b) or helpful (2c). To achieve similar generalization, models should have factorized representations (1a) with strong weights on object features and small but non-zero weights on background features (1b). (2a) In-distribution: In natural environments, foreground and background information correlates strongly. (2b) OOD: BACKGROUND-INVARIANCE. When the background is inconsistent with the foreground object, humans can ignore the background [46]. (2c) OOD: OBJECT-DISAMBIGUATION. When the object is hard to recognize on its own (like the highly blurred keyboard), humans can rely on the background (office setting) to infer what the object is [31].
How can humans both ignore backgrounds to recognize objects in new contexts as well as rely on background information to recognize ambiguous objects? Past work suggests that this adaptive use of context is possible via the factorization of foreground objects versus background contexts in visual representations [4, 53, 7, 28]. Factorized representations have the property that the features of the data are represented in different subspaces in the feature space. Specifically, for a model with representations in  $\mathbb{R}^d$ , with two orthogonal subspaces  $V \subset \mathbb{R}^d$  and  $V^\perp$ , we say the model factorizes its foreground and background representations when foreground information is represented in the subspace  $V$  and background information is represented in  $V^\perp$ .
The bases for the subspaces  $V$  and  $V^{\perp}$  do not have to align with the dimensions of the feature vectors (i.e. the standard basis of  $\mathbb{R}^d$ ), nor do representations have to be sparse, unlike common definitions of modularity or disentanglement [8]. Bengio et al. [5] suggest that factorization should broadly improve model generalization; we state this hypothesis specifically for the adaptive use of context as the Factorization Hypothesis:
Factorization Hypothesis: factorized representation is necessary for adaptive use of context.
This is not to say that factorized representation is sufficient for generalization. A model must also use the factorized representations in a certain way to generalize to both new contexts and ambiguous objects. In human vision, object-context interactions occur at very small timescales, suggesting simple feedforward weighting of evidence is sufficient [53]. For computer vision, we argue that the object representation typically provides strong evidence for the object's identity, while the context representation can serve as a tie-breaking feature that provides a small amount of evidence that is useful when the features of the object itself are ambiguous (all within a single forward pass). In this view, background information becomes useful for identifying objects when they are occluded, blurry, far away, or otherwise difficult to recognize. We state this view as the Feature Weighting Hypothesis:
Feature Weighting Hypothesis: model predictions should depend on both object and context features, with stronger weight given to object features but non-zero weight on context features.
In this paper, we argue that models generalize to new contexts and ambiguous objects by following the Factorization Hypothesis and the Feature Weighting Hypothesis. Experimentally, we (1) design testing procedures that capture generalization to both new contexts and ambiguous objects, (2) show that current models' generalization is predicted by how factorized their representations are and how they weight object/context features, and (3) design novel objectives for improving model generalization based on these hypotheses.
First, we formulate two kinds of OOD generalization settings where the context is either irrelevant or helpful: BACKGROUND-INVARIANCE and OBJECT-DISAMBIGUATION, which are illustrated in Fig. 1. In BACKGROUND-INVARIANCE, foreground objects and the background contexts are randomly paired (i.e., objects in new contexts), breaking spurious correlations observed in naturalistic training distributions. In OBJECT-DISAMBIGUATION, the object itself is hard to recognize or ambiguous (due to motion, lighting conditions, noise, etc.), while the background context is of the kind usually observed for the object. We find that there is a fundamental tradeoff in performance on our BACKGROUND-INVARIANCE vs. OBJECT-DISAMBIGUATION tests. This poses a problem for past work, which so far has only evaluated on one setting or the other. We show that methods focusing on BACKGROUND-INVARIANCE [2, 38, 21, 42] will hurt performance on OBJECT-DISAMBIGUATION without realizing it, and vice versa [29]. Following how human vision generalizes, we should aim for a single computer vision system that generalizes well to both OOD settings.
Second, we test the Factorization and Feature Weighting hypotheses by training a population of models on two standard benchmarks, COLOROBJECT and SCENEOBJECT [51, 27]. For each model, we measure factorization using interpretability methods including linear probes, representational similarity analysis [20], and a geometric analysis [28]. To test for feature weighting, we measure model sensitivity to foreground vs. the background. We do so by randomly switching the foreground or background and measuring the relative differences in the changes to model representations. Results show that more factorized models with appropriate feature weighting tend to achieve higher performances on both OOD settings, meaning they adaptively use the context to generalize to novel settings. We further confirm this finding by a causal analysis where we directly manipulate model representations.
Third, we design novel objectives for improving model generalization on BACKGROUN-DINVARIANCE and OBJECT-DISAMBIGUATION tests, motivated by the results of our factorization and feature weighting analysis. In order to encourage model factorization, we augment the data with random-background and background-only images (using ground-truth masks or pseudo-masks from SAM [19]), and we weigh the corresponding objective terms to encourage appropriate feature weighting of the foreground and background information. On two benchmark datasets, our method improves significantly over strong baselines on one OOD setting while matching performance on the other.
We summarize our main findings as follows:
1. We show that models that do better on BACKGROUND-INVARIANCE tend to do worse on OBJECT-DISAMBIGUATION and vice versa, highlighting a need to design systems that generalize across both OOD settings, similar to biological vision (Sec. 5).
2. We obtain both correlational and causal evidence for our Factorization and Feature Weighting hypotheses: more factorized models achieve better OOD generalization, and this requires models to assign appropriate weight to foreground features over background features (Sec. 6).
3. We design new model objectives motivated by the Factorization and Feature Weighting hypotheses. We find these objectives achieve a Pareto improvement over strong baselines, increasing performance on one OOD setting while matching performance on the other (Sec. 7).
Evaluating vision model generalization. Much research has evaluated the generalization capabilities of vision models in various OOD settings [37, 13, 12]. In particular, several benchmarks have been proposed for evaluating OOD generalization, such as ImageNet-C and ImageNet-9 [15, 46]. However, most prior works fail to test on OOD settings where spurious features like background are beneficial [38, 2, 21]. Liu et al. [29] test on an extreme version of OBJECT-DISAMBIGUATION, where the foreground object is completely masked out, but they do not test on the OOD setting where backgrounds are irrelevant. We evaluate vision models on two kinds of OOD settings where the context is either helpful or irrelevant. To our knowledge, no prior works have evaluated models in both settings.
Understanding how vision models generalize. Various works have explored the relationship between model properties, inductive biases, and generalization capabilities [32, 50, 16]. Many recent works try to predict OOD performance using in-distribution (ID) measures like model invariance and smoothness [45, 10, 33], but they do not control for in-distribution accuracy which has historically been the best predictor of OOD accuracy [43]. More recent work that controls for ID accuracy suggests that properties like model invariance, calibration, and robustness do not predict OOD generalization
(1a)In-distribution
(1b) Background-Invariance
(1) ColorObject
(1c) Object-Disambiguation
Figure 2: The visualization of COLOROBJECT (left) and SCENEOBJECT (right) datasets and their corresponding OOD test sets. For in-distribution sets (1a, 2a), there are certain natural co-occurrence probabilities between the foreground object classes and the background classes (e.g.  $80\%$ , shown by the random background of the last column of (1a, 2a)). For BACKGROUND-INVARIANCE sets (1b, 2b), there is no correlation between the foreground and background. For OBJECT-DISAMBIGUATION sets (1c, 2c), there is a perfect correlation between the foreground and the background classes, but the foreground objects themselves are hard to recognize due to natural corruptions
(2a)In-distribution
(2b) Background-Invariance
(2) SceneObject
(2c) Object-Disambiguation
better than ID accuracy [47]. Moreover, past analysis is generally correlational in nature. In this paper, we use interpretability methods to measure factorization and feature weighting metrics in vision models and analyze both correlational and causal relations to generalization in our two OOD settings. We do not know of an analysis that demonstrates a causal relationship between model properties and OOD generalization. In their review, Bengio et al. [5] hypothesize that models with more factorized representations should generalize better, but they do not test this experimentally. Our work provides empirical support for this claim specifically for the adaptive use of context for generalization.
Improving generalization. Various methods have been proposed to improve the OOD generalization of deep learning models. One line of research relying on metadata of multiple training domains to learn the causal (or 'invariant') features from multiple domains [36, 2, 40, 42]. Other methods make use of data augmentation to improve both ID and OOD performances, like MixUp [52], CutMix [48], and AutoAugment [9]. However, these works do not test on OOD settings where spurious features like background are beneficial. We show in Sec. 5 that methods designed to learn causal features either do not improve or even hurt the performances in the OBJECT-DISAMBIGUATION setting. Our augmentation method is built on the insight that both causal and spurious features can be useful, and these features need to be weighted properly so that the models can generalize to both OOD settings.
3 Problem Setup: Two OOD Generalization Settings for Vision Models
Here we introduce the training and testing environments that we would like vision models to perform well in, inspired by biological vision systems. Specifically, we consider scenarios where there are test-time shifts in (1) the correlations between foreground objects and background contexts and (2) the reliability of causal features (features of foreground objects), which can be corrupted by shifts in image lighting, blurring, object occlusion, or other sources of noise. We show an illustrative example in Figure 1: the butterfly is easily recognizable regardless of the background; the keyboard looks like a white blob in isolation but can be easily recognized when put in context. We use train and test sets constructed based on these scenarios to understand how vision systems can effectively use both the causal features of the foreground objects and the spurious features of the background contexts to adaptively generalize to domain shifts.
Notation. We use  $X$  and  $Y$  to denote random variables of the input and the output, while  $x$  and  $y$  denote data samples. We assume there is a set of environments  $\mathcal{E}$  (instantiated as datasets), which can be divided into in-distribution (ID) and out-of-distribution (OOD) sets:  $\mathcal{E}_{id}, \mathcal{E}_{ood} \subset \mathcal{E}$ . The models are trained on  $\mathcal{E}_{id}$ , and our goal is to learn a function  $f: X \to Y$  that generalizes well to  $\mathcal{E}_{ood}$ .
Data construction process. We consider settings where the inputs  $X^{e}$  from environment  $e$  are generated from latent variables  $Z_{c}^{e}, Z_{s}^{e}$ , where  $Z_{c}^{e}$  denotes the causal foreground class while  $Z_{s}^{e}$  denotes the spurious background class. The label  $Y^{e}$  is equivalent to  $Z_{c}^{e}$  (analogous to e.g. an object recognition task where the label is the object identity). The input  $\mathbf{x}$  contains both causal features (foreground object)  $\mathbf{x}_{c}$  and spurious features (background context)  $\mathbf{x}_{s}$ . For each sample
$Z_{c}^{e} = z_{c}^{e}$  and  $Z_{s}^{e} = z_{s}^{e}$ , we generate causal and spurious inputs by  $\mathbf{x}_c = G_c(z_c^e)$  and  $\mathbf{x}_s = G_s(z_s^e)$ , where  $G_{c}$  and  $G_{s}$  are maps from the latent space to the input space. We then corrupt the causal features with the random function  $\phi$  (for example, by blurring or adding noise to the image). Finally, we generate the complete input  $\mathbf{x} = G(\phi (\mathbf{x}_c),\mathbf{x}_s)$ , where  $G$  combines the causal and spurious features (e.g. overlays a foreground object onto a background image). Each environment differs in its co-occurrence probability  $p_{co}$  between  $Z_{c}^{e}$  and  $Z_{s}^{e}$ , and the level of corruption applied to the causal features  $\mathbf{x}_c$ . For ID environments  $e_{id}\in \mathcal{E}$ , we assume high co-occurrence probability and no corruption in the causal feature, identical to past works [38, 51].
Two OOD Settings. For OOD environments  $e_{\text{odd}} \in \mathcal{E}$ , we use two particular settings where the spurious background features are either irrelevant or helpful. We call them BACKGROUND-INVARIANCE and OBJECT-DISAMBIGUATION settings, respectively. To create these OOD settings for vision datasets, we need additional annotation of the background classes and the segmentation mask of the foreground objects. We assume we have access to these metadata in all our experiments.
BACKGROUND-INVARIANCE. In our BACKGROUND-INVARIANCE setting, foreground objects are randomly paired with background contexts (the co-occurrence probability  $p_{co} = 0$ ). We assume there is no noise to the causal features in this setting ( $\phi$  is an identity function). Here, models need to mainly rely on the causal features  $\mathbf{x}_c$  to achieve good performances. See Figure 1 and 2 for examples.
OBJECT-DISAMBIGUATION. In our OBJECT-DISAMBIGUATION setting, the noise level of the causal features is very high while the co-occurrence probability is also high (like it is in natural images). Here, models need to learn the correspondence between the foreground and background features and make use of the spurious contextual features  $\mathbf{x}_s$  to achieve good performance. That is, models must mimic how humans rely on context for object recognition given that objects in the real world can vary significantly in appearance due to factors such as lighting conditions, occlusions, and viewpoint changes [6, 17, 44, 35]. Contextual information, including scene layout, spatial relationships, and semantic associations allows humans to make educated guesses and disambiguate the identity of objects. For example, in Figure 1, the keyboard itself is highly blurred and hard to recognize, but it easily recognizable in context. Also see Figure 2 for examples in our datasets.
4 Experiment Setup
Datasets. We conduct our analysis and experiments on two standard benchmarks, COLOROBJECT and SCENEOBJECT [38, 51]. The datasets and their experimental settings follow the conventions in the IRM literature [2, 21, 38], but we add a novel OBJECT-DISAMBIGUATION test by using the available metadata. Please refer to Appendix A for further details of the experimental settings.
To set up the COLOROBJECT dataset, following prior works [51], we build a biased dataset using 10 classes of objects selected from MSCOCO dataset [26], which are then superimposed onto 10 colored backgrounds. We set a one-to-one relationship between the foreground classes and the background colors. The co-occurrence probabilities  $p_{co}$  are 1.0 and 0.7 for training (i.e., data from these two environments make up the training data), 0.0 for BACKGROUND-INVARIANCE, and 1.0 for OBJECT-DISAMBIGUATION (see Sec. 3 for definitions). To create the OBJECT-DISAMBIGUATION setting for COLOROBJECT dataset where the object itself is hard to recognize, we apply 15 kinds of natural corruptions (like motion blur, contrast change, and fog and snow effects) from Hendrycks and Dietterich [15] to the foreground object before superimposing it on the background image. The natural corruptions come in 5 levels, and we use the average accuracy across 5 levels as the final result. There are 16000 images for training, 2000 for validation, and 2000 for each of the test sets. See Appendix for accuracies across different levels of distribution shift. C. See Figure 2 for examples of ID, BACKGROUND-INVARIANCE, and OBJECT-DISAMBIGUATION images.
To set up the ScENEOBJECT dataset, following prior works [51], we use objects from 10 classes of the MSCOCO dataset and superimpose them onto scene background from 10 classes of the Places dataset to create the ScENEOBJECT dataset [54, 26]. We bind each object category to one scene category. The rest of the data construction is the same as for COLOROBJECT, except for that the co-occurrence probabilities  $p_{co}$  are 0.9 and 0.7 for training COLOROBJECT. See Figure 2 for examples.
Model Population. Following prior works, we use Wide ResNet 28-2 for the two datasets [49]. Using this model backbone, we train models based on the following methods: empirical risk minimiza
tion (ERM), IRM[2], VREx[21], GroupDRO[38], Fish[42], MLDG[25], and our own augmentation method (described in Sec. 7). The models are trained on a fixed set of training environments using different loss functions, random seeds, and other hyperparameters. Note that, across datasets, the noise level and the  $p_{co}$  in the training sets do not cover the noise level and the  $p_{co}$  in the two OOD test sets, so the OOD test sets remain out-of-distribution. We trained a total of 340 models, 170 for each of COLOROBJECT, and SCENEOBJECT datasets. See Appendix A for more details.
5 Negative Correlation Between BACKGROUND-INVARIANCE and OBJECT-DISAMBIGUATION Accuracy
Design. In this section, we evaluate a population of models in our two OOD settings where the background is either helpful or irrelevant, namely BACKGROUND-INVARIANCE and OBJECT-DISAMBIGUATION. As far as we know, no previous work has evaluated models along both of these dimensions of generalization. We use the population of 170 models for each dataset, described in Sec. 4. We aim to compare how models tend to perform on the two OOD settings.
Results. As shown in Figure 3, models that perform better on the BACKGROUND-INVARIANCE setting tend to perform worse on the OBJECT-DISAMBIGUATION setting, and vice versa. The Pearson correlation coefficients are -0.938 for COLOROBJECT and -0.877 for SCENEOBJECT, showing a strong negative correlation between the performances in the two OOD settings. The results are consistent when using a Transformer-based model (Swin) and across training sizes as well (see Appendix Table 7) [30]. Additionally, we find that methods designed to learn causal features, such as IRM, VREx, GroupDRO, Fish, and MLDG, demonstrate improved performance in the BACKGROUND-INVARIANCE setting, but
Figure 3:BACKGROUND-INVARIANCE VS. OBJECT-DISAMBIGUATION performances of all models in the COLOROBJECT (left), and SCENEOBJECT (right). Models that do better in one OOD setting tend to do worse in the other, showing strong negative correlations.
they significantly hurt OBJECT-DISAMBIGUATION performance on COLOROBJECT compared to ERM and four of five lower performances on SCENEOBJECT (see Appendix Table 3). These results highlight the need to make a choice about the tradeoff between foreground and background depending on the environment that is expected for deployment.
6 Understanding OOD Generalization: Which Vision Models Generalize Better And Why?