ジャコ Lab

プログラミング関連のメモ帳的ブログです

コミュニティパイプラインで IP-Adapter FaceID モデルを実行してみるができなかった件

github.com

前回の記事で IP-Adapter FaceID でエラーになり、色々調べているうちに同じような問題にハマっている人を見つけました。

前回の記事

なんかヒントになりそうなことが書かれているので深堀りしていきます

Issue を読み進める

github.com

Issue の中身を読んでいくと

diffusers 0.25.0 does not support the two models you mentioned because they are experimental versions.

と書かれていました。

diffusers v0.25.0 では、その2つのモデルはサポートされていないとな?

早速自分の diffusers のバージョンを調べてみましたが v0.27.2 ということで、少しバージョンは高いようです。

$ pip list | grep diffusers
diffusers                        0.27.2

続けて、以下のような記述があります。

PR #6276 is adding support for IPAdapter FaceID.

PR #6276 で IP-Adapter FaceID がサポートされた??

早速 PR #6276 を見てみます。

PR #6276 を読み進める

github.com

どうやら最終的にコミュニティパイプラインとしての実装で落ち着いたようです。

コミュニティパイプラインで IP-Adapter FaceID を実行してみる

github.com

とりあえず README 通りの実行

import diffusers
diffusers.utils.USE_PEFT_BACKEND = False
import torch
from diffusers.utils import load_image
import cv2
import numpy as np
from diffusers import DiffusionPipeline, AutoencoderKL, DDIMScheduler
from insightface.app import FaceAnalysis


noise_scheduler = DDIMScheduler(
    num_train_timesteps=1000,
    beta_start=0.00085,
    beta_end=0.012,
    beta_schedule="scaled_linear",
    clip_sample=False,
    set_alpha_to_one=False,
    steps_offset=1,
)
vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-mse").to(dtype=torch.float16)
pipeline = DiffusionPipeline.from_pretrained(
    "SG161222/Realistic_Vision_V4.0_noVAE",
    torch_dtype=torch.float16,
    scheduler=noise_scheduler,
    vae=vae,
    custom_pipeline="ip_adapter_face_id"
)
pipeline.load_ip_adapter_face_id("h94/IP-Adapter-FaceID", "ip-adapter-faceid_sd15.bin")
pipeline.to("cuda")

generator = torch.Generator(device="cpu").manual_seed(42)
num_images=2

image = load_image("https://huggingface.co/datasets/YiYiXu/testing-images/resolve/main/ai_face2.png")

app = FaceAnalysis(name="buffalo_l", providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
image = cv2.cvtColor(np.asarray(image), cv2.COLOR_BGR2RGB)
faces = app.get(image)
image = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
images = pipeline(
    prompt="A photo of a girl wearing a black dress, holding red roses in hand, upper body, behind is the Eiffel Tower",
    image_embeds=image,
    negative_prompt="monochrome, lowres, bad anatomy, worst quality, low quality", 
    num_inference_steps=20, num_images_per_prompt=num_images, width=512, height=704, 
    generator=generator
).images

for i in range(num_images):
    images[i].save(f"c{i}.png")

実行結果

AttributeError: 'Linear' object has no attribute 'set_lora_layer'
ぐぬぬ。。。エラーじゃん。。。

'Linear' object has no attribute 'set_lora_layer' について

PR #6276 内でも言及されてますが、

when diffusers.utils.USE_PEFT_BACKEND is True, this model can't be loaded. To bypass this constant, I made a little change in the definition, so you can put it to False

import diffusers
diffusers.utils.USE_PEFT_BACKEND = False

を実行すれば良い。となっており、README にも反映されています。

つまり、やってるのになぁ。。。

pip install で peft を入れるのが悪い可能性がある?

現在の pip install コマンドは以下のようになっています。

!pip install -U diffusers["torch"] transformers accelerate peft controlnet_aux onnxruntime insightface
onnxruntime insightface は昨日追加したやつ
peft は以下の記事でハマったが LoRA を使うときにエラーになってたので追加したやつ

zako-lab929.hatenablog.com

とりあえず外してみる?

pip install から peft を外してやってみましたが、ダメそうです。

AttributeError: 'Linear' object has no attribute 'set_lora_layer'

まとめ

詰んだ?