Diffusers 文件
Stable Diffusion 流水線
並獲得增強的文件體驗
開始使用
Stable Diffusion 流水線
Stable Diffusion 是由 CompVis、Stability AI 和 LAION 的研究人員和工程師建立的文字到影像潛在擴散模型。潛在擴散在較低維度的潛在空間上應用擴散過程,以減少記憶體和計算複雜度。這種特定型別的擴散模型由 Robin Rombach、Andreas Blattmann、Dominik Lorenz、Patrick Esser 和 Björn Ommer 在 《高解析度影像合成與潛在擴散模型》 中提出。
Stable Diffusion 在 LAION-5B 資料集的子集上使用 512x512 影像進行訓練。該模型使用凍結的 CLIP ViT-L/14 文字編碼器,根據文字提示調節模型。憑藉其 8.6 億引數的 UNet 和 1.23 億引數的文字編碼器,該模型相對輕量級,可以在消費級 GPU 上執行。
有關 Stable Diffusion 如何工作以及它與基礎潛在擴散模型的區別的更多詳細資訊,請參閱 Stability AI 的公告以及我們自己的部落格文章,瞭解更多技術細節。
您可以在 CompVis/stable-diffusion 找到 Stable Diffusion v1.0 的原始程式碼庫,在 Stability-AI/stablediffusion 找到 Stable Diffusion v2.0 的程式碼庫,以及它們用於各種任務的原始指令碼。其他不同 Stable Diffusion 版本和任務的官方檢查點可以在 CompVis、Runway 和 Stability AI Hub 組織上找到。探索這些組織,為您的用例找到最佳檢查點!
下表總結了可用的 Stable Diffusion 流水線、它們支援的任務和互動式演示
流水線 | 支援的任務 | 🤗 Space |
---|---|---|
StableDiffusion | 文字到影像 | |
StableDiffusionImg2Img | 影像到影像 | |
StableDiffusionInpaint | 影像修復 | |
StableDiffusionDepth2Img | 深度到影像 | |
StableDiffusionImageVariation | 影像變異 | |
StableDiffusionPipelineSafe | 過濾文字到影像 | |
StableDiffusion2 | 文字到影像、影像修復、深度到影像、超解析度 | |
StableDiffusionXL | 文字到影像、影像到影像 | |
StableDiffusionLatentUpscale | 超解析度 | |
StableDiffusionUpscale | 超解析度 | |
StableDiffusionLDM3D | 文字到 RGB、文字到深度、文字到全景 | |
StableDiffusionUpscaleLDM3D | ldm3d 超解析度 |
提示
為了幫助您充分利用 Stable Diffusion 流水線,這裡有一些提高效能和可用性的提示。這些提示適用於所有 Stable Diffusion 流水線。
探索速度和質量之間的權衡
StableDiffusionPipeline 預設使用 PNDMScheduler,但 🤗 Diffusers 提供了許多其他相容的排程器(其中一些速度更快或輸出質量更好)。例如,如果您想使用 EulerDiscreteScheduler 而不是預設的排程器:
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
# or
euler_scheduler = EulerDiscreteScheduler.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="scheduler")
pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", scheduler=euler_scheduler)
重用流水線元件以節省記憶體
為了節省記憶體並在多個流水線中重用相同的元件,請使用 .components
方法,以避免多次將權重載入到 RAM 中。
from diffusers import (
StableDiffusionPipeline,
StableDiffusionImg2ImgPipeline,
StableDiffusionInpaintPipeline,
)
text2img = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
img2img = StableDiffusionImg2ImgPipeline(**text2img.components)
inpaint = StableDiffusionInpaintPipeline(**text2img.components)
# now you can use text2img(...), img2img(...), inpaint(...) just like the call methods of each respective pipeline
使用 Gradio 建立 Web 演示
Stable Diffusion 流水線自動支援 Gradio,這是一個讓在 Web 上建立美觀且使用者友好的機器學習應用程式變得輕而易舉的庫。首先,請確保您已安裝 Gradio:
pip install -U gradio
然後,圍繞任何基於 Stable Diffusion 的流水線建立 Web 演示。例如,您可以使用 Gradio 的 Interface.from_pipeline
函式,只需一行程式碼即可建立影像生成流水線:
from diffusers import StableDiffusionPipeline
import gradio as gr
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
gr.Interface.from_pipeline(pipe).launch()
這將在您的瀏覽器中開啟一個直觀的拖放介面:
同樣,您可以使用以下程式碼為影像到影像流水線建立演示:
from diffusers import StableDiffusionImg2ImgPipeline
import gradio as gr
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5")
gr.Interface.from_pipeline(pipe).launch()
預設情況下,Web 演示在本地伺服器上執行。如果您想與他人共享,可以透過在 `launch()` 中設定 `share=True` 來生成一個臨時的公共連結。或者,您可以將您的演示託管在 Hugging Face Spaceshttps://huggingface.co/spaces 上,以獲得永久連結。
< > 在 GitHub 上更新