【Diffusers】IP-Adapter attention maskingで遊んでみた

はじめに

以前にmultiple IP-Adaptersを使ったことがあります。
touch-sp.hatenablog.com
それと似たようなものですが、画像のどの部分にIP-Adapterを使用するかをmask画像で大まかに指定することが可能になりました。
github.com

結果

用意した二人の顔写真


作成画像


さすがIP-Adapterで、人物の特徴を確かにとらえていると思います。

作成画像(mask画像を入れ替えたもの)


腕がおかしなことになってしまいましたが、mask画像を入れ替えると確かに人物の左右が入れ替わっています。

注意

Inpaintの時のようにmask画像を正確に反映はしないようです。
なんとなく画像の右側、左側程度に機能している印象です。

Pythonスクリプト

import torch
from diffusers import AutoPipelineForText2Image, DDIMScheduler
from transformers import CLIPVisionModelWithProjection
from diffusers.utils import load_image
from diffusers.image_processor import IPAdapterMaskProcessor

image_encoder = CLIPVisionModelWithProjection.from_pretrained(
    "IP-Adapter", 
    subfolder="models/image_encoder",
    torch_dtype=torch.float16,
)

pipeline = AutoPipelineForText2Image.from_pretrained(
    "model/modernDisneyXL_v3",
    torch_dtype=torch.float16,
    variant="fp16",
    image_encoder=image_encoder,
).to("cuda")
pipeline.scheduler = DDIMScheduler.from_config(
    pipeline.scheduler.config,
    clip_sample=False
)
pipeline.load_ip_adapter(
  "IP-Adapter", 
  subfolder="sdxl_models", 
  weight_name=["ip-adapter-plus-face_sdxl_vit-h.safetensors"] * 2
)
pipeline.set_ip_adapter_scale([0.7] * 2)
pipeline.enable_model_cpu_offload()

face_image1 = load_image("face0.png")
face_image2 = load_image("face1.png")
mask1 = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/ip_mask_mask1.png")
mask2 = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/ip_mask_mask2.png")

output_height = 1024
output_width = 1024
processor = IPAdapterMaskProcessor()
masks = processor.preprocess([mask1, mask2], height=output_height, width=output_width)

ip_images =[[face_image1], [face_image2]]

seed = 0
generator = torch.manual_seed(seed)

image = pipeline(
    prompt="2 girls, disney style",
    ip_adapter_image=ip_images,
    negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality", 
    num_inference_steps=30,
    num_images_per_prompt=1,
    generator=generator,
    cross_attention_kwargs={"ip_adapter_masks": masks}
).images[0]

image.save(f"result_{seed}.png")





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