ジャコ Lab

プログラミング関連のメモ帳的ブログです

LoRA ってなに?diffusers(Google Colab) で使ってみる

zako-lab929.hatenablog.com

前回の記事(上記)で Textual Inversion のリファレンス を読んだときに、
近くに LoRA のドキュメント がありました。
LoRA も調べていると面白そうなことができそうなので使ってみようと思います。

LoRA とは?

LoRA is a fast and lightweight training method that inserts and trains a significantly smaller number of parameters instead of all the model parameters. This produces a smaller file (~100 MBs) and makes it easier to quickly train a model to learn a new concept.

たぶん、モデルそのものをトレーニングし直すのではなく、大幅に少ない計算量で効率よくモデルをチューニングできる子なのだと思います。

diffusers ではどうやって使うんだろう?

前回 EasyNegative を使う際に Textual Inversion のリファレンスをググったおかげか、なんとなく公式ドキュメントの見方がわかった気がしますので、今回は diffusers の公式ドキュメントを読めばできるような気がします。
見慣れたものですね。

huggingface.co

きっとこの辺でしょう。

ドキュメントによると以下のようになっていますね。
そのまま実行してみます。

!pip install --upgrade diffusers[torch]
from diffusers import AutoPipelineForText2Image
import torch

pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")
pipeline.load_lora_weights("ostris/super-cereal-sdxl-lora", weight_name="cereal_box_sdxl_v1.safetensors")

prompt = "bears, pizza bites"
image = pipeline(prompt).images[0]
image

なんか Warning が出た

/usr/local/lib/python3.10/dist-packages/diffusers/loaders/lora.py:805: FutureWarning: `_modify_text_encoder` is deprecated and will be removed in version 0.27. You are using an old version of LoRA backend. This will be deprecated in the next releases in favor of PEFT make sure to install the latest PEFT and transformers packages in the future.
 deprecate("_modify_text_encoder", "0.27", LORA_DEPRECATION_MESSAGE) /usr/local/lib/python3.10/dist-packages/diffusers/loaders/lora.py:777: FutureWarning: `_remove_text_encoder_monkey_patch_classmethod` is deprecated and will be removed in version 0.27. You are using an old version of LoRA backend. This will be deprecated in the next releases in favor of PEFT make sure to install the latest PEFT and transformers packages in the future.  deprecate("_remove_text_encoder_monkey_patch_classmethod", "0.27", LORA_DEPRECATION_MESSAGE)

※お前のバージョン古いよって言われてる気がするけど一旦気にしないでおく

実行結果

ドキュメントに書いてあったクマさんと同じようなやつが出ましたね。
使い方は成功っぽいです。

LoRA を使わない場合は?

おお、全然違いますね。
このモデル( "stabilityai/stable-diffusion-xl-base-1.0" )では、本来このような絵になるはずが、
LoRA によってあっちの絵に変わった感じですね。

使った LoRA ってどんなやつ?

"ostris/super-cereal-sdxl-lora" これですね。

huggingface.co

なるほど。 スーパーシリアル SDXL LoRA
シリアルのパッケージみたいになるってことですかね?

Adapter for stabilityai/stable-diffusion-xl-base-1.0

という記載も見受けられるので、
LoRA は、モデルによって使えたり使えなかったりするかもしれないですね。 これは完全に stabilityai/stable-diffusion-xl-base-1.0 用なんですかね?

まとめ

LoRA は、モデルが生成する絵の雰囲気?絵そのもの?を変えてくれるっぽいです。