ジャコ Lab

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

もう1つの重み付け、Long Prompt Weighting Stable Diffusion を使ってみる

zako-lab929.hatenablog.com

Compel は diffusers の公式ドキュメントに載っていた重み付け用のモジュールでした。この記事では Long Prompt Weighting Stable Diffusion というものを使ってみようと思います

Long Prompt Weighting Stable Diffusion とは?

Community pipelines の1つで、トークンの長さ制限を無しに Stable Diffusion Pipeline を使えるようにしてくれて、Prompt の重み付けもサポートしてくれる機能のようです

Community pipelines とは?

diffusers には Community pipelines というものが存在しており、 コミュニティによってどんどん機能が追加されているようです。

初登場は diffusers v0.4.0 の頃で、
Long Prompt Weighting Stable Diffusion は v0.7.0 の頃のようです

Community pipelines の使い方

huggingface.co

from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4", 
    custom_pipeline="filename_in_the_community_folder", 
    use_safetensors=True
)

constom_pipeline という引数を使うようです

Long Prompt Weighting Stable Diffusion を使ってみる

Long Prompt Weighting Stable Diffusion を使うには custom_pipeline"lpw_stable_diffusion" を指定すれば良いようです。

詳細は以下に記載されております

github.com

記述的にはこんな感じでしょうか?
Prompt は README のものを、Pipeline は DiffusionPipeline ではなく AutoPipelineForText2Image にしてみました

import torch
from diffusers import AutoPipelineForText2Image

pipeline = AutoPipelineForText2Image.from_pretrained(
   'hakurei/waifu-diffusion',
    custom_pipeline="lpw_stable_diffusion",
    torch_dtype=torch.float16
).to("cuda")

prompt = "best_quality (1girl:1.3) bow bride brown_hair closed_mouth frilled_bow frilled_hair_tubes frills (full_body:1.3) fox_ear hair_bow hair_tubes happy hood japanese_clothes kimono long_sleeves red_bow smile solo tabi uchikake white_kimono wide_sleeves cherry_blossoms"
negative_prompt = "lowres, bad_anatomy, error_body, error_hair, error_arm, error_hands, bad_hands, error_fingers, bad_fingers, missing_fingers, error_legs, bad_legs, multiple_legs, missing_legs, error_lighting, error_shadow, error_reflection, text, error, extra_digit, fewer_digits, cropped, worst_quality, low_quality, normal_quality, jpeg_artifacts, signature, watermark, username, blurry"

image = pipeline(
    prompt,
    negative_prompt=negative_prompt,
    # Seed は比較用に適当に設定
    generator=torch.Generator(device="cuda").manual_seed(123456789)
).images[0]
image

lpw_stable_diffusion で重み付けして生成した画像

重み付けは、以下のような雰囲気だそうです

  • a baby deer with == (a baby deer with:1.0)
  • (big eyes) == (big eyes:1.1)
  • ((big eyes)) == (big eyes:1.21)
  • [big eyes] == (big eyes:0.91)

本当に重み付けできているのか?

色々試してみました

  1. そもそも custom_pipeline="lpw_stable_diffusion" なし
  2. 上記で生成した画像
  3. 2. から カッコ:1.3 を削除したもの

(左) lpw_stable_diffusion のロードなし | (中) lpw_stable_diffusion で重み付けして生成した画像 | (右) lpw_stable_diffusion のロードありで重み付けなし

custom_pipeline="lpw_stable_diffusion" を付けるだけで出力が変わりました (左画像 -> 真ん中の画像)
しかし重み付けの カッコ:1.3 を消しましたが、多少変わるも大きな変化はありません (真ん中画像 -> 右画像)

果たして効いているのか......??

分かりづらいので 1girl だけで試す

ここまで簡素化しました

prompt = "1girl"
# prompt = "(1girl:1.3)"

generator = torch.Generator(device="cuda").manual_seed(123456789)
image = pipeline(
    prompt,
    generator=generator
).images[0]
custom_pipeline="lpw_stable_diffusion" なし

(左) 1girl | (右) (1girl:1.3)

custom_pipeline="lpw_stable_diffusion" あり

(左) 1girl | (右) (1girl:1.3)

まとめ

Prompt を 1girl または (1girl:1.3) のみで試した場合に
custom_pipeline="lpw_stable_diffusion" なし のパターンでは、あまり変化が見られませんでしたが、
custom_pipeline="lpw_stable_diffusion" あり のパターンでは、明らかな変化が現れました

これは、ちゃんと効いていると見て良いでしょうか??