1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

stable diffusion で 画像を学習させる

Posted at

参考
https://resanaplaza.com/2023/06/18/__trashed-3/

今回はLoraを作る

LoRAとは、Low-Rank Adaptationの頭文字を取った略語で、追加学習の際に必要となるメモリと計算量を大幅に削減し、かつ数十枚という少ない画像データでも良好な結果が得られるという、画期的な手法

準備

traning フォルダを作成

・sozai
・outputs
・seisoku
・commandline.txt
・datasetconfig.toml (文字コード utf-8)

traningフォルダを右クリック
-> ターミナルで開く

以下をコピペ

git clone https://github.com/kohya-ss/sd-scripts.git
cd sd-scripts
python -m venv venv
.\venv\Scripts\activate
python.exe -m pip install --upgrade pip
pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
pip install --upgrade -r requirements.txt
pip install -U -I --no-deps https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/download/f/xformers-0.0.14.dev0-cp310-cp310-win_amd64.whl
copy .\bitsandbytes_windows\*.dll .\venv\Lib\site-packages\bitsandbytes\
copy .\bitsandbytes_windows\cextension.py .\venv\Lib\site-packages\bitsandbytes\cextension.py
copy .\bitsandbytes_windows\main.py .\venv\Lib\site-packages\bitsandbytes\cuda_setup\main.py

OK

設定

ターミナルより以下を入力

accelerate config

入力後、

・エンター 2回
・no を 3回
・エンターを1回
・最後にfp16と入力しエンター
(選択式じゃないのでカーソルを押すとエラーになる。)

Taggerをインスコ

Taggerは画像に埋め込まれたタグ(プロンプト)を参照しているのではなく、画像の中身を認識して適切なタグを生成してくれるため、stable diffusion で生成されていない、普通のデジカメで撮影した画像でもOKです。

stabldediffusion ui を起動
Extensions > installfromurl に以下を入力し、再起動

https://github.com/toriato/stable-diffusion-webui-wd14-tagger

画像を学習させる

ぱくたそから画像をぱくたそ
https://www.pakutaso.com/20120704195post-1718.html

ゆかたの女の子Sサイズを取得

sozaiフォルダに01.jpg として保存
(学習素材は、必ず連番にする。)

Tagger を選択
Batchfrom directroyを選択
Input directory に 素材フォルダのパスを入力
C:\Users\h\OneDrive\デスクトップ\traning\sozai

Outputdirectoryにも上記と同じ内容を入力

Additional tags に yukata と入力

Interrogate を押す

結果

01.txt が生成される。
テキストエディタで開き、 "学習させたいキーワードを削除"

今回は kimono を 削除

もし、女性として学習させたいなら、1girl を、人物の顔を学習させたいなら、smile や looking at、grin など顔に関するキーワードを、髪型なら blonde hair などを削除します。

commandline.txt を開き、以下をコピペ

accelerate launch --num_cpu_threads_per_process 1 train_network.py
 --pretrained_model_name_or_path=****モデルファイルのパス****
 --output_dir=****LoRAファイルの出力先フォルダのパス****
 --output_name=FILE_NAME_HERE
 --dataset_config=****datasetconfig.tomlファイルのパス****
 --train_batch_size=1
 --max_train_epochs=10
 --resolution=512,512
 --optimizer_type=AdamW8bit
 --learning_rate=1e-4
 --network_dim=128
 --network_alpha=64
 --enable_bucket
 --bucket_no_upscale
 --lr_scheduler=cosine_with_restarts
 --lr_scheduler_num_cycles=4
 --lr_warmup_steps=500
 --keep_tokens=1
 --shuffle_caption
 --caption_dropout_rate=0.05
 --save_model_as=safetensors
 --clip_skip=2
 --seed=42
 --color_aug
 --xformers
 --mixed_precision=fp16
 --network_module=networks.lora
 --persistent_data_loader_workers

datasetconfig.toml

[general]
[[datasets]]
[[datasets.subsets]]
image_dir = '****学習ファイル(素材ファイル)を格納したフォルダのパス****'
caption_extension = '.txt'
num_repeats = 10

traningフォルダ
-> sd-scripts を選択し右クリック
-> ターミナルで開く

.\venv\scripts\activate

を実行

commandline.txt をメモ帳で開いて、各行の改行コードを削除し、1つの長い文字列に編集してから、全選択(Ctrl+A)し、コマンドプロンプトに 貼り付け(Ctrl+V)して、エンターキーを押します。

traning out puts フォルダに lora ができるので、
リネームして完成

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?