Sign In

【玩转SD】概念提取法LECO,背景模糊lora实验

【玩转SD】概念提取法LECO,背景模糊lora实验

LECO的直译并不是概念提取,概念提取更多是我的个人意译。

LECO全称Low-rank adaptation for Erasing COncepts from diffusion models.

简单来说,通过Erasing Concepts from Diffusion Models(下称消除概念)的原理来把特定的tag从底模里学习到lora里面,来达到消除/增强特定概念的方法。

由于消除概念的原目的是消除大模型里面的特定概念,比如nsfw,所以通过LECO制作出来的lora,正向权重是消除概念,负面是增强概念,而负值可以去到-4~-8等等

消除概念的一些示例

我们可以看到第一列第二张图,在消除梵高的概念后,通过梵高tag,生成出来的图就和梵高的风格就完全不一样了,而消除其他艺术家,比如毕加索的风格,并没有影响到梵高tag生成的图,SLD是Safe Latent Diffusion,另一种避开nsfw的方法。更多的例子,更详细的解释看Ref.

而基于消除概念和conceptmod的启发,p1atdev魔改了lora训练的脚本,通过把消除的概念提取出来,让lora学习这个特定的概念,制作出消除了这个概念的lora,而Lora本身可以正向和负向操作,使用负值就可以增强这个被提取出来的概念。

相对于单纯的tag权重,lora由于学习到提取出来的概念的合集,可以让图片更多元化?

blur_background:2,4,6,8

blur_background, <lora:blur_background:-2>,

同样的参数,可以看到blur_background lora权重到达-6的时候会触发一个特写,这就是lora在学习过程中,因为blur_background产生的数据里面包含了特写的相关数据,所以在权重特别大的时候能触发出来(猜的)

负面tag: blur_background:2,4,6,8

<lora:blur_background:2>,

单说blur_background这个lora,比较推荐-6到正6的权重,正面权重可以不放blur_background来减少背景模糊的效果,负面权重可以放blur_background增强背景模糊效果。

那么是如何使用LECO来训练这个Lora的呢?

使用了p1atdev的LECO脚本(https://github.com/p1atdev/LECO)

Setup

conda create -n leco python=3.10 

pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118 

pip install xformers 

pip install -r requirements.txt

需要有8G 显存

open anaconda prompt

conda activate leco

cd到LECO文件夹

python ./train_lora.py --config_file "./examples/config.yaml"

讲解一下config.yaml

每个config都有对应的prompts.yaml,里面定义了要学(忘?)的概念

prompts_file: "./examples/blur_prompts.yaml"

pretrained_model:

  name_or_path: "X:/...略.../xxx.safetensors" 也可以是ckpt 文件路径

  v2: false 

  v_pred: true

network:

  type: "lierla"

  rank: 16

  alpha: 1.0

train:

  precision: "bfloat16"

  noise_scheduler: "ddim"

  iterations: 1000 一般1000步就够了,觉得不够的话就2000步,2000步以上就没多少意义了

  lr: 1e-4

  optimizer: "lion"

  lr_scheduler: "cosine"

save:

  name: "blur"

  path: "./output/blur"

  per_steps: 500 这里是每多少步保存一个lora文件

  precision: "bfloat16"

logging:

  use_wandb: false 如果有wandb野可以开

  verbose: false

other:

  use_xformers: true

blur_prompts.yaml

通过这个文件来决定要学习的概念

可以定义多个target概念,脚本会根据这些概念用底模生成数据来让lora学习

- target: "blur"

  resolution: 512

  batch_size: 3

- target: "blurry"

  resolution: 512

  batch_size: 3

- target: "blurry background"

  resolution: 512

  batch_size: 3

- target: "blur,blurry,blurry background"

  resolution: 512

  batch_size: 3

可以定义不同分辨率

- target: "blur"

  resolution: 640

  batch_size: 2

- target: "blurry"

  resolution: 640

  batch_size: 2

- target: "blurry background"

  resolution: 640

  batch_size: 2

- target: "blur,blurry,blurry background"

  resolution: 640

  batch_size: 2

- target: "blur"

  resolution: 768

  batch_size: 1

- target: "blurry"

  resolution: 768

  batch_size: 1

- target: "blurry background"

  resolution: 768

  batch_size: 1

- target: "blur,blurry,blurry background"

  resolution: 768

  batch_size: 1

具体的配合官方例子设置

一些官方的例子:

With "real life, instagram":

unrealWithout "real life, instagram":

unreal

laksjdjf的sd-trainer在dev里面也添加了LECO工具,可以改善p1atdev的训练过程中生成数据导致速度比较慢的问题。

除了单一概念的提出,有想过在提取出来的概念里面加一点各种各样的氛围,比如模糊背景加一个克苏鲁风格,那是不是就可以产生克苏鲁风格的模糊背景?

组合各种各样的概念融合进行提取,产生各种有趣的效果lora。

背景模糊lora

C站

https://civitai.com/models/100363/blur-background
l站

https://www.liblibai.com/modelinfo/b94b6999db15429493f613bef34f47b0?mine=1

看论文,研究脚本,训练lora,各种验证测试,还是花费了好几天的,

欢迎各位留言转发,

有能力的还可以给我充电支持一下,十分感谢。

https://www.buymeacoffee.com/JasonAICreator

有个AI群,欢迎各位加入一起研究进步。

Ref:

https://github.com/p1atdev/LECO

https://github.com/laksjdjf/sd-trainer/tree/dev

https://erasing.baulab.info/

0

Comments