Skip to content

Commit 6e3c1d0

Browse files
authored
Merge pull request kohya-ss#1879 from kohya-ss/dev
merge dev to main
2 parents e5ac095 + 345daaa commit 6e3c1d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1871
-456
lines changed

README-ja.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Python 3.10.6およびGitが必要です。
3636
- Python 3.10.6: https://www.python.org/ftp/python/3.10.6/python-3.10.6-amd64.exe
3737
- git: https://git-scm.com/download/win
3838

39+
Python 3.10.x、3.11.x、3.12.xでも恐らく動作しますが、3.10.6でテストしています。
40+
3941
PowerShellを使う場合、venvを使えるようにするためには以下の手順でセキュリティ設定を変更してください。
4042
(venvに限らずスクリプトの実行が可能になりますので注意してください。)
4143

@@ -45,7 +47,7 @@ PowerShellを使う場合、venvを使えるようにするためには以下の
4547

4648
## Windows環境でのインストール
4749

48-
スクリプトはPyTorch 2.1.2でテストしています。PyTorch 2.0.1、1.12.1でも動作すると思われます
50+
スクリプトはPyTorch 2.1.2でテストしています。PyTorch 2.2以降でも恐らく動作します
4951

5052
(なお、python -m venv~の行で「python」とだけ表示された場合、py -m venv~のようにpythonをpyに変更してください。)
5153

@@ -67,10 +69,12 @@ accelerate config
6769

6870
コマンドプロンプトでも同一です。
6971

70-
注:`bitsandbytes==0.43.0``prodigyopt==1.0``lion-pytorch==0.0.6``requirements.txt` に含まれるようになりました。他のバージョンを使う場合は適宜インストールしてください。
72+
注:`bitsandbytes==0.44.0``prodigyopt==1.0``lion-pytorch==0.0.6``requirements.txt` に含まれるようになりました。他のバージョンを使う場合は適宜インストールしてください。
7173

7274
この例では PyTorch および xfomers は2.1.2/CUDA 11.8版をインストールします。CUDA 12.1版やPyTorch 1.12.1を使う場合は適宜書き換えください。たとえば CUDA 12.1版の場合は `pip install torch==2.1.2 torchvision==0.16.2 --index-url https://download.pytorch.org/whl/cu121` および `pip install xformers==0.0.23.post1 --index-url https://download.pytorch.org/whl/cu121` としてください。
7375

76+
PyTorch 2.2以降を用いる場合は、`torch==2.1.2``torchvision==0.16.2` 、および `xformers==0.0.23.post1` を適宜変更してください。
77+
7478
accelerate configの質問には以下のように答えてください。(bf16で学習する場合、最後の質問にはbf16と答えてください。)
7579

