SlowGuess/NAACL_2021_OCR huggingface dataset:
Gabriel Ilharco Rowan Zellers Ali Farhadi Hannaneh Hajishirzi
{gamaga, rowanz, ali, hannaneh}@cs.washington.edu
The success of large-scale contextual language models has attracted great interest in probing what is encoded in their representations. In this work, we consider a new question: to what extent contextual representations of concrete nouns are aligned with corresponding visual representations? We design a probing model that evaluates how effective are text-only representations in distinguishing between matching and non-matching visual representations. Our findings show that language representations alone provide a strong signal for retrieving image patches from the correct object categories. Moreover, they are effective in retrieving specific instances of image patches; textual context plays an important role in this process. Visually grounded language models slightly outperform text-only language models in instance retrieval, but greatly under-perform humans. We hope our analyses inspire future research in understanding and improving the visual capabilities of language models.
Contextual language models trained on text-only corpora are prevalent in recent natural language processing (NLP) literature (Devlin et al., 2019; Liu et al., 2019b; Lan et al., 2019; Raffel et al., 2019). Understanding what their representations encode has been the goal of a number of recent studies (Belinkov and Glass, 2019; Rogers et al., 2020). Yet, much is left to be understood about whether—or to what extent—these models can encode visual information.
We study this problem in the context of language grounding (Searle et al., 1984; Harnad, 1990; McClelland et al., 2019; Bisk et al., 2020; Bender and Koller, 2020), empirically investigating whether text-only representations can naturally be connected to the visual domain, without explicit visual supervision in pre-training.
Figure 1: We introduce a probing mechanism that learns a mapping from contextual language representations to visual features. For a number of contextual language models, we evaluate how useful their representations are for retrieving matching image patches.
We argue that context plays a significant role in this investigation. In language, the ability to form context-dependent representations has shown to be crucial in designing pre-trained language models (Peters et al., 2018; Devlin et al., 2019). This is even more important for studying grounding since many visual properties depend strongly on context (Sadeghi and Farhadi, 2011). For instance, a "flying bat" shares very few visual similarities with a "baseball bat"; likewise, a "dog sleeping" looks different from a "dog running". While alignments between language representations and visual attributes have attracted past interest (Leong and Mihalcea, 2011; Lazaridou et al., 2014, 2015; Lucy and Gauthier, 2017; Collell Talleda et al., 2017), the role of context has been previously overlooked, leaving many open questions about what visual information contextual language representations encode.
In this work, we introduce a method for empirically probing contextual language representations and their relation to the visual domain. In general, probing examines properties for which the models are not designed to predict, but can be encoded in their representations (Shi et al., 2016; Rogers et al.,
Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pages 5367-5377 June 6-11, 2021. ©2021 Association for Computational Linguistics
Figure 2: Examples of retrieved image patches from text-only representations using our probe. All shown images are retrieved from MS-COCO (Lin et al., 2014), using representations from BERT base. Importantly, these object categories (e.g. kite) are previously unseen by our probe. On the bottom rows, we show examples of the influence of context in retrieval: while all retrieved image patches belong to the correct object category, cat, more descriptive contexts allow more accurate retrieval at the instance level.
2020). Here, our probe is a lightweight model trained to map language representations of concrete objects to corresponding visual representations. The probe (illustrated in Figure 1) measures whether language representations can be used to give higher scores to matching visual representations compared to mismatched ones.
Textual and visual representations are collected from image captioning data, where we find pairs of concrete words (e.g. cat or kite) and their corresponding image patches. The probe is trained using a contrastive loss (Oord et al., 2018) that gauges the mutual information between the language and visual representations. Given text-only representations of an unseen object category, the trained probe is evaluated by retrieving corresponding image patches for categories it has never seen during training. Qualitative examples can be found in Figure 2.
We examine representations from a number of contextual language models including BERT, RoBERTa, ALBERT and T5 (Devlin et al., 2019; Liu et al., 2019b; Lan et al., 2019; Raffel et al., 2019). For all of them, we find that interesting mappings can be learned from language to visual representations, as illustrated in Figure 2. In particular, using its top-5 predictions, BERT representations retrieve the correctly paired visual instance  $36\%$  of the time, strongly outperforming non-contextual language models (e.g., GloVe (Pennington et al.,
2014)). Moreover, for all examined models, image patches of the correct object category are retrieved with a recall of  $84 - 90\%$ . Our experiments are backed by a control task where visual representations are intentionally mismatched with their textual counterparts. Retrieval performance drops substantially in these settings, attesting the selectivity of our probe.
Moreover, we measure the impact of context on retrieval at the instance level. Contextual models substantially outperform non-contextual embeddings, but this difference disappears as context is gradually hidden from contextual models. When the context includes adjectives directly associated with the noun being inspected, we find significantly better instance retrieval performance.
Finally, we investigate a number of grounded language models—such as LXMERT and VILBERT (Tan and Bansal, 2019; Lu et al., 2019, 2020)—that see visual data in training, finding them to slightly outperform text-only models. Contrasting the learned mappings with human judgment, the examined visually grounded language models significantly underperform human subjects, exposing much room for future improvement.
What is encoded in language representations?
Understanding what information NLP models encode has attracted great interest in recent years
(Rogers et al., 2020). From factual (Petroni et al., 2019; Jawahar et al., 2019; Roberts et al., 2020) to linguistic (Conneau et al., 2018; Liu et al., 2019a; Talmor et al., 2019) and commonsense (Forbes et al., 2019) knowledge, a wide set of properties have been previously analysed. We refer to Belinkov and Glass (2019) and Rogers et al. (2020) for a more comprehensive literature review. A common approach, often used for inspecting contextual models, is probing (Shi et al., 2016; Adi et al., 2016; Conneau et al., 2018; Hewitt and Liang, 2019). In short, it consists of using supervised models to predict properties not directly inferred by the models. Probing is typically used in settings where discrete, linguistic annotations such as parts of speech are available. Our approach differs from previous work in both scope and methodology, using a probe to measure similarities with continuous, visual representations. Closer to our goal of better understanding grounding is the work of Cao et al. (2020), that design probes for examining multi-modal models. In contrast, our work examines text-only models and does not rely on their ability to process images.
Language grounding. A widely investigated research direction aims to connect natural language to the physical world (Bisk et al., 2020; McClelland et al., 2019; Tan and Bansal, 2019; Lu et al., 2019, 2020; Chen et al., 2020; Li et al., 2020; Tan and Bansal, 2020). This is typically done through training and evaluating models in tasks and datasets where both images and text are used, such as visual question answering (Antol et al., 2015; Hudson and Manning, 2019). A number of previous work have investigated mappings between language and visual representations or mappings from both to a shared space. Leong and Mihalcea (2011) investigate semantic similarities between words and images through a joint latent space, finding a positive correlation with human rated similarities. Similarly, Silberer and Lapata (2014) builds multi-modal representations by using stacked autoencoders. Socher et al. (2013) and Lazaridou et al. (2014) show that a shared latent space allows for zero-shot learning, demonstrating some generalization to previously unseen objects. Lazaridou et al. (2015) construct grounded word representations by exposing them to aligned visual features at training time. Lucy and Gauthier (2017) investigate how well word representations can predict perceptual and conceptual features, showing that a number of such features are not adequately predicted. Collell Talleda et al.
(2017) uses word embeddings to create a mapping from language to visual features, using its outputs to build multimodal representations. While our conclusions are generally aligned, our work differs from these in two important ways. Firstly, previous work studies context-independent word representations, while our method allows analysing language representations that depend on the context they are used in. We use this to examine a number of trained contextual language models. Secondly, while most previous work uses these mappings for building better grounded representations—often training the language models in the process—our work focuses on using them as a tool for inspecting already trained models, without modifying them.
Zero-shot detection. Recent work attempts to build object detectors that generalize to unseen object categories, by conditioning the predictions on word embeddings of the class (Rahman et al., 2018; Demirel et al., 2018), visual attributes (Demirel et al., 2018; Zhu et al., 2019; Mao et al., 2020) or text descriptions (Li et al., 2019). In our work, we use language representations of words in context (captions) as inputs. More fundamentally, although our experiments on unseen object categories can be used for zero-shot detection, we differ from previous work in motivation, which translates to further experimental differences. Given our goal to analyse already trained models (as opposed to learning a generalizable object detector), we train nothing apart from a lightweight probe in our analyses.
3 Probing contextual representations
Our main goal is to characterize the relation between contextual language representations and the visual domain. We first describe how language and visual representations of concrete concepts can be collected from image captioning datasets (§3.1). Next, we design a probe that examines the relation between these representations, learning a mapping from language to visual representations (§3.2). An overview is illustrated in Figure 3.
3.1 Collecting data
At the center of our analysis are contextual representations of visually observable nouns, which we refer to as object categories. Here, we describe how pairs of matching language and visual representations  $(\ell ,v)$  are collected from image captioning datasets.
Figure 3: An overview of the proposed probing procedure. Frozen language and vision models  $(\Lambda$  and  $\Theta)$  extract representations from matching pairs of words in text and objects in images. A probe  $\Psi_{\theta}$  is trained to map representations from text (green) to visual (blue) domains while maximally preserving mutual information. For a given language representation  $\ell_i$ , the loss (Equation 1) drives the probe's outputs  $\hat{v}_i = \Psi_\theta (\ell_i)$  to be maximally useful for finding the aligned visual representation  $v_{i}$  given all other visual representations in the batch  $(\mathcal{V}_i^{\mathrm{NEG}} = v_j,i\neq j)$ . For such, only the pair-wise dot products  $\langle \hat{v}_i,v_j\rangle$  are required (red).
Language representations  $(\ell)$  are extracted from image captions. To accommodate recent language models and tokenizers, we allow such representations to be contextual and have variable length, where each element in  $\ell$  has a fixed dimension  $d_{L}$ . The length of the representations  $\ell$  for each object category is determined by the tokenizer. We treat a model that extracts representations from text as a function  $\Lambda$  that maps a string  $o$  (here, object categories) in a larger textual context  $c$  (here, captions) to the representation  $\ell = \Lambda(o|c)$ . This formalism also encompasses non-contextual embeddings, with  $\Lambda(o|c) = \Lambda(o)$ .
Visual representations  $(v)$  are extracted from objects in images using a trained object detection model  $\Theta$ . For simplicity, we use  $v = \Theta(o \mid i)$  to refer to the extracted features corresponding to the detected object from image  $i$  that is both 1) classified as a member of object category  $o$  and 2) assigned the highest confidence by the model among those. Visual representations  $\Theta(o \mid i)$  have fixed dimensions  $d_V$ .
Paired data  $(\ell, v)$  with aligned representations is collected from an image captioning dataset with paired captions  $c$  and images  $i$ . For each image  $i$ , and each object  $o$  detected by the object detector  $\Theta$ , if  $o$  appears in some associated caption  $c$ , we include the pair  $(\ell = \Lambda(o \mid c), v = \Theta(o \mid i))$ . To avoid having multiple pairs  $(\ell, v)$  associated with
the same visual instance, we ensure that at most one pair  $(\ell ,v)$  per object category in each image is included. In this work, we use the 1600 object categories from Faster R-CNN (Ren et al., 2015) trained on Visual Genome (Krishna et al., 2017).
3.2 Probing representations
At a high level, language representations are inspected via a shallow neural probing model (Figure 3). In training, the probe learns a mapping from language to visual representations (§3.2.1). We then evaluate the quality of these mappings by measuring how well they can be used to retrieve matching image patches (§3.2.2).
3.2.1 Training the probe
The probe is optimized to maximally preserve the mutual information between the distributions of language and visual representations. This is done via InfoNCE (Oord et al., 2018) (Equation 1), a loss function commonly used for retrieval and contrastive learning (Le-Khac et al., 2020). We note the mutual information is a bottleneck on how well two random variables can be mapped to one another, given its relation to conditional entropy. In training, the probe  $\Psi_{\theta}$  with parameters  $\theta$  takes inputs  $\ell$  and estimates visual representations  $\hat{v} = \Psi_{\theta}(\ell)$  with the same dimensionality  $d_V$  as the corresponding visual representations  $v$ . For each pair  $(\ell, v)$ , this loss relies on a set of distractors  $\mathcal{V}_{\ell}^{\mathrm{NEG}}$ , containing visual representations which are not aligned with the language representations  $\ell$ . The representations in  $\mathcal{V}_{\ell}^{\mathrm{NEG}}$  are used
1 Conforming with sub-word tokenizers or multi-word expressions such as fire extinguisher.
for contrastive learning and are drawn from the same visual model, using different objects or images. Minimizing this loss drives the dot product  $\langle \Psi_{\theta}(\ell), u \rangle$  to be maximal for  $u = v$  and small for all  $u \in \mathcal{V}_{\ell}^{\mathrm{NEG}}$ . In other words, training pushes the estimates  $\hat{v} = \Psi_{\theta}(\ell)$  to be maximally useful in discerning between positive and negative visual pairings.
$$
\mathcal {L} _ {\text {I n f o N C E}} = - \mathbb {E} _ {\ell} \left[ \log \frac {e ^ {\langle \Psi_ {\theta} (\ell) , v _ {\ell} \rangle}}{\sum_ {v ^ {\prime} \in \{v \} \bigcup \mathcal {V} _ {\ell} ^ {\mathrm {N E G}}} e ^ {\langle \Psi_ {\theta} (\ell) , v ^ {\prime} \rangle}} \right] \tag {1}
$$
In practice, the expectation in Equation 1 is estimated over a batch of size  $B$  with samples of aligned language and visual representations  $((\ell_1,v_1),\ldots ,(\ell_B,v_B))$ . For efficiency, we use other visual representations in the batch as distractors for a given representation  $(\mathcal{V}_i^{\mathrm{NEG}} = \{v_j,j\neq i\})$ . Thus, only the dot products  $\langle \hat{v}_i = \Psi_\theta (\ell_i),v_j\rangle$  are needed to calculate the loss, as illustrated in Figure 3. Importantly, we note that the models used to extract representations are not trained or changed in any way during the probing procedure.
3.2.2 Evaluation procedure
For evaluation, we compute recall in retrieving image patches given objects in text, using new pairs of language and visual representations from unseen images and captions. Consider the set of all collected visual representations for evaluation,  $\mathcal{V}$ . For each language representation  $\ell$ , we use the trained probe to generate our estimate  $\hat{v} = \Psi_{\theta}(\ell)$ , and find the instances  $v' \in \mathcal{V}$  that maximize the dot product  $\langle \hat{v}, v' \rangle$ . Given an integer  $k$ , we consider recall at  $k$  at both instance and category levels. Formally:
Instance Recall (IR@k) measures how frequently the correct visual instance is retrieved. More precisely, it is the fraction of pairs  $(\ell ,v)$  where the instance  $v$  is in the top-  $k$  visual representations retrieved from  $\hat{v} = \Psi_{\theta}(\ell)$ .
Category Recall (CR@k) measures how frequently instances of the correct object category are retrieved. More precisely, it is the fraction of pairs  $(\ell, v = \Theta(o \mid i))$  where any of the top- $k$  retrieved visual representations  $v' = \Theta(o' \mid i')$  belongs to the same object category as  $v$  (i.e.  $o' = o$ ).
Higher IR and CR scores indicate better performance and, by definition,  $\mathrm{CR}@\mathrm{k}$  cannot be smaller than IR@k. These metrics form the basis of our
evaluation, and we take multiple steps to promote experimental integrity. Learned mappings are evaluated in two scenarios, where pairs  $(\ell, v)$  are collected using object categories either seen or unseen by the probe during training. The later is the focus of the majority of our experiments. For both scenarios, images and captions have no intersection with those used in training. Further, we create multiple seen/unseen splits from our data, training and testing on each split. We then report average and standard deviation of the recall scores across 5 splits.
4 Experimental settings
4.1 Language models
The majority of examined models are contextual representation models based on the transformer architecture (Vaswani et al., 2017) trained on text-only data. We examine the base  $(d_{L} = 768)$  and large  $(d_{L} = 1024)$  versions of BERT uncased, RoBERTa, ALBERT and T5 (Devlin et al., 2019; Liu et al., 2019b; Lan et al., 2019; Raffel et al., 2019). For T5, we also examine the small version, with  $d_{L} = 512$ . For all these models, we use pretrained weights from the HuggingFace Transformers library (Wolf et al., 2020)<sup>2</sup>, and use representations from the last layer. Additionally, we inspect non-contextual representations using GloVe embeddings (Pennington et al., 2014), using embeddings trained on 840 billion tokens of web data, with  $d_{L} = 300$  and a vocabulary size of 2.2 million.<sup>3</sup>
4.2 Vision models
As is common practice in natural language grounding literature (Anderson et al., 2018; Tan and Bansal, 2019; Su et al., 2020; Lu et al., 2020), we use a Faster R-CNN model (Ren et al., 2015) trained on Visual Genome (Krishna et al., 2017) to extract visual features with  $d_V = 2048$ . We use the trained network provided by Anderson et al. (2018)<sup>4</sup>, and do not fine-tune during probe training.
4.3 Data
We collect representations from two image captioning datasets, Flickr30k (Young et al., 2014), with over 150 thousand captions and 30 thousand images, and MS-COCO (Lin et al., 2014), with 600 thousand captions and 120 thousand images
$^{2}$ https://github.com/huggingface/transformers
<sup>3</sup>https://nlp.stanford.edu/projects/glove/
<sup>4</sup>https://github.com/peteanderson80/bottom-up-attention
in English. The larger MS-COCO is the focus of the majority of our experiments. We build disjoint training, validation and test sets from the aggregated training and validation image captions. To examine generalization to new objects, we test on representations from both seen or unseen object categories, built from images and captions not present in the training data. From the 1600 object categories of our object detector, we use 1400 chosen at random for training and seen evaluation. The remaining 200 are reserved for unseen evaluation. Furthermore, we train and test our probe 5 times, each with a different  $1400/200$  split of the object categories. For each object category split, we build validation and test sets with sizes proportional to the number of object categories present: seen test sets contain 7000 representation pairs and unseen test sets contain 1000 pairs. The validation sets used for development consists of seen object categories, with the same size as the seen test sets. All remaining data is used for training.
4.4 Control task
Contrasting the probe performance with a control task is central to probing (Hewitt and Liang, 2019). We follow this practice by learning in a control task where representations are mapped to permuted visual representations. More precisely, we replace each visual representation  $v = \Theta(o \mid i)$  with another  $v' = \Theta(o' \mid i')$  chosen at random from an object category  $o' = f(o)$  that depends on the original object category  $o$ . Here,  $f$  dictates a random permutation of the object categories. For instance, visual representations of the original category cat are replaced with representations from a second category dog; representations from the category dog are replaced by those from tree, and so on.
4.5 Implementation and hyper-parameters
Our probe consists of a shallow neural model. To process the naturally sequential language representations  $\ell$ , we use a single-layered model with LSTM cells (Hochreiter and Schmidhuber, 1997) with 256 hidden units and only unidirectional connections. The outputs are then projected by a linear layer to the visual space. The probe is trained using Adam optimizer (Kingma and Ba, 2015) with a learning rate of 0.0005, weight decay of 0.0005 and default remaining coefficients  $(\beta_{1} = 0.9, \beta_{2} = 0.999$  and  $\epsilon = 10^{-8}$ ). We train with a batch size of 3072, for a total of 5 epochs on one GPU.
#	Experiment	IR@1	IR@5	CR@1
0	Random	0.1 ± 0.1	0.5 ± 0.2	6.0 ± 2.0
1	Control	0.0 ± 0.0	0.4 ± 0.2	3.0 ± 1.4
2	GloVe	5.1 ± 0.5	18.5 ± 1.4	87.3 ± 3.5
3	BERT base	12.0 ± 1.0	36.0 ± 0.9	88.1 ± 2.4
4	BERT large	11.6 ± 0.7	34.9 ± 2.6	89.3 ± 2.4
5	RoBERTa base	11.6 ± 0.3	34.4 ± 2.2	90.4 ± 0.6
6	RoBERTa large	10.9 ± 1.1	32.8 ± 2.5	88.7 ± 3.2
7	ALBERT base	8.7 ± 0.2	28.8 ± 1.6	84.4 ± 2.1
8	ALBERT large	9.4 ± 1.0	28.8 ± 2.3	84.2 ± 4.2
9	T5 small	10.1 ± 0.7	32.9 ± 1.5	87.2 ± 4.1
10	T5 base	10.8 ± 0.8	33.3 ± 2.3	85.3 ± 2.8
11	T5 large	11.8 ± 0.5	34.7 ± 2.1	87.2 ± 2.4
Table 1: Average instance recall (IR@k) and category recall (CR@k) for test sets with unseen object categories. For each model, we train and evaluate 5 times, using different sets of object categories seen in training. Unlike the control task with permuted representations, mappings learned from sensible representations generalize well to unseen object categories.