Diffusers 文件
排程器
並獲得增強的文件體驗
開始使用
排程器
🤗 Diffusers 為擴散過程提供了許多排程器函式。排程器接收模型的輸出(擴散過程迭代的樣本)和一個時間步,以返回去噪後的樣本。時間步非常重要,因為它決定了擴散過程中的步驟;資料透過向前迭代 *n* 個時間步生成,而推理則透過向後傳播時間步發生。根據時間步,排程器可能是*離散的*(此時時間步是 `int`)或*連續的*(此時時間步是 `float`)。
根據上下文,排程器定義瞭如何迭代地向影像新增噪聲,或如何根據模型的輸出更新樣本
- 在*訓練*期間,排程器向樣本新增噪聲(有不同的新增噪聲的演算法)以訓練擴散模型
- 在*推理*期間,排程器定義瞭如何根據預訓練模型的輸出更新樣本
許多排程器都是從 Katherine Crowson 的 k-diffusion 庫中實現的,它們在 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=True` 和 `algorithm_type="sde-dpmsolver++"` 初始化 |
DPM++ 2S a | 不適用 | 與 `DPMSolverSinglestepScheduler` 非常相似 |
DPM++ 2S a Karras | 不適用 | 與 `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 | 不適用 | |
DPM fast | 不適用 | |
Euler | EulerDiscreteScheduler | |
Euler a | EulerAncestralDiscreteScheduler | |
Heun | HeunDiscreteScheduler | |
LMS | LMSDiscreteScheduler | |
LMS Karras | LMSDiscreteScheduler | 使用 `use_karras_sigmas=True` 初始化 |
不適用 | DEISMultistepScheduler | |
不適用 | UniPCMultistepScheduler |
噪聲 schedules 和 schedule 型別
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
所有排程器的基類。
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 (
str
或os.PathLike
, 可選) — 可以是以下之一:- 一個字串,即 Hub 上託管的預訓練模型的*模型 ID*(例如 `google/ddpm-celebahq-256`)。
- 一個指向*目錄*的路徑(例如 `./my_model_directory`),該目錄包含使用 save_pretrained() 儲存的排程器配置。
- subfolder (
str
, 可選) — Hub 或本地大型模型倉庫中模型檔案的子資料夾位置。 - return_unused_kwargs (
bool
, 可選, 預設為False
) — 是否返回未被 Python 類使用的 kwargs。 - cache_dir (
Union[str, os.PathLike]
, 可選) — 當不使用標準快取時,下載的預訓練模型配置將被快取到該目錄的路徑。 - force_download (
bool
, 可選, 預設為False
) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。 - proxies (
Dict[str, str]
, 可選) — 按協議或端點使用的代理伺服器字典,例如 `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`。代理在每次請求時使用。 - output_loading_info(`bool`, 可選, 預設為
False
) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。 - local_files_only(`bool`, 可選, 預設為 `False`) — 是否只加載本地模型權重和配置檔案。如果設定為 `True`,模型將不會從 Hub 下載。
- token (
str
或 bool, 可選) — 用於遠端檔案的 HTTP Bearer 授權的令牌。如果為 `True`,則使用 `diffusers-cli login` 生成的令牌(儲存在 `~/.huggingface` 中)。 - revision (
str
, 可選, 預設為"main"
) — 要使用的特定模型版本。它可以是分支名稱、標籤名稱、提交 ID 或 Git 允許的任何識別符號。
從本地目錄或 Hub 倉庫中預定義的 JSON 配置檔案例項化排程器。
save_pretrained
< 原始碼 >( save_directory: typing.Union[str, os.PathLike] push_to_hub: bool = False **kwargs )
引數
- save_directory (
str
或os.PathLike
) — 配置 JSON 檔案將被儲存的目錄(如果不存在將被建立)。 - push_to_hub (
bool
, 可選, 預設為False
) — 是否在儲存後將您的模型推送到 Hugging Face Hub。您可以使用 `repo_id` 指定要推送的倉庫(預設為您名稱空間中 `save_directory` 的名稱)。 - kwargs (
Dict[str, Any]
, 可選) — 傳遞給 push_to_hub() 方法的附加關鍵字引數。
將排程器配置物件儲存到目錄中,以便可以使用 from_pretrained() 類方法重新載入。
SchedulerOutput
class diffusers.schedulers.scheduling_utils.SchedulerOutput
< 原始碼 >( prev_sample: Tensor )
排程器 step
函式輸出的基類。
KarrasDiffusionSchedulers
KarrasDiffusionSchedulers
是 🤗 Diffusers 中排程器的廣泛概括。這類排程器在高層次上透過它們的噪聲取樣策略、網路型別和縮放、訓練策略以及損失加權方式來區分。
這類中的不同調度器,根據常微分方程(ODE)求解器型別,歸入上述分類,併為 🤗 Diffusers 中實現的主要排程器的設計提供了良好的抽象。這類排程器可以在此處找到。
PushToHubMixin
用於將模型、排程器或管道推送到Hugging Face Hub的Mixin。
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
) — 您希望將模型、排程器或 pipeline 檔案推送到的倉庫名稱。推送到組織時應包含您的組織名稱。`repo_id` 也可以是本地目錄的路徑。 - commit_message (
str
, 可選) — 推送時要提交的訊息。預設為 `"Upload {object}"`。 - private (
bool
, 可選) — 是否將倉庫設為私有。如果為 `None`(預設),倉庫將為公開,除非組織的預設設定為私有。如果倉庫已存在,則忽略此值。 - token (
str
, 可選) — 用於遠端檔案的 HTTP Bearer 授權的令牌。執行 `huggingface-cli login` 時生成的令牌(儲存在 `~/.huggingface` 中)。 - create_pr (
bool
, 可選, 預設為False
) — 是使用上傳的檔案建立 PR 還是直接提交。 - safe_serialization (
bool
, 可選, 預設為True
) — 是否將模型權重轉換為safetensors
格式。 - variant (
str
, 可選) — 如果指定,權重將以pytorch_model.<variant>.bin
格式儲存。
將模型、排程器或管道檔案上傳到 🤗 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")