はじめに
こちらのスクリプトをDiffusers用に書き換えました。touch-sp.hatenablog.com
スタイル画像
こちらの画像を使わせてもらいました。結果
シードを変えて4枚の画像を作成しました。スタイルを維持しながらウサギを猫に変えています。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 git+https://github.com/huggingface/diffusers pip install accelerate transformers peft
Pythonスクリプト
from diffusers import AutoPipelineForText2Image from diffusers.utils import load_image import torch from pathlib import Path pipeline = AutoPipelineForText2Image.from_pretrained( # downloaded from https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0 "model/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16" ).to("cuda") pipeline.load_ip_adapter( # downloaded from https://huggingface.co/h94/IP-Adapter "IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.safetensors") scale = { "down": {"block_2": [0.0, 1.0]}, "up": {"block_0": [0.0, 1.0, 0.0]}, } pipeline.set_ip_adapter_scale(scale) style_image = load_image("https://github.com/InstantStyle/InstantStyle/blob/main/assets/0.jpg?raw=true") Path("results").mkdir(exist_ok=False) seed_list =[0, 123, 2024, 20240423] for seed in seed_list: generator = torch.manual_seed(seed) image = pipeline( prompt="a cat, masterpiece, best quality, high quality", ip_adapter_image=style_image, negative_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry", guidance_scale=5, num_inference_steps=30, generator=generator, ).images[0] image.save(Path("results", f"result_{seed}.png").as_posix())