【StableDiffusionXLInstructPix2PixPipeline】ある画像に対して「写っている犬を猫に変えて」みたいなプロンプトで新しい画像が生成できる Instruct-Pix2Pix が SDXL に対応したのでさっそく使ってみました

はじめに

以前 Instruct-Pix2Pix の記事を書きました。
touch-sp.hatenablog.com
今回 SDXLに対応したとのことで改めて試してみました。
huggingface.co
768x768の画像データで学習されているようです。

特徴

写真に写る犬を猫に変換したい場合プロンプトは「replace dog with cat」のようにします。

それだけです。

このようなマスク画像を用意しなくて良いのが他のInpaintモデルとの違いになります。

結果

左が元画像、右が生成画像です。




比較として以前の結果ものせておきます。

Pythonスクリプト

import torch
from diffusers import StableDiffusionXLInstructPix2PixPipeline
from diffusers.utils import load_image

resolution = 768
image = load_image(
    "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
    ).resize((resolution, resolution))

pipe = StableDiffusionXLInstructPix2PixPipeline.from_pretrained(
    #"diffusers/sdxl-instructpix2pix-768", 
    "model/sdxl-instructpix2pix-768",
    torch_dtype=torch.float16
    ).to("cuda")

edit_instruction = "replace dog with cat"

for i in range(10):
    seed = 100000 + 10000 * i
    generator = torch.manual_seed(seed)
    edited_image = pipe(
        prompt=edit_instruction,
        image=image,
        height=resolution,
        width=resolution,
        guidance_scale=3.0,
        image_guidance_scale=1.5,
        num_inference_steps=30
        ).images[0]

    edited_image.save(f"instructpix2pix_result_seed{seed}.png")

設定

主な設定項目は「guidance_scale」と「image_guidance_scale」だと思います。
公式サイトの説明文とその翻訳(by DeepL)をのせておきます。

guidance_scale (default: 5.0)

公式サイトそのまま

Guidance scale is enabled by setting guidance_scale > 1.
Higher guidance scale encourages to generate images that are closely linked to the text prompt, usually at the expense of lower image quality.

DeepLによる日本語翻訳

guidance scaleは、guidance_scale > 1に設定することで有効になる。
guidance scaleを大きくすると、テキストプロンプトに密接にリンクした画像を生成するようになるが、通常は画質が低下する。

image_guidance_scale (default: 1.5)

公式サイトそのまま

Image guidance scale is enabled by setting image_guidance_scale > 1.
Higher image guidance scale encourages to generate images that are closely linked to the source image image, usually at the expense of lower image quality.

DeepLによる日本語翻訳

image_guidance_scaleは、image_guidance_scale > 1 に設定することで有効になります。
image_guidance_scaleを大きくすると、通常、画質が低下する代償として、ソース画像に密接にリンクした画像を生成するようになる。




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