Latent Diffusion¶
Latent Diffusion was proposed in High-Resolution Image Synthesis with Latent Diffusion Models by Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, Björn Ommer.
The abstract from the paper is:
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.
The original codebase can be found at CompVis/latent-diffusion.
Tip
Make sure to check out the Schedulers guide to learn how to explore the tradeoff between scheduler speed and quality, and see the reuse components across pipelines section to learn how to efficiently load the same components into multiple pipelines.
mindone.diffusers.LDMTextToImagePipeline
¶
Bases: DiffusionPipeline
Pipeline for text-to-image generation using latent diffusion.
This model inherits from [DiffusionPipeline
]. Check the superclass documentation for the generic methods
implemented for all pipelines (downloading, saving, running on a particular device, etc.).
PARAMETER | DESCRIPTION |
---|---|
vqvae |
Vector-quantized (VQ) model to encode and decode images to and from latent representations.
TYPE:
|
bert |
Text-encoder model based on [
TYPE:
|
tokenizer |
A
TYPE:
|
unet |
A
TYPE:
|
scheduler |
A scheduler to be used in combination with
TYPE:
|
Source code in mindone/diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
mindone.diffusers.LDMTextToImagePipeline.__call__(prompt, height=None, width=None, num_inference_steps=50, guidance_scale=1.0, eta=0.0, generator=None, latents=None, output_type='pil', return_dict=False, **kwargs)
¶
The call function to the pipeline for generation.
PARAMETER | DESCRIPTION |
---|---|
prompt |
The prompt or prompts to guide the image generation.
TYPE:
|
height |
The height in pixels of the generated image.
TYPE:
|
width |
The width in pixels of the generated image.
TYPE:
|
num_inference_steps |
The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
TYPE:
|
guidance_scale |
A higher guidance scale value encourages the model to generate images closely linked to the text
TYPE:
|
generator |
A
TYPE:
|
latents |
Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
tensor is generated by sampling using the supplied random
TYPE:
|
output_type |
The output format of the generated image. Choose between
TYPE:
|
return_dict |
Whether or not to return a [
TYPE:
|
>>> from mindone.diffusers import DiffusionPipeline
>>> # load model and scheduler
>>> ldm = DiffusionPipeline.from_pretrained("CompVis/ldm-text2im-large-256")
>>> # run pipeline in inference (sample random noise and denoise)
>>> prompt = "A painting of a squirrel eating a burger"
>>> images = ldm([prompt], num_inference_steps=50, eta=0.3, guidance_scale=6)[0]
>>> # save images
>>> for idx, image in enumerate(images):
... image.save(f"squirrel-{idx}.png")
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, ImagePipelineOutput]
|
[ |
Source code in mindone/diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
mindone.diffusers.LDMSuperResolutionPipeline
¶
Bases: DiffusionPipeline
A pipeline for image super-resolution using latent diffusion.
This model inherits from [DiffusionPipeline
]. Check the superclass documentation for the generic methods
implemented for all pipelines (downloading, saving, running on a particular device, etc.).
PARAMETER | DESCRIPTION |
---|---|
vqvae |
Vector-quantized (VQ) model to encode and decode images to and from latent representations.
TYPE:
|
unet |
A
TYPE:
|
scheduler |
A scheduler to be used in combination with
TYPE:
|
Source code in mindone/diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion_superresolution.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
mindone.diffusers.LDMSuperResolutionPipeline.__call__(image=None, batch_size=1, num_inference_steps=100, eta=0.0, generator=None, output_type='pil', return_dict=False)
¶
The call function to the pipeline for generation.
PARAMETER | DESCRIPTION |
---|---|
image |
TYPE:
|
batch_size |
Number of images to generate.
TYPE:
|
num_inference_steps |
The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
TYPE:
|
eta |
Corresponds to parameter eta (η) from the DDIM paper. Only applies
to the [
TYPE:
|
generator |
A
TYPE:
|
output_type |
The output format of the generated image. Choose between
TYPE:
|
return_dict |
Whether or not to return a [
TYPE:
|
>>> import requests
>>> from PIL import Image
>>> from io import BytesIO
>>> from mindone.diffusers import LDMSuperResolutionPipeline
>>> import mindspore as ms
>>> # load model and scheduler
>>> pipeline = LDMSuperResolutionPipeline.from_pretrained("CompVis/ldm-super-resolution-4x-openimages")
>>> # let's download an image
>>> url = (
... "https://user-images.githubusercontent.com/38061659/199705896-b48e17b8-b231-47cd-a270-4ffa5a93fa3e.png"
... )
>>> response = requests.get(url)
>>> low_res_img = Image.open(BytesIO(response.content)).convert("RGB")
>>> low_res_img = low_res_img.resize((128, 128))
>>> # run pipeline in inference (sample random noise and denoise)
>>> upscaled_image = pipeline(low_res_img, num_inference_steps=100, eta=1)[0][0]
>>> # save image
>>> upscaled_image.save("ldm_generated_image.png")
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, ImagePipelineOutput]
|
[ |
Source code in mindone/diffusers/pipelines/latent_diffusion/pipeline_latent_diffusion_superresolution.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
mindone.diffusers.pipelines.ImagePipelineOutput
dataclass
¶
Bases: BaseOutput
Output class for image pipelines.
Source code in mindone/diffusers/pipelines/pipeline_utils.py
69 70 71 72 73 74 75 76 77 78 79 80 |
|