Diffusers 文档

文本反转 (Textual Inversion)

Hugging Face's logo
加入 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]
< > 在 GitHub 上更新

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