素のStable DiffusionでNegative Promptを実装する

巷にはNegative Promptを実装したStable DiffusionのGUIツールがたくさんあります。
しかし、Stable Diffusionの本家のパラメータを見てもNegativePromptなるものはありません。
ですので、ちょこっと弄って素のStable DiffusionでもNegativePromptを使えるようにします。

修正方法

以下の処理を追加

  • ファイル:scripts/txt2img.py
  • 関数:parse_args()
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--prompt",
        type=str,
        nargs="?",
        default="a professional photograph of an astronaut riding a triceratops",
        help="the prompt to render"
    )
    # ---- ↓ここを追加 ----
    parser.add_argument(
        "--negative_prompts",
        type=str,
        nargs="?",
        default="",
        help="the negative prompt to render"
    )
    # ---- ↑ここを追加 ----
    parser.add_argument(
        "--outdir",
        type=str,
        nargs="?",
        help="dir to write results to",
        default="outputs/txt2img-samples"
    )

以下の処理を修正

  • ファイル:scripts/txt2img.py
  • 関数:main(opt)
    with torch.no_grad(), \
        precision_scope(opt.device), \
        model.ema_scope():
            all_samples = list()
            for n in trange(opt.n_iter, desc="Sampling"):
                for prompts in tqdm(data, desc="data"):
                    uc = None
                    # ---- ↓ここを修正 ----
                    if opt.scale != 1.0:
                        #uc = model.get_learned_conditioning(batch_size * [""])
                        uc = model.get_learned_conditioning(opt.negative_prompts)
                    # ---- ↑ここを修正 ----
                    if isinstance(prompts, tuple):
                        prompts = list(prompts)
                    c = model.get_learned_conditioning(prompts)
                    shape = [opt.C, opt.H // opt.f, opt.W // opt.f]
                    samples, _ = sampler.sample(S=opt.steps,
                                                     conditioning=c,
                                                     batch_size=opt.n_samples,
                                                     shape=shape,
                                                     verbose=False,
                                                     unconditional_guidance_scale=opt.scale,
                                                     unconditional_conditioning=uc,
                                                     eta=opt.ddim_eta,
                                                     x_T=start_code)

実験

少しネガティブプロンプトの動作を確認していきます。

ネガティブプロンプト無し

ネガティブプロンプトに何も指定していない状態。

python scripts/txt2img.py --prompt "a cute himalayan cat" --ckpt checkpoints/v2-1_512-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference.yaml --H 512 --W 768 --n_iter 4 --n_samples 1 --device cuda --seed 42 --n_rows 2

うん、かわいい。

ネガティブプロンプト 青い目

blue eyesをネガティブプロンプトに指定した状態。

python scripts/txt2img.py --prompt "a cute himalayan cat" --ckpt checkpoints/v2-1_512-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference.yaml --H 512 --W 768 --n_iter 4 --n_samples 1 --device cuda --seed 42 --n_rows 2 --negative_prompts "blue eyes"

青い目が茶色い目に変わりました。

ネガティブプロンプト 白い毛

white hairをネガティブプロンプトに指定した状態。

python scripts/txt2img.py --prompt "a cute himalayan cat" --ckpt checkpoints/v2-1_512-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference.yaml --H 512 --W 768 --n_iter 4 --n_samples 1 --device cuda --seed 42 --n_rows 2 --negative_prompts "white hair"

白い毛がだいぶブラウンよりになっています。

ネガティブプロンプト 白い毛, 青い目

white hair, blue eyesをネガティブプロンプトに指定した状態。

python scripts/txt2img.py --prompt "a cute himalayan cat" --ckpt checkpoints/v2-1_512-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference.yaml --H 512 --W 768 --n_iter 4 --n_samples 1 --device cuda --seed 42 --n_rows 2 --negative_prompts "white hair, blue eyes"

だいぶ茶色の毛の茶色い目の子たちです。

ネガティブプロンプト 猫

catをネガティブプロンプトに指定した状態。

python scripts/txt2img.py --prompt "a cute himalayan cat" --ckpt checkpoints/v2-1_512-ema-pruned.ckpt --config configs/stable-diffusion/v2-inference.yaml --H 512 --W 768 --n_iter 4 --n_samples 1 --device cuda --seed 42 --n_rows 2 --negative_prompts "cat"

さすがに猫を除くのは無理ですね、完全に無視されました。
ネガティブプロンプトを指定していない時と同じ。

まとめ

結構簡単にできました。
理屈はわかりません。恐縮です。

おわり

参考にしたページ

https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Negative-prompt
https://github.com/Stability-AI/StableDiffusion

素晴らしい

PR

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です