Diffusers 文档
DDPMScheduler
并获得增强的文档体验
开始使用
DDPMScheduler
Denoising Diffusion Probabilistic Models (DDPM) 由 Jonathan Ho、Ajay Jain 和 Pieter Abbeel 提出,这是一种同名的基于扩散的模型。在 🤗 Diffusers 库的上下文中,DDPM 指的是论文中的离散去噪调度器以及 pipeline。
该论文的摘要如下:
我们使用扩散概率模型(一类受非平衡热力学考虑启发的潜在变量模型)展示了高质量的图像合成结果。我们的最佳结果是通过根据扩散概率模型和带有 Langevin 动力学的去噪分数匹配之间的新颖联系而设计的加权变分界限进行训练而获得的,并且我们的模型自然地允许一种渐进的、有损的解压缩方案,该方案可以解释为自回归解码的概括。在无条件 CIFAR10 数据集上,我们获得了 9.46 的 Inception 分数和 3.17 的最先进 FID 分数。在 256x256 LSUN 上,我们获得了与 ProgressiveGAN 相似的样本质量。我们的实现可在 此 https URL 上找到。
DDPMScheduler
class diffusers.DDPMScheduler
< 源代码 >( num_train_timesteps: int = 1000 beta_start: float = 0.0001 beta_end: float = 0.02 beta_schedule: str = 'linear' trained_betas: typing.Union[numpy.ndarray, typing.List[float], NoneType] = None variance_type: str = 'fixed_small' clip_sample: bool = True prediction_type: str = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 clip_sample_range: float = 1.0 sample_max_value: float = 1.0 timestep_spacing: str = 'leading' steps_offset: int = 0 rescale_betas_zero_snr: bool = False )
参数
- num_train_timesteps (
int
, 默认为 1000) — 训练模型的扩散步数。 - beta_start (
float
, 默认为 0.0001) — 推理的起始beta
值。 - beta_end (
float
, 默认为 0.02) — 最终beta
值。 - beta_schedule (
str
, 默认为"linear"
) — beta 计划,从 beta 范围到用于步进模型的 betas 序列的映射。从linear
、scaled_linear
或squaredcos_cap_v2
中选择。 - trained_betas (
np.ndarray
, 可选) — 一个 betas 数组,用于直接传递给构造函数,而无需使用beta_start
和beta_end
。 - variance_type (
str
, 默认为"fixed_small"
) — 在将噪声添加到去噪样本时,裁剪方差。从fixed_small
,fixed_small_log
,fixed_large
,fixed_large_log
,learned
或learned_range
中选择。 - clip_sample (
bool
, 默认为True
) — 为了数值稳定性,裁剪预测的样本。 - clip_sample_range (
float
, 默认为 1.0) — 样本裁剪的最大幅度。仅当clip_sample=True
时有效。 - prediction_type (
str
, 默认为epsilon
, *可选*) — 调度器函数的预测类型;可以是epsilon
(预测扩散过程的噪声),sample
(直接预测带噪样本) 或v_prediction
(参见 Imagen Video 论文的 2.4 节). - thresholding (
bool
, 默认为False
) — 是否使用 “动态阈值” 方法。这不适用于潜在空间扩散模型,例如 Stable Diffusion。 - dynamic_thresholding_ratio (
float
, 默认为 0.995) — 动态阈值方法的比率。仅当thresholding=True
时有效。 - sample_max_value (
float
, 默认为 1.0) — 动态阈值的阈值。仅当thresholding=True
时有效。 - timestep_spacing (
str
, 默认为"leading"
) — 时间步长应如何缩放的方式。有关更多信息,请参阅 Common Diffusion Noise Schedules and Sample Steps are Flawed 的表 2。 - steps_offset (
int
, 默认为 0) — 添加到推理步骤的偏移量,某些模型系列需要这样做。 - rescale_betas_zero_snr (
bool
, 默认为False
) — 是否重新缩放 betas 以使其具有零终端信噪比 (SNR)。这使模型能够生成非常明亮和黑暗的样本,而不是将其限制为中等亮度的样本。与--offset_noise
松散相关。
DDPMScheduler
探索了去噪分数匹配和 Langevin 动力学采样之间的联系。
此模型继承自 SchedulerMixin 和 ConfigMixin。查看超类文档以获取库为所有调度器实现的通用方法,例如加载和保存。
scale_model_input
< 源代码 >( sample: Tensor timestep: typing.Optional[int] = None ) → torch.Tensor
确保与需要根据当前时间步长缩放去噪模型输入的调度器具有互换性。
set_timesteps
< 源代码 >( num_inference_steps: typing.Optional[int] = None device: typing.Union[str, torch.device] = None timesteps: typing.Optional[typing.List[int]] = None )
设置用于扩散链的离散时间步长(在推理之前运行)。
step
< 源代码 >( model_output: Tensor timestep: int sample: Tensor generator = None return_dict: bool = True ) → DDPMSchedulerOutput 或 tuple
参数
- model_output (
torch.Tensor
) — 来自学习的扩散模型的直接输出。 - timestep (
float
) — 扩散链中的当前离散时间步。 - sample (
torch.Tensor
) — 由扩散过程创建的样本的当前实例。 - generator (
torch.Generator
, *可选*) — 随机数生成器。 - return_dict (
bool
, *可选*, 默认为True
) — 是否返回 DDPMSchedulerOutput 或tuple
。
返回值
DDPMSchedulerOutput 或 tuple
如果 return_dict 为 True
, 则返回 DDPMSchedulerOutput,否则返回一个元组,其中第一个元素是样本张量。
通过反转 SDE,从上一个时间步预测样本。此函数从学习的模型输出(通常是预测的噪声)传播扩散过程。
DDPMSchedulerOutput
class diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput
< 源代码 >( prev_sample: Tensor pred_original_sample: typing.Optional[torch.Tensor] = None )
调度器的 step
函数输出的输出类。