Diffusers 文件
文字反轉 (Textual Inversion)
加入 Hugging Face 社群
並獲得增強的文件體驗
開始使用
文字反演
文字反演(Textual Inversion)是一種用於生成概念個性化影像的方法。它透過對模型的詞嵌入進行微調,使用 3-5 張概念影像(例如,畫素藝術),並將這些影像與一個獨特的標記(<sks>
)關聯起來。這使得您可以在提示詞中使用 <sks>
標記,從而觸發模型生成畫素藝術影像。
文字反演的權重非常輕量,通常只有幾 KB,因為它們只是詞嵌入。然而,這也意味著詞嵌入需要在透過 from_pretrained() 載入模型之後再載入。
import torch
from diffusers import AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5",
torch_dtype=torch.float16
).to("cuda")
使用 load_textual_inversion() 載入詞嵌入,並在提示詞中包含唯一的標記以啟用其生成。
pipeline.load_textual_inversion("sd-concepts-library/gta5-artwork")
prompt = "A cute brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration, <gta5-artwork> style"
pipeline(prompt).images[0]

文字反演還可以訓練學習**負向嵌入**,以避免生成不必要的特徵,例如“模糊”或“醜陋”。它對於提高影像質量非常有用。
EasyNegative 是一個廣泛使用的負向嵌入,其中包含多個已學習的負向概念。載入負向嵌入並指定與負向嵌入相關聯的檔名和標記。將該標記傳遞給流水線中的 negative_prompt
即可啟用它。
import torch
from diffusers import AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5",
torch_dtype=torch.float16
).to("cuda")
pipeline.load_textual_inversion(
"EvilEngine/easynegative",
weight_name="easynegative.safetensors",
token="easynegative"
)
prompt = "A cute brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration"
negative_prompt = "easynegative"
pipeline(prompt, negative_prompt).images[0]
