見出し画像

SDXLでオークLoRAを作成する試み

GPUの排熱が暖房になる季節になって参りました。
皆さまいかがお過ごしでしょうか。私は枕元でGPUファン全開でも眠れる体を手にいれました。
ところで最近、私的におすすめLoRAがありましてそれがこちらです。

その名もオークLoRA!これがあれば性癖のオークイラストを量産できるってワケよ!!
こんな雑なラフが~!!

こうなる~!(きゃっきゃ

https://twitter.com/tori29umai/status/1729657411290513753

しかしこちらのLoRAはSD1.5専用なのでSDXLでは使えません(巷ではX-Adapterなるものの話もでていますが)。
しかしありがたいことに!こちらのオークLoRAの作者であるAI筆様がオークLoRAに使ったデータセット公開してくださりました!!!

こちらがあればSDXLでもオークLoRAが作れるじゃんひゃっほう!となったのでチャレンジしてみることにしました

SDXLオークLoRA学習手順

①環境構築

まずいつもの環境作成から。サクッとメモだけ残しておきます。

git clone -b sdxl https://github.com/kohya-ss/sd-scripts.git
cd sd-scripts

python -m venv venv
.\venv\Scripts\activate

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
pip install -U xformers
pip install --upgrade -r requirements.txt
python -m pip install bitsandbytes==0.41.1 --prefer-binary --extra-index-url=https://jllllll.github.io/bitsandbytes-windows-webui 

accelerate config

- This machine
- No distributed training
- NO
- NO
- NO
- all
- bf16

②データセットの用意。キャプション付け

配布データ画像44枚を1000という名前のフォルダに格納

WebUIのtaggerでキャプション付け
Additional tags (comma split)

orc

Exclude tag

muscular, fang out, green skin, colored skin, pointy ears, tusks, thick eyebrows

オークの女性も出したかったので、敢えて性別指定系タグやヒゲ等は含めませんでした。

③設定ファイルの用意

SDXL.toml

pretrained_model_name_or_path = "C:/stable-diffusion-webui/models/Stable-diffusion/blue_pencil-XL-v2.0.0.safetensors"
sample_prompts = "C:/sd-scripts/user_config/orc/prompts.txt"
seed = 42
xformers = true
max_data_loader_n_workers = 4
persistent_data_loader_workers = true
gradient_checkpointing = true
resolution = "1024,1024"
enable_bucket = true
max_bucket_reso = 1600
train_batch_size = 12
mixed_precision = "bf16"
save_precision = "bf16"
save_every_n_steps = 500
sample_every_n_steps = 500
sample_sampler = "k_euler_a"
optimizer_type = "AdamW"
unet_lr = 3e-5
network_module = "networks.lora"
network_dim = 32
network_args = ["conv_dim=32"]
cache_latents = true
cache_latents_to_disk = true
caption_extension = ".txt"
save_state = true
flip_aug = true
shuffle_caption = true
keep_tokens = 1

prompts.txt

orc,  1boy,  mature male, muscular male, bara, monster boy, topless male, looking at viewer, short hair, upper body --w 1024 --h 1024 --d 1

④学習実行

accelerate launch ^
  --num_cpu_threads_per_process 12 ^
  sdxl_train_network.py ^
  --config_file="C:\sd-scripts\user_config\orc\SDXL.toml" ^
  --train_data_dir="C:\sd-scripts\user_config\orc\img" ^
  --output_dir="C:\sd-scripts\user_config\output\orc" ^
  --output_name=orc3 ^
  --max_train_steps 1500

ファイルサイズがでかいので半分にリサイズ


python .\networks\resize_lora.py ^
--save_to "C:\sd-scripts\user_config\output\orc\orc3_16.safetensors" ^
--model "C:\sd-scripts\user_config\output\orc\orc3.safetensors" ^
--new_rank 16 ^
--save_precision bf16 ^
--device cuda ^
--dynamic_param 16

⑤検証

①オークLoRAなし

orc, 1boy, solo, looking at viewer, upper body, white background Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1410822072, Size: 1024x1024, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, Version: v1.6.0-2-g4afaaf8a

①オークLoRAあり

orc, 1boy, solo, looking at viewer, upper body, white background <lora:orc3:1>  Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1410822072, Size: 1024x1024, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, Lora hashes: "orc3: b9fdaee01f36", Version: v1.6.0-2-g4afaaf8a

おお、洋ゲーみが薄れ、AI筆さんみのあるオークになっています・・・!
(個人的にこっちの方が好み)
もうちょいフロンプトを複雑にしてみましょう。

②オークLoRAなし

orc, male focus, solo, bara, large pectorals, pectorals, 1boy, muscular male, nipples, stomach, thighs, monster boy, navel, abs, thick thighs, navel hair, mature male, topless male, short hair, full body, loincloth, blush, looking at viewer, pubic hair, smile, male pubic hair, bulge, fangs, facial hair Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 2564636670, Size: 1024x1024, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, TI hashes: "negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a

②オークLoRAあり

orc, male focus, solo, bara, large pectorals, pectorals, 1boy, muscular male, nipples, stomach, thighs, monster boy, navel, abs, thick thighs, navel hair, mature male, topless male, short hair, full body, loincloth, blush, looking at viewer, pubic hair, smile, male pubic hair, bulge, fangs, facial hair <lora:orc3:1> Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 2564636670, Size: 1024x1024, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, Lora hashes: "orc3: b9fdaee01f36", TI hashes: "negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a

明らかにオーク概念を学習しています!すごい!!!

とりにく絵柄LoRAと組み合わせてみたり

③オークLoRAなし

solo, orc, 1boy, mature male, muscular male, bara, monster boy, topless male, looking at viewer, short hair, upper body white background <lora:test-sdxl-trnkegr-06:1> Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 2564636670, Size: 1024x1024, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, Lora hashes: "test-sdxl-trnkegr-06: 7f366d6bcbdb", TI hashes: "negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a

③オークLoRAあり

solo, orc, 1boy, mature male, muscular male, bara, monster boy, topless male, looking at viewer, short hair, upper body white background <lora:orc3:1>  <lora:test-sdxl-trnkegr-06:1> Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 2564636670, Size: 1024x1024, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, Lora hashes: "orc3: b9fdaee01f36, test-sdxl-trnkegr-06: 7f366d6bcbdb", TI hashes: "negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a

うっひょー!!AI筆さんオークLoRA、明らかに肉付きよくしてくれますね。性癖が助かる・・・!

おまけ(コピー機試してみた編)

以下はコピー機LoRAでオーク概念を学習できないか試みたログです。
結論から言うと普通に学習した感じの方が好みでした!
でもせっかくなので備忘録を。

①accelerate configをfp16に切り替える

accelerate config

- This machine
- No distributed training
- NO
- NO
- NO
- all
- fp16

②データセットの用意

まずAI筆様の公開された画像から1枚適当なものを選出
512*512にトリミングします。

キャプションは
orc, fang out colored skin, green skin, pointy ears, monster boy, beard, 1boy, solo, facial hair, male focus, pectorals, tank top, mature male, muscular, muscular male, bara, upper body, pectoral cleavage, looking at viewer, short hair, white tank top, large pectorals, smile, bare shoulders, white background

次に上記の画像をペイントフトで人間化します。
肌や髪:緑→赤みがかった色に
牙:ペイントソフトで消す
耳:ペイントソフトで丸くする

一先ずこんな感じでよいでしょう。
キャプションは
beard, 1boy, solo, facial hair, male focus, pectorals, tank top, mature male, muscular, muscular male, bara, upper body, red hair, pectoral cleavage, looking at viewer, short hair, white tank top, large pectorals, smile, bare shoulders, white background

③学習設定

ではこれらを使って学習していきます。
学習設定はこちらのNoteを参考にしていきます。

上記のPDFを参考にして、256、384、512の画像サイズを用意してフォルダ名を4000に(RTC3090でバッチサイズ12)

copi-ki_SDXL.toml

pretrained_model_name_or_path = "C:/stable-diffusion-webui/models/Stable-diffusion/blue_pencil-XL-v2.0.0.safetensors"
network_module = "networks.lora"
xformers = true
gradient_checkpointing = true
persistent_data_loader_workers = true
max_data_loader_n_workers = 12
enable_bucket = true
save_model_as = "safetensors"
lr_scheduler_num_cycles = 4
mixed_precision = "fp16"
learning_rate = 0.0001
resolution =  "512,512"
train_batch_size = 12
network_dim = 64
network_alpha =64
optimizer_type = "AdamW8bit"
bucket_no_upscale = true
save_precision = "fp16"
lr_scheduler = "linear"
min_bucket_reso = 64
max_bucket_reso = 1024
caption_extension = ".txt"
seed = 42
network_train_unet_only = true
no_half_vae = true

④学習開始

accelerate launch ^
  --num_cpu_threads_per_process 12 ^
  sdxl_train_network.py ^
  --config_file="C:\sd-scripts\user_config\orc\copi-ki_SDXL.toml" ^
  --train_data_dir="C:\sd-scripts\user_config\orc\base" ^
  --output_dir="C:\sd-scripts\user_config\output\orc" ^
  --output_name=copi-ki-base ^
  --max_train_steps 1500


accelerate launch ^
  --num_cpu_threads_per_process 12 ^
  sdxl_train_network.py ^
  --config_file="C:\sd-scripts\user_config\orc\copi-ki_SDXL.toml" ^
  --train_data_dir="C:\sd-scripts\user_config\orc\orc" ^
  --output_dir="C:\sd-scripts\user_config\output\orc" ^
  --output_name=copi-ki-orc ^
  --max_train_steps 1500

両方合わせて計3時間くらいかかりました。

⑤差分を抽出

python .\networks\merge_lora.py ^
--save_to "C:\sd-scripts\user_config\output\orc\orc2.safetensors" ^
--models "C:\sd-scripts\user_config\output\orc\copi-ki-orc.safetensors" "C:\sd-scripts\user_config\output\orc\copi-ki-base.safetensors" ^
--ratios 1.4 -1.4 ^
--concat ^
--shuffle ^
--save_precision fp16

⑥検証

orc, (upper body:1.6) bara, red tracksuit, male focus,1boy, solo, white shirt, muscular male, muscular, large pectorals, pectorals, mature male, facial hair, short hair, looking at viewer, shirt, black hair, smile, white background, SimplepositiveXLv1 Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1, Size: 768x1344, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, TI hashes: "SimplepositiveXLv1: 049fb42b64c9, SimplepositiveXLv1: 049fb42b64c9, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a


orc, (upper body:1.6) bara, red tracksuit, male focus,1boy, solo, white shirt, muscular male, muscular, large pectorals, pectorals, mature male, facial hair, short hair, looking at viewer, shirt, black hair, smile, white background, SimplepositiveXLv1 <lora:orc:1> Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1, Size: 768x1344, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, Lora hashes: "orc: b1a8657862c9", TI hashes: "SimplepositiveXLv1: 049fb42b64c9, SimplepositiveXLv1: 049fb42b64c9, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a

うーん、きっちりかっちりコピー機って感じではないですね。でもまぁ概念は学習はできてる感じ。

⑦再検証

くやしいので別設定を試します。次に参考にするのはこちらの設定。

画像データを1024*1024にアップスケールし、画像を1024一つに。フォルダ名は『192』

prompts.txt

, --w 1024 --h 1024 --d 1
, --w 1024 --h 1024 --d 2

copi-ki_SDXL2.toml

pretrained_model_name_or_path = "C:/stable-diffusion-webui/models/Stable-diffusion/blue_pencil-XL-v2.0.0.safetensors"
network_module = "networks.lora"
xformers = true
gradient_checkpointing = true
persistent_data_loader_workers = true
max_data_loader_n_workers = 4
save_model_as = "safetensors"
mixed_precision = "bf16"
unet_lr = 1e-3
resolution = "1024,1024"
train_batch_size = 12
network_dim = 64
network_args = ["conv_dim=32"]
optimizer_type = "adamw8bit"
save_precision = "bf16"
caption_extension = ".txt"
seed = 42
cache_latents = true
network_train_unet_only = true
cache_text_encoder_outputs = true
sample_sampler = "k_euler_a"
sample_prompts = "C:/sd-scripts/user_config/orc/prompts.txt"
save_every_n_epochs = 1
sample_every_n_epochs = 1

bf16で保存するので、accelerate configしておく

accelerate config

- This machine
- No distributed training
- NO
- NO
- NO
- all
- bf16

⑧学習開始(2回目)

accelerate launch ^
  --num_cpu_threads_per_process 12 ^
  sdxl_train_network.py ^
  --config_file="C:\sd-scripts\user_config\orc\copi-ki_SDXL2.toml" ^
  --train_data_dir="C:\sd-scripts\user_config\orc\base_1024" ^
  --output_dir="C:\sd-scripts\user_config\output\orc" ^
  --output_name=copi-ki-base2 ^^
  --max_train_epochs 

accelerate launch ^
  --num_cpu_threads_per_process 12 ^
  sdxl_train_network.py ^
  --config_file="C:\sd-scripts\user_config\orc\copi-ki_SDXL2.toml" ^
  --train_data_dir="C:\sd-scripts\user_config\orc\orc_1024" ^
  --output_dir="C:\sd-scripts\user_config\output\orc" ^
  --output_name=copi-ki-orc2 ^
  --max_train_epochs 12

両方合わせて計3時間くらいかかりました。

⑨差分を抽出(2回目)

python .\networks\merge_lora.py ^
--save_to "C:\sd-scripts\user_config\output\orc\orc2.safetensors" ^
--models "C:\sd-scripts\user_config\output\orc\copi-ki-orc2.safetensors" "C:\sd-scripts\user_config\output\orc\copi-ki-base2.safetensors" ^
--ratios 1.4 -1.4 ^
--concat ^
--shuffle ^
--save_precision bf16

⑩検証(2回目)


orc, (upper body:1.6) bara, red tracksuit, male focus,1boy, solo, white shirt, muscular male, muscular, large pectorals, pectorals, mature male, facial hair, short hair, looking at viewer, shirt, black hair, smile, white background, SimplepositiveXLv1 Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1, Size: 768x1344, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, TI hashes: "SimplepositiveXLv1: 049fb42b64c9, SimplepositiveXLv1: 049fb42b64c9, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a


orc, (upper body:1.6) bara, red tracksuit, male focus,1boy, solo, white shirt, muscular male, muscular, large pectorals, pectorals, mature male, facial hair, short hair, looking at viewer, shirt, black hair, smile, white background, SimplepositiveXLv1 <lora:orc2:1> Negative prompt: negativeXL_D sdxl-negprompt8-v1m unaestheticXL_AYv1 Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 1, Size: 768x1344, Model hash: d59627b749, Model: blue_pencil-XL-v2.0.0, Lora hashes: "orc2: fc84ee40b06e", TI hashes: "SimplepositiveXLv1: 049fb42b64c9, SimplepositiveXLv1: 049fb42b64c9, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117, negativeXL_D: fff5d51ab655, sdxl-negprompt8-v1m: 24350b43a034, unaestheticXL_AYv1: 8a94b6725117", Version: v1.6.0-2-g4afaaf8a

うーん。コピー機難しいね!!!

この記事が気に入ったらサポートをしてみませんか?