先日は stable diffusion を web ui で利用できるようにした。
kumak1.hatenablog.com
現在、世界中の人々が学習モデルを作っては公開されていて、ものすごい勢いだ。
が、ものづくりする人間の端くれとしては、自分に都合が良いアウトプットをする道具にしなきゃならない。
ので、LoRA (Low-Rank Adaptation) で学習モデルを作れるようにしよう。
昨今のデファクトスタンダードは kohya-ss/sd-scripts らしい。
(こういった、皆が使うものを作れるようになりたいものです)
これを web ui で操作できるようにしてくれた bmaltais/kohya_ss があるので、今回はこれを readme (windows向け) を M1 に読み替えながらセットアップしていく。
PyTorch は m1 でもピッとインストールできるが、 TensorFlow は conda から apple のライブラリ入れる必要があるので注意
brew install python@3.10
ghq get git@github.com:bmaltais/kohya_ss.git
cd ~/src/github.com/bmaltais/kohya_ss
python -m venv .venv
. .venv/bin/activate
pip3 install --upgrade pip
pip3 install torch torchvision
% pip3 list | grep torch
torch 1.13.1
torchvision 0.14.1
brew install miniforge
conda install -c apple tensorflow-deps
pip3 install tensorflow-macos tensorflow-metal tensorboard
% pip3 list | grep tensor
tensorboard 2.11.2
tensorboard-data-server 0.6.1
tensorboard-plugin-wit 1.8.1
tensorflow-estimator 2.11.0
tensorflow-macos 2.11.0
tensorflow-metal 0.7.1
pip install diffusers
pip install --use-pep517 --upgrade -r requirements.txt
accelerate config
設定はこんな感じで答えといた
% accelerate config
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------In which compute environment are you running?
This machine
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Which type of machine are you using?
MPS
Do you wish to optimize your script with torch dynamo?[yes/NO]:yes
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Which dynamo backend would you like to use?
inductor
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Do you wish to use FP16 or BF16 (mixed precision)?
fp16
accelerate configuration saved at /Users/kumak1/.cache/huggingface/accelerate/default_config.yaml
GUI の起動起動
python ./kohya_gui.py
エラー
:memo: ディレクトリ指定の際にエラーとなる
_tkinter
あたりで Main Thread がとれんらしいのだが・・
どこを改修すればいいか皆目検討もつかん・・
2023-02-24 02:09:26.392 Python[10430:188815] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
0 CoreFoundation 0x00000001b4e14418 __exceptionPreprocess + 176
1 libobjc.A.dylib 0x00000001b495eea8 objc_exception_throw + 60
2 CoreFoundation 0x00000001b4e3d830 _CFBundleGetValueForInfoKey + 0
3 AppKit 0x00000001b8012bd8 -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 320
4 AppKit 0x00000001b8001730 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 816
5 AppKit 0x00000001b80013f4 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48
6 libtk8.6.dylib 0x000000015c44cd84 TkMacOSXMakeRealWindowExist + 524
7 libtk8.6.dylib 0x000000015c44ca68 TkWmMapWindow + 56
8 libtk8.6.dylib 0x000000015c3c0270 MapFrame + 76
9 libtcl8.6.dylib 0x000000015c2f4b94 TclServiceIdle + 84
10 libtcl8.6.dylib 0x000000015c2d8ef0 Tcl_DoOneEvent + 296
11 libtk8.6.dylib 0x000000015c440a28 TkpInit + 720
12 libtk8.6.dylib 0x000000015c3b9278 Initialize + 2292
13 _tkinter.cpython-310-darwin.so 0x000000015b7e2a98 Tcl_AppInit + 80
14 _tkinter.cpython-310-darwin.so 0x000000015b7dca98 Tkapp_New + 592
15 _tkinter.cpython-310-darwin.so 0x000000015b7dc448 _tkinter_create + 580
16 Python 0x0000000100e04d7c cfunction_vectorcall_FASTCALL + 88
17 Python 0x0000000100eaccf8 call_function + 128
18 Python 0x0000000100eaa510 _PyEval_EvalFrameDefault + 43104
19 Python 0x0000000100e9ea5c _PyEval_Vector + 376
20 Python 0x0000000100db2eac _PyObject_FastCallDictTstate + 96
21 Python 0x0000000100e28abc slot_tp_init + 196
22 Python 0x0000000100e20a8c type_call + 288
23 Python 0x0000000100db2c44 _PyObject_MakeTpCall + 136
24 Python 0x0000000100eacd88 call_function + 272
25 Python 0x0000000100eaa538 _PyEval_EvalFrameDefault + 43144
26 Python 0x0000000100e9ea5c _PyEval_Vector + 376
27 Python 0x0000000100ec52e4 _PyObject_VectorcallTstate.4671 + 96
28 Python 0x0000000100ec5198 context_run + 92
29 Python 0x0000000100e04ccc cfunction_vectorcall_FASTCALL_KEYWORDS + 84
30 Python 0x0000000100eaa6d8 _PyEval_EvalFrameDefault + 43560
31 Python 0x0000000100e9ea5c _PyEval_Vector + 376
32 Python 0x0000000100eaccf8 call_function + 128
33 Python 0x0000000100eaa498 _PyEval_EvalFrameDefault + 42984
34 Python 0x0000000100e9ea5c _PyEval_Vector + 376
35 Python 0x0000000100eaccf8 call_function + 128
36 Python 0x0000000100eaa498 _PyEval_EvalFrameDefault + 42984
37 Python 0x0000000100e9ea5c _PyEval_Vector + 376
38 Python 0x0000000100db5fb8 method_vectorcall + 388
39 Python 0x0000000100f6cc44 thread_run + 120
40 Python 0x0000000100f0ba90 pythread_wrapper + 48
41 libsystem_pthread.dylib 0x00000001b4cba06c _pthread_start + 148
42 libsystem_pthread.dylib 0x00000001b4cb4e2c thread_start + 8
)
libc++abi: terminating with uncaught exception of type NSException
zsh: abort python3 ./kohya_gui.py
OK、いきなりGUIに行こうとした私が悪かった。
作者も Windows PC + NVIDIA GPU を想定したアプリだししゃーない。
CUIのミニマム構成で行こう
kohya-ss/sd-scripts で実行
サンプルとして使える画像とコードは、 作者本人のブログ からお借りする
セットアップ方法は bmaltais/kohya_ss
の時とほとんど変わらない。
学習のコマンド
accelerate launch --num_cpu_threads_per_process 4 train_network.py \
--pretrained_model_name_or_path="~/Downloads/lora_train_sample_pack/model.ckpt" \
--train_data_dir="~/Downloads/lora_train_sample_pack/train" \
--reg_data_dir="~/Downloads/lora_train_sample_pack/reg" \
--prior_loss_weight=1.0 --resolution 512 \
--output_dir="~/Downloads/lora_train_sample_pack/lora_output" \
--output_name=cjgg_frog \
--train_batch_size=4 --learning_rate=1e-4 --max_train_epochs 4 \
--use_8bit_adam --mixed_precision=fp16 --save_precision=fp16 \
--seed 42 --save_model_as=safetensors --save_every_n_epochs=1 \
--max_data_loader_n_workers=1 \
--network_module=networks.lora --network_dim=4 \
--training_comment="activate by usu frog"
が、エラー
prepare tokenizer
Use DreamBooth method.
prepare train images.
found directory 20_usu frog contains 15 image files
300 train images with repeating.
prepare reg images.
found directory 1_frog contains 50 image files
50 reg images.
loading image sizes.
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 65/65 [00:00<00:00, 8185.60it/s]
prepare dataset
prepare accelerator
/Users/kumak1/src/github.com/kohya-ss/sd-scripts/.venv/lib/python3.10/site-packages/torch/cuda/amp/grad_scaler.py:118: UserWarning: torch.cuda.amp.GradScaler is enabled, but CUDA is not available. Disabling.
warnings.warn("torch.cuda.amp.GradScaler is enabled, but CUDA is not available. Disabling.")
Using accelerator 0.15.0 or above.
load Diffusers pretrained models
accelerate config
で mps
を指定しているのだけど、アプリケーションの挙動は CUDA 使おうとしている。
感想
うーん、学習をさせるなら、やっぱり利用者の多い Windows を使うのが今はまだ安牌かな・・
しかし、ずっとぶん回しても問題ない消費電力と排熱な M1 Mac はやはり魅力的なので、引き続き Mac で LoRA 学習できる方法を模索する。
また、stable-diffusion-webui はちゃんとうごいているので、これにbuilt in である hypernetwork の学習を試してみる