ジャコ Lab

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

狙ったものがでない (3)

zako-lab929.hatenablog.com

zako-lab929.hatenablog.com

前回、前々回とモデルページに載っている画像を参考に、
全く同じプロンプトで同じものが出せないのか?を試しています。

zako-lab929.hatenablog.com

diffusers の scheduler について調べていた上記では、
scheduler を変えても全く同じ画像が出せる ことがわかっています。

あとは、パイプラインはモデルIDのみ指定して作っているので、
AnyLoRA_bakedVae_fp16_NOTpruned が使えていないんじゃないか説を残すのみとなっています。

これを試してダメだったら諦めです。

前回のパラメータ

パラメータ名
Scheduler DPMSolverSinglestepScheduler(..., use_karras_sigmas=True)
guidance_scale 9
num_inference_steps 30
Generator(seed) 3310753072
num_hidden_layers 11 (Clip skip 2)
size 512x512

今回は、これに加えて、 パイプラインを作るときのモデルの指定方法を変える予定です。

後述のコメントより、num_hidden_layers は誤りであることがわかっています
更に touch.Generator() を作る際に device="cuda" を入れていないことに気づきました
device="cuda" の有り無しで出力が少し変わったので何かしらの影響は出ていそうです

更にコメントより

num_hidden_layers の使い方が違いそうとのアドバイスをいただきました
id:touch-sp さん、コメントありがとうございます!

huggingface.co

ちゃんとドキュメントも確認し、仰る通りであることを確認しました

Load safetensors

huggingface.co

今回はモデルID指定ではなく、
Hugging Face リポジトリ上の特定の .safetensors を使いたいパターンです。

見るならこのドキュメントでしょうか?
自分はたぶん これ が使いたいはずです。

詳細は以下の記事に書きました

zako-lab929.hatenablog.com

その前に... use_safetensors=True とは...

use_safetensors=True これは付けるとどうなるんだろう?

If you look at the runwayml/stable-diffusion-v1-5 repository,

runwayml/stable-diffusion-v1-5 を見ると...

you’ll see weights inside the text_encoder, unet and vae subfolders are stored in the .safetensors format.

text_encoder とか unet とかの重み?がサブフォルダに .safetensors がある

By default, 🤗 Diffusers automatically loads these .safetensors files from their subfolders if they’re available in the model repository.

デフォルトは自動的にそれらを読み込む


と書いてあるのかな?
じゃあ、あまり気にしなくてよいでしょうか???

やりたいのはこれのはず!!

However, model weights are not necessarily stored in separate subfolders like in the example above. Sometimes, all the weights are stored in a single .safetensors file. In this case, if the weights are Stable Diffusion weights, you can load the file directly with the from_single_file() method:

from diffusers import StableDiffusionPipeline

pipeline = StableDiffusionPipeline.from_single_file(
    "https://huggingface.co/WarriorMama777/OrangeMixs/blob/main/Models/AbyssOrangeMix/AbyssOrangeMix.safetensors"
)

やってみる!!

出力のスクリプトは何個か作っておきます

clip_skip, num_hidden_layers 未指定

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    width=512, height=512,
).images[0]
image.save(f"{seed}_none.png")

num_hidden_layers 指定

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    num_hidden_layers=11,
    width=512, height=512,
).images[0]
image.save(f"{seed}_num_hidden_layers.png")

本命 clip_skip 指定

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=512, height=512,
).images[0]
image.save(f"{seed}_clipskip.png")

clip_skip x サイズ違い

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=448, height=448
).images[0]
image.save(f"{seed}_clip_skip448.png")

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=456, height=456
).images[0]
image.save(f"{seed}_clip_skip456.png")

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=768, height=768,
).images[0]
image.save(f"{seed}_clip_skip768.png")
450x450 付近を狙っているのはモデルページの画像がそのサイズだったからです。本当にそのサイズかは不明です。

結果

(touch.Generator に device="cuda" なし)

touch.Generator()

_(:3 」∠)チーン...

(touch.Generator に device="cuda" あり)

torch.Generator(device="cuda")

_(:3 」∠)チーン...

今回のスクリプト

折りたたみ

from diffusers import StableDiffusionPipeline, DPMSolverSinglestepScheduler
import torch

# AnyLoRA モデルの `AnyLoRA_bakedVae_fp16_NOTpruned` を使う
pipe = StableDiffusionPipeline.from_single_file(
    "https://huggingface.co/Lykon/AnyLoRA/blob/main/AnyLoRA_bakedVae_fp16_NOTpruned.safetensors",
).to("cuda")

# ローカル上にある lora.safetensors を使用する
pipe.load_lora_weights(".", weight_name="lora.safetensors")
# LoRA パラメータで出力にどの程度影響を与えるかを制御する
pipe.fuse_lora(lora_scale=0.6)

# Scheduler の設定
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True)

# プロンプトの準備
prompt = "~モデルページからプロンプトをコピペ~"
negative_prompt = "~モデルページからネガティブプロンプトをコピペ~"

# シード設定
seed = 3310753072
generator = torch.Generator(device="cuda")
generator.manual_seed(seed)

print(f"Seeds: {seed}")
print(f"Prompt: {prompt}")
print(f"NegativePrompt: {negative_prompt}")

# パイプライン実行
image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    width=512, height=512,
).images[0]
image.save(f"{seed}_none.png")

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    num_hidden_layers=11,
    width=512, height=512,
).images[0]
image.save(f"{seed}_num_hidden_layers.png")

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=512, height=512,
).images[0]
image.save(f"{seed}_clipskip.png")

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=768, height=768,
).images[0]
image.save(f"{seed}_clip_skip768.png")

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=448, height=448
).images[0]
image.save(f"{seed}_clip_skip448.png")

image = pipe(
    prompt,
    negative_prompt=negative_prompt,
    guidance_scale=9,
    num_inference_steps=30,
    generator=generator,
    clip_skip=2,
    width=456, height=456
).images[0]
image.save(f"{seed}_clip_skip456.png")

まとめ

ということで、ダメでした!
これ以上は差分がわからないため...諦めモードです!
でも色々調べられたからヨシとします!