Diffusers 文档

调度器

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

调度器

🤗 Diffusers 为扩散过程提供了许多调度器函数。调度器接受模型的输出 (扩散过程正在迭代的样本) 和时间步长,以返回去噪样本。时间步长很重要,因为它决定了步骤在扩散过程中的位置;数据通过向前迭代 n 个时间步长生成,而推理通过向后传播时间步长发生。根据时间步长,调度器可以是离散的,在这种情况下,时间步长是 int,也可以是连续的,在这种情况下,时间步长是 float

根据上下文,调度器定义了如何迭代地向图像添加噪声或如何根据模型的输出更新样本

  • 训练期间,调度器向样本添加噪声 (有不同的算法用于添加噪声) 以训练扩散模型
  • 推理期间,调度器定义了如何根据预训练模型的输出更新样本

许多调度器是从 k-diffusion 库由 Katherine Crowson 实现的,它们也广泛用于 A1111 中。为了帮助您将 k-diffusion 和 A1111 中的调度器映射到 🤗 Diffusers 中的调度器,请查看下表

A1111/k-diffusion 🤗 Diffusers 用法
DPM++ 2M DPMSolverMultistepScheduler
DPM++ 2M Karras DPMSolverMultistepScheduler 使用 use_karras_sigmas=True 初始化
DPM++ 2M SDE DPMSolverMultistepScheduler 使用 algorithm_type="sde-dpmsolver++" 初始化
DPM++ 2M SDE Karras DPMSolverMultistepScheduler 使用 use_karras_sigmas=Truealgorithm_type="sde-dpmsolver++" 初始化
DPM++ 2S a N/A 非常类似于 DPMSolverSinglestepScheduler
DPM++ 2S a Karras N/A 非常类似于 DPMSolverSinglestepScheduler(use_karras_sigmas=True, ...)
DPM++ SDE DPMSolverSinglestepScheduler
DPM++ SDE Karras DPMSolverSinglestepScheduler 使用 use_karras_sigmas=True 初始化
DPM2 KDPM2DiscreteScheduler
DPM2 Karras KDPM2DiscreteScheduler 使用 use_karras_sigmas=True 初始化
DPM2 a KDPM2AncestralDiscreteScheduler
DPM2 a Karras KDPM2AncestralDiscreteScheduler 使用 use_karras_sigmas=True 初始化
DPM adaptive N/A
DPM fast N/A
Euler EulerDiscreteScheduler
Euler a EulerAncestralDiscreteScheduler
Heun HeunDiscreteScheduler
LMS LMSDiscreteScheduler
LMS Karras LMSDiscreteScheduler 使用 use_karras_sigmas=True 初始化
N/A DEISMultistepScheduler
N/A UniPCMultistepScheduler

噪声计划表和计划表类型

A1111/k-diffusion 🤗 Diffusers
Karras 使用 use_karras_sigmas=True 初始化
sgm_uniform 使用 timestep_spacing="trailing" 初始化
simple 使用 timestep_spacing="trailing" 初始化
exponential 使用 timestep_spacing="linspace", use_exponential_sigmas=True 初始化
beta 使用 timestep_spacing="linspace", use_beta_sigmas=True 初始化

所有调度器都基于基础 SchedulerMixin 类构建,该类实现了所有调度器共享的底层实用程序。

SchedulerMixin

class diffusers.SchedulerMixin

< >

( )

所有调度器的基类。

SchedulerMixin 包含所有调度器共享的通用功能,例如通用加载和保存功能。

ConfigMixin 负责存储传递给调度器的 __init__ 函数的配置属性 (如 num_train_timesteps),这些属性可以通过 scheduler.config.num_train_timesteps 访问。

类属性

  • _compatibles (List[str]) — 与父调度器类兼容的调度器类列表。使用 from_config() 加载不同的兼容调度器类 (应由父类重写)。

from_pretrained

< >

( pretrained_model_name_or_path: typing.Union[str, os.PathLike, NoneType] = None subfolder: typing.Optional[str] = None return_unused_kwargs = False **kwargs )

