Diffusers documentation

DDIM

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

DDIM

Jiaming Song、Chenlin Meng 和 Stefano Ermon 提出的去噪扩散隐式模型(DDIM)。

该论文的摘要如下:

去噪扩散概率模型(DDPMs)无需对抗训练即可实现高质量的图像生成,但生成一个样本需要模拟马尔可夫链的许多步骤。为了加速采样,我们提出了去噪扩散隐式模型(DDIMs),这是一类更高效的迭代隐式概率模型,其训练流程与 DDPMs 相同。在 DDPMs 中,生成过程被定义为马尔可夫扩散过程的逆过程。我们构建了一类非马尔可夫扩散过程,它们具有相同的训练目标,但其逆过程的采样速度可以快得多。我们通过实验证明,与 DDPMs 相比,DDIMs 在墙钟时间上可以将高质量样本的生成速度提高 10 到 50 倍,允许我们在计算量和样本质量之间进行权衡,并且可以直接在潜在空间中执行语义上有意义的图像插值。

原始代码库可在 ermongroup/ddim 中找到。

DDIMPipeline

类 diffusers.DDIMPipeline

< >

( unet: UNet2DModelscheduler: DDIMScheduler )

参数

用于图像生成的 Pipeline。

该模型继承自 DiffusionPipeline。有关为所有 pipeline 实现的通用方法(下载、保存、在特定设备上运行等),请参阅父类文档。

__call__

< >

( batch_size: int = 1generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Noneeta: float = 0.0num_inference_steps: int = 50use_clipped_model_output: bool | None = Noneoutput_type: str | None = 'pil'return_dict: bool = True ) → ImagePipelineOutput or tuple

参数

  • batch_size (int,可选,默认为 1) — 要生成的图像数量。
  • generator (torch.Generator,可选) — 用于使生成过程确定性的 torch.Generator。
  • eta(float,可选,默认值为 0.0)— 对应 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中会被忽略。0 对应 DDIM,1 对应 DDPM。
  • num_inference_steps (int,可选,默认为 50) — 去噪步数。更多的去噪步数通常能生成更高质量的图像,但代价是推理速度变慢。
  • use_clipped_model_output (bool,可选,默认为 None) — 如果为 True 或 False,请参阅 DDIMScheduler.step() 的文档。如果为 None,则不会向下游的 scheduler 传递任何值(对于不支持此参数的 scheduler,请使用 None)。
  • output_type (str, 可选,默认为 "pil") — 生成图像的输出格式。可选 "pil" (PIL.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool,可选,默认为 True) — 是否返回 ImagePipelineOutput 而不是普通元组。

返回值

ImagePipelineOutput 或 tuple

如果 return_dict 为 True,则返回 ImagePipelineOutput;否则返回一个 tuple,其中第一个元素是包含生成图像的列表

用于生成的 pipeline 的调用函数。

示例:

>>> from diffusers import DDIMPipeline
>>> import PIL.Image
>>> import numpy as np

>>> # load model and scheduler
>>> pipe = DDIMPipeline.from_pretrained("fusing/ddim-lsun-bedroom")

>>> # run pipeline in inference (sample random noise and denoise)
>>> image = pipe(eta=0.0, num_inference_steps=50)

>>> # process image to PIL
>>> image_processed = image.cpu().permute(0, 2, 3, 1)
>>> image_processed = (image_processed + 1.0) * 127.5
>>> image_processed = image_processed.numpy().astype(np.uint8)
>>> image_pil = PIL.Image.fromarray(image_processed[0])

>>> # save image
>>> image_pil.save("test.png")

ImagePipelineOutput

类 diffusers.ImagePipelineOutput

< >

( images: list[PIL.Image.Image] | numpy.ndarray )

参数

  • images (List[PIL.Image.Image] 或 np.ndarray) — 长度为 batch_size 的去噪 PIL 图像列表,或形状为 (batch_size, height, width, num_channels) 的 NumPy 数组。

图像流水线输出类。

Update on GitHub