見出し画像

HuggingFace Diffusers v0.3.0の新機能

「Diffusers v0.3.0」の新機能についてまとめました。

・Diffusers v0.3.0

 1. Diffusers v0.3.0 のリリースノート

情報元となる「Diffusers 0.3.0」のリリースノートは、以下で参照できます。

2. 新しいドキュメント

ガイド、リンク、APIリファレンスを含む「Diffusers」のドキュメントページを作成しました。

3. 新しいAPI

「Pipeline」「Model」「Scheduler」の出力が、「sample」から「images」に置き換えられました。

image = pipe("The red cat is sitting on a chair")["sample"][0]

  ↓

image = pipe("The red cat is sitting on a chair").images[0]

# or
image = pipe("The red cat is sitting on a chair")["image"][0]
# or
image = pipe("The red cat is sitting on a chair")[0]

以下も同様です。

sample = unet(...).sample
prev_sample = scheduler(...).prev_sample

また、以下のメソッドに対して、破壊的な変更が加えられてます。

◎ VQModel.encode
単一のテンソルの代わりに dict/dataclass を返します。将来的には、複数のテンソルを返す可能性が非常に高くなります。
latents = model.encode(...) を latents = model.encode(...)[0] または latents = model.encode(...).latens に変更してください。

◎ VQModel.decode
単一のテンソルの代わりに dict/dataclass を返します。将来的には、複数のテンソルを返す可能性が非常に高くなります。
sample = model.decode(...) を sample = model.decode(...)[0] または sample = model.decode(...).sample に変更してください。

◎ VQModel.forward
単一のテンソルの代わりに dict/dataclass を返します。将来的には、複数のテンソルを返す可能性が非常に高くなります。
sample = model(...) を sample = model(...)[0] または sample = model(...).sample に変更してください。

◎ AutoencoderKL.encode
単一のテンソルの代わりに dict/dataclass を返します。将来的には、複数のテンソルを返す能性が非常に高くなります。
latent_dist = model.encode(...) を latent_dist = model.encode(...)[0] または latent_dist = model.encode(...).latent_dist に変更してください。

◎ AutoencoderKL.decode
単一のテンソルの代わりに dict/dataclass を返します。将来的には、複数のテンソルを返すことが必要になる可能性が非常に高くなります。
sample = model.decode(...) を sample = model.decode(...)[0] または sample = model.decode(...).sample に変更してください。

◎ AutoencoderKL.forward
単一のテンソルの代わりに dict/dataclass を返します。将来的には、複数のテンソルを返すことが必要になる可能性が非常に高くなります。
sample = model(...) を sample = model(...)[0] または sample = model(...).sample に変更してください。

4. 新しいStableDiffusionパイプライン

新しいStableDiffusionパイプラインが追加されました。

・Image-to-image generation : テキストプロンプトと画像から画像を生成するパイプライン。
・【実験的】Inpainting : テキストと画像とマスクから画像を生成するパイプライン。

詳しくは、新しい APIリファレンスを参照してください。

Colabのサンプルは、次のとおりです。

Text-to-Image Generation
Image-to-Image Text-Guided Generation
Text-Guided Image Inpainting

5. GPUのメモリが少ないマシンでの利用

現在のDiffusionモデルは、basujindal/stable-diffusion#117 で説明されている最適化のおかげで、10%の速度を犠牲にして、大幅に少ないVRAMで動作します。Attensionの最適化を利用するには、パイプラインをロードした後に enable_attention_slicing() で有効にするだけです。

from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4", 
    revision="fp16", 
    torch_dtype=torch.float16,
    use_auth_token=True
).to("cuda")
pipe.enable_attention_slicing()

これにより、より多くのユーザーが自分のコンピューターで 「Stable Diffusion」を利用できるようになります。

7. Textual Inversion

「Textual Inversion」を使用すると、わずか 3~5枚の画像を使用して、独自の画像で「Stable Diffusion」のモデルをパーソナライズできます。

・GitHub : https://github.com/huggingface/diffusers/tree/main/examples/textual_inversion
・Training : https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_textual_inversion_training.ipynb
・Inference : https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/stable_conceptualizer_inference.ipynb

8. Apple Silicon の MPSバックエンド

「Diffusers」は、PyTorch mps デバイスを使用して、「Stable Diffusion」の推論のためにApple シリコンと互換性があります。 M1 または M2 CPU を搭載した Mac に PyTorch Preview (Nightly) をインストールし、通常どおりパイプラインを使用する必要があります。

from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=True)
pipe = pipe.to("mps")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]

大幅なスピードアップ (M1 Max の 214 秒に対して 31 秒) が見られますが、まだいくつかの制限があります。 詳細については、ドキュメントを参照してください。

9. Stable Diffusion のための実験的な ONNX エクスポーターとパイプライン

ONNXランタイムと互換性のある新しい (そして実験的な) Stable Diffusionパイプラインを紹介します。これにより、ONNX をサポートする任意のハードウェアで 「Stable Diffusion」を実行できます (CPU の大幅な高速化を含む)。

「StableDiffusionPipeline」の代わりに「StableDiffusionOnnxPipeline」を使用する必要があります。また、リポジトリのonnx ブランチからウェイトをダウンロードし、使用するランタイムプロバイダ (次の例では CPU) を指定する必要があります。

from diffusers import StableDiffusionOnnxPipeline

pipe = StableDiffusionOnnxPipeline.from_pretrained(
    "CompVis/stable-diffusion-v1-4",
    revision="onnx",
    provider="CPUExecutionProvider",
    use_auth_token=True,
)

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]

【注意】上記のスクリプトは、外部の ONNX 重みをダウンロードするのに時間がかかるため、チェックポイントを自分で変換する方が高速です。独自のチェックポイントを変換するには、変換スクリプトをローカルで実行します。

python scripts/convert_stable_diffusion_checkpoint_to_onnx.py --model_path="CompVis/stable-diffusion-v1-4" --output_path="./stable_diffusion_onnx"

その後、ローカル パスからロードできます。

pipe = StableDiffusionOnnxPipeline.from_pretrained("./stable_diffusion_onnx", provider="CPUExecutionProvider")

次回



この記事が気に入ったらサポートをしてみませんか?