Diffusers 文档
一致性模型
并获得增强的文档体验
开始使用
一致性模型
一致性模型由 Yang Song、Prafulla Dhariwal、Mark Chen 和 Ilya Sutskever 在 一致性模型 中提出。
论文摘要如下:
扩散模型在图像、音频和视频生成领域取得了显著进展,但它们依赖于迭代采样过程,导致生成速度较慢。为了克服这一限制,我们提出了一致性模型,这是一个新的模型家族,通过直接将噪声映射到数据来生成高质量样本。它们天生支持快速一步生成,同时仍允许多步采样以权衡计算和样本质量。它们还支持零样本数据编辑,如图像修复、着色和超分辨率,而无需对这些任务进行显式训练。一致性模型可以通过蒸馏预训练扩散模型进行训练,也可以作为独立的生成模型进行训练。通过广泛的实验,我们证明它们在一步和少量步采样中优于现有扩散模型的蒸馏技术,在一步生成方面,在 CIFAR-10 上实现了 3.55 的新 SOTA FID,在 ImageNet 64x64 上实现了 6.20。当单独训练时,一致性模型成为一个全新的生成模型家族,在 CIFAR-10、ImageNet 64x64 和 LSUN 256x256 等标准基准上,其性能可以超越现有的一步非对抗性生成模型。
原始代码库可在 openai/consistency_models 找到,其他检查点可在 openai 获取。
技巧
为了进一步加速,使用 torch.compile
在 <1 秒内生成多张图像
import torch
from diffusers import ConsistencyModelPipeline
device = "cuda"
# Load the cd_bedroom256_lpips checkpoint.
model_id_or_path = "openai/diffusers-cd_bedroom256_lpips"
pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
pipe.to(device)
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
# Multistep sampling
# Timesteps can be explicitly specified; the particular timesteps below are from the original GitHub repo:
# https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L83
for _ in range(10):
image = pipe(timesteps=[17, 0]).images[0]
image.show()
ConsistencyModelPipeline
class diffusers.ConsistencyModelPipeline
< 来源 >( unet: UNet2DModel scheduler: CMStochasticIterativeScheduler )
参数
- unet (UNet2DModel) — 用于对编码图像潜变量进行去噪的
UNet2DModel
。 - scheduler (SchedulerMixin) — 与
unet
结合使用,用于对编码图像潜变量进行去噪的调度器。目前仅与 CMStochasticIterativeScheduler 兼容。
用于无条件或类条件图像生成的管道。
此模型继承自 DiffusionPipeline。请查阅超类文档,了解所有管道实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< 来源 >( batch_size: int = 1 class_labels: typing.Union[torch.Tensor, typing.List[int], int, NoneType] = None num_inference_steps: int = 1 timesteps: typing.List[int] = None generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: int = 1 ) → ImagePipelineOutput 或 tuple
参数
- batch_size (
int
, 可选, 默认为 1) — 要生成的图像数量。 - class_labels (
torch.Tensor
或List[int]
或int
, 可选) — 用于条件化类条件一致性模型的可选类标签。如果模型不是类条件的,则不使用。 - num_inference_steps (
int
, 可选, 默认为 1) — 去噪步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。 - timesteps (
List[int]
, 可选) — 用于去噪过程的自定义时间步。如果未定义,则使用等间距的num_inference_steps
时间步。必须按降序排列。 - generator (
torch.Generator
, 可选) — 用于使生成具有确定性的torch.Generator
。 - latents (
torch.Tensor
, 可选) — 从高斯分布采样的预生成噪声潜变量,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,则使用提供的随机generator
采样生成潜变量张量。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。可选择PIL.Image
或np.array
。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 而不是普通元组。 - callback (
Callable
, 可选) — 推理过程中每隔callback_steps
步调用一次的函数。该函数以以下参数调用:callback(step: int, timestep: int, latents: torch.Tensor)
。 - callback_steps (
int
, 可选, 默认为 1) — 调用callback
函数的频率。如果未指定,则在每一步都调用回调。
返回
ImagePipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ImagePipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表。
示例
>>> import torch
>>> from diffusers import ConsistencyModelPipeline
>>> device = "cuda"
>>> # Load the cd_imagenet64_l2 checkpoint.
>>> model_id_or_path = "openai/diffusers-cd_imagenet64_l2"
>>> pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
>>> pipe.to(device)
>>> # Onestep Sampling
>>> image = pipe(num_inference_steps=1).images[0]
>>> image.save("cd_imagenet64_l2_onestep_sample.png")
>>> # Onestep sampling, class-conditional image generation
>>> # ImageNet-64 class label 145 corresponds to king penguins
>>> image = pipe(num_inference_steps=1, class_labels=145).images[0]
>>> image.save("cd_imagenet64_l2_onestep_sample_penguin.png")
>>> # Multistep sampling, class-conditional image generation
>>> # Timesteps can be explicitly specified; the particular timesteps below are from the original GitHub repo:
>>> # https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L77
>>> image = pipe(num_inference_steps=None, timesteps=[22, 0], class_labels=145).images[0]
>>> image.save("cd_imagenet64_l2_multistep_sample_penguin.png")
ImagePipelineOutput
class diffusers.ImagePipelineOutput
< 来源 >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
图像流水线的输出类。