Overview

9 Text-to-image generation with latent diffusion

This chapter introduces latent diffusion models (LDMs) as a practical and efficient way to generate high-quality images from text. Instead of operating directly in pixel space—which is computationally expensive at high resolution—LDMs perform forward and reverse diffusion in a compact latent space learned by a variational autoencoder (VAE). A high-resolution image (for example, 3×512×512) is compressed to a much smaller representation (such as 4×64×64), cutting computation dramatically while preserving semantic content. After denoising in this latent space, a VAE decoder reconstructs detailed, high-resolution outputs. Beyond text-to-image synthesis, the approach also supports image editing guided by text prompts while maintaining scene structure.

The LDM architecture combines three components: a VAE for compression and reconstruction, a U-Net diffusion model for denoising in the latent space, and a CLIP model for text conditioning. Training proceeds on image–caption pairs: images are encoded into latents, noise is progressively added, and the diffusion model learns to predict and remove that noise conditioned on text. CLIP supplies text and image embeddings to align generated latents with the prompt. This division of labor balances efficiency and fidelity—the VAE shapes a smooth, informative latent manifold and restores fine details, while diffusion ensures realism and diversity. Because end-to-end training is resource-intensive, the chapter focuses on using pre-trained components to demonstrate the full pipeline.

Practically, the chapter walks through compressing and reconstructing images with a pre-trained VAE, then generating images by sampling in latent space with a noise scheduler and time embeddings. Text prompts are tokenized to a fixed length for CLIP, and classifier-free guidance blends conditional and unconditional predictions, with a cfg_scale parameter trading off diversity (lower values) against prompt adherence (higher values). For image-to-image editing, the original image is encoded to latents, a controllable amount of noise (strength) determines how much it changes, and guided denoising steers the result toward the new text description. Decoding intermediate and final latents back to pixel space reveals the gradual emergence of structure and detail, culminating in high-resolution images that accurately reflect the prompts.

