本日はドキュメントの Instant Generation の項目を進めていきます。
端的に言うと IP-Adapter + LCM LoRA です。
なんとこれはdiffusers v0.24.0のリリースノートにもあったので以前にやっていました
よって本日は IP-Adapter Face Model + LCM LoRA で進めていこうと思います。Face Model は ip-adapter-full-face_sd15 を使いますので、この前やった薔薇を持っている女性を生成してみます。
この前やった記事
IP-Adapter Face Model + LCM を試す
期待値はこんな感じ。これを高速生成できれば勝ちです
各種モデル等をロードする
・ベースモデル:dreamlike-art/dreamlike-anime-1.0
・IP-Adapter:ip-adapter-full-face_sd15
・LCM LoRA:latent-consistency/lcm-lora-sdv1-5
・スケジューラ:LCMScheduler
・IP-Adapter:ip-adapter-full-face_sd15
・LCM LoRA:latent-consistency/lcm-lora-sdv1-5
・スケジューラ:LCMScheduler
import torch from diffusers import DiffusionPipeline, LCMScheduler # ベースモデルをロードする pipe = DiffusionPipeline.from_pretrained( "dreamlike-art/dreamlike-anime-1.0", torch_dtype=torch.float16 ).to("cuda") # IP-Adapter をロードする pipe.load_ip_adapter( "h94/IP-Adapter", subfolder="models", weight_name="ip-adapter-full-face_sd15.bin" ) pipe.set_ip_adapter_scale(0.6) # LCM LoRA をロードする pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5") # スケジューラーを設定する pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config) # Enable memory savings pipe.enable_model_cpu_offload()
IP-Adapter に使用する画像をロードする
from diffusers.utils import load_image # IP-Adapter 用の画像をロードする ip_adapter_image_url = "https://raw.githubusercontent.com/tencent-ailab/IP-Adapter/main/assets/images/ai_face2.png" ip_adapter_image = load_image(ip_adapter_image_url)
パイプラインを実行する
import torch # シードを設定する generator = torch.Generator(device="cuda").manual_seed(42) # パイプラインを実行する prompt = "A photo of a girl wearing a black dress, holding red roses in hand, upper body, behind is the Eiffel Tower" images = pipe( prompt=prompt, ip_adapter_image=ip_adapter_image, num_inference_steps=4, num_images_per_prompt=4, guidance_scale=1, generator=generator, ).images
実行結果
from datetime import datetime from zoneinfo import ZoneInfo # Asia/Tokyo タイムゾーンの現在時刻を YYYYMMDDhhmmss 形式で得る formattedNow = datetime.now(tz=ZoneInfo("Asia/Tokyo")).strftime("%Y%m%d%H%M%S") for idx, image in enumerate(images): # 実行結果 image.save(f"image_{formattedNow}_{idx}_{generator.initial_seed()}.png")
期待通りの出来栄え!あとは速度が知りたいですね!
速度比較
パイプライン実行ロジックの速度を比較します。
前回 | 今回 (LCM LoRA適用) |
---|---|
50/50 [01:14<00:00, 1.46s/it] | 4/4 [00:04<00:00, 1.07s/it] |
早い!流石 LCM LoRA
まとめ
いい感じ