This repository contains training, generation and utility scripts for Stable Diffusion.
- January 12, 2023, 2023/1/23
- Metadata is saved on the model (.safetensors only) (model name, VAE name, training steps, learning rate etc.) The metadata will be able to inspect by sd-webui-additional-networks extension in near future. If you do not want to save it, specify
no_metadata
option. - メタデータが保存されるようになりました( .safetensors 形式の場合のみ)(モデル名、VAE 名、ステップ数、学習率など)。近日中に拡張から確認できるようになる予定です。メタデータを保存したくない場合は
no_metadata
オプションをしてしてください。
- Metadata is saved on the model (.safetensors only) (model name, VAE name, training steps, learning rate etc.) The metadata will be able to inspect by sd-webui-additional-networks extension in near future. If you do not want to save it, specify
January 9, 2023: Important information about the update can be found at the end of the page.
20231/1/9: 更新情報がページ末尾にありますのでご覧ください。
For easier use (GUI and PowerShell scripts etc...), please visit the repository maintained by bmaltais. Thanks to @bmaltais!
This repository contains the scripts for:
- DreamBooth training, including U-Net and Text Encoder
- fine-tuning (native training), including U-Net and Text Encoder
- LoRA training
- image generation
- model conversion (supports 1.x and 2.x, Stable Diffision ckpt/safetensors and Diffusers)
These files do not contain requirements for PyTorch. Because the versions of them depend on your environment. Please install PyTorch at first (see installation guide below.)
The scripts are tested with PyTorch 1.12.1 and 1.13.0, Diffusers 0.10.2.
All documents are in Japanese currently, and CUI based.
- DreamBooth training guide
- Step by Step fine-tuning guide: Including BLIP captioning and tagging by DeepDanbooru or WD14 tagger
- training LoRA
- note.com Image generation
- note.com Model conversion
Python 3.10.6 and Git:
- Python 3.10.6: https://www.python.org/ftp/python/3.10.6/python-3.10.6-amd64.exe
- git: https://git-scm.com/download/win
Give unrestricted script access to powershell so venv can work:
- Open an administrator powershell window
- Type
Set-ExecutionPolicy Unrestricted
and answer A - Close admin powershell window
Open a regular Powershell terminal and type the following inside:
git clone https://github.com/kohya-ss/sd-scripts.git
cd sd-scripts
python -m venv --system-site-packages venv
.\venv\Scripts\activate
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
cp .\bitsandbytes_windows\*.dll .\venv\Lib\site-packages\bitsandbytes\
cp .\bitsandbytes_windows\cextension.py .\venv\Lib\site-packages\bitsandbytes\cextension.py
cp .\bitsandbytes_windows\main.py .\venv\Lib\site-packages\bitsandbytes\cuda_setup\main.py
accelerate config
Answers to accelerate config:
- This machine
- No distributed training
- NO
- NO
- NO
- all
- fp16
note: Some user reports ValueError: fp16 mixed precision requires a GPU
is occured in training. In this case, answer 0
for the 6th question:
What GPU(s) (by id) should be used for training on this machine as a comma-seperated list? [all]:
(Single GPU with id 0
will be used.)
When a new release comes out you can upgrade your repo with the following command:
cd sd-scripts
git pull
.\venv\Scripts\activate
pip install --upgrade -r requirements.txt
Once the commands have completed successfully you should be ready to use the new version.
The implementation for LoRA is based on cloneofsimo's repo. Thank you for great work!!!
The majority of scripts is licensed under ASL 2.0 (including codes from Diffusers, cloneofsimo's), however portions of the project are available under separate license terms:
Memory Efficient Attention Pytorch: MIT
bitsandbytes: MIT
BLIP: BSD-3-Clause
All training scripts are updated.
- The
fine_tuning
option intrain_db.py
is removed. Please use DreamBooth with captions orfine_tune.py
. - The Hypernet feature in
fine_tune.py
is removed, will be implemented intrain_network.py
in future.
- Added
output_name
option. The name of output file can be specified.- With
--output_name style1
, the output file is likestyle1_000001.ckpt
(or.safetensors
) for each epoch andstyle1.ckpt
for last. - If ommitted (default), same to previous.
epoch-000001.ckpt
andlast.ckpt
.
- With
- Added
save_last_n_epochs
option. Keep only latest n files for the checkpoints and the states. Older files are removed. (Thanks to shirayu!)- If the options are
--save_every_n_epochs=2 --save_last_n_epochs=3
, in the end of epoch 8,epoch-000008.ckpt
is created andepoch-000002.ckpt
is removed.
- If the options are
- Added
max_token_length
option. Captions can have more than 75 tokens.
- The script now works without .npz files. If .npz is not found, the scripts get the latents with VAE.
- You can omit
prepare_buckets_latents.py
in preprocessing. However, it is recommended if you train more than 1 or 2 epochs. --resolution
option is required to specify the training resolution.
- You can omit
- Added
cache_latents
andcolor_aug
options.
- Now
--gradient_checkpointing
is effective for U-Net and Text Encoder.- The memory usage is reduced. The larger batch size is avilable, but the training speed will be slow.
- The training might be possible with 6GB VRAM for dimension=4 with batch size=1.
Documents are not updated now, I will update one by one.
学習スクリプトを更新しました。
train_db.py
のfine_tuning
は削除されました。キャプション付きの DreamBooth またはfine_tune.py
を使ってください。fine_tune.py
の Hypernet学習の機能は削除されました。将来的にtrain_network.py
に追加される予定です。
output_name
オプションを追加しました。保存されるモデルファイルの名前を指定できます。--output_name style1
と指定すると、エポックごとに保存されるファイル名はstyle1_000001.ckpt
(または.safetensors
) に、最後に保存されるファイル名はstyle1.ckpt
になります。- 省略時は今までと同じです(
epoch-000001.ckpt
およびlast.ckpt
)。
save_last_n_epochs
オプションを追加しました。最新の n ファイル、stateだけ保存し、古いものは削除します。(shirayu氏に感謝します。)- たとえば
--save_every_n_epochs=2 --save_last_n_epochs=3
と指定した時、8エポック目の終了時には、epoch-000008.ckpt
が保存されepoch-000002.ckpt
が削除されます。
- たとえば
max_token_length
オプションを追加しました。75文字を超えるキャプションが使えるようになります。
- .npzファイルがなくても動作するようになりました。.npzファイルがない場合、VAEからlatentsを取得して動作します。
prepare_buckets_latents.py
を前処理で実行しなくても良くなります。ただし事前取得をしておいたほうが、2エポック以上学習する場合にはトータルで高速です。- この場合、解像度を指定するために
--resolution
オプションが必要です。
cache_latents
とcolor_aug
オプションを追加しました。
--gradient_checkpointing
がU-NetとText Encoderにも有効になりました。- メモリ消費が減ります。バッチサイズを大きくできますが、トータルでの学習時間は長くなるかもしれません。
- dimension=4のLoRAはバッチサイズ1で6GB VRAMで学習できるかもしれません。
ドキュメントは未更新ですが少しずつ更新の予定です。