Diffusers 文档
文本到图像
并获得增强的文档体验
开始使用
文本到图像
当您想到扩散模型时,文本到图像通常是首先浮现在脑海中的功能之一。文本到图像根据文本描述(例如,“丛林中的宇航员,冷色调,柔和色彩,细节丰富,8k”)生成图像,这种描述也称为 *提示词*。
从宏观角度来看,扩散模型接收一个提示词和一些随机初始噪声,然后迭代地去除噪声以构建图像。*去噪* 过程由提示词引导,一旦去噪过程在预定的时间步数后结束,图像表示将被解码为图像。
阅读 Stable Diffusion 如何工作? 博客文章,了解有关潜在扩散模型工作原理的更多信息。
您可以通过两个步骤在 🤗 Diffusers 中从提示词生成图像:
- 将检查点加载到 AutoPipelineForText2Image 类中,该类会自动根据检查点检测要使用的相应管道类。
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(
"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 模型或适配器可与文本到图像模型一起使用,以更直接地控制图像生成。由于它们的架构和训练过程不同,每个模型的结果略有不同,但无论您选择哪个模型,它们的用法或多或少都是相同的。让我们为每个模型使用相同的提示词并比较它们的结果。
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 模型是辅助模型或适配器,在文本到图像模型(例如 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




配置管道参数
管道中有许多参数可以配置,它们会影响图像的生成方式。您可以更改图像的输出大小,指定负面提示以提高图像质量等。本节将深入探讨如何使用这些参数。
高度和宽度
`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
对象通过设置手动种子来实现在管道中的可复现性。您可以使用 `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
控制图像生成
除了配置管道参数外,还有几种方法可以更好地控制图像的生成方式,例如提示词加权和 ControlNet 模型。
提示词加权
提示词加权是一种提高或降低提示词中概念重要性的技术,用于强调或最小化图像中的某些特征。我们建议使用 Compel 库来帮助您生成加权提示词嵌入。
了解如何在 提示词加权 指南中创建提示词嵌入。本示例重点介绍如何在管道中使用提示词嵌入。
创建嵌入后,您可以将它们传递给管道中的 `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 以节省更多内存。
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)
有关如何优化代码以节省内存和加速推理的更多提示,请阅读 加速推理 和 减少内存使用 指南。
< > 在 GitHub 上更新