Diffusers documentation
Consistency Models
Consistency Models
Consistency Models は、Yang Song、Prafulla Dhariwal、Mark Chen、Ilya Sutskever による Consistency Models で提案されました。
論文の抄録は以下の通りです:
Diffusionモデルは画像・音声・動画生成の分野を大きく前進させましたが、反復的なサンプリングプロセスに依存しているため、生成が遅いという課題があります。この制約を克服するために、ノイズをデータへ直接マッピングすることで高品質なサンプルを生成する新しいモデルファミリーであるconsistencyモデルを提案します。consistencyモデルは設計上、1ステップでの高速生成をサポートしつつ、計算量とサンプル品質のトレードオフを可能にするマルチステップサンプリングにも対応します。また、これらのタスクに対する明示的なトレーニングを必要とせずに、画像インペインティング、カラー化、スーパーリゾリューションなどのゼロショットデータ編集にも対応します。consistencyモデルは、事前学習済みDiffusionモデルからの蒸留によって学習することも、完全に独立した生成モデルとして学習することも可能です。広範な実験を通じて、1ステップおよび少数ステップのサンプリングにおいて、既存のDiffusionモデル向け蒸留手法を上回ることを示しました。1ステップ生成において、CIFAR-10でFID 3.55、ImageNet 64x64で6.20という新たなSOTAを達成しています。独立して学習された場合、consistencyモデルは、CIFAR-10、ImageNet 64x64、LSUN 256x256といった標準ベンチマークにおいて、既存の1ステップ・非敵対的生成モデルを上回る性能を発揮する、新しい生成モデルファミリーとなります。
元のコードベースはopenai/consistency_modelsで確認でき、追加のチェックポイントはopenaiで利用できます。
このパイプラインは、dg845 と ayushtues によって貢献されました。❤️
ヒント
さらに高速化するために、torch.compile を使用して1秒未満で複数の画像を生成できます:
import torch
from diffusers import ConsistencyModelPipeline
device = "cuda" # or "mps", "xpu", "cpu"
# Load the cd_bedroom256_lpips checkpoint.
model_id_or_path = "openai/diffusers-cd_bedroom256_lpips"
pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, dtype=torch.float16)
pipe.to(device)
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
# Multistep sampling
# Timesteps can be explicitly specified; the particular timesteps below are from the original GitHub repo:
# https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L83
for _ in range(10):
image = pipe(timesteps=[17, 0]).images[0]
image.show()ConsistencyModelPipeline
class diffusers.ConsistencyModelPipeline
< source >( unet: UNet2DModelscheduler: CMStochasticIterativeScheduler )
パラメータ
- unet (UNet2DModel) —
エンコードされた画像の潜在変数からノイズを除去するための
UNet2DModel。 - scheduler (SchedulerMixin) —
unetと組み合わせて、エンコードされた画像の潜在変数をデノイズするために使用するスケジューラ。現在、CMStochasticIterativeScheduler でのみ互換性があります。
無条件またはクラス条件付きの画像生成を行うPipelineです。
このモデルは DiffusionPipeline を継承しています。すべてのパイプラインに実装されている汎用的なメソッド(ダウンロード、保存、特定のデバイスでの実行など)については、スーパークラスのドキュメントを参照してください。
__call__
< source >( batch_size: int = 1class_labels: typing.Union[torch.Tensor, list[int], int, NoneType] = Nonenum_inference_steps: int = 1timesteps: list = Nonegenerator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = Nonelatents: typing.Optional[torch.Tensor] = Noneoutput_type: str | None = 'pil'return_dict: bool = Truecallback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = Nonecallback_steps: int = 1 ) → ImagePipelineOutput or tuple
パラメータ
- batch_size (
int、任意、デフォルトは 1)— 生成する画像の数を指定します。 - class_labels (
torch.Tensorまたはlist[int]またはint、省略可) — クラス条件付き一貫性モデルの条件付けに使用する任意のクラスラベルです。モデルがクラス条件付きでない場合は使用されません。 - num_inference_steps (
int, 任意、デフォルト値は 1) — デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。 - timesteps (
list[int], 任意) — ノイズ除去プロセスに使用するカスタムタイムステップ。未定義の場合、等間隔のnum_inference_steps個のタイムステップが使用されます。降順で指定する必要があります。 - generator (
torch.Generator、省略可) — 生成を決定論的にするためのtorch.Generator。 - latents (
torch.Tensor, 任意) — ガウス分布からサンプリングされた事前生成されたノイズ付き latents。画像生成のインプットとして使用します。異なるプロンプトで同じ生成を調整するために使用できます。指定されない場合、指定されたランダムgeneratorを用いてサンプリングを行い、latents テンソルが生成されます。 - output_type (
str、任意、デフォルトは"pil") — 生成された画像の出力形式です。PIL.Imageまたはnp.arrayから選択します。 - return_dict (
bool、省略可能、デフォルトはTrue) —Trueの場合、通常のタプルではなく ImagePipelineOutput を返します。 - callback (
Callable, 省略可) — 推論中にcallback_stepsステップごとに呼び出される関数です。この関数は、以下の引数とともに呼び出されます:callback(step: int, timestep: int, latents: torch.Tensor)。 - callback_steps (
int、任意、デフォルトは 1) —callback関数が呼び出される頻度です。指定しない場合、callback はすべてのステップで呼び出されます。
戻り値
ImagePipelineOutput または tuple
return_dict が True の場合、ImagePipelineOutput が返され、それ以外の場合は tuple が返されます。このとき、最初の要素は生成された画像のリストです。
例:
>>> import torch
>>> from diffusers import ConsistencyModelPipeline
>>> device = "cuda"
>>> # Load the cd_imagenet64_l2 checkpoint.
>>> model_id_or_path = "openai/diffusers-cd_imagenet64_l2"
>>> pipe = ConsistencyModelPipeline.from_pretrained(model_id_or_path, torch_dtype=torch.float16)
>>> pipe.to(device)
>>> # Onestep Sampling
>>> image = pipe(num_inference_steps=1).images[0]
>>> image.save("cd_imagenet64_l2_onestep_sample.png")
>>> # Onestep sampling, class-conditional image generation
>>> # ImageNet-64 class label 145 corresponds to king penguins
>>> image = pipe(num_inference_steps=1, class_labels=145).images[0]
>>> image.save("cd_imagenet64_l2_onestep_sample_penguin.png")
>>> # Multistep sampling, class-conditional image generation
>>> # Timesteps can be explicitly specified; the particular timesteps below are from the original GitHub repo:
>>> # https://github.com/openai/consistency_models/blob/main/scripts/launch.sh#L77
>>> image = pipe(num_inference_steps=None, timesteps=[22, 0], class_labels=145).images[0]
>>> image.save("cd_imagenet64_l2_multistep_sample_penguin.png")ImagePipelineOutput
class diffusers.ImagePipelineOutput
< source >( images: list[PIL.Image.Image] | numpy.ndarray )
画像パイプライン用の出力クラス。