はじめに
タイトル通りSDXL 1.0 (Stable Diffusion XL 1.0) で Inpainting をやってみました。mask画像とプロンプトを用意する必要があります。SDXL 1.0の基本的な使い方はこちらを参照して下さい。touch-sp.hatenablog.com
結果
写真に写る犬を猫に変換しています。元画像、mask画像は512x512ですが下記スクリプトの結果は1024x1024になります。
Pythonスクリプト
from diffusers import AutoPipelineForInpainting from diffusers.utils import load_image import torch pipe = AutoPipelineForInpainting.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True ).to("cuda") img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png" prompt = "cat, sitting on a park bench, 8k" image = pipe( prompt=prompt, image=load_image(img_url), mask_image=load_image(mask_url), num_inference_steps=50, strength=0.80).images[0] image.save("inpaint_result.png")
その他のInpaintingモデル
PaintByExample
touch-sp.hatenablog.compix2pix-zero
touch-sp.hatenablog.comInstruct-Pix2Pix
mask画像は不要です。「replace dog with cat」みたいなプロンプトで変換可能です。最も直感的で自分はこのモデルが好きです。
touch-sp.hatenablog.com
DiffEdit
mask画像は不要です。元画像のプロンプト「a dog sitting on a park bench」、
目標画像(ターゲット画像)のプロンプト「a cat sitting on a park bench」みたいなプロンプトで変換可能です。
touch-sp.hatenablog.com