patrickvonplaten commited on
Commit
85b62fa
2 Parent(s): 2720c36 d71cb1d

Merge branch 'main' of https://huggingface.co/CompVis/latent-diffusion-celeba-256 into main

Browse files
Files changed (1) hide show
  1. README.md +108 -0
README.md ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - ldm_diffusion
4
+ ---
5
+
6
+ # Latent Diffusion Models (LDM)
7
+
8
+ **Paper**: [High-Resolution Image Synthesis with Latent Diffusion Models](https://arxiv.org/abs/2112.10752)
9
+
10
+ **Abstract**:
11
+
12
+ *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.*
13
+
14
+ **Authors**
15
+
16
+ *Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, Björn Ommer*
17
+
18
+ ## Usage
19
+
20
+ ### Unrolled loop
21
+
22
+ ```python
23
+ from diffusers import UNetUnconditionalModel, DDIMScheduler, VQModel
24
+ import torch
25
+ import PIL.Image
26
+ import numpy as np
27
+ import tqdm
28
+
29
+ seed = 3
30
+
31
+ # load all models
32
+ unet = UNetUnconditionalModel.from_pretrained("./", subfolder="unet")
33
+ vqvae = VQModel.from_pretrained("./", subfolder="vqvae")
34
+ scheduler = DDIMScheduler.from_config("./", subfolder="scheduler")
35
+
36
+ # set to cuda
37
+ torch_device = "cuda" if torch.cuda.is_available() else "cpu"
38
+
39
+ unet.to(torch_device)
40
+ vqvae.to(torch_device)
41
+
42
+ # generate gaussian noise to be decoded
43
+ generator = torch.manual_seed(seed)
44
+ noise = torch.randn(
45
+ (1, unet.in_channels, unet.image_size, unet.image_size),
46
+ generator=generator,
47
+ ).to(torch_device)
48
+
49
+ # set inference steps for DDIM
50
+ scheduler.set_timesteps(num_inference_steps=200)
51
+
52
+ image = noise
53
+ for t in tqdm.tqdm(scheduler.timesteps):
54
+ # predict noise residual of previous image
55
+ with torch.no_grad():
56
+ residual = unet(image, t)["sample"]
57
+
58
+ # compute previous image x_t according to DDIM formula
59
+ prev_image = scheduler.step(residual, t, image, eta=0.0)["prev_sample"]
60
+
61
+ # x_t-1 -> x_t
62
+ image = prev_image
63
+
64
+ # decode image with vae
65
+ with torch.no_grad():
66
+ image = vqvae.decode(image)
67
+
68
+ # process image
69
+ image_processed = image.cpu().permute(0, 2, 3, 1)
70
+ image_processed = (image_processed + 1.0) * 127.5
71
+ image_processed = image_processed.numpy().astype(np.uint8)
72
+ image_pil = PIL.Image.fromarray(image_processed[0])
73
+
74
+ image_pil.save(f"generated_image_{seed}.png")
75
+ ```
76
+
77
+ ### pipeline
78
+
79
+ ```python
80
+ from diffusers import LatentDiffusionUncondPipeline
81
+ import torch
82
+ import PIL.Image
83
+ import numpy as np
84
+ import tqdm
85
+
86
+ seed = 3
87
+
88
+ pipeline = LatentDiffusionUncondPipeline.from_pretrained("./")
89
+
90
+ # generatae image by calling the pipeline
91
+ generator = torch.manual_seed(seed)
92
+ image = pipeline(generator=generator, num_inference_steps=200)["sample"]
93
+
94
+ # process image
95
+ image_processed = image.cpu().permute(0, 2, 3, 1)
96
+ image_processed = (image_processed + 1.0) * 127.5
97
+ image_processed = image_processed.numpy().astype(np.uint8)
98
+ image_pil = PIL.Image.fromarray(image_processed[0])
99
+
100
+ image_pil.save(f"generated_image_{seed}.png")
101
+ ```
102
+
103
+ ## Samples
104
+
105
+ 1. ![sample_1](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/generated_image_0.png)
106
+ 2. ![sample_1](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/generated_image_1.png)
107
+ 3. ![sample_1](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/generated_image_2.png)
108
+ 4. ![sample_1](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/generated_image_3.png)