SlowGuess/NeurIPS_2023_OCR huggingface dataset:
James Oldfield $^{1*}$  Christos Tzelepis $^{1}$  Yannis Panagakis $^{2,3}$
Mihalis A. Nicolaou<sup>4</sup> Ioannis Patras<sup>1</sup>
$^{1}$ Queen Mary University of London  $^{2}$ National and Kapodistrian University of Athens
$^{3}$ Archimedes/Athena RC  $^{4}$ The Cyprus Institute
Latent image representations arising from vision-language models have proved immensely useful for a variety of downstream tasks. However, their utility is limited by their entanglement with respect to different visual attributes. For instance, recent work has shown that CLIP image representations are often biased towards specific visual properties (such as objects or actions) in an unpredictable manner. In this paper, we propose to separate representations of the different visual modalities in CLIP's joint vision-language space by leveraging the association between parts of speech and specific visual modes of variation (e.g. nouns relate to objects, adjectives describe appearance). This is achieved by formulating an appropriate component analysis model that learns subspaces capturing variability corresponding to a specific part of speech, while jointly minimising variability to the rest. Such a subspace yields disentangled representations of the different visual properties of an image or text in closed form while respecting the underlying geometry of the manifold on which the representations lie. What's more, we show the proposed model additionally facilitates learning subspaces corresponding to specific visual appearances (e.g. artists' painting styles), which enables the selective removal of entire visual themes from CLIP-based text-to-image synthesis. We validate the model both qualitatively, by visualising the subspace projections with a text-to-image model and by preventing the imitation of artists' styles, and quantitatively, through class invariance metrics and improvements to baseline zero-shot classification.
Many recent advances in machine learning have been driven by vision-language (VL) models' ability to learn powerful, generalisable image representations from natural language supervision [1, 2, 3]. The image features from VL models well-capture representations of many visual attributes as evidenced by the broad applicability they have found for use in downstream tasks. The image or text encoders of CLIP in particular [1] have been used for controllable image synthesis [4, 5, 6], image captioning [7, 8], and multiple other discriminative tasks [9, 10, 11]. However, modelling the many different visual modalities in a single vector representation is not without its drawbacks—recent work shows that CLIP's visual representations are often entangled. For example, Goh et al. [12] find that specific neurons fire in response to both images containing a visual concept and images of text relating to the same concept. This leaves CLIP open to vulnerabilities in the form of 'Typographic attacks'—writing another class name as text on the image can often cause CLIP to predict this class with a higher probability than that of the original image's true category. Other recent works show that CLIP's visual representations encode task-specific attributes (such as of the object or action depicted in an image) in
*Corresponding author: j.a. oldfield@qmul.ac.uk
Figure 1: CLIP represents multiple visual modes of variation in an embedding (e.g. the 'object' and its 'appearance'). The learnt PoS subspaces more reliably separate the constituent visual components.
an unpredictable manner, and often the embedding is biased in the prominence with which it encodes different modalities [13]. We show in Figure 1a a motivating example of this problem identified by Menon et al. [13]–the ‘goldfish’ noun embedding dominates the image’s representation despite there being multiple additional labels which accurately describe important information about the image’s contents. As a visual example, we find that CLIP encodings of text prompts containing ‘visually polysemous’ [14] phrases of artists’ names lead CLIP-based text-to-image models [15] to synthesize an unpredictable combination of both images of the artists and of artworks in their signature styles (as shown in Figure 1b). Multiple visual associations of the text prompt, including both the appearance of the artist themselves and the style of their artwork, are entangled in the same CLIP embedding. For VL representations to make for useful image features, it’s vital that the particular modalities of interest are indeed well-represented in the embedding. One popular means to this end is fine-tuning the representations for specific downstream tasks [9]. However, this not only requires additional computation but makes the restrictive assumption of the existence of labelled data for each task.
In this paper, we address the problem of better disentangling the modes of visual variation in CLIP's shared vision-language space. In particular, we ask the question: do there exist subspaces in CLIP's joint VL space that capture the representations of the 'content' of an image or text, that are invariant to its 'appearance'? To take the first steps towards achieving this we leverage the association between parts of speech in natural language and specific modes of visual variation: our learnt noun subspace isolates representations of the 'object' of an image or text prompt (e.g. an animal in an image, or the noun described in a sentence), and the adjective space its appearance (e.g. whether an object is shiny, or a scene is snowy). This image-text semantic similarity is instilled in the representations through CLIP's contrastive learning training objective, through which it is encouraged to learn image encodings that have large cosine similarity in the shared vision-language space with text encodings of captions describing its visual content. We show how by using example words in the parts of speech categories in the WordNet [16] database, one can extract representations of both image and text embeddings that better isolate the individual modes of visual variation. This is in contrast to the method of VL model 'prompting' [17, 18, 13], which often steers the embeddings of either only the text or image modality. We take inspiration from the related line of work that finds so-called 'interpretable directions' in latent space [19, 20, 21, 22, 23] that capture high-level semantic attributes—however, we learn subspaces that capture variation uniquely present amongst representations of words of particular parts of speech. We achieve this by formulating an appropriate objective function which we show can be manipulated into a well-known trace maximisation problem with a fast solution given in closed form. Since the CLIP representations live on the hypersphere [24], we further propose a manifold generalisation of the subspaces [25, 26] (illustrated in Figure 2) that share the property of capturing the variance of only the desired visual attributes, yet better respect the manifold on which the data lie. Concretely, we compute the proposed component analysis in the tangent space to the sphere's intrinsic mean, which can be seen as a local approximation of the manifold [27].
The method's ability to disentangle visual modes of variation is measured both qualitatively and quantitatively. Using a popular CLIP-based text-to-image model we demonstrate visually how the learnt PoS subspaces can better separate the content from the style associated with a text prompt. We show, for example, how simply projecting onto the orthogonal complements of the noun and adjective subspaces respectively can more reliably produce images of either the artists' work, or of the artists themselves, as shown in rows 2 and 3 of Figure 1b. We find removing a CLIP representations' adjective space component to be remarkably effective for preventing the visual imitation of artists' styles in this new class of CLIP-based text-to-image models [15], addressing societal concerns of the technology. Further, we show our objective additionally facilitates learning subspaces corresponding to more specific visual appearances (e.g. 'gory'). Projections onto the orthogonal complements of such subspaces consequently remove entire visual themes from text-to-image models, whilst preserving existing concepts through the PoS guidance.
Finally, we validate the subspaces' ability to isolate visual modes of variation quantitatively through a measure of class invariance (comparing to two existing baselines), and by showing how the baseline zero-shot classification protocol in the learnt subspaces leads to higher accuracies on the ViT-B-32 CLIP model on 14/15 datasets considered. Our contributions can be summarised as follows:
- We present a method for learning geometry-aware subspaces in CLIP's shared vision-language space that disentangle representations of the content in an image or text prompt from the way it looks, using parts of speech as supervision. To the best of our knowledge, we are the first to use the semantic relationship between parts of speech and specific visual modes of variation to disentangle CLIP's shared vision-language space.
- We formulate and solve an appropriate trace maximisation problem that admits a fast closed-form solution respecting the manifold on which the data lie.
- We validate our method's success at disentangling the visual modes of variation both quantitatively and qualitatively: by visually separating a text prompt's 'content' from its 'appearance' with text-to-image models for the former and through improved zero-shot classification in the submanifolds for the latter.
- We show the model's ability to further learn subspaces of more specific appearance-based variation (e.g. artists' styles), providing a way of erasing entire visual themes from CLIP-based text-to-image models.
We first recall the basics of CLIP. We then motivate and introduce our objective function for learning parts of speech-specific linear subspaces in Section 2.1, and detail its closed-form solution in Section 2.1.1. Finally, in Section 2.2 we show how to learn subspaces of the tangent space to the CLIP VL hypersphere's intrinsic mean, which better respects the geometry of the manifold on which the CLIP representations lie.
CLIP preliminaries Pre-trained image and text encoders map images and text respectively to latent representations  $\mathbf{z}_I, \mathbf{z}_T \in \mathbb{R}^d$  in a shared 'vision-language' (VL) space [1]. For test-time zero-shot classification, candidate text prompts are first encoded with the text encoder. Then, the cosine similarity between an encoded image representation of interest and the text candidates is computed with  $S(\mathbf{z}_T, \mathbf{z}_I) = \mathbf{z}_T^\top \mathbf{z}_I / (||\mathbf{z}_T|| \cdot ||\mathbf{z}_I||)$ , after which the softmax function is applied to determine the most likely text label for the image. We now address the task of extracting representations of solely the target modes of variation in the section that follows.
2.1 Objective
We seek a lower-dimensional subspace on which to project either a text or image CLIP representation  $\mathbf{z} \in \mathbb{R}^d$  to predictably isolate the desired visual mode of variation. We achieve this through natural language supervision in the form of words from the different parts of speech. Let the elements of a set  $\mathcal{C}$  index into the relevant word class of interest (i.e.  $\mathcal{C} = \{N, A, V, R\}$  for nouns, adjectives, verbs, and adverbs respectively), and  $\mathbf{X}_i \in \mathbb{R}^{d \times n}, \forall i \in \mathcal{C}$  contain in their columns the CLIP encodings of  $n$  words belonging to word class  $i$  [16]. Then, for a word class  $i \in \mathcal{C}$  of interest, we seek a  $k$ -dimensional subspace of  $\mathbb{R}^d$  spanned by the columns of a learnt  $\mathbf{W}_i \in \mathbb{R}^{d \times k}$  in which the CLIP
(1) Map to Tangent space at intrinsic mean  $\mu$
(2) Project onto desired subspace
(3) Map back to VL sphere
Figure 2: Our proposed method, shown on toy data, for learning 'geodesic submanifolds' [25] that capture the variance of visual attributes uniquely associated with specific parts of speech. After mapping to the tangent space (1), a linear subspace of the tangent space is learnt that captures the variation in only the target class  $i$  (leading eigenvectors of  $\hat{\mathbf{C}}_i$ ). Test samples can then be projected onto this subspace (2), and mapped back (3) to the VL sphere.
representations of the words in the class of interest  $i$  have a large norm and the remaining categories' representations in  $\mathcal{C} \setminus \{i\}$  are close to the zero vector. Intuitively, a hyperplane with this property models factors of variation that are uniquely present in representations of text that belong to a particular part of speech. We quantify this by formulating the following objective function:
$$
\mathbf {W} _ {i} = \underset {\mathbf {W} _ {i} ^ {\top} \mathbf {W} _ {i} = \mathbf {I} _ {k}} {\arg \max } \left\{(1 - \lambda) \left\| \mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {i} \right\| _ {F} ^ {2} - \sum_ {j \in \mathcal {C} \backslash \{i \}} \lambda \left\| \mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {j} \right\| _ {F} ^ {2} \right\}, \tag {1}
$$
where  $\lambda \in [0,1]$  is a hyperparameter that controls the importance of killing the variation in the non-target categories relative to preserving the variation in the target class.
2.1.1 Closed-form solution
Imposing column orthonormality on  $\mathbf{W}_i$  not only ensures its columns span a full  $k$ -dimensional subspace, but also allows Equation (1) to be solved in closed form. Concretely, we first manipulate the objective as follows
$$
\begin{array}{l} (1 - \lambda) | | \mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {i} | | _ {F} ^ {2} - \sum_ {j \in \mathcal {C} \backslash \{i \}} \lambda | | \mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {j} | | _ {F} ^ {2} \\ = (1 - \lambda) \operatorname {t r} \left(\left(\mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {i}\right) ^ {\top} \left(\mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {i}\right)\right) - \sum_ {j \in \mathcal {C} \backslash \{i \}} \lambda \operatorname {t r} \left(\left(\mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {j}\right) ^ {\top} \left(\mathbf {W} _ {i} ^ {\top} \mathbf {X} _ {j}\right)\right) (2) \\ = \operatorname {t r} \left(\mathbf {W} _ {i} ^ {\top} \left((1 - \lambda) \mathbf {X} _ {i} \mathbf {X} _ {i} ^ {\top} - \sum_ {j \in \mathcal {C} \backslash \{i \}} \lambda \mathbf {X} _ {j} \mathbf {X} _ {j} ^ {\top}\right) \mathbf {W} _ {i}\right) (3) \\ = \operatorname {t r} \left(\mathbf {W} _ {i} ^ {\top} \mathbf {C} _ {i} \mathbf {W} _ {i}\right), (4) \\ \end{array}
$$
where  $\mathbf{C}_i = \big((1 - \lambda)\mathbf{X}_i\mathbf{X}_i^\top -\sum_{j\in \mathcal{C}\setminus \{i\}}\lambda \mathbf{X}_j\mathbf{X}_j^\top \big)$ . Having now reformulated our original objective in Equation (1) as a (constrained) trace maximisation problem, its solution  $\mathbf{W}_i$  is given in closed form² as the leading  $k$  eigenvectors of  $\mathbf{C}_i$ .
For ease of presentation, we have assumed the number of data points in each class to be equal. One can account for class imbalance straightforwardly whilst retaining a solution in closed form however: multiplying each Frobenius norm term in the original objective of Equation (1) by  $\frac{1}{n_p}$  (where  $n_p$  is the number of columns of  $\mathbf{X}_p$ ,  $\forall p \in \mathcal{C}$ ) leads to  $\mathbf{C}_i = \left(\frac{1 - \lambda}{n_i} \mathbf{X}_i \mathbf{X}_i^\top - \sum_{j \in \mathcal{C} \setminus \{i\}} \frac{\lambda}{n_j} \mathbf{X}_j \mathbf{X}_j^\top\right)$  in Equation (4).
$^{2}$ Corollary 4.3.39 of Horn and Johnson [28]. Note that all summands in  $\mathbf{C}_i$  are symmetric.
Special cases To contrast the proposed objective with related decompositions, it's instructive to consider the resulting subspaces for extreme values of  $\lambda$  (for zero-mean data). In the limit case of  $\lambda := 0$  for example,  $\mathbf{W}_i$  is given by the top principal components of the data points for one particular class  $i$ . Conversely, a value of  $\lambda := 1$  gives the bottom principal components of datapoints in all other classes  $j \in \mathcal{C} \setminus \{i\}$ . Thus,  $\lambda$  can be seen as providing a trade-off between hyperplanes well-capturing the variance of the attributes in the target class and lying near-orthogonal to the points of the remaining classes. Finally, one recovers regular PCA when  $\mathbf{C}_i := \sum_{j \in \mathcal{C}} \frac{1}{n_j} \mathbf{X}_j \mathbf{X}_j^\top$ .
2.2 From subspaces to submanifolds
Through CLIP's choice of the cosine similarity as an objective function during training, the unit-norm VL representations live on the hypersphere  $\mathbf{z}_I$ ,  $\mathbf{z}_T \in \mathbb{S}^{d-1} \subset \mathbb{R}^d$  [24]. However, subspace learning in the ambient Euclidean space  $\mathbb{R}^d$  does not respect the underlying geometry of the manifold on which the data lie. An orthogonal projection of a vector onto the learnt Euclidean subspaces is not guaranteed to result in a vector that remains on the sphere, even if all the input data do. Motivated by this, we extend the component analysis for linear subspaces developed in Equation (1) to geodesic submanifolds [25]. As demonstrated in Fletcher et al. [25] for PCA, an analogous approximate projection onto the geodesic submanifolds capturing the desired variances can be made by applying the exact same component analysis of Equation (1) in the tangent space  $\mathcal{T}_{\mu}\mathbb{S}^{d-1} \subset \mathbb{R}^d$  to the VL sphere data's intrinsic mean  $\mu \in \mathbb{S}^{d-1}$  instead. To this end, we use the so-called Logarithmic Map  $\mathrm{Log}_{\mathbf{p}}: \mathbb{S}^{d-1} \to \mathcal{T}_{\mathbf{p}}\mathbb{S}^{d-1}$ , which maps points on the sphere to the tangent space at a reference point  $\mathbf{p} \in \mathbb{S}^{d-1}$  and its inverse, the Exponential Map  $\mathrm{Exp}_{\mathbf{p}}: \mathcal{T}_{\mathbf{p}}\mathbb{S}^{d-1} \to \mathbb{S}^{d-1}$  to map points back onto the hypersphere (whose well-known definitions are provided in the supplementary material).
We can then compute the subspace of the tangent space to the intrinsic mean spanned by the columns of a  $\hat{\mathbf{W}}_i\in \mathbb{R}^{d\times k}$  for word class  $i$  as the leading  $k$  eigenvectors of  $\hat{\mathbf{C}}_i = \sum_{n}(1 - \lambda)\mathrm{Log}_{\mu}(\mathbf{x}_{in})\mathrm{Log}_{\mu}(\mathbf{x}_{in})^\top -\sum_{j\in \mathcal{C}\setminus \{i\}}\lambda \mathrm{Log}_{\mu}(\mathbf{x}_{jn})\mathrm{Log}_{\mu}(\mathbf{x}_{jn})^\top$ . We have again assumed an equal number of class data points purely for ease of presentation. This whole process is visualised in Figure 2.
By projecting onto the learnt subspaces of  $\mathcal{T}_{\mu}\mathbb{S}^{d - 1}$ , one can better isolate or remove the visual attributes associated with part of speech  $i$ . To isolate or kill the attributes we compute the projection onto the column space (i) or orthogonal complements (ii) respectively with
(i)  $\Pi_i(\mathbf{z}) = \mathrm{Exp}_{\mu}(\hat{\mathbf{W}}_i\hat{\mathbf{W}}_i^\top \mathrm{Log}_{\mu}(\mathbf{z}))$
(ii)  $\Pi_i^\perp (\mathbf{z}) = \mathrm{Exp}_{\boldsymbol{\mu}}\big((\mathbf{I}_d - \hat{\mathbf{W}}_i\hat{\mathbf{W}}_i^\top)\mathrm{Log}_{\boldsymbol{\mu}}(\mathbf{z})\big).$  (5)
Projection onto the Euclidean subspaces can be computed using the projection matrices  $\mathbf{P}_i = \mathbf{W}_i\mathbf{W}_i^\top$  and  $\mathbf{P}_i^\perp = \mathbf{I}_d - \mathbf{P}_i$ . The way in which one can extract representations relating to the visual modes of variation for the parts of speech is explored in detail in Section 3.
Here we present both qualitative (Section 3.1) and quantitative (Section 3.2) experiments to validate the model's ability to disentangle the object in a CLIP text or image representation from its appearance. We henceforth use 'subspace' to refer throughout to the manifold generalisation from Section 2.2.
Implementation details For all experiments, we use the following 4 parts of speech: nouns, adjectives, verbs, and adverbs. Our labelled data points (with which we compute the closed-form solution of Equation (4)) for these parts of speech are given by the WordNet [16] database. There are a total of 112219, 18021, 7295, and 3910 text-string data points from each of the categories respectively, after filtering out any word that appears in two or more parts of speech. We set  $\lambda := 1/2$  for all experiments. For all quantitative results, we use the base CLIP ViT-B-32 model. Please see the supplementary material for ablation studies on  $\lambda$  and experiments on additional CLIP architectures.
3.1 Qualitative results
3.1.1 Visual disentanglement
Recent CLIP-based text-to-image models (TTIMs) learn a mapping from the CLIP embeddings to synthetic images depicting visually a text prompt [15, 29, 30]–a process that Menon et al. [13]
Figure 3: The synthetic images from the original CLIP representations (top row) and when first projecting them onto the orthogonal complements of the noun and adjective subspaces to remove the 'content' and 'appearance' component, respectively.
show is prone to also inheriting task bias. We begin in this section by following the experimental protocol of Materzynska et al. [31], using a recent popular CLIP-based TTIM [15] from LAION to demonstrate visually how our PoS subspaces can succeed in predictably isolating visual variation in the CLIP representations pertaining to either the object described in a text prompt or its appearance.
As one motivating example, we first study a curious instance of 'visually polysemous' [14] natural language phrases—terms that have multiple visual associations. In particular, we find that when prompted with artists' names, TTIMs produce both artworks in their style and images of the person themselves (e.g. the top row of Figure 3). That is to say, CLIP entangles in its representation these two dual meanings of the artists—their works' style and their physical appearance. We find that the PoS subspaces offer an intuitive way of reliably separating factors of visual variation in the representations for not just visually polysemous phrases, but also for natural language prompts more generally.
Concretely, projecting onto the orthogonal complement of the noun subspace  $\Pi_{\overline{N}}^{\perp}(\mathbf{z}_T)$  successfully removes from the embedding visual information about the object/content described implicitly in a text prompt  $T$ . For example, as visualised in row 2 of Figure 3 by reliably synthesising just artwork in an artist's style, or by leaving just the snowy or multicoloured appearance-based textures. On the other hand, projecting onto the adjective subspace's orthogonal complement  $\Pi_A^\perp (\mathbf{z}_T)$  removes the visual appearances and styles associated with a text description (row 3 of Figure 3). For the visually polysemous artists' names, this isolates the representations of the artists themselves as humans-removing representations of their artwork styles. For the more complex prompts, this produces images of just the basic object described in the text prompt instead, such as the penguin or New York City. Many more examples of this visual disentanglement, details on the experimental setup, and ablation studies can be found in the supplementary material.
3.1.2 Style-blocking subspace projections
One societal concern with free-form TTIMs is their ability to produce imitation artworks copying the style of artists. Here, we show how the learnt adjective subspace can be used as is as a step towards mitigating this. To achieve this, one simply modifies the TTIM forward pass to first project the CLIP text representations onto the orthogonal complement of the adjective subspace with  $\Pi_A^\perp (\mathbf{z}_T)$  before feeding it into the image generator. We see from the results in Figure 4 that this modification indeed prevents the imitation of the visual styles of a range of artists (even with multiple forms of sentence structure in the prompt), whilst still enabling a diverse set of images to be generated nonetheless.