Stable Diffusion派生モデルをDiffusersから使う

最終更新日:2023年8月11日

この記事の内容は古くなっています。
「from_single_file」を使うとこの記事にあるような変換は不要です。
こちらを参照して下さい。

一応古くなった記事も残しておきます。

はじめに

多くのStable Diffusion派生モデルがありますがすべてが「Diffusers」から使えるわけではありません。

「ckpt」ファイルしか公開されていないものを「Diffusers」から使用するためには一工夫が必要です。

とはいっても「Diffusers」公式が変換ツールを公開してくれているのでそれを使うだけです。

さっそくやってみたのでその過程を記録しておきます。

環境

通常の「Diffusers」使用環境に加えて「omegaconf」と「torchmetrics」が必要になります。

pip install omegaconf torchmetrics



通常の「Diffusers」使用環境はこちらの通りに構築しています。

方法

今回は「Healy's Anime Blend」というモデルを変換してみます。

こちらのブログ記事でその存在を知りました。
hnnitns.hatenablog.com

ckptファイルのダウンロード

公式から「healysAnimeBlend_17.ckpt」をダウンロードします。

YAMLファイルのダウンロード

「Healy's Anime Blend」のベースモデルはSD1.5とのことなのでStable Diffusion v1.xの公式リポジトリから「v1-inference.yaml」をダウンロードします。「configs/stable-diffusion」フォルダ内にあります。

Diffusersリポジトリのクローン

git clone https://github.com/huggingface/diffusers
cd diffusers/scripts

「scripts」フォルダ内に「convert_original_stable_diffusion_to_diffusers.py」というファイルが存在します。今回はそちらを使用します。

「scripts」フォルダへ先ほどダウンロードした「healysAnimeBlend_17.ckpt」と「v1-inference.yaml」を移動させます。

実行

ヘルプを頼りに実行します。

"--checkpoint_path"
help="Path to the checkpoint to convert."

"--original_config_file"
help="The YAML config file corresponding to the original architecture.",

"--num_in_channels"
help="The number of input channels. If `None` number of input channels will be automatically inferred.",

"--scheduler_type"
help="Type of scheduler to use. Should be one of ['pndm', 'lms', 'ddim', 'euler', 'euler-ancestral', 'dpm']",

"--pipeline_type"
help="The pipeline type. If `None` pipeline will be automatically inferred.",

"--image_size"
help="The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Siffusion v2 Base. Use 768 for Stable Diffusion v2."

"--prediction_type"
help="The prediction type that the model was trained on. Use 'epsilon' for Stable Diffusion v1.X and Stable diffusion v2 Base. Use 'v-prediction' for Stable Diffusion v2."

"--extract_ema"
    "Only relevant for checkpoints that have both EMA and non-EMA weights. Whether to extract the EMA weights or not. Defaults to `False`. Add `--extract_ema` to extract the EMA weights. EMA weights usually yield higher quality images for inference. Non-EMA weights are usually better to continue fine-tuning."

"--upcast_attention"
help="Whether the attention computation should always be upcasted. This is necessary when running stable diffusion 2.1."

"--dump_path"
help="Path to the output model."

"--device"
help="Device to use (e.g. cpu, cuda:0, cuda:1, etc.)"
python convert_original_stable_diffusion_to_diffusers.py --checkpoint_path healysAnimeBlend_17.ckpt --original_config_file v1-inference.yaml --image_size 512 --prediction_type epsilon --extract_ema --dump_path anime-blend --device cuda:0

これで「anime-blend」というフォルダが作成されます。

わかりやすく改行したのがこちらです。

python convert_original_stable_diffusion_to_diffusers.py ^
  --checkpoint_path healysAnimeBlend_17.ckpt ^
  --original_config_file v1-inference.yaml ^
  --image_size 512 ^
  --prediction_type epsilon ^
  --extract_ema ^
  --dump_path anime-blend ^
  --device cuda:0

動作確認

import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    './anime-blend',
    torch_dtype=torch.float32
).to('cuda')

prompt = "masterpiece, best quality, high quality, absurdres, 1girl, green hair, sweater, looking at viewer, upper body, beanie, outdoors, watercolor, night, turtleneck"
 
negative_prompt = "worst quality, low quality, medium quality, deleted, lowres, comic, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"

image = pipe(
    prompt = prompt,
    negative_prompt = negative_prompt
    ).images[0]  

image.save('result.png')

結果


追記(SD2.1の場合)

Waifu Diffusion 1.5 Beta 2 - Frost Aesthetic Verを変換した際のコマンドを残しておきます。

python convert_original_stable_diffusion_to_diffusers.py ^
  --checkpoint_path wd15-beta2-frosty-aesthetic-fp32.safetensors ^
  --original_config_file v2-inference.yaml ^
  --image_size 768 ^
  --prediction_type v-prediction ^
  --extract_ema ^
  --dump_path wd15-beta2-extra ^
  --upcast_attention ^
  --from_safetensors ^
  --to_safetensors ^
  --device cuda:0

追記(SDXL)の場合

別記事を書きました。
touch-sp.hatenablog.com