Eight steps for building a text-to-image generator from scratch. This chapter lays the groundwork for step 6, Latent Diffusion, showing you how to generate images in a lower-dimensional latent space, guided by text using the CLIP model from the previous chapter.
How a variational autoencoder (VAE) compresses images to representations in the lower-dimensional (latent) space and then reconstructs the images. A VAE consists of an encoder and a decoder. The encoder compresses images (e.g., with a size of 3x512x512) into a lower-dimensional space (i.e., the latent space, where images have a size of, e.g., 4x64x64). The decoder takes the encoded images in the latent space and reconstructs the images to the original high resolution space (with a size of 3x512x512 in this example).
How a trained latent diffusion model (LDM) generates an image based on a text prompt. The text prompt ("as shown at the top left corner) is first encoded into a text embedding. To generate an image in the lower-dimensional space (latent space), we start with an image of pure noise (bottom left). We use the trained U-Net to iteratively denoise the image, conditional on the text embedding so the generated image matches the text prompt, with the guidance of a trained contrast language-image pre-training (CLIP) model. The generated latent image (bottom right) is presented to a trained VAE to convert it to a high-resolution image, which is the final output (top right).
Modifying an existing image with a text prompt using the pre-trained latent diffusion model. The original image is shown in the middle of the figure. Using "a white horse walks on the sunny sandy beach" as the prompt, the resulting image is shown on the left. When we use "a zebra walks on the white, snow-covered ground" as the text prompt, the resulting image is shown on the right.
Comparing the original images with latent images. The 1st and 3rd images in the figure are original images of a beaver and a dolphin, respectively, with a shape of 3x512x512. We use a pre-trained VAE encoder to compress the images to a lower-dimensional space. The compressed latent images have a shape of 4x64x64, with only about 2% of the number of pixels of the original images. However, we can still tell that the 2nd image is a beaver and the last image is a dolphin.
Comparing the original images with reconstructed images. The 1st and 3rd images in the figure are original images of a beaver and a dolphin, respectively, with a shape of 3x512x512. We use a pre-trained VAE encoder to compress the images into lower-dimensional latent images with a shape of 4x64x64. We then use the pre-trained VAE decoder to convert the latent images to the original high-dimensional space, as shown in the 2nd and 4th images in the figure.
An image generated by a pre-trained latent diffusion model with a text prompt " a banana riding a motorcycle, wearing sunglasses and a straw hat, highly detailed, ultra sharp, cinematic, 100mm lens, 8k resolution."
Generated latent images in time steps 800, 600, …, and 0. These images are generated with a pre-trained latent diffusion model. We start with an image with pure random noise at t=1000. We use the diffusion model to denoise it iteratively and obtain the noisy image at t=800, which is shown in the far left of the figure. We continue to denoise the image and obtain images at t=600, 400, and 200. Finally, we obtain the image at t=0, which is shown at the far right of the figure. The images have a shape of 4x64x64.
Converting the intermediate latent images into the high-dimensional space. The five latent images in figure 9.8, each with a shape of 4x64x64, are converted by the pre-trained VAE decoder into the high-dimensional space, with a shape of 3x512x512.
An image of a horse. We’ll use a pretrained latent diffusion model (LDM) to modify this image based on text prompts.
A modified horse image with the latent diffusion model (LDM). We use the text prompt “a white horse walks on the sandy sunny beach” to modify the horse image shown in figure 9.10 using a pretrained LDM.
A modified image with a pretrained latent diffusion model (LDM) based on the text prompt “a zebra walks on the white, snow-covered ground.” The original image is a white horse as shown in figure 9.10.
An image generated by a trained latent diffusion model based on a text prompt when we set the cfg_scale parameter to 12. The text prompt is " a banana riding a motorcycle, wearing sunglasses and a straw hat, highly detailed, ultra sharp, cinematic, 100mm lens, 8k resolution." The classifier-free guidance parameter, cfg_scale, steers the generation process towards outputs that better match the provided conditional prompt, while reducing artifacts. A low value of the cfg_scale parameter (say, 5) leads to a more diverse output, and a high value (say, 12) leads to an output that aligns more closely with the text prompt.

Summary

  • Generating high-resolution images with diffusion models conditional on text prompts is computationally costly, making training and inference inefficient and slow. To overcome the challenge, Rombach et al. in 2022 proposed a latent diffusion model (LDM). Instead of conducting forward and reverse diffusion processes on high-resolution images, the authors propose conducting the diffusion processes in a lower-dimensional space (i.e., the latent space).
  • Since images in the latent space are much smaller in size, the diffusion processes are much faster. However, this means lower quality of the generated images. To compensate for this, a variational autoencoder (VAE) is used to convert the low-resolution latent images back into high-resolution ones as the final output.
  • A VAE is a generative model composed of an encoder and a decoder. The encoder transforms high-resolution images into compact, lower-dimensional representations, which the decoder uses to reconstruct the original image. The decoder’s ability to convert low-resolution images into high-resolution output is an essential part of the LDM.
  • An LDM can also modify an existing image based on a text prompt, similar to how it generates an image from scratch, but with a key difference: you start from an encoded version of the image rather than pure noise.
  • The classifier-free guidance parameter, cfg_scale, controls the diversity versus fidelity of generated images. A low cfg_scale value leads to more diverse output. A high cfg_scale value leads to an output that is more aligned with the text prompt.
  • One of the state-of-the-art text-to-image generators, Stable Diffusion, which we’ll cover in the next chapter, is based on LDM.

FAQ

What is a latent diffusion model (LDM) and why is it used for text-to-image generation?LDMs perform the forward and reverse diffusion processes in a compact latent space rather than directly on high-resolution pixel images. This dramatically reduces memory and compute requirements while preserving semantic structure. A VAE handles the mapping between pixel space and latent space, enabling efficient training and fast inference without sacrificing final image quality.
How do variational autoencoders (VAEs) fit into an LDM pipeline?The VAE encoder compresses a high-resolution image (e.g., 3×512×512) into a low-dimensional latent representation (e.g., 4×64×64). The diffusion model operates entirely in this latent space. After denoising, the VAE decoder reconstructs a detailed high-resolution image from the final latent, bridging efficiency and visual fidelity.
How much dimensionality reduction do LDMs typically achieve?A common setup maps a 3×512×512 RGB image to a 4×64×64 latent, which is a 48× reduction in data size. Despite the compression, the latent still captures essential structure and semantics, enabling accurate reconstruction by the VAE decoder.
What role does CLIP play in text-to-image generation with LDMs?CLIP encodes the text prompt into an embedding and provides guidance so the denoised latent aligns with the description. During training or inference, the model encourages similarity between text and image embeddings, steering generation toward prompt-consistent outputs. In practice, both conditional (prompt) and unconditional embeddings are used to balance alignment and diversity.
What is classifier-free guidance (cfg_scale), and how does it affect results?Classifier-free guidance mixes conditional and unconditional predictions: output = uncond + cfg_scale × (cond − uncond). Lower cfg_scale (e.g., ~5) yields more diverse, sometimes less prompt-aligned images; higher values (e.g., 8–12) typically improve prompt adherence but can reduce diversity or cause overfitting if set too high. Choosing cfg_scale is a trade-off between fidelity to the prompt and variety.
How are timesteps and time embeddings used during diffusion?A noise scheduler defines a sequence of timesteps for inference (e.g., 50 steps). At each step, a sinusoidal time embedding encodes the current timestep and is fed to the denoising U-Net so it knows how much noise to remove. Iterating from noisy latents toward cleaner ones produces the final latent that the VAE decodes to pixels.
How do you generate an image from scratch versus edit an existing image?To generate from scratch, start the reverse diffusion from pure Gaussian noise in latent space, guided by the text prompt. To edit, first encode the existing image into the latent space with the VAE encoder, then add a controlled amount of noise and run the denoising process conditioned on the new prompt. A strength parameter controls how far the result deviates from the original image.
What models and files are needed to run the chapter’s code examples?You need a pre-trained VAE (e.g., a .ckpt with encoder/decoder weights), a CLIP text tokenizer’s vocab.json and merges.txt, and pre-trained weights for the diffusion and CLIP models. The chapter references repos from CompVis and Umar Jamil for model definitions and pipelines. A CUDA-enabled GPU is recommended; a Colab notebook is provided for convenience.
How do LDMs convert between latent and pixel spaces during synthesis?The diffusion U-Net denoises a 4×64×64 latent through multiple timesteps. You can decode intermediate latents with the VAE decoder to inspect progress, or only decode the final latent once denoising completes. The last decode produces the final high-resolution image (e.g., 3×512×512).
Are there practical tips for prompts, seeding, and steps?CLIP text inputs are standardized to 77 tokens; shorter prompts are padded and longer ones truncated. Setting a random seed ensures reproducible outputs. The number of inference steps (e.g., 50) trades speed for quality—more steps can improve detail and stability but cost more compute.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Text-to-Image Generator (from Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Text-to-Image Generator (from Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Text-to-Image Generator (from Scratch) ebook for free