【Diffusers】DiffusersからT2I-Adapterが使えるようになったのでBRA(Beautiful Realistic Asians) V6と組み合わせて使ってみました

from PIL import Image
import numpy as np
import torch
from diffusers.utils import load_image
from controlnet_aux import PidiNetDetector
from diffusers import StableDiffusionAdapterPipeline, T2IAdapter
from compel import Compel

image = load_image("https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/examples/sketch/human.png")
image.save('original.png')

Pidi = PidiNetDetector.from_pretrained("lllyasviel/Annotators")

threshold = 0.6
pidi_array = np.array(Pidi(image).convert("L"))
bool_array = pidi_array > (255 * threshold)
result_array = np.where(bool_array == False, 0, 255).astype(np.uint8)
result_pillow = Image.fromarray(result_array)        
result_pillow.save("sketch.png")

adapter = T2IAdapter.from_pretrained("adapter/t2iadapter_sketch_sd15v2", torch_dtype=torch.float16)
pipe = StableDiffusionAdapterPipeline.from_pretrained(
    "model/Brav6",
    adapter=adapter,
    safety_checker=None,
    torch_dtype=torch.float16
)
pipe.to("cuda")

prompt = "masterpiece++, 8k+, photorealistic+, (best quality)+, absurdres, attractive, ultra high res, ultra realistic, highly detailed, photo of pretty Japanese woman"
negative_prompt = "(Worst Quality)++, (low quality)+, text, username, watermark"

compel_proc = Compel(
    tokenizer=pipe.tokenizer,
    text_encoder=pipe.text_encoder,
    truncate_long_prompts=False)

prompt_embeds = compel_proc([prompt])
negative_prompt_embeds = compel_proc([negative_prompt])

seed = 20000
n_samples = 10

for i in range(n_samples):
    seed_i = seed + i * 1000
    generator = torch.manual_seed(seed_i)
    out_image = pipe(
        prompt_embeds=prompt_embeds,
        negative_prompt_embeds = negative_prompt_embeds,
        image=result_pillow,
        width=768,
        height=768,
        generator=generator
    ).images[0]

    out_image.save(f"result_seed{seed_i}.png")

T2I-Adapterのsketchを使う場合には「controlnet_aux」の「PidiNetDetector」を使うのが良さそうです。

スクリプト内の「threshold」の値を変更するとスケッチの線の太さが変わります。

ControlNetとの違いは?

Diffusersの公式ページにこのように書いてあります。

原文そのまま

T2I-Adapter is similar to ControlNet. 
T2i-Adapter uses a smaller auxiliary network which is only run once for the entire diffusion process. 
However, T2I-Adapter performs slightly worse than ControlNet.

DeepLによる翻訳

T2I-Adapter は ControlNet に似ている。
T2i-Adapterは、拡散プロセス全体で一度だけ実行される、より小さな補助ネットワークを使用する。
ただし、T2i-Adapterの性能はControlNetよりも若干劣る。




このエントリーをはてなブックマークに追加