Diffusers documentation
DDPMScheduler
DDPMScheduler
Jonathan Ho、Ajay Jain 和 Pieter Abbeel 提出的 Denoising Diffusion Probabilistic Models(DDPM)提出了一种同名的基于扩散的模型。在 🤗 Diffusers 库的语境中,DDPM 既指代论文中的离散去噪调度器,也指代相应的 pipeline。
该论文的摘要如下:
我们展示了使用扩散概率模型(一类受非平衡热力学启发的隐变量模型)进行高质量图像合成的结果。我们的最佳结果是通过在根据扩散概率模型与朗之万动力学去噪分数匹配之间新发现的联系而设计的加权变分界上训练获得的,并且我们的模型自然地支持一种渐进式有损解压方案,该方案可被解释为自回归解码的推广。在无条件的 CIFAR10 数据集上,我们获得了 9.46 的 Inception 分数和 3.17 的 FID 分数,达到了当前最优水平。在 256x256 LSUN 上,我们获得了与 ProgressiveGAN 相似的样本质量。我们的实现可在 this https URL 获取。
DDPMScheduler
class diffusers.DDPMScheduler
< source >( num_train_timesteps: int = 1000beta_start: float = 0.0001beta_end: float = 0.02beta_schedule: typing.Literal['linear', 'scaled_linear', 'squaredcos_cap_v2', 'sigmoid'] = 'linear'trained_betas: numpy.ndarray | list[float] | None = Nonevariance_type: typing.Literal['fixed_small', 'fixed_small_log', 'fixed_large', 'fixed_large_log', 'learned', 'learned_range'] = 'fixed_small'clip_sample: bool = Trueprediction_type: typing.Literal['epsilon', 'sample', 'v_prediction'] = 'epsilon'thresholding: bool = Falsedynamic_thresholding_ratio: float = 0.995clip_sample_range: float = 1.0sample_max_value: float = 1.0timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'leading'steps_offset: int = 0rescale_betas_zero_snr: bool = False )
参数
- num_train_timesteps (
int,默认为1000) — 用于训练模型的扩散步数。 - beta_start (
float,默认为0.0001) — 推理的起始beta值。 - beta_end (
float,默认为0.02) — 最终的beta值。 - beta_schedule(
"linear"、"scaled_linear"、"squaredcos_cap_v2"或"sigmoid",默认为"linear")—— beta 调度,即将 beta 范围映射为用于模型步进的一系列 beta 值。 - trained_betas (
np.ndarray, 可选) — 一个直接传递给构造函数的 betas 数组,无需使用beta_start和beta_end。 - variance_type(
"fixed_small"、"fixed_small_log"、"fixed_large"、"fixed_large_log"、"learned"或"learned_range",默认为"fixed_small")—— 在对去噪样本添加噪声时,对方差进行裁剪。 - clip_sample (
bool,默认为True) — 对预测样本进行裁剪,以确保数值稳定性。 - clip_sample_range (
float,默认为1.0) — 样本裁剪的最大幅度。仅在clip_sample=True时有效。 - prediction_type (
"epsilon","sample", or"v_prediction", defaults to"epsilon") — Prediction type of the scheduler function; can beepsilon(predicts the noise of the diffusion process),sample(directly predicts the noisy sample) orv_prediction` (see section 2.4 of Imagen Video paper). - thresholding (
bool,默认为False) — 是否使用“动态阈值”方法。此方法不适用于 Stable Diffusion 等潜在空间扩散模型。 - dynamic_thresholding_ratio (
float, 默认值为0.995) — 动态阈值化方法的比例。仅在thresholding=True时有效。 - sample_max_value (
float,默认为1.0) — 动态阈值化的阈值。仅在thresholding=True时有效。 - timestep_spacing (
"linspace","leading", or"trailing", defaults to"leading") — The way the timesteps should be scaled. Refer to Table 2 of the Common Diffusion Noise Schedules and Sample Steps are Flawed for more information. - steps_offset(
int,默认为0)— 添加到推理步数中的偏移量,某些模型系列需要此参数。 - rescale_betas_zero_snr (
bool,默认为False) — 是否重新缩放 betas 以实现零终端 SNR。这使模型能够生成非常明亮和非常暗的样本,而不是将其限制在中等亮度的样本上。与--offset_noise大致相关。
DDPMScheduler 探索了去噪分数匹配与朗之万动力学采样之间的联系。
该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。
add_noise
< source >( original_samples: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor
根据每个时间步的噪声幅度,向原始样本添加噪声(即前向扩散过程)。
get_velocity
< source >( sample: Tensornoise: Tensortimesteps: IntTensor ) → torch.Tensor
根据速度公式,从样本和噪声计算速度预测。
previous_timestep
< source >( timestep: int ) → int or torch.Tensor
计算扩散链中的前一个时间步。
scale_model_input
< source >( sample: Tensortimestep: int | None = None ) → torch.Tensor
确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。
set_timesteps
< source >( num_inference_steps: int = Nonedevice: typing.Union[str, torch.device] = Nonetimesteps: list[int] | None = None )
设置扩散链所使用的离散时间步(需在推理之前运行)。
步骤
< source >( model_output: Tensortimestep: intsample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → DDPMSchedulerOutput or tuple
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - timestep (
int) — 扩散链中的当前离散时间步。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - generator (
torch.Generator,可选) — 随机数生成器。 - return_dict (
bool,默认为True) — 是否返回 DDPMSchedulerOutput 或tuple。
返回值
DDPMSchedulerOutput 或 tuple
If return_dict is True, DDPMSchedulerOutput is returned, otherwise a
tuple is returned where the first element is the sample tensor.
通过逆向 SDE 预测上一时间步的样本。此函数将扩散过程从学习到的模型输出(通常是预测的噪声)进行传播。
DDPMSchedulerOutput
class diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput
< source >( prev_sample: Tensorpred_original_sample: typing.Optional[torch.Tensor] = None )
调度器 step 函数输出的输出类。