今回は、LoRAを使って今使っているモデルに追加学習を行い、もっと自分好みのイラストを生成する方法を紹介します。
画像生成は「Stable Diffusion Web UI」を使っていこうと思います。
※もともとこの記事では、モデルにChilloutMixを使っていましたが、非公開になってしまったので、RealDosMixを使う方法に変更しました。(現在、ChilloutMixは復活しています)
LoRAについて
LoRA(Low-Rank Adaptation)は、モデルに追加学習するための仕組みのひとつです。
ふだん使っているモデルを、ちょっとアレンジしたいときに便利です。
お気に入りのモデルに追加学習することで、もっと自分好みにアレンジできます。
「AIコスプレイヤー」というのを聞いたことがある人もいるかもしれませんが、それもこのLoRAという学習方法で追加学習を行い、生成された画像だったりします。
つまり、お気に入りのモデルに、好きなアニメやゲームのLoRAモデルを追加学習することで、お気に入りのモデルが生成するキャラクターが、好きなアニメやゲームのコスプレをしてくれる、というものです。
LoRAの仲間には、Textual Inversion、Hypernetworksなどがあり、LoRAを含めてこれら3つはStable Diffusion Web UIでかんたんに使うことができます。
しかし今回は、LoRAのみに焦点を当てて紹介していきたいと思います。
RealDosMix + Makima (Chainsaw Man) LoRA
今回、「RealDosMix」を使って生成したイラストの人物に、チェンソーマンのマキマのコスプレをさせてみます。
そのために、「Makima (Chainsaw Man) LoRA」を使います。
必要なものは、以下からダウンロードしてください。(ログインが必要です)
CIVITAI[RealDosMix]:(https://civitai.com/models/6925/realdosmix)
CIVITAI[Makima (Chainsaw Man) LoRA]:(https://civitai.com/models/5373/makima-chainsaw-man-lora)
「RealDosMix」と「Makima (Chainsaw Man) LoRA」を使って生成したものが、こちらです。

では、実際にやってみましょう。
LoRAを使うための準備
「Stable Diffusion Web UI」でLoRAを使うための準備をしていきます。
モデルのファイルは、「models/Stable-diffusion/
」に配置します。
LoRAのファイルは、「models/Lora/
」に配置します。
また、VAEも必要なので「Hugging Face[stabilityai/sd-vae-ft-mse-original]」からダウンロードします。
Hugging Face[stabilityai/sd-vae-ft-mse-original]:https://huggingface.co/stabilityai/sd-vae-ft-mse-original
VAEのファイルは「models/VAE/
」に配置します。
stable-diffusion-webui/
|-- models/
| |-- Stable-diffusion/
| | `-- realdosmix_.safetensors
| |-- Lora/
| | `-- makimaChainsawMan_offset.safetensors
| |-- VAE/
| | `-- vae-ft-mse-840000-ema-pruned.safetensors
| `-- ~省略~/
`-- ~省略~/
もし、Google Colabを使うのでしたら、次のコードを実行します。
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
%cd /content/stable-diffusion-webui
!wget https://civitai.com/api/download/models/8137 -O /content/stable-diffusion-webui/models/Stable-diffusion/realdosmix_.safetensors
%mkdir -p /content/stable-diffusion-webui/models/Lora/
!wget https://civitai.com/api/download/models/6244 -O /content/stable-diffusion-webui/models/Lora/makimaChainsawMan_offset.safetensors
!wget https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors -O /content/stable-diffusion-webui/models/VAE/vae-ft-mse-840000-ema-pruned.safetensors
!python launch.py --share --xformers --enable-insecure-extension-access
(※2023/06/26 コードを更新しました)
Google ColabでStable Diffusion Web UIを使う方法については、こちらを参考にしてください。
「Stable Diffusion Web UI」でLoRAを使ってみよう
では、「Stable Diffusion Web UI」でLoRAを使ってみましょう。
①の部分をクリックすると、②のようにLoRAを選べるようになります。
LoRAの項目を選択し、③のようなさきほど配置したLoRAのファイルをクリック(ここではmakimaChainsawMan_offset)すると、プロンプトにLoRAを使うためのものが追加されます。

また、LoRAのファイルの配布ページで、トリガーワード(Trigger Words)が書かれていることが多いです。
そちらも忘れずに、プロンプトに入力します。
例えば、Makima (Chainsaw Man) LoRAであれば、「makima \(chainsaw man\)
」となります。

また、さきほど追加した「lora:makimaChainsawMan_offset
」の後ろの数値は、デフォルトでは1となっていますが、これは、LoRAをどのぐらい適用するかの数値です。
「Makima (Chainsaw Man) LoRA」のoffsetバージョンでは、1〜1.4の間にするといいようです。

これで、マキマの格好をした、実写風のイラストを生成することができます。

RealDosMix + Makima (Chainsaw Man) LoRAで作ったイラスト
さきほどのイラストですが、次のようなプロンプトと設定で生成しました。

(8k, RAW photo, masterpiece, best quality, realistic:1.4), makima \(chainsaw man\), (red hair)+(long braided hair)+(bangs), yellow eyes, golden eyes, (ringed eyes), ((asian)), (white shirt), (necktie), tied up bangs, stare, smile, (evil:1.2), looking at viewer <lora:makimaChainsawMan_offset:1.4>
ネガティブプロンプト
(low quality, worst quality, trembling, cropped face:1.4), nsfw, blurry, messy drawing, bad hand, backlight
- Steps: 20
- Sampler: Euler a
- CFG scale: 6
- Seed: 3435386892
- Face restoration: CodeFormer
- Size: 512×728
- Model: realdosmix_
- Clip skip: 2
まとめ
今回は、Stable Diffusion Web UIでの、LoRAの使い方を紹介しました。
LoRAを使うと、ふだん使っているモデルを少しだけアレンジすることができます。
なので、実写風のイラストを生成できるモデルと一緒に使うと、まるでコスプレをしているかのようなイラストを生成することができます。
Launching Web UI with arguments: –share –xformers –enable-insecure-extension-access
2023-03-26 17:47:27.142407: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-03-26 17:47:28.304548: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library ‘libnvinfer.so.7’; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/lib64-nvidia
2023-03-26 17:47:28.304658: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library ‘libnvinfer_plugin.so.7’; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /usr/lib64-nvidia
2023-03-26 17:47:28.304676: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
Error loading script: lora_script.py
Traceback (most recent call last):
File “/content/stable-diffusion-webui/modules/scripts.py”, line 256, in load_scripts
script_module = script_loading.load_module(scriptfile.path)
File “/content/stable-diffusion-webui/modules/script_loading.py”, line 11, in load_module
module_spec.loader.exec_module(module)
File “”, line 850, in exec_module
File “”, line 228, in _call_with_frames_removed
File “/content/stable-diffusion-webui/extensions-builtin/Lora/scripts/lora_script.py”, line 4, in
import lora
File “/content/stable-diffusion-webui/extensions-builtin/Lora/lora.py”, line 238, in
def lora_apply_weights(self: torch.nn.Conv2d | torch.nn.Linear | torch.nn.MultiheadAttention):
TypeError: unsupported operand type(s) for |: ‘type’ and ‘type’
大変わかりやすい記事でいつも助かっております。ありがとうございます。
Loraを導入しようと思い、こちらの記事に記載されているコードをコピペしたところ、StableDiffusionは開けたのですが、Loraが使えませんでした。
colabで表示されたエラー近辺のコードを上記に添付させていただきましたので、もし可能でしたら解決法を教えていただけますと幸いです。
よろしくお願いいたします。