Imagicをローカルマシンで実行する
from Imagicで遊ぶ
環境
WSL2 ubuntu 20.04
jupyter-lab 3.4.8
CUDA 11.7
RTX3090
jupyterlabをインストールするとWSLのlocalhostにたったjupyterのサーバーのWeb UIにWindowsからアクセスできる
code:zsh
pip install jupyterlab
✅WSL2上で動かしたAUTOMATIC1111版Stable Diffusion web UIにWindowsからアクセスできないではできなかったのになぜ...?
https://github.com/ShivamShrirao/diffusers/tree/main/examples/imagic をcloneしてWeb UIから実行していく
上のnotebookはcolab前提なのでいろんなトラブルがあったが、すべて潰した結果ローカルで動かすことができた
notebookをこうすればいい
https://gist.github.com/motoso/37ed39beb6e1693f23797db4ea06d93d
課題
この環境でも学習に10分程度かかるので、いろんなシチュエーションをたくさん出すにはひと工夫必要
ローカルにすでにあるモデルを使うにはどう指定すれば?
ckptだけではだめなようだ
--.icon
ローカル版の様々なトラブル
huggingfaceにログインできない
https://medium.com/@yashsk8/logging-in-to-huggingface-from-jupyter-notebook-without-interactive-prompt-2cb945b4905c こうした
xformersをインストールできない
code:zsh
# これはRTX3090環境ではエラーになる
%pip install -q https://github.com/metrolobo/xformers_wheels/releases/download/1d31a3ac_various_6/xformers-0.0.14.dev0-cp37-cp37m-linux_x86_64.whl
# These were compiled on Tesla T4, should also work on P100, thanks to https://github.com/metrolobo
# なので迂回策の方をや...40分!?うそやろ...
# If precompiled wheels don't work, install it with the following command. It will take around 40 minutes to compile.
%pip install git+https://github.com/facebookresearch/xformers@1d31a3a#egg=xformers
# コンパイルエラー
RuntimeError:
The detected CUDA version (11.7) mismatches the version that was used to compile
PyTorch (10.2). Please make sure to use the same CUDA versions.
installedなPythorchは1.12.1+cu102なのに、エラー原因の10.2はどこからきたの?
Pytorchをアップデートしてみる
https://pytorch.org/ でCUDA 11.7対応を探して入れる
code:zsh
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu117
# jupyterで実行できなかったのでこうする
pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cu117
print(torch.__version__) # 1.12.1+cu102になった。kernel rebootが必要?
# でも今回は進んでいる様子...
%pip install git+https://github.com/facebookresearch/xformers@1d31a3a#egg=xformers
# できた
OUTPUT_DIRの工夫が必要
こんな感じにしておく
code:py.diff
# google driveはつかわない
- save_to_gdrive = True #@param {type:"boolean"}
+ save_to_gdrive = False #@param {type:"boolean"}
# pathをProject rootにしておく
- OUTPUT_DIR = "/content/" + OUTPUT_DIR
+ import os
+ OUTPUT_DIR = os.getcwd() + "/content/" + OUTPUT_DIR
学習に失敗する
code:zsh
!accelerate launch train_imagic.py \
--pretrained_model_name_or_path=$MODEL_NAME \
--output_dir=$OUTPUT_DIR \
--input_image=$INPUT_IMAGE \
--target_text="{TARGET_TEXT}" \
--seed=3434554 \
--resolution=512 \
--mixed_precision="fp16" \
--use_8bit_adam \
--gradient_accumulation_steps=1 \
--emb_learning_rate=1e-3 \
--learning_rate=1e-6 \
--emb_train_steps=500 \
--max_train_steps=1000
# ...
AttributeError: 'NoneType' object has no attribute 'cuDeviceGetCount'
code:diff
!accelerate launch train_imagic.py \
--pretrained_model_name_or_path=$MODEL_NAME \
--output_dir=$OUTPUT_DIR \
--input_image=$INPUT_IMAGE \
--target_text="{TARGET_TEXT}" \
--seed=3434554 \
--resolution=512 \
--mixed_precision="fp16" \
- --use_8bit_adam \
--gradient_accumulation_steps=1 \
--emb_learning_rate=1e-3 \
--learning_rate=1e-6 \
--emb_train_steps=500 \
--max_train_steps=1000
なぜかこれで進むようになった
optimizing embeddingは500itあり、RTX3090で2.77it/s
code:Zsh
Optimizing embedding: 100%|███████| 500/500 02:59<00:00, 2.79it/s, loss=0.139
Fine Tuning: 100%|██████████████| 1000/1000 07:39<00:00, 2.18it/s, loss=0.108
Tesla T4の2倍ぐらい速い
500/1000で学習に20分、1000/2000で40分程度かかりますhttps://colab.research.google.com/drive/10HV7FdKm9js9YXuF_XSun_bx59xp6ywj?usp=sharing#scrollTo=Rxg0y5MBudmd
Convert weights to ckpt to use in web UIs like AUTOMATIC1111
code:zsh
!python convert_diffusers_to_original_stable_diffusion.py --model_path $OUTPUT_DIR --checkpoint_path $ckpt_path $half_arg
code:zsh
ModuleNotFoundError: No module named 'torch'
どうやら%pip list と!pipは結果が違うらしい
!はシェル実行
%はmagic function
https://ipython.readthedocs.io/en/stable/interactive/python-ipython-diff.html#magics
python - What is %pylab? - Stack Overflow
Imagicで遊ぶ#6350ee04774b17000030b672ここで入れたけど、これは!のpipとは違うらしいので手動でpipをいれた
なんなんだ...わからん
%pip
すべての %pip コマンドはノートブックの先頭に配置する必要があります。 環境を変更する %pip コマンドの後に、ノートブックの状態がリセットされます。 ノートブックで Python メソッドまたは変数を作成し、それよりも後のセルで %pip コマンドを使用すると、メソッドまたは変数は失われます。
%pip を使用してコア Python パッケージ (IPython など) をアップグレード、変更、またはアンインストールすると、一部の機能が期待どおりに動作しなくなる場合があります。 たとえば、IPython 7.21 以上は、Databricks Runtime 8.1 以下と互換性がありません。 このような問題が発生した場合は、環境をリセットしてください。リセットするには、ノートブックをデタッチしてもう一度アタッチするか、クラスターを再起動します。
https://learn.microsoft.com/ja-jp/azure/databricks/libraries/notebooks-python-libraries#install-notebook-scoped-libraries-with-pip
%pipの方いいという意見もある
https://stackoverflow.com/a/61212861
もしかして、!pythonしているpython(=systemのpython)とjupyter nodeのpythonは異なるの?
Detected CUDA version (11.7) mismatches the version that was used to compile PyTorch (10.2) - #2 by ptrblck - PyTorch Forums
Since you are using an Ampere GPU you would need to use CUDA >=11.0 and the 10.2 runtime won’t even work.
Make sure to use the PyTorch binaries with CUDA 11 and try to rebuild.
jupyter-labの不満