7680
```txt

README.md

Lines changed: 197 additions & 2 deletions
Large diffs are not rendered by default.

_typos.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Instruction: https://github.com/marketplace/actions/typos-action#getting-started
33

44
[default.extend-identifiers]
5+
ddPn08="ddPn08"
56

67
[default.extend-words]
78
NIN="NIN"
@@ -27,6 +28,7 @@ rik="rik"
2728
koo="koo"
2829
yos="yos"
2930
wn="wn"
31+
hime="hime"
3032

3133

3234
[files]

docs/config_README-en.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ These are options related to the configuration of the data set. They cannot be d
128128

129129
* `batch_size`
130130
* This corresponds to the command-line argument `--train_batch_size`.
131+
* `max_bucket_reso`, `min_bucket_reso`
132+
* Specify the maximum and minimum resolutions of the bucket. It must be divisible by `bucket_reso_steps`.
131133

132134
These settings are fixed per dataset. That means that subsets belonging to the same dataset will share these settings. For example, if you want to prepare datasets with different resolutions, you can define them as separate datasets as shown in the example above, and set different resolutions for each.
133135

docs/config_README-ja.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ DreamBooth の手法と fine tuning の手法の両方とも利用可能な学
118118

119119
* `batch_size`
120120
* コマンドライン引数の `--train_batch_size` と同等です。
121+
* `max_bucket_reso`, `min_bucket_reso`
122+
* bucketの最大、最小解像度を指定します。`bucket_reso_steps` で割り切れる必要があります。
121123

122124
これらの設定はデータセットごとに固定です。
123125
つまり、データセットに所属するサブセットはこれらの設定を共有することになります。

docs/masked_loss_README-ja.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## マスクロスについて
2+
3+
マスクロスは、入力画像のマスクで指定された部分だけ損失計算することで、画像の一部分だけを学習することができる機能です。
4+
たとえばキャラクタを学習したい場合、キャラクタ部分だけをマスクして学習することで、背景を無視して学習することができます。
5+
6+
マスクロスのマスクには、二種類の指定方法があります。
7+
8+
- マスク画像を用いる方法
9+
- 透明度(アルファチャネル)を使用する方法
10+
11+
なお、サンプルは [ずんずんPJイラスト/3Dデータ](https://zunko.jp/con_illust.html) の「AI画像モデル用学習データ」を使用しています。
12+
13+
### マスク画像を用いる方法
14+
15+
学習画像それぞれに対応するマスク画像を用意する方法です。学習画像と同じファイル名のマスク画像を用意し、それを学習画像と別のディレクトリに保存します。
16+
17+
- 学習画像
18+
![image](https://github.com/kohya-ss/sd-scripts/assets/52813779/607c5116-5f62-47de-8b66-9c4a597f0441)
19+
- マスク画像
20+
![image](https://github.com/kohya-ss/sd-scripts/assets/52813779/53e9b0f8-a4bf-49ed-882d-4026f84e8450)
21+
22+
```.toml
23+
[[datasets.subsets]]
24+
image_dir = "/path/to/a_zundamon"
25+
caption_extension = ".txt"
26+
conditioning_data_dir = "/path/to/a_zundamon_mask"
27+
num_repeats = 8
28+
```
29+
30+
マスク画像は、学習画像と同じサイズで、学習する部分を白、無視する部分を黒で描画します。グレースケールにも対応しています(127 ならロス重みが 0.5 になります)。なお、正確にはマスク画像の R チャネルが用いられます。
31+
32+
DreamBooth 方式の dataset で、`conditioning_data_dir` で指定したディレクトリにマスク画像を保存してください。ControlNet のデータセットと同じですので、詳細は [ControlNet-LLLite](train_lllite_README-ja.md#データセットの準備) を参照してください。
33+
34+
### 透明度(アルファチャネル)を使用する方法
35+
36+
学習画像の透明度(アルファチャネル)がマスクとして使用されます。透明度が 0 の部分は無視され、255 の部分は学習されます。半透明の場合は、その透明度に応じてロス重みが変化します(127 ならおおむね 0.5)。
37+
38+
![image](https://github.com/kohya-ss/sd-scripts/assets/52813779/0baa129b-446a-4aac-b98c-7208efb0e75e)
39+
40+
※それぞれの画像は透過PNG
41+
42+
学習時のスクリプトのオプションに `--alpha_mask` を指定するか、dataset の設定ファイルの subset で、`alpha_mask` を指定してください。たとえば、以下のようになります。
43+
44+
```toml
45+
[[datasets.subsets]]
46+
image_dir = "/path/to/image/dir"
47+
caption_extension = ".txt"
48+
num_repeats = 8
49+
alpha_mask = true
50+
```
51+
52+
## 学習時の注意事項
53+
54+
- 現時点では DreamBooth 方式の dataset のみ対応しています。
55+
- マスクは latents のサイズ、つまり 1/8 に縮小されてから適用されます。そのため、細かい部分(たとえばアホ毛やイヤリングなど)はうまく学習できない可能性があります。マスクをわずかに拡張するなどの工夫が必要かもしれません。
56+
- マスクロスを用いる場合、学習対象外の部分をキャプションに含める必要はないかもしれません。(要検証)
57+
- `alpha_mask` の場合、マスクの有無を切り替えると latents キャッシュが自動的に再生成されます。

docs/masked_loss_README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Masked Loss
2+
3+
Masked loss is a feature that allows you to train only part of an image by calculating the loss only for the part specified by the mask of the input image. For example, if you want to train a character, you can train only the character part by masking it, ignoring the background.
4+
5+
There are two ways to specify the mask for masked loss.
6+
7+
- Using a mask image
8+
- Using transparency (alpha channel) of the image
9+
10+
The sample uses the "AI image model training data" from [ZunZunPJ Illustration/3D Data](https://zunko.jp/con_illust.html).
11+
12+
### Using a mask image
13+
14+
This is a method of preparing a mask image corresponding to each training image. Prepare a mask image with the same file name as the training image and save it in a different directory from the training image.
15+
16+
- Training image
17+
![image](https://github.com/kohya-ss/sd-scripts/assets/52813779/607c5116-5f62-47de-8b66-9c4a597f0441)
18+
- Mask image
19+
![image](https://github.com/kohya-ss/sd-scripts/assets/52813779/53e9b0f8-a4bf-49ed-882d-4026f84e8450)
20+
21+
```.toml
22+
[[datasets.subsets]]
23+
image_dir = "/path/to/a_zundamon"
24+
caption_extension = ".txt"
25+
conditioning_data_dir = "/path/to/a_zundamon_mask"
26+
num_repeats = 8
27+
```
28+
29+
The mask image is the same size as the training image, with the part to be trained drawn in white and the part to be ignored in black. It also supports grayscale (127 gives a loss weight of 0.5). The R channel of the mask image is used currently.
30+
31+
Use the dataset in the DreamBooth method, and save the mask image in the directory specified by `conditioning_data_dir`. It is the same as the ControlNet dataset, so please refer to [ControlNet-LLLite](train_lllite_README.md#Preparing-the-dataset) for details.
32+
33+
### Using transparency (alpha channel) of the image
34+
35+
The transparency (alpha channel) of the training image is used as a mask. The part with transparency 0 is ignored, the part with transparency 255 is trained. For semi-transparent parts, the loss weight changes according to the transparency (127 gives a weight of about 0.5).
36+
37+
![image](https://github.com/kohya-ss/sd-scripts/assets/52813779/0baa129b-446a-4aac-b98c-7208efb0e75e)
38+
39+
※Each image is a transparent PNG
40+
41+
Specify `--alpha_mask` in the training script options or specify `alpha_mask` in the subset of the dataset configuration file. For example, it will look like this.
42+
43+
```toml
44+
[[datasets.subsets]]
45+
image_dir = "/path/to/image/dir"
46+
caption_extension = ".txt"
47+
num_repeats = 8
48+
alpha_mask = true
49+
```
50+
51+
## Notes on training
52+
53+
- At the moment, only the dataset in the DreamBooth method is supported.
54+
- The mask is applied after the size is reduced to 1/8, which is the size of the latents. Therefore, fine details (such as ahoge or earrings) may not be learned well. Some dilations of the mask may be necessary.
55+
- If using masked loss, it may not be necessary to include parts that are not to be trained in the caption. (To be verified)
56+
- In the case of `alpha_mask`, the latents cache is automatically regenerated when the enable/disable state of the mask is switched.

docs/train_README-ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ masterpiece, best quality, 1boy, in business suit, standing at street, looking b
648648
649649
詳細については各自お調べください。
650650
651-
任意のスケジューラを使う場合、任意のオプティマイザと同様に、`--scheduler_args`でオプション引数を指定してください。
651+
任意のスケジューラを使う場合、任意のオプティマイザと同様に、`--lr_scheduler_args`でオプション引数を指定してください。
652652
653653
### オプティマイザの指定について
654654

docs/train_README-zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ masterpiece, best quality, 1boy, in business suit, standing at street, looking b
582582
583583
有关详细信息,请自行研究。
584584
585-
要使用任何调度程序,请像使用任何优化器一样使用“--scheduler_args”指定可选参数。
585+
要使用任何调度程序,请像使用任何优化器一样使用“--lr_scheduler_args”指定可选参数。
586586
### 关于指定优化器
587587
588588
使用 --optimizer_args 选项指定优化器选项参数。可以以key=value的格式指定多个值。此外,您可以指定多个值,以逗号分隔。例如,要指定 AdamW 优化器的参数,``--optimizer_args weight_decay=0.01 betas=.9,.999``。

docs/train_network_README-ja.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ accelerate launch --num_cpu_threads_per_process 1 train_network.py
102102
* Text Encoderに関連するLoRAモジュールに、通常の学習率(--learning_rateオプションで指定)とは異なる学習率を使う時に指定します。Text Encoderのほうを若干低めの学習率(5e-5など)にしたほうが良い、という話もあるようです。
103103
* `--network_args`
104104
* 複数の引数を指定できます。後述します。
105+
* `--alpha_mask`
106+
* 画像のアルファ値をマスクとして使用します。透過画像を学習する際に使用します。[PR #1223](https://github.com/kohya-ss/sd-scripts/pull/1223)
105107

106108
`--network_train_unet_only``--network_train_text_encoder_only` の両方とも未指定時(デフォルト)はText EncoderとU-Netの両方のLoRAモジュールを有効にします。
107109

@@ -181,16 +183,16 @@ python networks\extract_lora_from_dylora.py --model "foldername/dylora-model.saf
181183

182184
詳細は[PR #355](https://github.com/kohya-ss/sd-scripts/pull/355) をご覧ください。
183185

184-
SDXLは現在サポートしていません。
185-
186186
フルモデルの25個のブロックの重みを指定できます。最初のブロックに該当するLoRAは存在しませんが、階層別LoRA適用等との互換性のために25個としています。またconv2d3x3に拡張しない場合も一部のブロックにはLoRAが存在しませんが、記述を統一するため常に25個の値を指定してください。
187187

188+
SDXL では down/up 9 個、middle 3 個の値を指定してください。
189+
188190
`--network_args` で以下の引数を指定してください。
189191

190192
- `down_lr_weight` : U-Netのdown blocksの学習率の重みを指定します。以下が指定可能です。
191-
- ブロックごとの重み : `"down_lr_weight=0,0,0,0,0,0,1,1,1,1,1,1"` のように12個の数値を指定します
193+
- ブロックごとの重み : `"down_lr_weight=0,0,0,0,0,0,1,1,1,1,1,1"` のように12個(SDXL では 9 個)の数値を指定します
192194
- プリセットからの指定 : `"down_lr_weight=sine"` のように指定します(サインカーブで重みを指定します)。sine, cosine, linear, reverse_linear, zeros が指定可能です。また `"down_lr_weight=cosine+.25"` のように `+数値` を追加すると、指定した数値を加算します(0.25~1.25になります)。
193-
- `mid_lr_weight` : U-Netのmid blockの学習率の重みを指定します。`"down_lr_weight=0.5"` のように数値を一つだけ指定します。
195+
- `mid_lr_weight` : U-Netのmid blockの学習率の重みを指定します。`"down_lr_weight=0.5"` のように数値を一つだけ指定します(SDXL の場合は 3 個)
194196
- `up_lr_weight` : U-Netのup blocksの学習率の重みを指定します。down_lr_weightと同様です。
195197
- 指定を省略した部分は1.0として扱われます。また重みを0にするとそのブロックのLoRAモジュールは作成されません。
196198
- `block_lr_zero_threshold` : 重みがこの値以下の場合、LoRAモジュールを作成しません。デフォルトは0です。
@@ -215,6 +217,9 @@ network_args = [ "block_lr_zero_threshold=0.1", "down_lr_weight=sine+.5", "mid_l
215217

216218
フルモデルの25個のブロックのdim (rank)を指定できます。階層別学習率と同様に一部のブロックにはLoRAが存在しない場合がありますが、常に25個の値を指定してください。
217219

220+
SDXL では 23 個の値を指定してください。一部のブロックにはLoRA が存在しませんが、`sdxl_train.py`[階層別学習率](./train_SDXL-en.md) との互換性のためです。
221+
対応は、`0: time/label embed, 1-9: input blocks 0-8, 10-12: mid blocks 0-2, 13-21: output blocks 0-8, 22: out` です。
222+
218223
`--network_args` で以下の引数を指定してください。
219224

220225
- `block_dims` : 各ブロックのdim (rank)を指定します。`"block_dims=2,2,2,2,4,4,4,4,6,6,6,6,8,6,6,6,6,4,4,4,4,2,2,2,2"` のように25個の数値を指定します。

docs/train_network_README-zh.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ LoRA的模型将会被保存在通过`--output_dir`选项指定的文件夹中
101101
* 当在Text Encoder相关的LoRA模块中使用与常规学习率(由`--learning_rate`选项指定)不同的学习率时,应指定此选项。可能最好将Text Encoder的学习率稍微降低(例如5e-5)。
102102
* `--network_args`
103103
* 可以指定多个参数。将在下面详细说明。
104+
* `--alpha_mask`
105+
* 使用图像的 Alpha 值作为遮罩。这在学习透明图像时使用。[PR #1223](https://github.com/kohya-ss/sd-scripts/pull/1223)
104106

105107
当未指定`--network_train_unet_only``--network_train_text_encoder_only`时(默认情况),将启用Text Encoder和U-Net的两个LoRA模块。
106108

0 commit comments

Comments
 (0)