Diffusers 文档
文本到图像
并获取增强的文档体验
开始使用
文本到图像
当您想到扩散模型时,文本到图像通常是首先想到的事情之一。文本到图像从文本描述(例如,“丛林中的宇航员,冷色调,柔和的色彩,细节丰富,8k”)生成图像,这也称为提示。
从非常高的层面来看,扩散模型接受提示和一些随机初始噪声,并迭代地去除噪声以构建图像。去噪过程由提示引导,一旦去噪过程在预定数量的时间步后结束,图像表示就被解码成图像。
阅读 Stable Diffusion 如何工作? 博客文章,以了解有关潜在扩散模型如何工作的更多信息。
您可以通过 🤗 Diffusers 分两步从提示生成图像
- 将检查点加载到 AutoPipelineForText2Image 类中,该类根据检查点自动检测要使用的适当 pipeline 类
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16"
).to("cuda")
- 将提示传递给 pipeline 以生成图像
image = pipeline(
"stained glass of darth vader, backlight, centered composition, masterpiece, photorealistic, 8k"
).images[0]
image

热门模型
最常见的文本到图像模型是 Stable Diffusion v1.5、Stable Diffusion XL (SDXL) 和 Kandinsky 2.2。还有 ControlNet 模型或 adapters 可以与文本到图像模型一起使用,以便更直接地控制图像生成。由于每个模型的架构和训练过程略有不同,因此每个模型的结果略有不同,但无论您选择哪个模型,它们的使用方式都大致相同。让我们为每个模型使用相同的提示并比较它们的结果。
Stable Diffusion v1.5
Stable Diffusion v1.5 是从 Stable Diffusion v1-4 初始化的潜在扩散模型,并在 LAION-Aesthetics V2 数据集中 512x512 图像上微调了 595K 步。您可以像这样使用此模型
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16"
).to("cuda")
generator = torch.Generator("cuda").manual_seed(31)
image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", generator=generator).images[0]
image
Stable Diffusion XL
SDXL 是先前 Stable Diffusion 模型的更大版本,并且涉及一个两阶段模型过程,该过程为图像添加了更多细节。它还包括一些额外的微条件,以生成以主体为中心的高质量图像。查看更全面的 SDXL 指南,以了解有关如何使用它的更多信息。一般来说,您可以像这样使用 SDXL
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16"
).to("cuda")
generator = torch.Generator("cuda").manual_seed(31)
image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", generator=generator).images[0]
image
Kandinsky 2.2
Kandinsky 模型与 Stable Diffusion 模型略有不同,因为它还使用图像先验模型来创建嵌入,这些嵌入用于更好地对齐扩散模型中的文本和图像。
使用 Kandinsky 2.2 最简单的方法是
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
).to("cuda")
generator = torch.Generator("cuda").manual_seed(31)
image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", generator=generator).images[0]
image
ControlNet
ControlNet 模型是辅助模型或 adapters,它们在文本到图像模型(例如 Stable Diffusion v1.5)之上进行微调。将 ControlNet 模型与文本到图像模型结合使用,为更明确地控制如何生成图像提供了多种选择。使用 ControlNet,您可以向模型添加额外的条件输入图像。例如,如果您提供人体姿势的图像(通常表示为连接成骨架的多个关键点)作为条件输入,则模型会生成遵循图像姿势的图像。查看更深入的 ControlNet 指南,以了解有关其他条件输入以及如何使用它们的更多信息。
在本示例中,让我们使用人体姿势估计图像来调节 ControlNet。加载在人体姿势估计上预训练的 ControlNet 模型
from diffusers import ControlNetModel, AutoPipelineForText2Image
from diffusers.utils import load_image
import torch
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/control_v11p_sd15_openpose", torch_dtype=torch.float16, variant="fp16"
).to("cuda")
pose_image = load_image("https://huggingface.co/lllyasviel/control_v11p_sd15_openpose/resolve/main/images/control.png")
将 controlnet
传递给 AutoPipelineForText2Image,并提供提示和姿势估计图像
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16, variant="fp16"
).to("cuda")
generator = torch.Generator("cuda").manual_seed(31)
image = pipeline("Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", image=pose_image, generator=generator).images[0]
image




