Diffusers 文件

載入社群管道和元件

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

載入社群管道和元件

社群管道

請參閱 GitHub Issue #841,瞭解更多關於為什麼我們要新增社群管道的背景資訊,以幫助每個人輕鬆分享他們的工作而不會被拖慢速度。

社群管道是任何與原始論文實現不同(例如,StableDiffusionControlNetPipeline 對應於 使用 ControlNet 條件進行文字到影像生成 論文)的 DiffusionPipeline 類。它們提供附加功能或擴充套件管道的原始實現。

有許多很棒的社群管道,例如 Marigold 深度估計InstantID,您可以在此處找到所有官方社群管道。

社群管道有兩種型別:儲存在 Hugging Face Hub 上的管道和儲存在 Diffusers GitHub 倉庫上的管道。Hub 管道完全可定製(排程器、模型、管道程式碼等),而 Diffusers GitHub 管道僅限於自定義管道程式碼。

GitHub 社群管道 HF Hub 社群管道
用法 相同 相同
評審流程 在 GitHub 上開啟拉取請求並經過 Diffusers 團隊的評審流程才能合併;可能較慢 直接上傳到 Hub 倉庫,無需任何評審;這是最快的工作流程
可見性 包含在官方 Diffusers 倉庫和文件中 包含在您的 HF Hub 個人資料中,依賴您自己的使用/推廣來獲得可見性
Hub 管道
GitHub 管道

要載入 Hugging Face Hub 社群管道,請將社群管道的倉庫 ID 傳遞給 custom_pipeline 引數,以及要從中載入管道權重和元件的模型倉庫。例如,以下示例從 hf-internal-testing/diffusers-dummy-pipeline 載入一個虛擬管道,並從 google/ddpm-cifar10-32 載入管道權重和元件。

透過從 Hugging Face Hub 載入社群管道,您將信任您正在載入的程式碼是安全的。請務必在載入和自動執行之前線上檢查程式碼!

from diffusers import DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    "google/ddpm-cifar10-32", custom_pipeline="hf-internal-testing/diffusers-dummy-pipeline", use_safetensors=True
)

從本地檔案載入

如果傳遞檔案路徑,社群管道也可以從本地檔案載入。傳遞目錄的路徑必須包含一個包含管道類的 pipeline.py 檔案。

pipeline = DiffusionPipeline.from_pretrained(
    "stable-diffusion-v1-5/stable-diffusion-v1-5",
    custom_pipeline="./path/to/pipeline_directory/",
    clip_model=clip_model,
    feature_extractor=feature_extractor,
    use_safetensors=True,
)

從特定版本載入

預設情況下,社群管道從 Diffusers 的最新穩定版本載入。要從另一個版本載入社群管道,請使用 custom_revision 引數。

main
舊版本

例如,從主分支載入

pipeline = DiffusionPipeline.from_pretrained(
    "stable-diffusion-v1-5/stable-diffusion-v1-5",
    custom_pipeline="clip_guided_stable_diffusion",
    custom_revision="main",
    clip_model=clip_model,
    feature_extractor=feature_extractor,
    use_safetensors=True,
)

使用 from_pipe 載入

社群管道也可以使用 from_pipe() 方法載入,該方法允許您載入和重用多個管道而無需額外的記憶體開銷(在重用管道指南中瞭解更多資訊)。記憶體需求由載入的最大單個管道決定。

例如,讓我們載入一個支援長提示和權重的社群管道,該管道來自 Stable Diffusion 管道。

import torch
from diffusers import DiffusionPipeline

pipe_sd = DiffusionPipeline.from_pretrained("emilianJR/CyberRealistic_V3", torch_dtype=torch.float16)
pipe_sd.to("cuda")
# load long prompt weighting pipeline
pipe_lpw = DiffusionPipeline.from_pipe(
    pipe_sd,
    custom_pipeline="lpw_stable_diffusion",
).to("cuda")

prompt = "cat, hiding in the leaves, ((rain)), zazie rainyday, beautiful eyes, macro shot, colorful details, natural lighting, amazing composition, subsurface scattering, amazing textures, filmic, soft light, ultra-detailed eyes, intricate details, detailed texture, light source contrast, dramatic shadows, cinematic light, depth of field, film grain, noise, dark background, hyperrealistic dslr film still, dim volumetric cinematic lighting"
neg_prompt = "(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime, mutated hands and fingers:1.4), (deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, disconnected limbs, mutation, mutated, ugly, disgusting, amputation"
generator = torch.Generator(device="cpu").manual_seed(20)
out_lpw = pipe_lpw(
    prompt,
    negative_prompt=neg_prompt,
    width=512,
    height=512,
    max_embeddings_multiples=3,
    num_inference_steps=50,
    generator=generator,
    ).images[0]
out_lpw
帶有長提示權重的 Stable Diffusion
Stable Diffusion

社群管道示例

社群管道是一種非常有趣和富有創意的方式,可以擴充套件原始管道的功能,新增新的獨特功能。您可以在 diffusers/examples/community 資料夾中找到所有社群管道,並附帶了如何使用它們的推理和訓練示例。

本節展示了一些社群管道,希望它能激發您建立自己的管道(歡迎為您的社群管道提交 PR 並提醒我們進行審查)!

from_pipe() 方法對於載入社群管道特別有用,因為其中許多管道沒有預訓練權重,並且在現有管道(如 Stable Diffusion 或 Stable Diffusion XL)之上添加了功能。您可以在使用 from_pipe 載入部分了解更多關於 from_pipe() 方法的資訊。

萬壽菊
HD-Painter

