使用 T2I-Adapters 對 SDXL 進行高效可控生成

釋出於 2023 年 9 月 8 日
在 GitHub 上更新

T2I-Adapter 是一個高效的即插即用模型,它能為預訓練的文字到影像模型提供額外引導,同時凍結原始大型文字到影像模型。T2I-Adapter 將 T2I 模型中的內部知識與外部控制訊號對齊。我們可以根據不同的條件訓練各種介面卡,實現豐富的控制和編輯效果。

作為同時期工作,ControlNet 具有類似的功能並被廣泛使用。然而,它的執行成本可能非常高昂。這是因為在逆向擴散過程的每個去噪步驟中,ControlNet 和 UNet 都需要執行。此外,ControlNet 強調將 UNet 編碼器複製為控制模型的重要性,導致引數數量更大。因此,生成過程受 ControlNet 大小的限制(越大,過程越慢)。

在此方面,T2I-Adapters 比 ControlNet 具有競爭優勢。T2I-Adapters 尺寸更小,並且與 ControlNet 不同,T2I-Adapters 在整個去噪過程中只需執行一次。

模型型別 模型引數 儲存 (fp16)
ControlNet-SDXL 1251 M 2.5 GB
ControlLoRA(秩為 128) 197.78 M(減少 84.19%) 396 MB(減少 84.53%)
T2I-Adapter-SDXL 79 M(減少 93.69% 158 MB(減少 94%

在過去幾週中,Diffusers 團隊和 T2I-Adapter 作者一直在合作,以便在 diffusers 中支援 Stable Diffusion XL (SDXL) 的 T2I-Adapters。在這篇部落格文章中,我們分享了從頭開始在 SDXL 上訓練 T2I-Adapters 的發現,一些引人注目的結果,當然還有各種條件(草圖、Canny、線稿、深度和 OpenPose)下的 T2I-Adapter 檢查點!

Collage of the results

與 T2I-Adapter 的早期版本 (SD-1.4/1.5) 相比,T2I-Adapter-SDXL 仍沿用原始方法,使用一個 79M 的介面卡驅動 2.6B 的 SDXL!T2I-Adapter-SDXL 在繼承 SDXL 高質量生成的同時,保持了強大的控制能力!

使用 diffusers 訓練 T2I-Adapter-SDXL

我們的訓練指令碼是基於 diffusers 提供的官方示例構建的。

本部落格文章中提到的大多數 T2I-Adapter 模型都是在 LAION-Aesthetics V2 的 3M 高解析度影像-文字對上訓練的,具體設定如下:

  • 訓練步驟:20000-35000
  • 批次大小:資料並行,單個 GPU 批次大小為 16,總批次大小為 128。
  • 學習率:恆定學習率 1e-5。
  • 混合精度:fp16

我們鼓勵社群使用我們的指令碼來訓練自定義且強大的 T2I-Adapters,在速度、記憶體和質量之間取得競爭性的權衡。

diffusers 中使用 T2I-Adapter-SDXL

在這裡,我們以線稿條件為例,演示 T2I-Adapter-SDXL 的用法。首先,安裝所需的依賴項

pip install -U git+https://github.com/huggingface/diffusers.git
pip install -U controlnet_aux==0.0.7 # for conditioning models and detectors
pip install transformers accelerate 

T2I-Adapter-SDXL 的生成過程主要包括以下兩個步驟:

  1. 條件影像首先被準備成適當的控制影像格式。
  2. 控制影像提示被傳遞給 StableDiffusionXLAdapterPipeline

讓我們看一個使用線稿介面卡的簡單示例。我們首先初始化 SDXL 的 T2I-Adapter 管道和線稿檢測器。

import torch
from controlnet_aux.lineart import LineartDetector
from diffusers import (AutoencoderKL, EulerAncestralDiscreteScheduler,
                       StableDiffusionXLAdapterPipeline, T2IAdapter)
from diffusers.utils import load_image, make_image_grid

# load adapter
adapter = T2IAdapter.from_pretrained(
    "TencentARC/t2i-adapter-lineart-sdxl-1.0", torch_dtype=torch.float16, varient="fp16"
).to("cuda")

# load pipeline
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
euler_a = EulerAncestralDiscreteScheduler.from_pretrained(
    model_id, subfolder="scheduler"
)
vae = AutoencoderKL.from_pretrained(
    "madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
)
pipe = StableDiffusionXLAdapterPipeline.from_pretrained(
    model_id,
    vae=vae,
    adapter=adapter,
    scheduler=euler_a,
    torch_dtype=torch.float16,
    variant="fp16",
).to("cuda")

# load lineart detector
line_detector = LineartDetector.from_pretrained("lllyasviel/Annotators").to("cuda")

然後,載入影像以檢測線稿

url = "https://huggingface.co/Adapter/t2iadapter/resolve/main/figs_SDXLV1.0/org_lin.jpg"
image = load_image(url)
image = line_detector(image, detect_resolution=384, image_resolution=1024)

Lineart Dragon

然後我們生成

prompt = "Ice dragon roar, 4k photo"
negative_prompt = "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured"
gen_images = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    image=image,
    num_inference_steps=30,
    adapter_conditioning_scale=0.8,
    guidance_scale=7.5,
).images[0]
gen_images.save("out_lin.png")

Lineart Generated Dragon

有兩個重要的引數需要理解,它們可以幫助您控制條件化的程度。

  1. adapter_conditioning_scale

    此引數控制條件化對輸入的影響程度。值越高表示條件化效果越強,反之亦然。

  2. adapter_conditioning_factor

    此引數控制應該應用條件化的初始生成步驟數。值應設定在 0-1 之間(預設值為 1)。`adapter_conditioning_factor=1` 的值意味著介面卡應該應用於所有時間步,而 `adapter_conditioning_factor=0.5` 意味著它只應用於前 50% 的步驟。

有關更多詳細資訊,歡迎您查閱官方文件

試用演示

您可以在此空間或下方嵌入的遊樂場中輕鬆試用 T2I-Adapter-SDXL

您還可以嘗試使用草圖模型構建的 Doodly,它將您的塗鴉轉換為逼真的影像(透過語言監督)

更多結果

下面,我們展示了使用不同條件獲得的結果。我們還補充了指向其相應預訓練檢查點的連結。它們的模型卡包含更多關於它們如何訓練以及示例用法的資訊。

線稿引導

線稿引導的更多結果 模型來自 TencentARC/t2i-adapter-lineart-sdxl-1.0

草圖引導

草圖引導結果 模型來自 TencentARC/t2i-adapter-sketch-sdxl-1.0

Canny 引導

草圖引導結果 模型來自 TencentARC/t2i-adapter-canny-sdxl-1.0

深度引導

深度引導結果 深度引導模型分別來自 TencentARC/t2i-adapter-depth-midas-sdxl-1.0TencentARC/t2i-adapter-depth-zoe-sdxl-1.0

OpenPose 引導

OpenPose 引導結果 模型來自 TencentARC/t2i-adapter-openpose-sdxl-1.0


致謝:非常感謝 William Berman 幫助我們訓練模型並分享他的見解。

社群

註冊登入 發表評論

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