配置 pipeline 参数
pipeline 中有许多参数可以配置,这些参数会影响图像的生成方式。您可以更改图像的输出大小、指定负面提示以提高图像质量等等。本节将深入探讨如何使用这些参数。
高度和宽度
height
和 width
参数控制生成图像的高度和宽度(以像素为单位)。默认情况下,Stable Diffusion v1.5 模型输出 512x512 图像,但您可以将其更改为 8 的倍数的任何大小。例如,要创建矩形图像
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16"
).to("cuda")
image = pipeline(
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", height=768, width=512
).images[0]
image

其他模型可能具有不同的默认图像大小,具体取决于训练数据集中的图像大小。例如,SDXL 的默认图像大小为 1024x1024,使用较低的 height
和 width
值可能会导致图像质量降低。请务必先查看模型的 API 参考!
引导比例
guidance_scale
参数影响提示对图像生成的影响程度。较低的值赋予模型“创造力”,以生成与提示更松散相关的图像。较高的 guidance_scale
值会推动模型更紧密地遵循提示,如果此值过高,您可能会在生成的图像中观察到一些伪影。
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")
image = pipeline(
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k", guidance_scale=3.5
).images[0]
image



负面提示
就像提示引导生成一样,负面提示引导模型远离您不希望模型生成的事物。这通常用于通过删除较差或不良的图像特征(例如“低分辨率”或“不良细节”)来提高整体图像质量。您还可以使用负面提示来删除或修改图像的内容和样式。
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")
image = pipeline(
prompt="Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
negative_prompt="ugly, deformed, disfigured, poor details, bad anatomy",
).images[0]
image


生成器
通过设置手动种子,torch.Generator
对象可以在 pipeline 中实现可重复性。 您可以使用 Generator
生成图像批次,并迭代改进从种子生成的图像,详情请参阅使用确定性生成提高图像质量指南。
您可以如下所示设置种子和 Generator
。 使用 Generator
创建图像应每次返回相同的结果,而不是随机生成新图像。
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")
generator = torch.Generator(device="cuda").manual_seed(30)
image = pipeline(
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
generator=generator,
).images[0]
image
控制图像生成
除了配置 pipeline 的参数(例如提示权重和 ControlNet 模型)之外,还有几种方法可以更好地控制图像的生成方式。
提示权重
提示权重是一种技术,用于增加或减少提示中概念的重要性,以强调或最小化图像中的某些特征。 我们建议使用 Compel 库来帮助您生成加权提示嵌入。
了解如何在提示权重指南中创建提示嵌入。 此示例重点介绍如何在 pipeline 中使用提示嵌入。
创建嵌入后,您可以将其传递给 pipeline 中的 prompt_embeds
(如果您使用负面提示,则为 negative_prompt_embeds
)参数。
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")
image = pipeline(
prompt_embeds=prompt_embeds, # generated from Compel
negative_prompt_embeds=negative_prompt_embeds, # generated from Compel
).images[0]
ControlNet
正如您在 ControlNet 部分中看到的,这些模型通过结合额外的条件图像输入,提供了一种更灵活、更准确的图像生成方式。 每个 ControlNet 模型都经过特定类型的条件图像的预训练,以生成与其相似的新图像。 例如,如果您采用在深度图上预训练的 ControlNet 模型,您可以将深度图作为条件输入提供给模型,它将生成一个保留其中空间信息的图像。 这比在提示中指定深度信息更快更简单。 您甚至可以使用 MultiControlNet 组合多个条件输入!
您可以使用多种类型的条件输入,🤗 Diffusers 支持用于 Stable Diffusion 和 SDXL 模型的 ControlNet。 请查看更全面的 ControlNet 指南,了解如何使用这些模型。
优化
扩散模型很大,并且图像去噪的迭代性质在计算上既昂贵又密集。 但这并不意味着您需要访问强大的 GPU 甚至许多 GPU 才能使用它们。 有许多优化技术可以在消费者级和免费层资源上运行扩散模型。 例如,您可以半精度加载模型权重以节省 GPU 内存并提高速度,或者将整个模型卸载到 GPU 以节省更多内存。
如果您使用的是 PyTorch 2.0,PyTorch 2.0 还支持一种更节省内存的注意力机制,称为缩放点积注意力,该机制会自动启用。 您可以将此与 torch.compile
结合使用,以进一步加快代码速度。
from diffusers import AutoPipelineForText2Image
import torch
pipeline = AutoPipelineForText2Image.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16, variant="fp16").to("cuda")
pipeline.unet = torch.compile(pipeline.unet, mode="reduce-overhead", fullgraph=True)
有关如何优化代码以节省内存和加速推理的更多提示,请阅读内存和速度以及 Torch 2.0 指南。
< > 在 GitHub 上更新