Diffusers 文件

DPMSolverMultistepInverse

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

DPMSolverMultistepInverse

DPMSolverMultistepInverse 是來自 DPM-Solver: A Fast ODE Solver for Diffusion Probabilistic Model Sampling in Around 10 StepsDPM-Solver++: Fast Solver for Guided Sampling of Diffusion Probabilistic Models(作者:Cheng Lu, Yuhao Zhou, Fan Bao, Jianfei Chen, Chongxuan Li, Jun Zhu)的反向排程器。

此實現主要基於 Null-text Inversion for Editing Real Images using Guided Diffusion Models 的 DDIM 反演定義和 Xiang-cd/DiffEdit-stable-diffusionDiffEdit 潛在反演的筆記本實現。

提示

支援 Imagen 的動態閾值處理,對於畫素空間擴散模型,您可以同時設定 algorithm_type="dpmsolver++"thresholding=True 以使用動態閾值處理。此閾值處理方法不適用於潛在空間擴散模型(如 Stable Diffusion)。

DPMSolverMultistepInverseScheduler

class diffusers.DPMSolverMultistepInverseScheduler

< >

( 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 solver_order: int = 2 prediction_type: str = 'epsilon' thresholding: bool = False dynamic_thresholding_ratio: float = 0.995 sample_max_value: float = 1.0 algorithm_type: str = 'dpmsolver++' solver_type: str = 'midpoint' lower_order_final: bool = True euler_at_final: bool = False use_karras_sigmas: typing.Optional[bool] = False use_exponential_sigmas: typing.Optional[bool] = False use_beta_sigmas: typing.Optional[bool] = False use_flow_sigmas: typing.Optional[bool] = False flow_shift: typing.Optional[float] = 1.0 lambda_min_clipped: float = -inf variance_type: typing.Optional[str] = None timestep_spacing: str = 'linspace' steps_offset: int = 0 )

引數

  • num_train_timesteps (int, 預設為 1000) — 訓練模型的擴散步數。
  • beta_start (float, 預設為 0.0001) — 推理的起始 beta 值。
  • beta_end (float, 預設為 0.02) — 最終 beta 值。
  • beta_schedule (str, 預設為 "linear") — Beta 排程,將 beta 範圍對映到一系列用於模型步進的 beta 值。可選擇 linearscaled_linearsquaredcos_cap_v2
  • trained_betas (np.ndarray, 可選) — 直接向建構函式傳遞一個 beta 陣列,以繞過 beta_startbeta_end
  • solver_order (int, 預設為 2) — DPMSolver 階數,可以是 123。建議在引導取樣中使用 solver_order=2,在無條件取樣中使用 solver_order=3
  • 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=Truealgorithm_type="dpmsolver++" 時有效。
  • algorithm_type (str, 預設為 dpmsolver++) — 求解器的演算法型別;可以是 dpmsolverdpmsolver++sde-dpmsolversde-dpmsolver++dpmsolver 型別實現了 DPMSolver 論文中的演算法,dpmsolver++ 型別實現了 DPMSolver++ 論文中的演算法。建議在 Stable Diffusion 等引導取樣中使用 dpmsolver++sde-dpmsolver++solver_order=2
  • solver_type (str, 預設為 midpoint) — 二階求解器的求解器型別;可以是 midpointheun。求解器型別會稍微影響樣本質量,特別是對於少量步驟。建議使用 midpoint 求解器。
  • lower_order_final (bool, 預設為 True) — 是否在最後幾步使用低階求解器。僅對少於 15 個推理步驟有效。這可以穩定 DPMSolver 在少於 15 個步驟的取樣,特別是對於少於或等於 10 個步驟的情況。
  • euler_at_final (bool, 預設為 False) — 是否在最後一步使用尤拉方法。這是數值穩定性與細節豐富性之間的權衡。這可以穩定 DPMSolver 的 SDE 變體在少量推理步驟下的取樣,但有時可能導致模糊。
  • use_karras_sigmas (bool, 可選, 預設為 False) — 是否在取樣過程中使用 Karras sigmas 作為噪聲排程中的步長。如果為 True,則根據一系列噪聲水平 {σi} 確定 sigmas。
  • use_exponential_sigmas (bool, 可選, 預設為 False) — 是否在取樣過程中使用指數 sigmas 作為噪聲排程中的步長。
  • use_beta_sigmas (bool, 可選, 預設為 False) — 是否在取樣過程中使用 beta sigmas 作為噪聲排程中的步長。有關更多資訊,請參閱 Beta Sampling is All You Need
  • lambda_min_clipped (float, 預設為 -inf) — 用於數值穩定性的 lambda(t) 最小值的剪裁閾值。這對於餘弦 (squaredcos_cap_v2) 噪聲排程至關重要。
  • variance_type (str, 可選) — 對於預測方差的擴散模型,設定為“learned”或“learned_range”。如果設定,模型的輸出將包含預測的高斯方差。
  • timestep_spacing (str, 預設為 "linspace") — 時間步長的縮放方式。有關更多資訊,請參閱 Common Diffusion Noise Schedules and Sample Steps are Flawed 的表 2。
  • steps_offset (int, 預設為 0) — 根據某些模型系列的要求,新增到推理步驟的偏移量。

