SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Yong-Hyun Park\*1, Mingi Kwon\*2, Jaewoong Choi3, Junghyo Jo1, Youngjung Uh12,
$^{1}$ Seoul National University  $^{2}$ Yonsei University  $^{3}$ Korea Institute for Advanced Study
Despite the success of diffusion models (DMs), we still lack a thorough understanding of their latent space. To understand the latent space  $\mathbf{x}_t \in \mathcal{X}$ , we analyze them from a geometrical perspective. Our approach involves deriving the local latent basis within  $\mathcal{X}$  by leveraging the pullback metric associated with their encoding feature maps. Remarkably, our discovered local latent basis enables image editing capabilities by moving  $\mathbf{x}_t$ , the latent space of DMs, along the basis vector at specific timesteps. We further analyze how the geometric structure of DMs evolves over diffusion timesteps and differs across different text conditions. This confirms the known phenomenon of coarse-to-fine generation, as well as reveals novel insights such as the discrepancy between  $\mathbf{x}_t$  across timesteps, the effect of dataset complexity, and the time-varying influence of text prompts. To the best of our knowledge, this paper is the first to present image editing through  $\mathbf{x}$ -space traversal, editing only once at specific timestep  $t$  without any additional training, and providing thorough analyses of the latent structure of DMs. The code to reproduce our experiments can be found at https://github.com/enkeejunior1/Diffusion-Pullback.
The diffusion models (DMs) are powerful generative models that have demonstrated impressive performance [22, 51, 52, 17, 37]. DMs have shown remarkable applications, including text-to-image synthesis [45, 46, 5, 36], inverse problems [14, 31], and image editing [21, 54, 39, 35].
Despite their achievements, the research community lacks a comprehensive understanding of the latent space of DMs and its influence on the generated results. So far, the completely diffused images are considered as latent variables but it does not have useful properties for controlling the results. For example, traversing along a direction from a latent produces weird changes in the results. Fortunately, Kwon et al. [26] consider the intermediate feature space of the diffusion kernel, referred to as  $\mathcal{H}$ , as a semantic latent space and show its usefulness on controlling generated images. In the similar sense, some works investigate the feature maps of the self-attention or cross-attention operations for controlling the results [21, 54, 39], improving sample quality [8], or downstream tasks such as semantic segmentation [32, 55].
Still, the structure of the space  $\mathcal{X}_t$  where latent variables  $\{\mathbf{x}_t\}$  live remains unexplored despite its crucial role in understanding DMs. It is especially challenging because 1) the model is trained to estimate the forward noise which does not depend on the input, as opposed to other typical supervisions such as classification or similarity, and 2) there are lots of latent variables over multiple recursive timesteps. In this paper, we aim to analyze  $\mathcal{X}$  in conjunction with its corresponding
*Equal Contribution † Corresponding authors
(a) Conceptual illustration
Figure 1: Conceptual illustration of local geometric structure. (a) The local basis  $\{\mathbf{v}_1, \mathbf{v}_2, \dots\}$  of the local latent subspace  $\mathcal{T}_{\mathbf{x}_t}$  within the latent space  $\mathcal{X}$  is interlinked with the local basis  $\{\mathbf{u}_1, \mathbf{u}_2, \dots\}$  of the local tangent space  $\mathcal{T}_{\mathbf{h}_t}$  in the feature space  $\mathcal{H}$ . (b) The derivation of these local bases is facilitated through the singular value decomposition (SVD) of the Jacobian, which emanates from the U-Net responsible for encoding the feature map  $f$ , linking  $\mathcal{X}$  and  $\mathcal{H}$ .
(b) Notation
representation  $\mathcal{H}$ , by incorporating a local geometry to  $\mathcal{X}$  using the concept of a pullback metric in Riemannian geometry.
First, we discover the local latent basis for  $\mathcal{X}$  and the corresponding local tangent basis for  $\mathcal{H}$ . The local basis is obtained by performing singular value decomposition (SVD) of the Jacobian of the mapping from  $\mathcal{X}$  to  $\mathcal{H}$ . To validate the discovered local latent basis, we demonstrate that walking along the basis can edit real images in a semantically meaningful way. Furthermore, we can use the discovered local latent basis vector to edit other samples by using parallel transport, when they exhibit comparable local geometric structures. Note that existing editing methods manipulate the self-attention map or cross-attention map over multiple timesteps [21, 54, 39]. On the other hand, we manipulate only  $\mathbf{x}_t$  once at a specific timestep.
Second, we investigate how the latent structures differ across different timesteps and samples as follows. The frequency domain of the local latent basis shifts from low-frequency to high-frequency along the generative process. We explicitly confirm it using power spectral density analysis. The difference between local tangent spaces of different samples becomes larger along the generative process. The local tangent spaces at various diffusion timesteps are similar to each other if the model is trained on aligned datasets such as CelebA-HQ or Flowers. However, this homogeneity does not occur on complex datasets such as ImageNet.
Finally, we examine how the prompts affect the latent structure of text-to-image DMs as follows. Similar prompts yield similar latent structures. Specifically, we find a positive correlation between the similarity of prompts and the similarity of local tangent spaces. The influence of text on the local tangent space becomes weaker along the generative process.
Our work examines the geometry of  $\mathcal{X}$  and  $\mathcal{H}$  using Riemannian geometry. We discover the latent structure of  $\mathcal{X}$  and how it evolves during the generative process and is influenced by prompts. This geometric exploration deepens our understanding of DMs.
Diffusion Models. Recent advances in DMs make great progress in the field of image synthesis and show state-of-the-art performance [50, 22, 51]. An important subject in the diffusion model is the introduction of gradient guidance, including classifier-free guidance, to control the generative process [17, 47, 4, 30, 36, 46]. The work by Song et al. [52] has facilitated the unification of DMs with score-based models using SDEs, enhancing our understanding of DMs as a reverse diffusion process. However, the latent space is still largely unexplored, and our understanding is limited.
The study of latent space in GANs. The study of latent spaces has gained significant attention in recent years. In the field of Generative Adversarial Networks (GANs), researchers have proposed various methods to manipulate the latent space to achieve the desired effect in the generated images
[44, 41, 1, 20, 49, 59, 38]. More recently, several studies [60, 10] have examined the geometrical properties of latent space in GANs and utilized these findings for image manipulations. These studies bring the advantage of better understanding the characteristics of the latent space and facilitating the analysis and utilization of GANs. In contrast, the latent space of DMs remains poorly understood, making it difficult to fully utilize their capabilities.
Image manipulation in DMs. Early works include Choi et al. [11] and Meng et al. [33] have attempted to manipulate the resulting images of DMs by replacing latent variables, allowing the generation of desired random images. However, due to the lack of semantics in the latent variables of DMs, current approaches have critical problems with semantic image editing. Alternative approaches have explored the potential of using the feature space within the U-Net for semantic image manipulation. For example, Kwon et al. [26] have shown that the bottleneck of the U-Net,  $\mathcal{H}$ , can be used as a semantic latent space. Specifically, they used CLIP [43] to identify directions within  $\mathcal{H}$  that facilitate genuine image editing. Baranchuk et al. [6] and Tumanyan et al. [54] use the feature map of the U-Net for semantic segmentation and maintaining the structure of generated images. Unlike previous works, our editing method finds the editing direction without supervision, and directly traverses the latent variable along the latent basis.
Riemannain Geometry. Some studies have applied Riemannian geometry to analyze the latent spaces of deep generative models, such as Variational Autoencoders (VAEs) and GANs [2, 48, 9, 3, 27, 28, 57]. Shao et al. [48] proposed a pullback metric on the latent space from image space Euclidean metric to analyze the latent space's geometry. This method has been widely used in VAEs and GANs because it only requires a differentiable map from latent space to image space. However, no studies have investigated the geometry of latent space of DMs utilizing the pullback metric.
3 Discovering the latent basis of DMs
In this section, we explain how to extract a latent structure of  $\mathcal{X}$  using differential geometry. First, we introduce a key concept in our method: the pullback metric. Next, by adopting the local Euclidean metric of  $\mathcal{H}$  and utilizing the pullback metric, we discover the local latent basis of the  $\mathcal{X}$ . Moreover, although the direction we found is local, we show how it can be applied to other samples via parallel transport. Finally, we introduce  $\mathbf{x}$ -space guidance for editing data in the  $\mathcal{X}$  to enhance the quality of image generation.
3.1 Pullback metric
We consider a curved manifold,  $\mathcal{X}$ , where our latent variables  $\mathbf{x}_t$  exist. The differential geometry represents  $\mathcal{X}$  through patches of tangent spaces,  $\mathcal{T}_{\mathbf{x}}$ , which are vector spaces defined at each point  $\mathbf{x}$ . Then, all the geometrical properties of  $\mathcal{X}$  can be obtained from the inner product of  $\|d\mathbf{x}\|^2 = \langle d\mathbf{x}, d\mathbf{x} \rangle_{\mathbf{x}}$  in  $\mathcal{T}_{\mathbf{x}}$ . However, we do not have any knowledge of  $\langle d\mathbf{x}, d\mathbf{x} \rangle_{\mathbf{x}}$ . It is definitely not a Euclidean metric. Furthermore, samples of  $\mathbf{x}_t$  at intermediate timesteps of DMs include inevitable noise, which prevents finding semantic directions in  $\mathcal{T}_{\mathbf{x}}$ .
Fortunately, Kwon et al. [26] observed that  $\mathcal{H}$ , defined by the bottleneck layer of the U-Net, exhibits local linear structure. This allows us to adopt the Euclidean metric on  $\mathcal{H}$ . In differential geometry, when a metric is not available on a space, pullback metric is used. If a smooth map exists between the original metric-unavailable domain and a metric-available codomain, the pullback metric is used to measure the distances in the domain space. Our idea is to use the pullback Euclidean metric on  $\mathcal{H}$  to define the distances between the samples in  $\mathcal{X}$ .
DMs are trained to infer the noise  $\epsilon_{t}$  from a latent variable  $\mathbf{x}_t$  at each diffusion timestep  $t$ . Each  $\mathbf{x}_t$  has a different internal representation  $\mathbf{h}_t$ , the bottleneck representation of the U-Net, at different  $t$ 's. The differentiable map between  $\mathcal{X}$  and  $\mathcal{H}$  is denoted as  $f: \mathcal{X} \to \mathcal{H}$ . Hereafter, we refer to  $\mathbf{x}_t$  as  $\mathbf{x}$  for brevity unless it causes confusion. It is important to note that our method can be applied at any timestep in the denoising process. We consider a linear map,  $\mathcal{T}_{\mathbf{x}} \rightarrow \mathcal{T}_{\mathbf{h}}$ , between the domain and codomain tangent spaces. This linear map can be described by the Jacobian  $J_{\mathbf{x}} = \nabla_{\mathbf{x}}\mathbf{h}$  which determines how a vector  $\mathbf{v} \in \mathcal{T}_{\mathbf{x}}$  is mapped into a vector  $\mathbf{u} \in \mathcal{T}_{\mathbf{h}}$  by  $\mathbf{u} = J_{\mathbf{x}}\mathbf{v}$ .
Using the local linearity of  $\mathcal{H}$ , we assume the metric,  $||d\mathbf{h}||^2 = \langle d\mathbf{h}, d\mathbf{h} \rangle_{\mathbf{h}} = d\mathbf{h}^\top d\mathbf{h}$  as a usual dot product defined in the Euclidean space. To assign a geometric structure to  $\mathcal{X}$ , we use the pullback
Figure 2: Image editing with the discovered latent basis. (a) Schematic depiction of our image editing procedure.  $①$  An input image is subjected to DDIM inversion, resulting in an initial noisy sample  $\mathbf{x}_T$ .  $②$  The sample  $\mathbf{x}_T$  is progressively denoised until reaching the point  $t$  through DDIM generation.  $③$  Subsequently, the local latent basis  $\{\mathbf{v}_1,\dots ,\mathbf{v}_n\}$  is identified by using the pullback metric.  $④$  This enables the manipulation of the sample  $\mathbf{x}_t$  along one of the basis vectors using  $\mathbf{x}$ -space guidance.  $⑤$  The DDIM generation concludes with the progression from the modified latent variable  $\tilde{\mathbf{x}}_t$ . (b) Examples of edited images using a selected basis vector. The latent basis vector could be conditioned on prompts and it facilitates text-aligned manipulations.
metric of the corresponding  $\mathcal{H}$ . In other words, the norm of  $\mathbf{v} \in T_{\mathbf{x}}$  is measured by the norm of corresponding codomain tangent vector:
$$
\left\| \mathbf {v} \right\| _ {\mathrm {p b}} ^ {2} \triangleq \langle \mathbf {u}, \mathbf {u} \rangle_ {\mathbf {h}} = \mathbf {v} ^ {\top} J _ {\mathbf {x}} ^ {\top} J _ {\mathbf {x}} \mathbf {v}. \tag {1}
$$
3.2 Finding local latent basis
Using the pullback metric, we define the local latent vector  $\mathbf{v} \in \mathcal{T}_{\mathbf{x}}$  that shows a large variability in  $\mathcal{T}_{\mathbf{h}}$ . We find a unit vector  $\mathbf{v}_1$  that maximizes  $||\mathbf{v}||_{\mathrm{pb}}^2$ . By maximizing  $||\mathbf{v}||_{\mathrm{pb}}^2$  while remaining orthogonal to  $\mathbf{v}_1$ , one can obtain the second unit vector  $\mathbf{v}_2$ . This process can be repeated to have  $n$  latent directions of  $\{\mathbf{v}_1, \mathbf{v}_2, \dots, \mathbf{v}_n\}$  in  $\mathcal{T}_{\mathbf{x}}$ . In practice,  $\mathbf{v}_i$  corresponds to the  $i$ -th right singular vector from the singular value decomposition (SVD) of  $J_{\mathbf{x}} = U\Lambda V^{\top}$ , i.e.,  $J_{\mathbf{x}}\mathbf{v}_i = \Lambda_i\mathbf{u}_i$ . Since the Jacobian of too many parameters is not tractable, we use a power method [18, 34, 19] to approximate the SVD of  $J_{\mathbf{x}}$  (See Appendix D for the time complexity and Appendix F for the detailed algorithm).
Henceforth, we refer to  $\mathcal{T}_{\mathbf{x}}$  as a local latent subspace, and  $\{\mathbf{v}_1,\mathbf{v}_2,\dots ,\mathbf{v}_n\}$  as the corresponding local latent basis.
$$
\mathcal {T} _ {\mathbf {x}} \triangleq \operatorname {s p a n} \left\{\mathbf {v} _ {1}, \mathbf {v} _ {2} \dots , \mathbf {v} _ {n} \right\}, \text {w h e r e} \mathbf {v} _ {i} \text {i s} i \text {- t h r i g h t s i n g u l a r v e c t o r f o r} J _ {\mathbf {x}}. \tag {2}
$$
Using the linear transformation between  $\mathcal{T}_{\mathbf{x}}$  and  $\mathcal{T}_{\mathbf{h}}$  via the Jacobian  $J_{\mathbf{x}}$ , one can also obtain corresponding directions in  $\mathcal{T}_{\mathbf{h}}$ . In practice,  $\mathbf{u}_i$  corresponds to the  $i$ -th left singular vector from the SVD of  $J_{\mathbf{x}}$ . After selecting the top  $n$  (e.g.,  $n = 50$ ) directions of large eigenvalues, we can approximate any vector in  $\mathcal{T}_{\mathbf{h}}$  with a finite basis,  $\{\mathbf{u}_1, \mathbf{u}_2, \dots, \mathbf{u}_n\}$ . When we refer to a local tangent space henceforth, it means the  $n$ -dimensional low-rank approximation of the original tangent space.
$$
\mathcal {T} _ {\mathrm {h}} \triangleq \operatorname {s p a n} \left\{\mathbf {u} _ {1}, \mathbf {u} _ {2} \dots , \mathbf {u} _ {n} \right\}, \text {w h e r e} \mathbf {u} _ {i} \text {i s t h e i - t h l e s i n g u l a r v e c t o r o f} J _ {\mathbf {x}}. \tag {3}
$$
The collection of local latent basis vectors,  $\{\mathbf{v}_1,\mathbf{v}_2,\dots ,\mathbf{v}_n\}$ , obtained through our proposed method, can be interpreted as a signal that the model is highly response to for a given  $\mathbf{x}$ . On the other hand, the basis of the local tangent space, denoted as  $\{\mathbf{u}_1,\mathbf{u}_2\dots ,\mathbf{u}_n\}$ , can be viewed as the corresponding representation associated with the signal.
In Stable Diffusion, the prompt also influences the Jacobian, which means that the local basis also depends on it. We can utilize any prompt to obtain a local latent basis, and different prompts create distinct geometrical structures. For the sake of brevity, we will omit the word local unless it leads to confusion.
3.3 Generating edited images with x-space guidance
A naive approach for manipulating a latent variable  $\mathbf{x}$  using a latent vector  $\mathbf{v}$  is through simple addition, specifically  $\mathbf{x} + \gamma \mathbf{v}$ . However, using the naive approach sometime leads to noisy image
generation. To address this issue, instead of directly using the basis for manipulation, we use a basis vector that has passed through the decoder once for manipulation. The  $\mathbf{x}$ -space guidance is defined as follows
$$
\tilde {\mathbf {x}} _ {\mathrm {X G}} = \mathbf {x} + \gamma \left[ \epsilon_ {\theta} (\mathbf {x} + \mathbf {v}) - \epsilon_ {\theta} (\mathbf {x}) \right] \tag {4}
$$
where  $\gamma$  is a hyper-parameter controlling the strength of editing and  $\epsilon_{\theta}$  is a diffusion model. Equation 4 is inspired by classifier-free guidance, but the key difference is that it is directly applied in the latent space  $\mathcal{X}$ . Our  $\mathbf{x}$ -space guidance provides qualitatively similar results to direct addition, while it shows better fidelity. (See Appendix C for ablation study.)
3.4 The overall process of image editing
In this section, we summarize the entire editing process with five steps: 1) The input image is inverted into initial noise  $\mathbf{x}_T$  using DDIM inversion. 2)  $\mathbf{x}_T$  is gradually denoised until  $t$  through DDIM generation. 3) Identify local latent basis  $\{\mathbf{v}_1,\dots ,\mathbf{v}_n\}$  using the pullback metric at  $t$ . 4) Manipulate  $\mathbf{x}_t$  along the one of basis vectors using the  $\mathbf{x}$ -space guidance. 5) The DDIM generation is then completed with the modified latent variable  $\tilde{\mathbf{x}}_t$ . Figure 2 illustrates the entire editing process.
In the context of a text-to-image model, such as Stable Diffusion, it becomes possible to include textual conditions while deriving local basis vectors. Although we do not use any text guidance during DDIM inversion and generation, a local basis with text conditions enables semantic editing that matches the given text. Comprehensive experiments can be found in Section 4.1.
It is noteworthy that our approach needs no extra training and simplifies image editing by only adjusting the latent variable within a single timestep.
3.5 Editing various samples with parallel transport
Let us consider a scenario where our aim is to edit ten images, changing straight hair to curly hair. Due to the nature of the unsupervised image editing method, it is becomes imperative to manually check the semantic relevance of the latent basis vector in the edited results. Thus, to edit every samples, we have to manually find a straight-to-curly basis vector for individual samples.
One way to alleviate this tedious task is to apply the curly hair vector obtained from one image to other images. However, the basis vector  $\mathbf{v} \in \mathcal{T}_{\mathbf{x}}$  obtained at  $\mathbf{x}$  cannot be used for the other sample  $\mathbf{x}'$  because  $\mathbf{v} \notin \mathcal{T}_{\mathbf{x}'}$ . Thus, in order to apply the direction we obtained to another sample, it is necessary to relocate the extracted direction to a new tangent space.
To achieve this, we use parallel transport that moves  $\mathbf{v}_i$  onto the new tangent space  $\mathcal{T}_{\mathbf{x}'}$ . In the realm of differential geometry, parallel transport is a technique to relocate a tangent vector to different position with minimal direction change, while keeping the vector tangent on the manifold [48]. It is notable that in curved space, parallel transport can significantly modify the original vector. Therefore, it is beneficial to apply the parallel transport in  $\mathcal{H}$ , where relatively flatter than  $\mathcal{X}$ .
The process of moving a tangent vector  $\mathbf{v} \in \mathcal{T}_{\mathbf{x}}$  to  $\mathbf{v}' \in \mathcal{T}_{\mathbf{x}'}$  through parallel transport in  $\mathcal{H}$  is summarized as follows. First, we convert the latent direction  $\mathbf{v}_i \in \mathcal{T}_{\mathbf{x}}$  to the corresponding direction of  $\mathbf{u}_i \in \mathcal{T}_{\mathbf{h}}$ . Second, we apply the parallel transport  $\mathbf{u}_i \in \mathcal{T}_{\mathbf{h}}$  to  $\mathbf{u}'_i \in \mathcal{T}_{\mathbf{h}'}$ , where  $\mathbf{h}' = f(\mathbf{x}')$ . In the general case, parallel transport involves iterative projection and normalization on the tangent space along the path connecting two points [48]. However, in our case, we assume that  $\mathcal{H}$  has Euclidean geometry. Therefore, we move  $\mathbf{u}$  directly onto  $\mathcal{T}_{\mathbf{h}'}$  through projection, without the need for an iterative process. Finally, transform  $\mathbf{u}_i'$  into  $\mathbf{v}_i' \in \mathcal{X}$ . Using this parallel transport of  $\mathbf{v}_i \rightarrow \mathbf{v}_i'$  via  $\mathcal{H}$ , we can apply the local latent basis obtained from  $\mathbf{x}$  to edit or modify the input  $\mathbf{x}'$ .
4 Findings and results
In this section, we analyze the geometric structure of DMs with our method. § 4.1 demonstrates that the latent basis found by our method can be used for image editing. In § 4.2, we investigate how the geometric structure of DMs evolves as the generative process progresses. Lastly, in § 4.3, we examine how the geometric properties of the text-condition model change with a given text.
The implementation details of our work are provided on Appendix B. The source code for our experiments can be found at https://github.com/enkeejunior1/Diffusion-Pullback.
Figure 3: Examples of image edition using the latent basis. The attributes are manually interpreted since the editing directions are not intentionally supervised. For Stable Diffusion, we used an empty string as a prompt. Each column represents edits made at different diffusion timesteps (0.6T, 0.8T, and  $T$  for the unconditional diffusion model; 0.6T and 0.7T for Stable Diffusion).
Figure 4: Examples of image edition using top-3 latent basis vectors. Each column is edited using a different latent vector  $\{\mathbf{v}_1, \mathbf{v}_2, \mathbf{v}_3\}$ . Each group of columns represents edits made at different diffusion timesteps (0.6T and 0.7T). Notably, when given the "Lion" prompt, it is evident that all the top latent basis vectors align with the direction of the prompt.
4.1 Image editing with the latent basis
In this subsection, we demonstrate the capability of our discovered latent basis for image editing. To extract the latent variables from real images for editing purposes, we use DDIM inversion. In experiments with Stable Diffusion (SD), we do not use guidance, i.e., unconditional sampling, for both DDIM inversion and DDIM sampling processes. This ensures that our editing results solely depend on the latent variable, not on other factors such as prompt conditions.
Figure 5: Examples of image edition using latent basis vectors discovered with various prompts. Each column is edited using the latent basis vector obtained from a different text prompt. Importantly, our method employs each prompt only once to derive the local latent basis.
"Running dog"
"Running cat"
" Sitting dog "