https://huggingface.co/docs/diffusers/main/en/using-diffusers/loading_adaptershttps://huggingface.co/docs/diffusers/main/en/using-diffusers/loading_adapters
有几种训练技术可以个性化扩散模型,生成特定主题的图像或某些风格的图像。每种训练方法都会产生不同类型的适配器。一些适配器会生成全新的模型,而其他适配器只修改较小的一组嵌入或权重。这意味着每个适配器的加载过程也是不同的。
1.Dreambooth
DreamBooth针对一个主题的几张图像微调整个扩散模型,以生成该主题的具有新风格和设置的图像。这种方法是通过在提示中使用一个特殊单词来触发模型学习与主题图像相关联。在所有的训练方法中,DreamBooth生成的文件大小最大(通常为几GB),因为它是一个完整的模型。
from diffusers import AutoPipelineForText2Image
import torchpipeline = AutoPipelineForText2Image.from_pretrained("sd-dreambooth-library/herge-style", torch_dtype=torch.float16).to("cuda")
prompt = "A cute herge_style brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration"
image = pipeline(prompt).images[0]
2.Textual inversion
Textual inversion与DreamBooth非常相似,也可以个性化扩散模型,从仅有的几张图像中生成特定的概念(风格、物体)。这种方法通过训练和寻找新的嵌入来表示在提示中使用特殊单词提供的图像。因此,扩散模型的权重保持不变,而训练过程会生成一个相对较小(几KB)的文件。由于文本反演会创建嵌入,它不能像DreamBooth一样单独使用,需要另一个模型。
from diffusers import AutoPipelineForText2Image
import torchpipeline = AutoPipelineForText2Image.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")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"
image = pipeline(prompt).images[0]
文本反演还可以训练不受欢迎的内容,以创建负向嵌入,防止模型生成具有这些不受欢迎的内容的图像,例如模糊的图像或手上额外的手指。这是一个快速改进提示的简单方法。您也可以使用load_textual_inversion()来加载嵌入,但这次需要两个参数:
weight_name:如果文件以特定名称保存在Diffusers格式中,或者文件存储在A1111格式中,则指定要加载的权重文件。 token:指定在提示中使用的特殊单词,以触发嵌入。
pipeline.load_textual_inversion("sayakpaul/EasyNegative-test", weight_name="EasyNegative.safetensors", token="EasyNegative"
)prompt = "A cute brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration, EasyNegative"
negative_prompt = "EasyNegative"image = pipeline(prompt, negative_prompt=negative_prompt, num_inference_steps=50).images[0]
3.lora
LoRA是一种流行的训练技术,因为它速度快且生成较小的文件大小(几百MB),可以训练模型从仅有的几张图像中学习新的风格。它通过向扩散模型中插入新的权重,然后仅对新的权重进行训练,而不是整个模型。LoRA是一种非常通用的训练技术,可与其他训练方法一起使用。例如,通常使用DreamBooth和LoRA共同训练模型。
from diffusers import AutoPipelineForText2Image
import torchpipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")pipeline.load_lora_weights("ostris/super-cereal-sdxl-lora", weight_name="cereal_box_sdxl_v1.safetensors")
prompt = "bears, pizza bites"
image = pipeline(prompt).images[0]
load_lora_weights()方法会将LoRA的权重加载到UNet和文本编码器中。这是加载LoRA首选的方式,因为它可以处理以下情况:1.LoRA的权重没有分别给UNet和文本编码器的单独标识符;2.LoRA的权重有单独给UNet和文本编码器的标识符。但是,如果只需要将LoRA的权重加载到UNet中,那么可以使用load_attn_procs()方法。加载jbilcke-hf/sdxl-cinematic-1的LoRA权重:
from diffusers import AutoPipelineForText2Image
import torchpipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")
pipeline.unet.load_attn_procs("jbilcke-hf/sdxl-cinematic-1", weight_name="pytorch_lora_weights.safetensors")# use cnmt in the prompt to trigger the LoRA
prompt = "A cute cnmt eating a slice of pizza, stunning color scheme, masterpiece, illustration"
image = pipeline(prompt).images[0]
可以传递cross_attention_kwargs={"scale":0.5}来调节lora的权重。
4. load multiple lora
融合权重可以加快推理延迟,因为不需要单独加载基础模型和LoRA!可以使用save_pretrained()保存融合后的管道,以避免每次使用模型时都需要加载和融合权重。
from diffusers import StableDiffusionXLPipeline, AutoencoderKL
import torchvae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
pipeline = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",vae=vae,torch_dtype=torch.float16,
).to("cuda")pipeline.load_lora_weights("ostris/ikea-instructions-lora-sdxl")
pipeline.fuse_lora(lora_scale=0.7)# to unfuse the LoRA weights
pipeline.unfuse_lora()pipeline.load_lora_weights("ostris/super-cereal-sdxl-lora")
pipeline.fuse_lora(lora_scale=0.7)prompt = "A cute brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration"
image = pipeline(prompt).images[0]
5.PEFT
from diffusers import DiffusionPipeline
import torchpipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")
pipeline.load_lora_weights("ostris/ikea-instructions-lora-sdxl", weight_name="ikea_instructions_xl_v1_5.safetensors", adapter_name="ikea")
pipeline.load_lora_weights("ostris/super-cereal-sdxl-lora", weight_name="cereal_box_sdxl_v1.safetensors", adapter_name="cereal")pipeline.set_adapters(["ikea", "cereal"], adapter_weights=[0.7, 0.5])prompt = "A cute brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration"
image = pipeline(prompt, num_inference_steps=30, cross_attention_kwargs={"scale": 1.0}).images[0]
kohya and TheLastBen
from diffusers import AutoPipelineForText2Image
import torchpipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0").to("cuda")
pipeline.load_lora_weights("path/to/weights", weight_name="blueprintify-sd-xl-10.safetensors")# use bl3uprint in the prompt to trigger the LoRA
prompt = "bl3uprint, a highly detailed blueprint of the eiffel tower, explaining how to build all parts, many txt, blueprint grid backdrop"
image = pipeline(prompt).images[0]
无法加载LyCORIS