# DDIM

Jiaming Song、Chenlin Meng、Stefano Ermon による [Denoising Diffusion Implicit Models](https://huggingface.co/papers/2010.02502) (DDIM)。

論文の抄録は以下の通りです：

*デノイジング拡散確率モデル（DDPM）は、敵対的学習なしで高品質な画像生成を実現していますが、サンプルを生成するには多くのステップでマルコフ連鎖をシミュレートする必要があります。サンプリングを高速化するために、DDPMと同じ訓練手順を持つ、より効率的な反復型暗黙的確率モデルのクラスであるデノイジング拡散暗黙的モデル（DDIM）を提案します。DDPMでは、生成過程はマルコフ拡散過程の逆過程として定義されます。我々は、同じ訓練目的関数に導く非マルコフ拡散過程のクラスを構築し、その逆過程からのサンプリングを大幅に高速化します。実験により、DDIMはDDPMと比較して実時間ベースで10倍から50倍高速に高品質なサンプルを生成でき、計算量とサンプル品質のトレードオフが可能であり、潜在空間内で意味のある画像補間を直接実行できることを示しました。*

元のコードベースは[ermongroup/ddim](https://github.com/ermongroup/ddim)で確認できます。

## DDIMPipeline[[diffusers.DDIMPipeline]]

#### diffusers.DDIMPipeline[[diffusers.DDIMPipeline]]

```python
diffusers.DDIMPipeline(unet: UNet2DModel, scheduler: DDIMScheduler)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/ddim/pipeline_ddim.py#L37)

**パラメータ:**

unet ([UNet2DModel](/docs/diffusers/main/ja/api/models/unet2d#diffusers.UNet2DModel)) : エンコードされた画像の潜在変数をノイズ除去するための`UNet2DModel`。

scheduler ([SchedulerMixin](/docs/diffusers/main/ja/api/schedulers/overview#diffusers.SchedulerMixin)) : `unet` と組み合わせて、エンコードされた画像のノイズ除去に使用するスケジューラ。[DDPMScheduler](/docs/diffusers/main/ja/api/schedulers/ddpm#diffusers.DDPMScheduler) または [DDIMScheduler](/docs/diffusers/main/ja/api/schedulers/ddim#diffusers.DDIMScheduler) のいずれかです。

画像生成用のPipeline。

このモデルは [DiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline) を継承しています。すべてのパイプラインに実装されている汎用的なメソッド（ダウンロード、保存、特定のデバイスでの実行など）については、スーパークラスのドキュメントを参照してください。

#### __call__[[diffusers.DDIMPipeline.__call__]]

```python
__call__(batch_size: int = 1, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, eta: float = 0.0, num_inference_steps: int = 50, use_clipped_model_output: bool | None = None, output_type: str | None = 'pil', return_dict: bool = True)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/ddim/pipeline_ddim.py#L62)

**パラメータ:**

batch_size (`int`、*任意*、デフォルトは 1) : 生成する画像の数。

generator (`torch.Generator`, *任意*) : 生成を決定論的にするための [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html)。

eta (`float`、*任意*、デフォルト値は 0.0) : [DDIM](https://huggingface.co/papers/2010.02502)論文のパラメータ eta (η) に相当します。[DDIMScheduler](/docs/diffusers/main/ja/api/schedulers/ddim#diffusers.DDIMScheduler)でのみ適用され、他のスケジューラーでは無視されます。`0`は DDIM に、`1`は DDPM に相当します。

num_inference_steps (`int`、*任意*、デフォルト値は 50) : デノイジングのステップ数です。デノイジングのステップ数を増やすと、推論速度が遅くなる代わりに、通常はより高品質な画像が得られます。

use_clipped_model_output (`bool`、*任意*、デフォルトは `None`) : `True` または `False` の場合、[DDIMScheduler.step()](/docs/diffusers/main/ja/api/schedulers/ddim#diffusers.DDIMScheduler.step) のドキュメントを参照してください。`None` の場合、スケジューラに何も渡されません（この引数をサポートしないスケジューラには `None` を使用してください）。

output_type (`str`、*任意*、デフォルトは`"pil"`) : 生成された画像の出力形式。`"pil"` (`PIL.Image`)、`"np"` (`np.array`)、または`"pt"` (`torch.Tensor`) から選択します。

return_dict (`bool`、*任意*、デフォルトは`True`) : プレーンなタプルではなく、[ImagePipelineOutput](/docs/diffusers/main/ja/api/pipelines/consistency_models#diffusers.ImagePipelineOutput)を返すかどうか。

**戻り値:** [ImagePipelineOutput](/docs/diffusers/main/ja/api/pipelines/consistency_models#diffusers.ImagePipelineOutput) または `tuple`

`return_dict` が `True` の場合、[ImagePipelineOutput](/docs/diffusers/main/ja/api/pipelines/consistency_models#diffusers.ImagePipelineOutput) が返され、それ以外の場合は、最初の要素が生成された画像のリストである `tuple` が返されます。

生成用の pipeline への呼び出し関数。

例:

```py
>>> from diffusers import DDIMPipeline
>>> import PIL.Image
>>> import numpy as np

>>> # load model and scheduler
>>> pipe = DDIMPipeline.from_pretrained("fusing/ddim-lsun-bedroom")

>>> # run pipeline in inference (sample random noise and denoise)
>>> image = pipe(eta=0.0, num_inference_steps=50)

>>> # process image to PIL
>>> image_processed = image.cpu().permute(0, 2, 3, 1)
>>> image_processed = (image_processed + 1.0) * 127.5
>>> image_processed = image_processed.numpy().astype(np.uint8)
>>> image_pil = PIL.Image.fromarray(image_processed[0])

>>> # save image
>>> image_pil.save("test.png")
```

## ImagePipelineOutput[[diffusers.ImagePipelineOutput]]

#### diffusers.ImagePipelineOutput[[diffusers.ImagePipelineOutput]]

```python
diffusers.ImagePipelineOutput(images: list[PIL.Image.Image] | numpy.ndarray)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/pipeline_utils.py#L135)

**パラメータ:**

images (`List[PIL.Image.Image]` または `np.ndarray`) : 長さ `batch_size` のデノイズ済み PIL 画像のリスト、または形状 `(batch_size, height, width, num_channels)` の NumPy 配列。

画像パイプライン用の出力クラス。

