This repository contains training, generation and utility scripts for Stable Diffusion.
January 9, 2023: 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)
About requirements.txt
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.
Links to how-to-use documents
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
Windows Required Dependencies
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 Unrestrictedand answer A - Close admin powershell window
Windows Installation
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.)
Upgrade
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.
License
The majority of scripts is licensed under ASL 2.0 (including codes from Diffusers), however portions of the project are available under separate license terms:
Memory Efficient Attention Pytorch: MIT
bitsandbytes: MIT
BLIP: BSD-3-Clause
Updates: Jan 9. 2023
All training scripts are updated.
Breaking Changes
- The
fine_tuningoption intrain_db.pyis removed. Please use DreamBooth with captions orfine_tune.py. - The Hypernet feature in
fine_tune.pyis removed, will be implemented intrain_network.pyin future.
Features, Improvements and Bug Fixes
for all script: train_db.py, fine_tune.py and train_network.py
- Added
output_nameoption. 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.ckptfor last. - If ommitted (default), same to previous.
epoch-000001.ckptandlast.ckpt.
- With
- Added
save_last_n_epochsoption. 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.ckptis created andepoch-000002.ckptis removed.
- If the options are
train_db.py
- Added
max_token_lengthoption. Captions can have more than 75 tokens.
fine_tune.py
- 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.pyin preprocessing. However, it is recommended if you train more than 1 or 2 epochs. --resolutionoption is required to specify the training resolution.
- You can omit
- Added
cache_latentsandcolor_augoptions.
train_network.py
- Now
--gradient_checkpointingis 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.
更新情報 (2023/1/9)
学習スクリプトを更新しました。
削除された機能
train_db.pyのfine_tuningは削除されました。キャプション付きの DreamBooth またはfine_tune.pyを使ってください。fine_tune.pyの Hypernet学習の機能は削除されました。将来的にtrain_network.pyに追加される予定です。
その他の機能追加、バグ修正など
学習スクリプトに共通: train_db.py, fine_tune.py and 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が削除されます。
- たとえば
train_db.py
max_token_lengthオプションを追加しました。75文字を超えるキャプションが使えるようになります。
fine_tune.py
- .npzファイルがなくても動作するようになりました。.npzファイルがない場合、VAEからlatentsを取得して動作します。
prepare_buckets_latents.pyを前処理で実行しなくても良くなります。ただし事前取得をしておいたほうが、2エポック以上学習する場合にはトータルで高速です。- この場合、解像度を指定するために
--resolutionオプションが必要です。
cache_latentsとcolor_augオプションを追加しました。
train_network.py
--gradient_checkpointingがU-NetとText Encoderにも有効になりました。- メモリ消費が減ります。バッチサイズを大きくできますが、トータルでの学習時間は長くなるかもしれません。
- dimension=4のLoRAはバッチサイズ1で6GB VRAMで学習できるかもしれません。
ドキュメントは未更新ですが少しずつ更新の予定です。