参数

  • pretrained_model_name_or_path (stros.PathLike, 可选) — 可以是以下之一:

    • 一个字符串,预训练模型的模型 ID (例如 google/ddpm-celebahq-256),托管在 Hub 上。
    • 目录的路径 (例如 ./my_model_directory),其中包含使用 save_pretrained() 保存的调度器配置。
  • subfolder (str, optional) — Hub 或本地更大的模型仓库中模型文件的子文件夹位置。
  • return_unused_kwargs (bool, optional, defaults to False) — 是否返回 Python 类未使用的 kwargs。默认为 False
  • cache_dir (Union[str, os.PathLike], optional) — 如果不使用标准缓存,则为下载的预训练模型配置缓存到的目录路径。
  • force_download (bool, optional, defaults to False) — 是否强制(重新)下载模型权重和配置文件,覆盖缓存版本(如果存在)。默认为 False
  • proxies (Dict[str, str], optional) — 按协议或端点使用的代理服务器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用于每个请求。
  • output_loading_info(bool, optional, defaults to False) — 是否也返回一个包含缺失键、意外键和错误消息的字典。默认为 False
  • local_files_only(bool, optional, defaults to False) — 是否仅加载本地模型权重和配置文件。如果设置为 True,则不会从 Hub 下载模型。默认为 False
  • token (str or bool, optional) — 用作远程文件 HTTP Bearer 授权的令牌。如果为 True,则使用从 diffusers-cli login 生成的令牌(存储在 ~/.huggingface 中)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名称、标签名称、提交 ID 或 Git 允许的任何标识符。默认为 "main"

从本地目录或 Hub 仓库中的预定义 JSON 配置文件实例化调度器。

要使用私有或门控模型,请使用 huggingface-cli login 登录。您还可以激活特殊的“离线模式”,以便在防火墙环境中使用此方法。

save_pretrained

< >

( save_directory: typing.Union[str, os.PathLike] push_to_hub: bool = False **kwargs )

参数

  • save_directory (str or os.PathLike) — 将在其中保存配置 JSON 文件的目录(如果不存在将创建)。
  • push_to_hub (bool, optional, defaults to False) — 保存后是否将模型推送到 Hugging Face Hub。您可以使用 repo_id 指定要推送到的仓库(默认为您命名空间中 save_directory 的名称)。默认为 False
  • kwargs (Dict[str, Any], optional) — 传递给 push_to_hub() 方法的其他关键字参数。

将调度器配置对象保存到目录,以便可以使用 from_pretrained() 类方法重新加载它。

SchedulerOutput

class diffusers.schedulers.scheduling_utils.SchedulerOutput

< >

( prev_sample: Tensor )

参数

  • prev_sample (torch.Tensor of shape (batch_size, num_channels, height, width) for images) — 上一个时间步的计算样本 (x_{t-1})prev_sample 应在去噪循环中用作下一个模型输入。

调度器的 step 函数输出的基类。

KarrasDiffusionSchedulers

KarrasDiffusionSchedulers 是 🤗 Diffusers 中调度器的广泛概括。此类中的调度器在高层次上通过其噪声采样策略、网络和缩放类型、训练策略以及损失加权方式来区分。

此类中不同的调度器,取决于常微分方程 (ODE) 求解器类型,属于上述分类,并为 🤗 Diffusers 中实现的主要调度器的设计提供了良好的抽象。此类中的调度器在此处给出

PushToHubMixin

class diffusers.utils.PushToHubMixin

< >

( )

一个 Mixin,用于将模型、调度器或管道推送到 Hugging Face Hub。

push_to_hub

< >

( repo_id: str commit_message: typing.Optional[str] = None private: typing.Optional[bool] = None token: typing.Optional[str] = None create_pr: bool = False safe_serialization: bool = True variant: typing.Optional[str] = None )

参数

  • repo_id (str) — 您想要将模型、调度器或管道文件推送到的仓库的名称。当推送到组织时,它应包含您的组织名称。 repo_id 也可以是本地目录的路径。
  • commit_message (str, optional) — 推送时要提交的消息。默认为 "Upload {object}"
  • private (bool, optional) — 是否将仓库设为私有。如果为 None(默认值),则仓库将为公开,除非组织的默认设置为私有。如果仓库已存在,则忽略此值。
  • token (str, optional) — 用作远程文件 HTTP Bearer 授权的令牌。运行 huggingface-cli login 时生成的令牌(存储在 ~/.huggingface 中)。
  • create_pr (bool, optional, defaults to False) — 是否创建包含上传文件的 PR 或直接提交。默认为 False
  • safe_serialization (bool, 可选, 默认为 True) — 是否将模型权重转换为 safetensors 格式。
  • variant (str, 可选) — 如果指定,权重将以 pytorch_model.<variant>.bin 格式保存。

将模型、调度器或pipeline文件上传到 🤗 Hugging Face Hub。

示例

from diffusers import UNet2DConditionModel

unet = UNet2DConditionModel.from_pretrained("stabilityai/stable-diffusion-2", subfolder="unet")

# Push the `unet` to your namespace with the name "my-finetuned-unet".
unet.push_to_hub("my-finetuned-unet")

# Push the `unet` to an organization with the name "my-finetuned-unet".
unet.push_to_hub("your-org/my-finetuned-unet")
< > 在 GitHub 上更新