Marigold 是一種深度估計擴散管道,它利用了擴散模型中豐富的現有固有視覺知識。它接收輸入影像並對其進行去噪和解碼,生成深度圖。Marigold 即使在以前未見過的影像上也能表現良好。

import torch
from PIL import Image
from diffusers import DiffusionPipeline
from diffusers.utils import load_image

pipeline = DiffusionPipeline.from_pretrained(
    "prs-eth/marigold-lcm-v1-0",
    custom_pipeline="marigold_depth_estimation",
    torch_dtype=torch.float16,
    variant="fp16",
)

pipeline.to("cuda")
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/community-marigold.png")
output = pipeline(
    image,
    denoising_steps=4,
    ensemble_size=5,
    processing_res=768,
    match_input_res=True,
    batch_size=0,
    seed=33,
    color_map="Spectral",
    show_progress_bar=True,
)
depth_colored: Image.Image = output.depth_colored
depth_colored.save("./depth_colored.png")
原始影像
彩色深度影像

社群元件

社群元件允許使用者構建可能包含 Diffusers 中不包含的自定義元件的管道。如果您的管道包含 Diffusers 尚未支援的自定義元件,您需要將其實現作為 Python 模組提供。這些自定義元件可以是 VAE、UNet 和排程器。在大多數情況下,文字編碼器是從 Transformers 庫匯入的。管道程式碼本身也可以自定義。

本節展示了使用者應如何使用社群元件來構建社群管道。

您將使用 showlab/show-1-base 管道檢查點作為示例。

  1. 從 Transformers 匯入並載入文字編碼器
from transformers import T5Tokenizer, T5EncoderModel

pipe_id = "showlab/show-1-base"
tokenizer = T5Tokenizer.from_pretrained(pipe_id, subfolder="tokenizer")
text_encoder = T5EncoderModel.from_pretrained(pipe_id, subfolder="text_encoder")
  1. 載入排程器
from diffusers import DPMSolverMultistepScheduler

scheduler = DPMSolverMultistepScheduler.from_pretrained(pipe_id, subfolder="scheduler")
  1. 載入影像處理器
from transformers import CLIPImageProcessor

feature_extractor = CLIPImageProcessor.from_pretrained(pipe_id, subfolder="feature_extractor")

在步驟 4 和 5 中,自定義 UNet管道 實現必須與其檔案所示格式匹配,此示例才能正常工作。

  1. 現在您將載入一個自定義 UNet,在此示例中,為方便起見,它已在 showone_unet_3d_condition.py 中實現。您會注意到 UNet3DConditionModel 類名已更改為 ShowOneUNet3DConditionModel,因為 UNet3DConditionModel 在 Diffusers 中已經存在。ShowOneUNet3DConditionModel 類所需的任何元件都應放在 showone_unet_3d_condition.py 中。

    完成此操作後,您可以初始化 UNet

    from showone_unet_3d_condition import ShowOneUNet3DConditionModel
    
    unet = ShowOneUNet3DConditionModel.from_pretrained(pipe_id, subfolder="unet")
  2. 最後,您將載入自定義管道程式碼。在此示例中,它已為您建立在 pipeline_t2v_base_pixel.py 中。此指令碼包含一個自定義的 TextToVideoIFPipeline 類,用於從文字生成影片。就像自定義 UNet 一樣,自定義管道正常工作所需的任何程式碼都應放在 pipeline_t2v_base_pixel.py 中。

一旦一切就緒,您可以使用 ShowOneUNet3DConditionModel 初始化 TextToVideoIFPipeline

from pipeline_t2v_base_pixel import TextToVideoIFPipeline
import torch

pipeline = TextToVideoIFPipeline(
    unet=unet,
    text_encoder=text_encoder,
    tokenizer=tokenizer,
    scheduler=scheduler,
    feature_extractor=feature_extractor
)
pipeline = pipeline.to(device="cuda")
pipeline.torch_dtype = torch.float16

將管道推送到 Hub 與社群共享!

pipeline.push_to_hub("custom-t2v-pipeline")

管道成功推送後,您需要進行一些更改

  1. model_index.json 中的 _class_name 屬性更改為 "pipeline_t2v_base_pixel""TextToVideoIFPipeline"
  2. showone_unet_3d_condition.py 上傳到 unet 子資料夾。
  3. pipeline_t2v_base_pixel.py 上傳到管道倉庫

要執行推理,在初始化管道時新增 trust_remote_code 引數以處理所有“幕後”的“魔法”。

作為 trust_remote_code=True 的額外預防措施,我們強烈建議您將提交雜湊值傳遞給 from_pretrained() 中的 revision 引數,以確保程式碼沒有被惡意更新(除非您完全信任模型所有者)。

from diffusers import DiffusionPipeline
import torch

pipeline = DiffusionPipeline.from_pretrained(
    "<change-username>/<change-id>", trust_remote_code=True, torch_dtype=torch.float16
).to("cuda")

prompt = "hello"

# Text embeds
prompt_embeds, negative_embeds = pipeline.encode_prompt(prompt)

# Keyframes generation (8x64x40, 2fps)
video_frames = pipeline(
    prompt_embeds=prompt_embeds,
    negative_prompt_embeds=negative_embeds,
    num_frames=8,
    height=40,
    width=64,
    num_inference_steps=2,
    guidance_scale=9.0,
    output_type="pt"
).frames

作為額外參考,請檢視 stabilityai/japanese-stable-diffusion-xl 的倉庫結構,它也使用了 trust_remote_code 功能。

from diffusers import DiffusionPipeline
import torch

pipeline = DiffusionPipeline.from_pretrained(
    "stabilityai/japanese-stable-diffusion-xl", trust_remote_code=True
)
pipeline.to("cuda")
< > 在 GitHub 上更新

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