【InstantStyle①】スタイル画像とプロンプトから新しい画像を生成する

はじめに

InstantStyleというモデルを使ってみました。
github.com
できることが色々あるようですが今回はスタイル画像1枚とプロンプトから新しい画像を生成してみます。

スタイル画像

サンプルにある画像をそのまま使わせてもらいました。

目的

スタイルを維持して猫の画像を作成します。
使用するのはスタイル画像とプロンプトのみです。

使用したプロンプトは以下の通りです。
prompt

a cat, masterpiece, best quality, high quality

negative prompt

text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry

結果

サンプルとして方法が3つ紹介されていたのですべて実行してみました。

target_blocks=["block"](for original IP-Adapter)


target_blocks=["up_blocks.0.attentions.1"](for style blocks only)


target_blocks=["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"](for style+layout blocks)


Pythonスクリプト

import torch
from diffusers import StableDiffusionXLPipeline
from diffusers.utils import load_image

from ip_adapter import IPAdapterXL

base_model_path = "sdxl_pipeline/stable-diffusion-xl-base-1.0"
image_encoder_path = "sdxl_models/image_encoder"
ip_ckpt = "sdxl_models/ip-adapter_sdxl.bin"
device = "cuda"

pipe = StableDiffusionXLPipeline.from_pretrained(
    base_model_path,
    torch_dtype=torch.float16,
    variant="fp16"
)

target_blocks_list = [
    # for original IP-Adapter
    ["block"],
    # for style blocks only
    ["up_blocks.0.attentions.1"],
    # for style+layout blocks
    ["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"] 
]

image = load_image("./assets/0.jpg").resize((512, 512))

for i in range(3):
    ip_model = IPAdapterXL(
        pipe, 
        image_encoder_path, 
        ip_ckpt,
        device, 
        target_blocks=target_blocks_list[i]
    )

    images = ip_model.generate(
        pil_image=image,
        prompt="a cat, masterpiece, best quality, high quality",
        negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
        scale=1.0,
        guidance_scale=5,
        num_samples=1,
        num_inference_steps=30, 
        seed=42
    )

    images[0].resize((512, 512)).save(f"result{i}.jpg")

PC環境

Windows 11
CUDA 11.8
Python 3.11

Python環境構築

pip install torch==2.2.2+cu118 --index-url https://download.pytorch.org/whl/cu118
pip install diffusers[torch]
pip install transformers einops
pip install git+https://github.com/tencent-ailab/IP-Adapter.git





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