# 概述

🤗 Diffusers 提供了一系列训练脚本，用于训练您自己的扩散模型。您可以在 [diffusers/examples](https://github.com/huggingface/diffusers/tree/main/examples) 中找到我们所有的训练脚本。

每个训练脚本都是：

- **自包含**：训练脚本不依赖任何本地文件，运行脚本所需的所有包均从 `requirements.txt` 文件中安装。
- **易于调整**：训练脚本展示了如何针对特定任务训练扩散模型，但无法直接适用于所有训练场景。您可能需要根据具体用例对训练脚本进行适配。为此，我们完全开放了数据预处理代码和训练循环，以便您自行修改使用。
- **对初学者友好**：训练脚本的设计初衷是易于上手和理解，而非采用最新的尖端方法以追求最优且最具竞争力的结果。我们认为过于复杂的训练方法会被有意排除在外。
- **单一用途**：每个训练脚本都明确针对单一任务进行设计，以保持其可读性和可理解性。

我们当前的训练脚本集合包括：

| 训练 | 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) | 👍 | 👍 |
| [文本反转](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) | 👍 |  |
| [自定义扩散](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 有特定的 requirements 文件。如果您使用的是这些脚本之一，请确保安装其对应的 requirements 文件。

```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) 以启用内存高效的注意力机制

