SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Manuel Brack $^{1,2}$  Felix Friedrich $^{2,3}$  Dominik Hintersdorf $^{2}$  Lukas Struppek $^{2}$  Patrick Schramowski $^{1,2,3,4}$  Kristian Kersting $^{1,2,3,5}$
$^{1}$ German Research Center for Artificial Intelligence (DFKI),
$^{2}$ Computer Science Department, TU Darmstadt  $^{3}$ Hessian.AI,
$^{4}$ LAION,  $^{5}$ Centre for Cognitive Science, TU Darmstadt
brack@cs.tu-darmstadt.de
Text-to-image diffusion models have recently received a lot of interest for their astonishing ability to produce high-fidelity images from text only. However, achieving one-shot generation that aligns with the user's intent is nearly impossible, yet small changes to the input prompt often result in very different images. This leaves the user with little semantic control. To put the user in control, we show how to interact with the diffusion process to flexibly steer it along semantic directions.
This semantic guidance (SEGA) generalizes to any generative architecture using classifier-free guidance. More importantly, it allows for subtle and extensive edits, changes in composition and style, as well as optimizing the overall artistic conception. We demonstrate SEGA's effectiveness on both latent and pixel-based diffusion models such as Stable Diffusion, Paella and DeepFloyd-IF using a variety of tasks, thus providing strong evidence for its versatility, flexibility and improvements over existing methods<sup>1</sup>.
The recent popularity of text-to-image diffusion models (DMs) [28, 25, 27] can largely be attributed to their versatility, expressiveness, and—most importantly—the intuitive interface they provide to users. The generation's intent can easily be expressed in natural language, with the model producing faithful interpretations of a text prompt. Despite the impressive capabilities of these models, the initially generated images are rarely of high quality. Accordingly, a human user will likely be unsatisfied with certain aspects of the initial image, which they will attempt to improve over multiple iterations. Unfortunately, the diffusion process is rather fragile as small changes to the input prompt lead to entirely different images. Consequently, fine-grained semantic control over the generation process is necessary, which should be as easy and versatile to use as the initial generation.
Previous attempts to influence dedicated concepts during the generation process require additional segmentation masks, extensions to the architecture, model fine-tuning, or embedding optimization [1, 8, 13, 35]. While these techniques produce satisfactory results, they disrupt the fast, exploratory workflow that is the strong suit of diffusion models in the first place. We propose Semantic Guidance (SEGA) to uncover and interact with semantic directions inherent to the model. SEGA requires no additional training, no extensions to the architecture, nor external guidance and is calculated within a single forward pass. We demonstrate that this semantic control can be inferred from simple textual descriptions using the model's noise estimate alone. With this, we also refute previous research
<sup>1</sup>Implementation available in diffusers: https://huggingface.co/docs/diffusers/api/pipelines/semantic_stable_diffusion
(a) A (latent) diffusion process inherently organizes concepts and learns implicitly relationships between them, although there is no supervision.
(b) Guidance arithmetic: Guiding the image 'a portrait of a king' (left) using 'king'-'male' + female' results in an image of a 'queen' (right).
Figure 1: Semantic guidance (SEGA) applied to the image 'a portrait of a king' (Best viewed in color)
claiming these estimates to be unsuitable for semantic control [15]. The guidance directions uncovered with SEGA are robust, scale monotonically, and are largely isolated. This enables simultaneous applications of subtle edits to images, changes in composition and style, as well as optimizing the artistic conception. Furthermore, SEGA allows for probing the latent space of diffusion models to gain insights into how abstract concepts are represented by the model and how their interpretation reflects on the generated image. Additionally, SEGA is architecture-agnostic and compatible with various generative models, including latent[27, 26] and pixel-based diffusion[28].
In this paper, we establish the methodical benefits of SEGA and demonstrate that this intuitive, lightweight approach offers sophisticated semantic control over image generations. Specifically, we contribute by (i) devising a formal definition of Semantic Guidance and discussing the numerical intuition of the corresponding semantic space, (ii) demonstrating the robustness, uniqueness, monotonicity, and isolation of semantic vectors, (iii) providing an exhaustive empirical evaluation of SEGA's semantic control, and (iv) showcasing the benefits of SEGA over related methods.
Semantic Dimensions. Research on expressive semantic vectors that allow for meaningful interpolation and arithmetic pre-date generative diffusion models. Addition and subtraction on text embeddings such as word2vec [18, 19] have been shown to reflect semantic and linguistic relationships in natural language [20, 34]. One of the most prominent examples is that the vector representation of 'King - male + female' is very close to 'Queen'. SEGA enables similar arithmetic for image generation with diffusion (cf. Fig. 1b). StyleGANs [11, 12] also contains inherent semantic dimensions that can be utilized during generation. For example, Patashnik et al. [22] combined these models with CLIP [24] to offer limited textual control over generated attributes. However, training StyleGANs at scale with subsequent fine-tuning is notoriously fragile due to the challenging balance between reconstruction and adversarial loss. Yet, large-scale pre-training is the base of flexible and capable generative models [23].
Image Diffusion. Recently, large-scale, text-guided DMs have enabled a more versatile approach for image generation [28, 25, 2]. Especially latent diffusion models [27] have been gaining much attention. These models perform the diffusion process on a compressed space perceptually equivalent to the image space. For one, this approach reduces computational requirements. Additionally, the latent representations can be utilized for other downstream applications [6, 17].
Image Editing. While these models produce astonishing, high-quality images, fine-grained control over this process remains challenging. Minor changes to the text prompt often lead to entirely different images. One approach to tackle this issue is inpainting, where the user provides additionally semantic masks to restrict changes to certain areas of the image [1, 21]. Other methods involve computationally expensive fine-tuning of the model to condition it on the source image before applying edits [13, 33]. In contrast, SEGA performs edits on the relevant image regions through text descriptions alone and requires no tuning.
Semantic Control. Other works have explored more semantically grounded approaches for interacting with image generation. Prompt-to-Prompt utilizes the semantics of the model's cross-attention layers that attribute pixels to tokens from the text prompt [8]. Dedicated operations on the cross
attention maps enable various changes to the generated image. On the other hand, SEGA does not require token-based conditioning and allows for combinations of multiple semantic changes. Wu et al. [35] studied the disentanglement of concepts for DMs using linear combinations of text embeddings. However, for each text prompt and target concept, a dedicated combination must be inferred through optimization. Moreover, the approach only works for more substantial changes to an image and fails for small edits. SEGA, in contrast, is capable of performing such edits without optimization.
Noise-Estimate Manipulation. Our work is closely related to previous research working directly on the noise estimates of DMs. Liu et al. [16] combine multiple estimates to facilitate changes in image composition. However, more subtle semantic changes to an image remain unfeasible with this method. In fact, Kwon et al. [15] argue that the noise-estimate space of DMs is unsuited for semantic manipulation of the image. Instead, they use a learned mapping function on changes to the bottleneck of the underlying U-Net. This approach enables various manipulations that preserve the original image quality. However, it does not allow for arbitrary spontaneous edits of the image, as each editing concept requires minutes of training. SEGA, in comparison, requires no extension to the architecture and produces semantic vectors ad-hoc for any textual prompt. Lastly, Safe Latent Diffusion (SLD) uses targeted manipulation of the noise estimate to suppress the generation of inappropriate content [29]. Instead of arbitrary changes to an image, SLD prevents one dedicated concept from being generated. Additionally, SLD is complex, and the hyperparameter formulation can be improved through a deeper understanding of the numerical properties of DMs' noise estimate space.
3 Semantic Guidance
Let us now devise Semantic Guidance for diffusion models.
3.1 Guided Diffusion
The first step towards SEGA is guided diffusion. Specifically, diffusion models (DM) iteratively denoise a Gaussian distributed variable to produce samples of a learned data distribution. For text-to-image generation, the model is conditioned on a text prompt  $p$  and guided toward an image faithful to that prompt. The training objective of a DM  $\hat{x}_{\theta}$  can be written as
$$
\mathbb {E} _ {\mathbf {x}, \mathbf {c} _ {p}, \epsilon , t} \left[ w _ {t} | | \hat {\mathbf {x}} _ {\theta} \left(\alpha_ {t} \mathbf {x} + \omega_ {t} \epsilon , \mathbf {c} _ {p}\right) - \mathbf {x} | | _ {2} ^ {2} \right] \tag {1}
$$
where  $(\mathbf{x},\mathbf{c}_p)$  is conditioned on text prompt  $p$ ,  $t$  is drawn from a uniform distribution  $t\sim \mathcal{U}([0,1])$ ,  $\epsilon$  sampled from a Gaussian  $\epsilon \sim \mathcal{N}(0,\mathbf{I})$ , and  $w_{t},\omega_{t},\alpha_{t}$  influence the image fidelity depending on  $t$ . Consequently, the DM is trained to denoise  $\mathbf{z}_t\coloneqq \mathbf{x} + \epsilon$  yielding  $\mathbf{x}$  with the squared error loss. At inference, the DM is sampled using the prediction of  $\mathbf{x} = (\mathbf{z}_t - \tilde{\epsilon}_{\theta})$ , with  $\tilde{\epsilon}_{\theta}$  as described below.
Classifier-free guidance [10] is a conditioning method using a purely generative diffusion model, eliminating the need for an additional pre-trained classifier. During training, the text conditioning  $\mathbf{c}_p$  drops randomly with a fixed probability, resulting in a joint model for unconditional and conditional objectives. During inference, the score estimates for the  $\mathbf{x}$ -prediction are adjusted so that:
with guidance scale  $s_g$  and  $\epsilon_{\theta}$  defining the noise estimate with parameters  $\theta$ . Intuitively, the unconditioned  $\epsilon$ -prediction is pushed in the direction of the conditioned one, with  $s_g$  determining the extent of the adjustment.
3.2 Semantic Guidance on Concepts
We introduce SEGA to influence the diffusion process along several directions. To this end, we substantially extend the principles introduced in classifier-free guidance by solely interacting with the concepts already present in the model's latent space. Therefore, SEGA requires no additional training, no extensions to the architecture, and no external guidance. Instead, it is calculated during the existing diffusion iteration. More specifically, SEGA uses multiple textual descriptions  $e_i$ , representing the given target concepts of the generated image, in addition to the text prompt  $p$ .
Intuition. The overall idea of SEGA is best explained using a 2D abstraction of the high dimensional  $\epsilon$ -space, as shown in Fig. 1. Intuitively, we can understand the space as a composition of arbitrary
sub-spaces representing semantic concepts. Let us consider the example of generating an image of a king. The unconditioned noise estimate (black dot) starts at some random point in the  $\epsilon$ -space without semantic grounding. The guidance corresponding to the prompt "a portrait of a king" represents a vector (blue vector) moving us into a portion of  $\epsilon$ -space where the concepts 'male' and royal overlap, resulting in an image of a king. We can now further manipulate the generation process using SEGA. From the unconditioned starting point, we get the directions of 'male' and 'female' (orange/green lines) using estimates conditioned on the respective prompts. If we subtract this inferred 'male' direction from our prompt guidance and add the 'female' one, we now reach a point in the  $\epsilon$ -space at the intersection of the 'royal' and 'female' sub-spaces, i.e., a queen. This vector represents the final direction (red vector) resulting from semantic guidance.
Isolating Semantics in Diffusion. Next, we investigate the actual noise-estimate space of DMs on the example of Stable Diffusion (SD). This enables extracting semantic concepts from within that space and applying them during image generation.
Numerical values of  $\epsilon$ -estimates are generally Gaussian distributed. While the value in each dimension of the latent vector can differ significantly between seeds, text prompts, and diffusion steps, the overall distribution always remains similar to a Gaussian distribution (cf. App. B). Using the arithmetic principles of classifier-free guidance, we can now identify those dimensions of a latent vector encoding an arbitrary semantic concept. To that end, we calculate the noise estimate  $\epsilon_{\theta}(\mathbf{z}_t,\mathbf{c}_e)$ , which is conditioned on a concept description  $e$ . We then take the difference between  $\epsilon_{\theta}(\mathbf{z}_t,\mathbf{c}_e)$  and the unconditioned estimate  $\epsilon_{\theta}(\mathbf{z}_t)$  and scale it. Again, the numerical values of the resulting latent vector are Gaussian distributed, as shown in Fig. 2. We will demonstrate that those latent dimensions falling into the upper and lower tail of the distribution alone encode the target concept. We empirically determined that using only  $1 - 5\%$  of the  $\epsilon$ -estimate's dimensions is sufficient to apply the desired changes to an image. Consequently, the resulting concept vectors are largely isolated; thus, multiple ones can be applied simultaneously without interference (cf. Sec. 4).
Figure 2: Numerical intuition of semantic guidance. The difference between the concept-conditioned and unconditioned estimates is first scaled. Subsequently, the tail values represent the dimensions of the specified concept. Distribution plots calculated using kernel-density estimates with Gaussian smoothing.
We subsequently refer to the space of these sparse noise-estimate vectors as semantic space.
One Direction. Let us formally define the intuition for SEGA by starting with a single direction, i.e., editing prompt. Again, we use three  $\epsilon$ -predictions to move the unconditioned score estimate  $\epsilon_{\theta}(\mathbf{z}_t)$  towards the prompt conditioned estimate  $\epsilon_{\theta}(\mathbf{z}_t,\mathbf{c}_p)$  and simultaneously away/towards the concept conditioned estimate  $\epsilon_{\theta}(\mathbf{z}_t,\mathbf{c}_e)$ , depending on the editing direction. Formally, we compute  $\overline{\epsilon}_{\theta}(\mathbf{z}_t,\mathbf{c}_p,\mathbf{c}_e) =$
$$
\epsilon_ {\theta} \left(\mathbf {z} _ {t}\right) + s _ {g} \left(\epsilon_ {\theta} \left(\mathbf {z} _ {t}, \mathbf {c} _ {p}\right) - \epsilon_ {\theta} \left(\mathbf {z} _ {t}\right)\right) + \gamma \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) \tag {3}
$$
with the semantic guidance term  $\gamma$
$$
\gamma \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) = \mu (\psi ; s _ {e}, \lambda) \psi \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) \tag {4}
$$
where  $\mu$  applies an edit guidance scale  $s_e$  elementwise, and  $\psi$  depends on the edit direction:
$$
\psi \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) = \left\{ \begin{array}{l l} \epsilon_ {\theta} \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) - \epsilon_ {\theta} \left(\mathbf {z} _ {t}\right) & \text {i f p o s . g u i d a n c e} \\ - \left(\epsilon_ {\theta} \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) - \epsilon_ {\theta} \left(\mathbf {z} _ {t}\right)\right) & \text {i f n e g . g u i d a n c e} \end{array} \right. \tag {5}
$$
Thus, changing the guidance direction is reflected by the direction between  $\epsilon_{\theta}(\mathbf{z}_t,\mathbf{c}_e)$  and  $\epsilon_{\theta}(\mathbf{z}_t)$ .
The term  $\mu$  (Eq. 4) considers those dimensions of the prompt conditioned estimate relevant to the defined editing prompt  $e$ . To this end,  $\mu$  takes the largest absolute values of the difference between the unconditioned and concept-conditioned estimates. This corresponds to the upper and lower tail of the numerical distribution as defined by percentile threshold  $\lambda$ . All values in the tails are scaled by an edit scaling factor  $s_e$ , with everything else being set to 0, such that
$$
\mu (\psi ; s _ {e}, \lambda) = \left\{ \begin{array}{l l} s _ {e} & \text {w h e r e} | \psi | \geq \eta_ {\lambda} (| \psi |) \\ 0 & \text {o t h e r w i s e} \end{array} \right. \tag {6}
$$
where  $\eta_{\lambda}(\psi)$  is the  $\lambda$ -th percentile of  $\psi$ . Consequently, a larger  $s_e$  increases SEGA's effect.
SEGA can also be theoretically motivated in the mathematical background of DMs [9, 32, 14]. The isolated semantic guidance equation for the positive direction without the classifier-free guidance term can be written as
$$
\bar {\epsilon} _ {\theta} \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) \approx \epsilon_ {\theta} \left(\mathbf {z} _ {t}\right) + \mu \left(\epsilon_ {\theta} \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) - \epsilon_ {\theta} \left(\mathbf {z} _ {t}\right)\right) \tag {7}
$$
given Eqs. 3, 4, 5. Further, let us assume an implicit classifier for  $p(\mathbf{c}_e|\mathbf{z}_t) \propto \frac{p(\mathbf{z}_t,|\mathbf{c}_e)}{p(z_t)}$ , where  $p(z)$  is the marginal of  $z$  for the variance-preserving Markov process  $q(z|x)$  and  $x \sim p(x)$  [31]. Assuming exact estimates  $\epsilon^{*}(\mathbf{z}_{t},\mathbf{c}_{e})$  of  $p(\mathbf{z}_t|\mathbf{c}_e)$  and  $\epsilon^{*}(\mathbf{z}_{t})$  of  $p(z_{t})$  the gradient of the resulting classifier can be written as  $\nabla_{\mathbf{z}_t}\log p(\mathbf{c}_e|\mathbf{z}_t) = -\frac{1}{\omega_t}\big(\epsilon^* (\mathbf{z}_t,\mathbf{c}_e) - (\epsilon^* (\mathbf{c}_e)\big)$ . Using this implicit classifier for classifier guidance [5] results in noise estimate  $\bar{\epsilon}^{*}(\mathbf{z}_{t},\mathbf{c}_{e}) = \epsilon^{*}(\mathbf{z}_{t}) + w(\epsilon^{*}(\mathbf{z}_{t},\mathbf{c}_{e}) - \epsilon^{*}(\mathbf{z}_{t}))$ . This is fundamentally similar to SEGA as shown in Eq. 7 with  $\mu$  isolating the dimensions of the classifier signal with the largest absolute value (cf. Eq. 6). However, it should be noted that  $\bar{\epsilon}^{*}(\mathbf{z}_{t},\mathbf{c}_{e})$  is profoundly different from  $\bar{\epsilon}_{\theta}(\mathbf{z}_{t},\mathbf{c}_{e})$  as the latter's expressions are outputs of an unconstrained network and not the gradient of a classifier. Consequently, there are no guarantees [10] for the performance of SEGA. Nevertheless, this derivation provides a solid theoretical foundation and we are able to demonstrate the effectiveness of SEGA empirically in Sec. 5.
To offer even more control over the diffusion process, we make two adjustments to the methodology presented above. We add warm-up parameter  $\delta$  that will apply guidance  $\gamma$  after an initial warm-up period in the diffusion process, i.e.,  $\gamma (\mathbf{z}_t,\mathbf{c}_e)\coloneqq \mathbf{0}$  if  $t < \delta$ . Naturally, higher values for  $\delta$  lead to less substantial adjustments of the generated image. If we aim to keep the overall image composition unchanged, selecting a sufficiently high  $\delta$  ensures only altering fine-grained output details.
Furthermore, we add a momentum term  $\nu_{t}$  to the semantic guidance  $\gamma$  to accelerate guidance over time steps for dimensions that are continuously guided in the same direction. Hence,  $\gamma_{t}$  is defined as:
$$
\gamma \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) = \mu (\psi ; s _ {e}, \lambda) \psi \left(\mathbf {z} _ {t}, \mathbf {c} _ {e}\right) + s _ {m} \nu_ {t} \tag {8}
$$
with momentum scale  $s_m \in [0,1]$  and  $\nu$  being updated as
$$
\nu_ {t + 1} = \beta_ {m} \nu_ {t} + (1 - \beta_ {m}) \gamma_ {t} \tag {9}
$$
where  $\nu_{0} = 0$  and  $\beta_{m}\in [0,1)$ . Thus, larger  $\beta_{m}$  lead to less volatile changes in momentum. Momentum is already built up during the warm-up period, even though  $\gamma_{t}$  is not applied during these steps.
Beyond One Direction. Now, we are ready to move beyond using just one direction towards multiple concepts  $e_i$  and, in turn, combining multiple calculations of  $\gamma_t$ .
For all  $e_i$ , we calculate  $\gamma_t^i$  as described above with each defining their own hyperparameter values  $\lambda^i$ ,  $s_e^i$ . The weighted sum of all  $\gamma_t^i$  results in
$$
\hat {\gamma} _ {t} \left(\mathbf {z} _ {t}, \mathbf {c} _ {e _ {i}}\right) = \sum_ {i \in I} g _ {i} \gamma_ {t} ^ {i} \left(\mathbf {z} _ {t}, \mathbf {c} _ {e _ {i}}\right) \tag {10}
$$
In order to account for different warm-up periods,  $g_{i}$  is defined as  $g_{i} = 0$  if  $t < \delta_{i}$ . However, momentum is built up using all editing prompts and applied once all warm-up periods are completed, i.e.,  $\forall \delta_{i} : \delta_{i} \geq t$ . We provide a pseudo-code implementation of SEGA in App. A and more detailed intuition of each hyper-parameter along with visual ablations in App. C.
SEGA's underlying methodology is architecture-agnostic and applicable to any model employing classifier-free guidance. Consequently, we implemented SEGA for various generative models of different architectures and make our code available online. If not stated otherwise the examples presented in the main body, are generated using our implementation based on SD v1.5² with images from other models and architectures included in App. F. We note that SEGA can easily be applied to real images using reconstruction techniques for diffusion models, which we regard as future work.
4 Properties of Semantic Space
With the fundamentals of semantic guidance established, we next investigate the properties of SEGA's semantic space. In addition to the following discussion, we present further examples in the Appendix.
(a) Robustness of guidance vectors. Results for guiding towards ' glasses' in various domains without specifying how the concept should be incorporated.
(b) Uniqueness of guidance vectors. The vector for glasses' is calculated once on the blue-marked image and subsequently applied to other prompts (without colored border).
(c) Monotonicity of guidance vectors. The guidance scale for 'smile' is semantically reflected in the images.
Figure 3: Robustness, uniqueness and monotonicity of SEGA guidance vectors. In a) and b) the top row depicts the unchanged image, bottom row depicts the ones guided towards 'glasses'. (Best viewed in color)
Robustness. SEGA behaves robustly for incorporating arbitrary concepts into the original image. In Fig. 3a, we applied guidance for the concept 'glasses' to images from different domains. Notably, this prompt does not provide any context on how to incorporate the glasses into the given image and thus leaves room for interpretation. The depicted examples showcase how SEGA extracts best-effort integration of the target concept into the original image that is semantically grounded. This makes SEGA's use easy and provides the same exploratory nature as the initial image generation.