DPMSolverMultistepInverseSchedulerDPMSolverMultistepScheduler 的反向排程器。

此模型繼承自 SchedulerMixinConfigMixin。請檢視超類文件,瞭解庫為所有排程器實現的通用方法,例如載入和儲存。

convert_model_output

< >

( model_output: Tensor *args sample: Tensor = None **kwargs ) torch.Tensor

引數

  • model_output (torch.Tensor) — 從學習到的擴散模型直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的樣本的當前例項。

返回

torch.Tensor

轉換後的模型輸出。

將模型輸出轉換為 DPMSolver/DPMSolver++ 演算法所需的相應型別。DPM-Solver 旨在離散化噪聲預測模型的積分,而 DPM-Solver++ 旨在離散化資料預測模型的積分。

演算法和模型型別是解耦的。噪聲預測模型和資料預測模型都可以使用DPMSolver或DPMSolver++。

dpm_solver_first_order_update

< >

( model_output: Tensor *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) torch.Tensor

引數

  • model_output (torch.Tensor) — 從學習到的擴散模型直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的樣本的當前例項。

返回

torch.Tensor

上一個時間步的樣本張量。

一階DPMSolver(等同於DDIM)的一個步驟。

multistep_dpm_solver_second_order_update

< >

( model_output_list: typing.List[torch.Tensor] *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) torch.Tensor

引數

  • model_output_list (List[torch.Tensor]) — 從學習到的擴散模型在當前和後續時間步長的直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的樣本的當前例項。

返回

torch.Tensor

上一個時間步的樣本張量。

二階多步DPMSolver的一個步驟。

multistep_dpm_solver_third_order_update

< >

( model_output_list: typing.List[torch.Tensor] *args sample: Tensor = None noise: typing.Optional[torch.Tensor] = None **kwargs ) torch.Tensor

引數

  • model_output_list (List[torch.Tensor]) — 來自當前和後續時間步的已學習擴散模型的直接輸出。
  • sample (torch.Tensor) — 由擴散過程建立的當前樣本例項。

返回

torch.Tensor

上一個時間步的樣本張量。

三階多步DPMSolver的一個步驟。

scale_model_input

< 原始碼 >

( sample: Tensor *args **kwargs ) torch.Tensor

引數

  • sample (torch.Tensor) — 輸入樣本。

返回

torch.Tensor

一個縮放後的輸入樣本。

確保與需要根據當前時間步縮放去噪模型輸入的排程器互換使用。

set_timesteps

< 原始碼 >

( num_inference_steps: int = None device: typing.Union[str, torch.device] = None )

引數

  • num_inference_steps (int) — 使用預訓練模型生成樣本時使用的擴散步數。
  • device (strtorch.device, 可選) — 時間步應移動到的裝置。如果為 None,時間步將不會移動。

設定用於擴散鏈的離散時間步(在推理之前執行)。

步驟

< 原始碼 >

( model_output: Tensor timestep: typing.Union[int, torch.Tensor] sample: Tensor generator = None variance_noise: typing.Optional[torch.Tensor] = None return_dict: bool = True ) SchedulerOutputtuple

引數

  • model_output (torch.Tensor) — 已學習擴散模型的直接輸出。
  • timestep (int) — 擴散鏈中的當前離散時間步。
  • sample (torch.Tensor) — 由擴散過程建立的當前樣本例項。
  • generator (torch.Generator, 可選) — 隨機數生成器。
  • variance_noise (torch.Tensor) — 用於透過直接提供方差本身的噪聲來生成噪聲的替代方法。對於 CycleDiffusion 等方法很有用。
  • return_dict (bool) — 是否返回 SchedulerOutputtuple

返回

排程器輸出tuple

如果 return_dictTrue,則返回 SchedulerOutput,否則返回一個元組,其中第一個元素是樣本張量。

透過反轉 SDE 預測前一個時間步的樣本。此函式使用多步 DPMSolver 傳播樣本。

SchedulerOutput

class diffusers.schedulers.scheduling_utils.SchedulerOutput

< 原始碼 >

( prev_sample: Tensor )

引數

  • prev_sample (torch.Tensor, 形狀為影像的 (batch_size, num_channels, height, width)) — 上一時間步的計算樣本 (x_{t-1})prev_sample 應在去噪迴圈中用作下一個模型輸入。

排程器 step 函式輸出的基類。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.