【Diffusers】Attend and Exciteを使ってみる

はじめに

DiffusersからAttend and Excite(Attention-Based Semantic Guidance for Text-to-Image Diffusion Models)が使えるようになっていたのでさっそく使ってみました。
github.com

疑問点

通常のStable Diffusionなどでは、複数のオブジェクトを生成するのが難しい。それを解決するのが「Attend and Excite」と説明されています。

しかし、SD web UI(AUTOMATIC1111)などを使うとpromptの重みづけが簡単にできます。

(強調)[控えめ]などなど。

それと似たようなものと考えて問題ないのでしょうか?

環境

Windows 11
CUDA 11.6.2
Python 3.10.9
pip install torch==1.13.1+cu116 torchvision==0.14.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
pip install git+https://github.com/huggingface/diffusers.git
pip install transformers==4.26.1
pip install accelerate==0.16.0

Pythonスクリプト

import torch
from diffusers import StableDiffusionAttendAndExcitePipeline

#model_id = "CompVis/stable-diffusion-v1-4"
model_id = "stable-diffusion-v1-4"
pipe = StableDiffusionAttendAndExcitePipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
prompt = "a cat and a frog"

token_indices = [2, 5]
seed = 6141
generator = torch.Generator("cuda").manual_seed(seed)

images = pipe(
    prompt=prompt,
    token_indices=token_indices,
    guidance_scale=7.5,
    generator=generator,
    num_inference_steps=50,
    max_iter_to_alter=25,
).images

image = images[0]
image.save(f"{prompt}_{seed}.png")

結果


確かに複数の(この場合2つの)オブジェクトが描画されています。

注意点

「a cat and a frog」というprompt中の「cat」と「frog」に注目しています。
指定するインデックは2と5になるようです。

token_indices = [2, 5]

なんとなく1と4を指定してしまいそうになるので注意が必要です。