# 拡散モデルの学習

無条件画像生成は、拡散モデルの代表的な応用の一つであり、学習に使用したデータセットの画像に似た画像を生成します。通常、特定のデータセットで事前学習済みモデルをファインチューニングすることで、最も良い結果が得られます。これらのチェックポイントは[Hub](https://huggingface.co/search/full-text?q=unconditional-image-generation&type=model)上で多く公開されていますが、お好みのものが見つからない場合は、ご自身でトレーニングすることもできます。

このチュートリアルでは、[Smithsonian Butterflies](https://huggingface.co/datasets/huggan/smithsonian_butterflies_subset) データセットの一部を使用して、[UNet2DModel](/docs/diffusers/v0.43.0/ja/api/models/unet2d#diffusers.UNet2DModel) をゼロから学習する方法を学び、オリジナルの🦋蝶🦋を生成します。

> [!TIP]
> 💡 このトレーニングチュートリアルは、[Training with 🧨 Diffusers](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/training_example.ipynb)ノートブックをベースにしています。拡散モデルの仕組みなど、追加の詳細や背景については、ノートブックを参照してください。

開始する前に、画像データセットの読み込みと前処理を行うための🤗 Datasets、およびGPUの台数を問わずトレーニングを簡素化するための🤗 Accelerateがインストールされていることを確認してください。以下のコマンドを実行すると、トレーニング指標を可視化するための[TensorBoard](https://www.tensorflow.org/tensorboard)もインストールされます（トレーニングの追跡には[Weights & Biases](https://docs.wandb.ai/)を使用することもできます）。

```py
# uncomment to install the necessary libraries in Colab
#!pip install diffusers[training]
```

コミュニティにモデルを共有することをお勧めします。そのためには、Hugging Face アカウントにログインする必要があります（アカウントをお持ちでない場合は、[こちら](https://hf.co/join)から作成してください！）。ノートブックからログインし、プロンプトが表示された際にトークンを入力します。トークンに write ロールが付与されていることを確認してください。

```py
>>> from huggingface_hub import notebook_login

>>> notebook_login()
```

または、ターミナルからログインします:

```bash
hf auth login
```

モデルのチェックポイントは非常に大きいため、これらの大きなファイルをバージョン管理するために[Git-LFS](https://git-lfs.com/)をインストールしてください。

```bash
!sudo apt -qq install git-lfs
!git config --global credential.helper store
```

## トレーニング設定

利便性のため、学習ハイパーパラメータ（必要に応じて調整してください）を含む`TrainingConfig`クラスを作成します。

```py
>>> from dataclasses import dataclass

>>> @dataclass
... class TrainingConfig:
...     image_size = 128  # the generated image resolution
...     train_batch_size = 16
...     eval_batch_size = 16  # how many images to sample during evaluation
...     num_epochs = 50
...     gradient_accumulation_steps = 1
...     learning_rate = 1e-4
...     lr_warmup_steps = 500
...     save_image_epochs = 10
...     save_model_epochs = 30
...     mixed_precision = "fp16"  # `no` for float32, `fp16` for automatic mixed precision
...     output_dir = "ddpm-butterflies-128"  # the model name locally and on the HF Hub

...     push_to_hub = True  # whether to upload the saved model to the HF Hub
...     hub_model_id = "<your-username>/<my-awesome-model>"  # the name of the repository to create on the HF Hub
...     hub_private_repo = None
...     overwrite_output_dir = True  # overwrite the old model when re-running the notebook
...     seed = 0

>>> config = TrainingConfig()
```

## データセットを読み込む

🤗 Datasets ライブラリを使用して、[Smithsonian Butterflies](https://huggingface.co/datasets/huggan/smithsonian_butterflies_subset) データセットを簡単に読み込むことができます:

```py
>>> from datasets import load_dataset

>>> config.dataset_name = "huggan/smithsonian_butterflies_subset"
>>> dataset = load_dataset(config.dataset_name, split="train")
```

> [!TIP]
> 💡 [HugGan Community Event](https://huggingface.co/huggan) で追加のデータセットを見つけることも、ローカルに [`ImageFolder`](https://huggingface.co/docs/datasets/image_dataset#imagefolder) を作成して独自のデータセットを使用することもできます。データセットが HugGan Community Event のものなら `config.dataset_name` にリポジトリ ID を、独自の画像を使用するなら `imagefolder` を設定してください。

🤗 Datasets は [Image](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Image) 機能を使用して、画像データを自動的にデコードし、[`PIL.Image`](https://pillow.readthedocs.io/en/stable/reference/Image.html) として読み込みます。これにより、以下のように可視化できます:

```py
>>> import matplotlib.pyplot as plt

>>> fig, axs = plt.subplots(1, 4, figsize=(16, 4))
>>> for i, image in enumerate(dataset[:4]["image"]):
...     axs[i].imshow(image)
...     axs[i].set_axis_off()
>>> fig.show()
```

    

ただし、画像のサイズはすべて異なるため、まず前処理を行う必要があります。

* `Resize` は、画像のサイズを `config.image_size` で定義されたサイズに変更します。
* `RandomHorizontalFlip` は、画像をランダムに左右反転させることでデータセットを拡張します。
* `Normalize` は、モデルが期待する [-1, 1] の範囲にピクセル値を再スケーリングするために重要です。

```py
>>> from torchvision import transforms

>>> preprocess = transforms.Compose(
...     [
...         transforms.Resize((config.image_size, config.image_size)),
...         transforms.RandomHorizontalFlip(),
...         transforms.ToTensor(),
...         transforms.Normalize([0.5], [0.5]),
...     ]
... )
```

🤗 Datasetsの[set_transform](https://huggingface.co/docs/datasets/v5.2.0/en/package_reference/main_classes#datasets.Dataset.set_transform)メソッドを使用して、学習中に`preprocess`関数をオンザフライで適用します:

```py
>>> def transform(examples):
...     images = [preprocess(image.convert("RGB")) for image in examples["image"]]
...     return {"images": images}

>>> dataset.set_transform(transform)
```

画像を再度可視化して、リサイズされたことを確認してみてください。これで、[DataLoader](https://pytorch.org/docs/stable/data#torch.utils.data.DataLoader)でデータセットをラップして学習に備える準備が整いました!

```py
>>> import torch

>>> train_dataloader = torch.utils.data.DataLoader(dataset, batch_size=config.train_batch_size, shuffle=True)
```

## UNet2DModel を作成する

🧨 Diffusers の事前学習済みモデルは、望むパラメータを指定してそのモデルクラスから簡単に作成できます。例えば、[UNet2DModel](/docs/diffusers/v0.43.0/ja/api/models/unet2d#diffusers.UNet2DModel) を作成するには:

```py
>>> from diffusers import UNet2DModel

>>> model = UNet2DModel(
...     sample_size=config.image_size,  # the target image resolution
...     in_channels=3,  # the number of input channels, 3 for RGB images
...     out_channels=3,  # the number of output channels
...     layers_per_block=2,  # how many ResNet layers to use per UNet block
...     block_out_channels=(128, 128, 256, 256, 512, 512),  # the number of output channels for each UNet block
...     down_block_types=(
...         "DownBlock2D",  # a regular ResNet downsampling block
...         "DownBlock2D",
...         "DownBlock2D",
...         "DownBlock2D",
...         "AttnDownBlock2D",  # a ResNet downsampling block with spatial self-attention
...         "DownBlock2D",
...     ),
...     up_block_types=(
...         "UpBlock2D",  # a regular ResNet upsampling block
...         "AttnUpBlock2D",  # a ResNet upsampling block with spatial self-attention
...         "UpBlock2D",
...         "UpBlock2D",
...         "UpBlock2D",
...         "UpBlock2D",
...     ),
... )
```

サンプル画像の形状がモデルの出力形状と一致していることを、すばやく確認しておくのがよい場合があります。

```py
>>> sample_image = dataset[0]["images"].unsqueeze(0)
>>> print("Input shape:", sample_image.shape)
Input shape: torch.Size([1, 3, 128, 128])

>>> print("Output shape:", model(sample_image, timestep=0).sample.shape)
Output shape: torch.Size([1, 3, 128, 128])
```

素晴らしい！次に、画像にノイズを追加するためにスケジューラーが必要です。

## スケジューラーを作成する

スケジューラは、モデルをトレーニングに使用しているか推論に使用しているかによって、動作が異なります。推論時には、スケジューラはノイズから画像を生成します。トレーニング時には、スケジューラは拡散プロセスの特定の時点におけるモデルの出力（またはサンプル）を取得し、*ノイズスケジュール*と*更新ルール*に従って画像にノイズを適用します。

[DDPMScheduler](/docs/diffusers/v0.43.0/ja/api/schedulers/ddpm#diffusers.DDPMScheduler)を見てみましょう。先ほどの`sample_image`にランダムなノイズを加えるために、`add_noise`メソッドを使用します。

```py
>>> import torch
>>> from PIL import Image
>>> from diffusers import DDPMScheduler

>>> noise_scheduler = DDPMScheduler(num_train_timesteps=1000)
>>> noise = torch.randn(sample_image.shape)
>>> timesteps = torch.LongTensor([50])
>>> noisy_image = noise_scheduler.add_noise(sample_image, noise, timesteps)

>>> Image.fromarray(((noisy_image.permute(0, 2, 3, 1) + 1.0) * 127.5).type(torch.uint8).numpy()[0])
```

    

モデルの学習目的は、画像に追加されたノイズを予測することです。このステップでの損失は、次の式で計算できます。

```py
>>> import torch.nn.functional as F

>>> noise_pred = model(noisy_image, timesteps).sample
>>> loss = F.mse_loss(noise_pred, noise)
```

## モデルを学習

ここまでで、モデルの学習を開始するための大部分の要素が揃いました。あとは、それらをすべて組み立てるだけです。

まず、オプティマイザーと学習率スケジューラーが必要です:

```py
>>> from diffusers.optimization import get_cosine_schedule_with_warmup

>>> optimizer = torch.optim.AdamW(model.parameters(), lr=config.learning_rate)
>>> lr_scheduler = get_cosine_schedule_with_warmup(
...     optimizer=optimizer,
...     num_warmup_steps=config.lr_warmup_steps,
...     num_training_steps=(len(train_dataloader) * config.num_epochs),
... )
```

次に、モデルを評価する方法が必要です。評価には、[DDPMPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/ddpm#diffusers.DDPMPipeline)を使用してサンプル画像のバッチを生成し、グリッドとして保存できます:

```py
>>> from diffusers import DDPMPipeline
>>> from diffusers.utils import make_image_grid
>>> import os

>>> def evaluate(config, epoch, pipeline):
...     # Sample some images from random noise (this is the backward diffusion process).
...     # The default pipeline output type is `List[PIL.Image]`
...     images = pipeline(
...         batch_size=config.eval_batch_size,
...         generator=torch.Generator(device='cpu').manual_seed(config.seed), # Use a separate torch generator to avoid rewinding the random state of the main training loop
...     ).images

...     # Make a grid out of the images
...     image_grid = make_image_grid(images, rows=4, cols=4)

...     # Save the images
...     test_dir = os.path.join(config.output_dir, "samples")
...     os.makedirs(test_dir, exist_ok=True)
...     image_grid.save(f"{test_dir}/{epoch:04d}.png")
```

これにより、🤗 Accelerate を使用して、TensorBoard によるログ記録、勾配の蓄積、混合精度学習を簡単に実行できるトレーニングループに、これらのコンポーネントをすべて組み込むことができます。モデルを Hub にアップロードするには、リポジトリ名と情報を取得する関数を作成し、それを Hub にプッシュします。

> [!TIP]
> 💡 以下の学習ループは長く、複雑に見えるかもしれませんが、後で1行のコードで学習を開始できることを考えると、その価値は十分にあります。今すぐ画像生成を始めたい場合は、以下のコードをコピーして実行してください。モデルの学習が完了するのを待っている間など、後でいつでも戻ってきて、学習ループをより詳しく確認できます。🤗

```py
>>> from accelerate import Accelerator
>>> from huggingface_hub import create_repo, upload_folder
>>> from tqdm.auto import tqdm
>>> from pathlib import Path
>>> import os

>>> def train_loop(config, model, noise_scheduler, optimizer, train_dataloader, lr_scheduler):
...     # Initialize accelerator and tensorboard logging
...     accelerator = Accelerator(
...         mixed_precision=config.mixed_precision,
...         gradient_accumulation_steps=config.gradient_accumulation_steps,
...         log_with="tensorboard",
...         project_dir=os.path.join(config.output_dir, "logs"),
...     )
...     if accelerator.is_main_process:
...         if config.output_dir is not None:
...             os.makedirs(config.output_dir, exist_ok=True)
...         if config.push_to_hub:
...             repo_id = create_repo(
...                 repo_id=config.hub_model_id or Path(config.output_dir).name, exist_ok=True
...             ).repo_id
...         accelerator.init_trackers("train_example")

...     # Prepare everything
...     # There is no specific order to remember, you just need to unpack the
...     # objects in the same order you gave them to the prepare method.
...     model, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(
...         model, optimizer, train_dataloader, lr_scheduler
...     )

...     global_step = 0

...     # Now you train the model
...     for epoch in range(config.num_epochs):
...         progress_bar = tqdm(total=len(train_dataloader), disable=not accelerator.is_local_main_process)
...         progress_bar.set_description(f"Epoch {epoch}")

...         for step, batch in enumerate(train_dataloader):
...             clean_images = batch["images"]
...             # Sample noise to add to the images
...             noise = torch.randn(clean_images.shape, device=clean_images.device)
...             bs = clean_images.shape[0]

...             # Sample a random timestep for each image
...             timesteps = torch.randint(
...                 0, noise_scheduler.config.num_train_timesteps, (bs,), device=clean_images.device,
...                 dtype=torch.int64
...             )

...             # Add noise to the clean images according to the noise magnitude at each timestep
...             # (this is the forward diffusion process)
...             noisy_images = noise_scheduler.add_noise(clean_images, noise, timesteps)

...             with accelerator.accumulate(model):
...                 # Predict the noise residual
...                 noise_pred = model(noisy_images, timesteps, return_dict=False)[0]
...                 loss = F.mse_loss(noise_pred, noise)
...                 accelerator.backward(loss)

...                 if accelerator.sync_gradients:
...                     accelerator.clip_grad_norm_(model.parameters(), 1.0)
...                 optimizer.step()
...                 lr_scheduler.step()
...                 optimizer.zero_grad()

...             progress_bar.update(1)
...             logs = {"loss": loss.detach().item(), "lr": lr_scheduler.get_last_lr()[0], "step": global_step}
...             progress_bar.set_postfix(**logs)
...             accelerator.log(logs, step=global_step)
...             global_step += 1

...         # After each epoch you optionally sample some demo images with evaluate() and save the model
...         if accelerator.is_main_process:
...             pipeline = DDPMPipeline(unet=accelerator.unwrap_model(model), scheduler=noise_scheduler)

...             if (epoch + 1) % config.save_image_epochs == 0 or epoch == config.num_epochs - 1:
...                 evaluate(config, epoch, pipeline)

...             if (epoch + 1) % config.save_model_epochs == 0 or epoch == config.num_epochs - 1:
...                 if config.push_to_hub:
...                     upload_folder(
...                         repo_id=repo_id,
...                         folder_path=config.output_dir,
...                         commit_message=f"Epoch {epoch}",
...                         ignore_patterns=["step_*", "epoch_*"],
...                     )
...                 else:
...                     pipeline.save_pretrained(config.output_dir)
```

ふー、かなり多くのコードになりましたね！ついに🤗 Accelerateの[notebook_launcher](https://huggingface.co/docs/accelerate/v1.15.0/en/package_reference/launchers#accelerate.notebook_launcher)関数を使ってトレーニングを開始する準備が整いました。トレーニングループ、すべてのトレーニング引数、そしてトレーニングに使用するプロセス数（この値は利用可能なGPUの数に変更できます）を関数に渡します：

```py
>>> from accelerate import notebook_launcher

>>> args = (config, model, noise_scheduler, optimizer, train_dataloader, lr_scheduler)

>>> notebook_launcher(train_loop, args, num_processes=1)
```

トレーニングが完了したら、拡散モデルによって生成された最終的な🦋画像🦋を確認してください！

```py
>>> import glob

>>> sample_images = sorted(glob.glob(f"{config.output_dir}/samples/*.png"))
>>> Image.open(sample_images[-1])
```

    

## 次のステップ

無条件画像生成は、学習可能なタスクの一例です。[🧨 Diffusers Training Examples](../training/overview)ページにアクセスして、他のタスクや学習手法を探索できます。以下は、学習できる内容の例です。

* [Textual Inversion](../training/text_inversion)は、モデルに特定の視覚的概念を学習させ、生成された画像にそれを統合するアルゴリズムです。
* [DreamBooth](../training/dreambooth)は、対象の複数の入力画像から、その対象のパーソナライズされた画像を生成する技術です。
* [ガイド](../training/text2image)：独自のデータセットで Stable Diffusion モデルをファインチューニングする方法。
* [ガイド](../training/lora): LoRAは、非常に大規模なモデルをより高速にファインチューニングするためのメモリ効率の高い手法です。

