見出し画像

HuggingFace Diffusers v0.10.0の新機能

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

・Diffusers v0.10.0

前回

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

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

2. Depth-Guided Stable Diffusion と 2.1 チェックポイント

「Depth-Guided Stable Diffusion」を完全サポートされています。このモデルは、「MiDaS」による単眼深度の推定値で調整されており、「structure-preserving img2img」 および 「shape-conditional synthesis」 に使用できます。

「MiDaS」は、ソースからTransformersをインストールする必要があります。

pip install --upgrade git+https://github.com/huggingface/transformers/
import torch
import requests
from PIL import Image
from diffusers import StableDiffusionDepth2ImgPipeline

pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(
   "stabilityai/stable-diffusion-2-depth",
   torch_dtype=torch.float16,
).to("cuda")

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
init_image = Image.open(requests.get(url, stream=True).raw)

prompt = "two tigers"
n_propmt = "bad, deformed, ugly, bad anotomy"
image = pipe(prompt=prompt, image=init_image, negative_prompt=n_propmt, strength=0.7).images[0]

「Stable Diffusion 2.1」チェックポイントもリリースされ、完全サポートしています。

3. safetensors

「safetensors」をサポートしました。これは、テンソルを安全に (pickle ではなく) 格納するための新しい形式です。

比較の詳細は、こちらを参照。

pip install safetensors
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
pipe.save_pretrained("./safe-stable-diffusion-2-1", safe_serialization=True)

# you can also push this checkpoint to the HF Hub and load from there
safe_pipe = StableDiffusionPipeline.from_pretrained("./safe-stable-diffusion-2-1")

4. 新規パイプラインの追加

4-1. Paint-by-example

「Paint by Example」の実装。拡散モデルによる模範解答ベースの画像編集。

import PIL
import requests
import torch
from io import BytesIO
from diffusers import DiffusionPipeline

def download_image(url):
    response = requests.get(url)
    return PIL.Image.open(BytesIO(response.content)).convert("RGB")

img_url = "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/image/example_1.png"
mask_url = "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/mask/example_1.png"
example_url = "https://raw.githubusercontent.com/Fantasy-Studio/Paint-by-Example/main/examples/reference/example_1.jpg"

init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))
example_image = download_image(example_url).resize((512, 512))

pipe = DiffusionPipeline.from_pretrained("Fantasy-Studio/Paint-by-Example", torch_dtype=torch.float16)
pipe = pipe.to("cuda")

image = pipe(image=init_image, mask_image=mask_image, example_image=example_image).images[0]

4-2. Audio Diffusion と Latent Audio Diffusion

「Audio Diffusion」は、オーディオサンプルをメルスペクトログラムイメージとの間で変換することにより、拡散モデルを使用したイメージ生成の最近の進歩を活用します。

from IPython.display import Audio
from diffusers import DiffusionPipeline

pipe = DiffusionPipeline.from_pretrained("teticio/audio-diffusion-ddim-256").to("cuda")

output = pipe()
display(output.images[0])
display(Audio(output.audios[0], rate=pipe.mel.get_sample_rate()))

4-3. [実験的] Stable Diffusion のための K-Diffusion パイプライン

このパイプラインは、@crowsonkb の k-diffusion の最新のスケジューラをサポートするために追加されています。このパイプラインの目的は、スケジューラの実装と更新を比較することです。そのため、他のパイプラインの新機能がサポートされる可能性は低いです。

pip install k-diffusion
from diffusers import StableDiffusionKDiffusionPipeline
import torch

pipe = StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base")
pipe = pipe.to("cuda")

pipe.set_scheduler("sample_heun")
image = pipe("astronaut riding horse", num_inference_steps=25).images[0]

5. 新規スケジューラの追加

5-1. Heun scheduler inspired

@crowsonkb’s k-diffusionから移植されたKarras et. al.スケジューラ。


from diffusers import HeunDiscreteScheduler

pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
pipe.scheduler = HeunDiscreteScheduler.from_config(pipe.scheduler.config)

5-2. シングルステップ DPM-Solver

詳しくは、元の論文その改良版を参照してください。

from diffusers import DPMSolverSinglestepScheduler

pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1")
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(pipe.scheduler.config)

次回



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