๐ Latent Diffusion Models (LDM)
Latent Diffusion Models (LDMs) decompose the image formation process, enabling high - resolution image synthesis with reduced computational requirements. They achieve state - of - the - art results in various tasks like unconditional image generation.
๐ Quick Start
โจ Features
- Efficient Computation: By operating in the latent space of pretrained autoencoders, LDMs significantly reduce the computational resources needed for training and inference compared to pixel - based diffusion models.
- Flexible Generation: With cross - attention layers, LDMs can generate images based on general conditioning inputs such as text or bounding boxes.
- High - Quality Results: Achieve a new state of the art for image inpainting and competitive performance in tasks like unconditional image generation, semantic scene synthesis, and super - resolution.
๐ฆ Installation
To use LDMs, you need to install the diffusers
library:
!pip install diffusers
๐ป Usage Examples
Basic Usage
from diffusers import DiffusionPipeline
model_id = "CompVis/ldm-celebahq-256"
pipeline = DiffusionPipeline.from_pretrained(model_id)
image = pipeline(num_inference_steps=200)["sample"]
image[0].save("ldm_generated_image.png")
Advanced Usage
from diffusers import UNet2DModel, DDIMScheduler, VQModel
import torch
import PIL.Image
import numpy as np
import tqdm
seed = 3
unet = UNet2DModel.from_pretrained("CompVis/ldm-celebahq-256", subfolder="unet")
vqvae = VQModel.from_pretrained("CompVis/ldm-celebahq-256", subfolder="vqvae")
scheduler = DDIMScheduler.from_config("CompVis/ldm-celebahq-256", subfolder="scheduler")
torch_device = "cuda" if torch.cuda.is_available() else "cpu"
unet.to(torch_device)
vqvae.to(torch_device)
generator = torch.manual_seed(seed)
noise = torch.randn(
(1, unet.in_channels, unet.sample_size, unet.sample_size),
generator=generator,
).to(torch_device)
scheduler.set_timesteps(num_inference_steps=200)
image = noise
for t in tqdm.tqdm(scheduler.timesteps):
with torch.no_grad():
residual = unet(image, t)["sample"]
prev_image = scheduler.step(residual, t, image, eta=0.0)["prev_sample"]
image = prev_image
with torch.no_grad():
image = vqvae.decode(image)
image_processed = image.cpu().permute(0, 2, 3, 1)
image_processed = (image_processed + 1.0) * 127.5
image_processed = image_processed.clamp(0, 255).numpy().astype(np.uint8)
image_pil = PIL.Image.fromarray(image_processed[0])
image_pil.save(f"generated_image_{seed}.png")
๐ Documentation
Paper: High - Resolution Image Synthesis with Latent Diffusion Models
Abstract:
By decomposing the image formation process into a sequential application of denoising autoencoders, diffusion models (DMs) achieve state - of - the - art synthesis results on image data and beyond. Additionally, their formulation allows for a guiding mechanism to control the image generation process without retraining. However, since these models typically operate directly in pixel space, optimization of powerful DMs often consumes hundreds of GPU days and inference is expensive due to sequential evaluations. To enable DM training on limited computational resources while retaining their quality and flexibility, we apply them in the latent space of powerful pretrained autoencoders. In contrast to previous work, training diffusion models on such a representation allows for the first time to reach a near - optimal point between complexity reduction and detail preservation, greatly boosting visual fidelity. By introducing cross - attention layers into the model architecture, we turn diffusion models into powerful and flexible generators for general conditioning inputs such as text or bounding boxes and high - resolution synthesis becomes possible in a convolutional manner. Our latent diffusion models (LDMs) achieve a new state of the art for image inpainting and highly competitive performance on various tasks, including unconditional image generation, semantic scene synthesis, and super - resolution, while significantly reducing computational requirements compared to pixel - based DMs.
Authors:
Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, Bjรถrn Ommer
๐ License
This project is licensed under the Apache 2.0 license.
Samples



