# 概要

🤗 Diffusers は、独自の拡散モデルを学習するためのトレーニングスクリプトのコレクションを提供しています。すべてのトレーニングスクリプトは [diffusers/examples](https://github.com/huggingface/diffusers/tree/main/examples) にあります。

各トレーニングスクリプトは、次のとおりです。

- **自己完結型**: 学習スクリプトはローカルファイルに依存せず、スクリプトの実行に必要なすべてのパッケージは`requirements.txt`ファイルからインストールされます。
- **カスタマイズしやすい**: 学習スクリプトは、特定のタスクに対して拡散モデルを学習する方法の例であり、すべての学習シナリオでそのまま動作するわけではありません。具体的なユースケースに合わせて学習スクリプトを調整する必要がある場合が多いです。そのため、データ前処理コードと学習ループを完全に公開しており、ご自身の用途に合わせて修正できるようにしています。
- **初心者向け**: 学習スクリプトは、最新かつ最先端の手法を用いて最良で競争力のある結果を得ることを目的とするのではなく、初心者が理解しやすく扱いやすいように設計されています。複雑すぎると判断される学習手法は、意図的に除外されています。
- **単一目的**: 各トレーニングスクリプトは、読みやすく理解しやすいように、1つのタスクにのみ特化して設計されています。

現在のトレーニングスクリプトのコレクションには、以下が含まれます。

| トレーニング | SDXL対応 | LoRAサポート |
|---|---|---|
| [無条件画像生成](https://github.com/huggingface/diffusers/tree/main/examples/unconditional_image_generation) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/training_example.ipynb) |  |  |
| [テキストから画像へ](https://github.com/huggingface/diffusers/tree/main/examples/text_to_image) | 👍 | 👍 |
| [テキストualインバージョン](https://github.com/huggingface/diffusers/tree/main/examples/textual_inversion) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_textual_inversion_training.ipynb) |  |  |
| [DreamBooth](https://github.com/huggingface/diffusers/tree/main/examples/dreambooth) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_dreambooth_training.ipynb) | 👍 | 👍 |
| [ControlNet](https://github.com/huggingface/diffusers/tree/main/examples/controlnet) | 👍 |  |
| [InstructPix2Pix](https://github.com/huggingface/diffusers/tree/main/examples/instruct_pix2pix) | 👍 |  |
| [カスタム Diffusion](https://github.com/huggingface/diffusers/tree/main/examples/custom_diffusion) |  |  |
| [T2I-Adapters](https://github.com/huggingface/diffusers/tree/main/examples/t2i_adapter) | 👍 |  |
| [Kandinsky 2.2](https://github.com/huggingface/diffusers/tree/main/examples/kandinsky2_2/text_to_image) |  | 👍 |

これらの例は**積極的に**メンテナンスされています。期待通りに動作しない場合は、お気軽にissueを開いてください。別のトレーニング例を含めるべきだと感じられる場合は、[Feature Request](https://github.com/huggingface/diffusers/issues/new?assignees=&labels=&template=feature_request.md&title=)を開始して、機能のアイデアについて議論し、それが自己完結型で、調整が容易で、初心者向けで、単一目的であるという基準を満たしているかどうかを確認してください。

## インストール

新しい仮想環境でソースからライブラリをインストールし、最新バージョンのサンプルスクリプトが正常に実行できることを確認してください。

```bash
git clone https://github.com/huggingface/diffusers
cd diffusers
pip install .
```

次に、トレーニングスクリプトのフォルダ（例: [DreamBooth](https://github.com/huggingface/diffusers/tree/main/examples/dreambooth)）に移動し、`requirements.txt` ファイルをインストールします。一部のトレーニングスクリプトには、SDXL や LoRA 用の特定の要件ファイルがあります。これらのスクリプトを使用する場合は、対応する要件ファイルをインストールすることを確認してください。

```bash
cd examples/dreambooth
pip install -r requirements.txt
# to train SDXL with DreamBooth
pip install -r requirements_sdxl.txt
```

トレーニングの高速化とメモリ使用量の削減のため、以下をお勧めします。

- PyTorch 2.0 以降を使用すると、[scaled dot product attention](../optimization/fp16#scaled-dot-product-attention)が自動的にトレーニング中に使用されます（トレーニングコードの変更は不要です）
- メモリ効率の高いアテンションを有効化するために[xFormers](../optimization/attention_backends)をインストールする

