はじめに
IP-Adapterに関してはこちらを参照して下さい。touch-sp.hatenablog.com
touch-sp.hatenablog.com
環境
必要とするのはこれだけです。(PyTorchは自分の環境に合わせて変更して下さい)pip install torch==2.1.1+cu121 torchvision==0.16.1+cu121 --index-url https://download.pytorch.org/whl/cu121 pip install diffusers[torch] pip install transformers
モデルのダウンロード
実行スクリプト内でこのように書けば事前のダウンロードは不要です。pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="models", weight_name="ip-adapter_sd15.bin")
どうしても明示的にダウンロードを行いたい方は以下を行ってください。
ダウンロード用のスクリプトを書きました。
今回はSDXLを使用する前提です。
import os from huggingface_hub import hf_hub_download from huggingface_hub import snapshot_download repo_id="h94/IP-Adapter" folder = os.path.basename(repo_id) hf_hub_download( repo_id=repo_id, filename="sdxl_models/image_encoder/config.json", local_dir=folder, local_dir_use_symlinks=False ) snapshot_download( repo_id=repo_id, allow_patterns=[ "sdxl_models/*.safetensors" ], local_dir=folder, local_dir_use_symlinks=False )
以下でもダウンロードできますが不要なファイルまでダウンロードされてしまいます。
git lfs install git clone https://huggingface.co/h94/IP-Adapter
実行スクリプト
from diffusers import AutoPipelineForText2Image import torch from diffusers.utils import load_image pipeline = AutoPipelineForText2Image.from_pretrained( #"stabilityai/stable-diffusion-xl-base-1.0", "model/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16 ).to("cuda") pipeline.load_ip_adapter( #"h94/IP-Adapter", "IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.safetensors" ) scale = 0.6 pipeline.set_ip_adapter_scale(scale) image_url = "https://cdn-ak.f.st-hatena.com/images/fotolife/t/touch-sp/20230912/20230912133611.jpg" ip_image = load_image(image_url) generator = torch.manual_seed(33) image = pipeline( prompt="best quality, high quality, wearing sunglasses", ip_adapter_image=ip_image, negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality", num_inference_steps=50, generator=generator ).images[0] image.save(f"sdxl_t2i_scale{scale}.png") from diffusers.utils import make_image_grid result = make_image_grid([ip_image, image], rows=1, cols=2) result.save("ip-adapter_result.jpg")
結果
女性にサングラスをかけさせようとした結果です。左の画像が元画像、右の画像が今回作成した画像です。残念ながら同一人物には見えませんが・・・。元画像は「firsttunnerxl_v10.safetensors」を使って作成したものです。
touch-sp.hatenablog.com