Diffusers 有哪些新功能?🎨
一個半月前,我們釋出了 `diffusers`,這是一個為跨模態擴散模型提供模組化工具箱的庫。幾周後,我們釋出了對 Stable Diffusion 的支援,這是一種高質量的文字到影像模型,並提供免費演示供所有人試用。除了耗費大量 GPU 外,在過去三週,團隊決定為該庫新增一到兩個新功能,我們希望社群會喜歡!這篇部落格文章概述了 `diffusers` 0.3 版本中的新功能!請記得給 GitHub 倉庫 一個 ⭐。
影像到影像 (Image to Image) 流水線
最受歡迎的功能之一是影像到影像生成。這個流水線允許您輸入一張影像和一個提示,它將根據這些輸入生成一張影像!
我們來看看基於官方 Colab notebook 的一些程式碼。
from diffusers import StableDiffusionImg2ImgPipeline
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
revision="fp16",
torch_dtype=torch.float16,
use_auth_token=True
)
# Download an initial image
# ...
init_image = preprocess(init_img)
prompt = "A fantasy landscape, trending on artstation"
images = pipe(prompt=prompt, init_image=init_image, strength=0.75, guidance_scale=7.5, generator=generator)["sample"]
沒有時間寫程式碼?不用擔心,我們還建立了一個 空間演示,您可以直接在那裡試用
文字反轉 (Textual Inversion)
文字反轉 (Textual Inversion) 允許您僅使用 3-5 個樣本,即可在您自己的影像上個性化 Stable Diffusion 模型。透過此工具,您可以訓練一個關於概念的模型,然後將該概念與社群中的其他人分享!
短短幾天內,社群就分享了 200 多個概念!快來看看吧!
- 包含概念的組織。
- Navigator Colab:視覺化瀏覽和使用社群建立的 150 多個概念。
- 訓練 Colab:教 Stable Diffusion 一個新概念,並與社群分享。
- 推理 Colab:使用學習到的概念執行 Stable Diffusion。
實驗性修復 (Inpainting) 流水線
修復功能允許您提供一張影像,然後選擇影像中的一個區域(或提供一個蒙版),並使用 Stable Diffusion 替換該蒙版區域。這是一個示例:

您可以嘗試一個極簡的 Colab notebook 或檢視下面的程式碼。演示即將推出!
from diffusers import StableDiffusionInpaintPipeline
pipe = StableDiffusionInpaintPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
revision="fp16",
torch_dtype=torch.float16,
use_auth_token=True
).to(device)
images = pipe(
prompt=["a cat sitting on a bench"] * 3,
init_image=init_image,
mask_image=mask_image,
strength=0.75,
guidance_scale=7.5,
generator=None
).images
請注意,這是實驗性的,因此仍有改進空間。
針對小型 GPU 的最佳化
經過一些改進,擴散模型可以佔用更少的視訊記憶體。🔥 例如,Stable Diffusion 只佔用 3.2GB!這會產生完全相同的結果,但速度會犧牲 10%。以下是如何使用這些最佳化:
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
revision="fp16",
torch_dtype=torch.float16,
use_auth_token=True
)
pipe = pipe.to("cuda")
pipe.enable_attention_slicing()
這非常令人興奮,因為它將進一步降低使用這些模型的門檻!
Mac OS 上的 Diffusers
🍎 沒錯!又一個廣受好評的功能釋出了!請閱讀官方文件中的完整說明(包括效能比較、規格等)。
使用 PyTorch mps 裝置,擁有 M1/M2 硬體的使用者可以執行 Stable Diffusion 推理。🤯 這對使用者來說只需要最少的設定,快來試試吧!
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True)
pipe = pipe.to("mps")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
實驗性 ONNX 匯出器和流水線
新的實驗性流水線允許使用者在任何支援 ONNX 的硬體上執行 Stable Diffusion。這是一個如何使用它的示例(請注意,正在使用 `onnx` 修訂版):
from diffusers import StableDiffusionOnnxPipeline
pipe = StableDiffusionOnnxPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4",
revision="onnx",
provider="CPUExecutionProvider",
use_auth_token=True,
)
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
另外,您也可以直接使用匯出指令碼將您的 SD 檢查點轉換為 ONNX。
python scripts/convert_stable_diffusion_checkpoint_to_onnx.py --model_path="CompVis/stable-diffusion-v1-4" --output_path="./stable_diffusion_onnx"
新文件
以上所有功能都非常酷。作為開源庫的維護者,我們深知高質量文件的重要性,它能讓任何人儘可能輕鬆地試用該庫。
💅 因此,我們進行了一次文件衝刺,非常高興釋出我們的文件的第一個版本。這是第一個版本,所以我們計劃新增許多內容(並且始終歡迎貢獻!)。
文件的一些亮點
社群
在我們進行上述工作的同時,社群也沒有閒著!以下是一些(儘管不全面)已完成工作的亮點:
Stable Diffusion 影片
透過探索潛在空間並在文字提示之間進行形態轉換,使用 Stable Diffusion 建立 🔥 影片。您可以:
- 夢想同一提示的不同版本
- 在不同的提示之間進行形態轉換
Stable Diffusion Videos 工具可透過 pip 安裝,附帶 Colab notebook 和 Gradio notebook,並且非常易於使用!
這裡是一個例子
from stable_diffusion_videos import walk
video_path = walk(['a cat', 'a dog'], [42, 1337], num_steps=3, make_video=True)
Diffusers Interpret
Diffusers interpret 是一個基於 `diffusers` 構建的可解釋性工具。它具有以下酷炫功能:
- 檢視擴散過程中的所有影像
- 分析提示中每個 token 如何影響生成
- 如果您想了解影像的某個部分,可以在指定的邊界框內進行分析
# pass pipeline to the explainer class
explainer = StableDiffusionPipelineExplainer(pipe)
# generate an image with `explainer`
prompt = "Corgi with the Eiffel Tower"
output = explainer(
prompt,
num_inference_steps=15
)
output.normalized_token_attributions # (token, attribution_percentage)
#[('corgi', 40),
# ('with', 5),
# ('the', 5),
# ('eiffel', 25),
# ('tower', 25)]
日語 Stable Diffusion
名字說明了一切!JSD 的目標是訓練一個也能捕捉文化、身份和獨特表達資訊的模型。它使用 1 億張帶有日語字幕的圖片進行訓練。您可以在模型卡片中閱讀有關模型訓練方式的更多資訊
Waifu Diffusion
Waifu Diffusion 是一個經過微調的 SD 模型,用於生成高質量的動漫影像。

交叉注意力控制
交叉注意力控制允許透過修改擴散模型的注意力圖來精細控制提示。您可以做一些很酷的事情:
- 替換提示中的目標(例如,將 cat 替換為 dog)
- 減少或增加提示中單詞的重要性(例如,如果您希望“岩石”獲得較少的注意力)
- 輕鬆注入樣式
以及更多!請檢視倉庫。
可複用種子
Stable Diffusion 最令人印象深刻的早期演示之一是種子的重用以調整影像。其思想是使用感興趣影像的種子,並使用不同的提示生成新影像。這會產生一些很酷的結果!請檢視 Colab
感謝閱讀!
希望您閱讀愉快!請記得在我們的GitHub 倉庫中給我們點贊,並加入Hugging Face Discord 伺服器,我們那裡有一個專門討論擴散模型的頻道。那裡會分享庫的最新訊息!
歡迎隨時提出功能請求和錯誤報告!如果沒有如此優秀的社群,這一切都不可能實現。