扩散课程文档

制作一个类条件扩散模型

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Open In Colab

制作一个类条件扩散模型

在本笔记本中,我们将演示一种向扩散模型添加条件信息的方法。具体来说,我们将基于第一单元的“从零开始”示例,在 MNIST 上训练一个类条件扩散模型,我们可以在推理时指定我们希望模型生成的数字。

正如本单元导言中提到的,这只是我们向扩散模型添加额外条件信息的众多方法之一,之所以选择这种方法是因为它相对简单。就像第一单元的“从零开始”笔记本一样,本笔记本主要用于说明目的,如果您愿意,可以安全地跳过它。

设置和数据准备

>>> %pip install -q diffusers
     |████████████████████████████████| 503 kB 7.2 MB/s 
     |████████████████████████████████| 182 kB 51.3 MB/s 
[?25h
>>> import torch
>>> import torchvision
>>> from torch import nn
>>> from torch.nn import functional as F
>>> from torch.utils.data import DataLoader
>>> from diffusers import DDPMScheduler, UNet2DModel
>>> from matplotlib import pyplot as plt
>>> from tqdm.auto import tqdm

>>> device = "mps" if torch.backends.mps.is_available() else "cuda" if torch.cuda.is_available() else "cpu"
>>> print(f"Using device: {device}")
Using device: cuda
>>> # Load the dataset
>>> dataset = torchvision.datasets.MNIST(
...     root="mnist/", train=True, download=True, transform=torchvision.transforms.ToTensor()
... )

>>> # Feed it into a dataloader (batch size 8 here just for demo)
>>> train_dataloader = DataLoader(dataset, batch_size=8, shuffle=True)

>>> # View some examples
>>> x, y = next(iter(train_dataloader))
>>> print("Input shape:", x.shape)
>>> print("Labels:", y)
>>> plt.imshow(torchvision.utils.make_grid(x)[0], cmap="Greys")
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to mnist/MNIST/raw/train-images-idx3-ubyte.gz

创建一个类条件 UNet

我们将按以下方式输入类条件

  • 创建一个标准的 UNet2DModel,其中包含一些额外的输入通道
  • 通过嵌入层将类标签映射到形状为 (class_emb_size) 的学习向量
  • 将此信息与 net_input = torch.cat((x, class_cond), 1) 连接起来,作为内部 UNet 输入的额外通道
  • 将此 net_input(总共有 class_emb_size+1 个通道)馈送到 UNet 以获得最终预测

在本例中,我将 class_emb_size 设置为 4,但这完全是任意的,您可以探索将其大小设置为 1(以查看它是否仍然有效)、大小设置为 10(以匹配类别的数量),或者直接用类标签的简单 one-hot 编码替换学习到的 nn.Embedding。

以下是实现的样子

class ClassConditionedUnet(nn.Module):
    def __init__(self, num_classes=10, class_emb_size=4):
        super().__init__()

        # The embedding layer will map the class label to a vector of size class_emb_size
        self.class_emb = nn.Embedding(num_classes, class_emb_size)

        # Self.model is an unconditional UNet with extra input channels to accept the conditioning information (the class embedding)
        self.model = UNet2DModel(
            sample_size=28,  # the target image resolution
            in_channels=1 + class_emb_size,  # Additional input channels for class cond.
            out_channels=1,  # the number of output channels
            layers_per_block=2,  # how many ResNet layers to use per UNet block
            block_out_channels=(32, 64, 64),
            down_block_types=(
                "DownBlock2D",  # a regular ResNet downsampling block
                "AttnDownBlock2D",  # a ResNet downsampling block with spatial self-attention
                "AttnDownBlock2D",
            ),
            up_block_types=(
                "AttnUpBlock2D",
                "AttnUpBlock2D",  # a ResNet upsampling block with spatial self-attention
                "UpBlock2D",  # a regular ResNet upsampling block
            ),
        )

    # Our forward method now takes the class labels as an additional argument
    def forward(self, x, t, class_labels):
        # Shape of x:
        bs, ch, w, h = x.shape

        # class conditioning in right shape to add as additional input channels
        class_cond = self.class_emb(class_labels)  # Map to embedding dimension
        class_cond = class_cond.view(bs, class_cond.shape[1], 1, 1).expand(bs, class_cond.shape[1], w, h)
        # x is shape (bs, 1, 28, 28) and class_cond is now (bs, 4, 28, 28)

        # Net input is now x and class cond concatenated together along dimension 1
        net_input = torch.cat((x, class_cond), 1)  # (bs, 5, 28, 28)

        # Feed this to the UNet alongside the timestep and return the prediction
        return self.model(net_input, t).sample  # (bs, 1, 28, 28)

如果任何形状或变换令人困惑,请添加打印语句以显示相关形状并检查它们是否符合您的预期。我还注释了一些中间变量的形状,希望能使事情更清楚。

训练和采样

之前我们会做类似 prediction = unet(x, t) 的操作,现在我们将添加正确的标签作为第三个参数 (prediction = unet(x, t, y)) 在训练期间,在推理时,我们可以传递我们想要的任何标签,如果一切顺利,模型应该生成匹配的图像。在本例中,y 是 MNIST 数字的标签,值从 0 到 9。

训练循环与第一单元的示例非常相似。我们现在预测的是噪声(而不是第一单元中的去噪图像),以匹配我们正在使用的默认 DDPMScheduler 期望的目标,该调度器用于在训练期间添加噪声并在推理时生成样本。训练需要一段时间 - 加快训练速度可能是一个有趣的迷你项目,但你们大多数人可能只需浏览代码(实际上是整个笔记本)而无需运行它,因为我们只是在说明一个想法。

# Create a scheduler
noise_scheduler = DDPMScheduler(num_train_timesteps=1000, beta_schedule="squaredcos_cap_v2")
>>> # @markdown Training loop (10 Epochs):

>>> # Redefining the dataloader to set the batch size higher than the demo of 8
>>> train_dataloader = DataLoader(dataset, batch_size=128, shuffle=True)

>>> # How many runs through the data should we do?
>>> n_epochs = 10

>>> # Our network
>>> net = ClassConditionedUnet().to(device)

>>> # Our loss function
>>> loss_fn = nn.MSELoss()

>>> # The optimizer
>>> opt = torch.optim.Adam(net.parameters(), lr=1e-3)

>>> # Keeping a record of the losses for later viewing
>>> losses = []

>>> # The training loop
>>> for epoch in range(n_epochs):
...     for x, y in tqdm(train_dataloader):

...         # Get some data and prepare the corrupted version
...         x = x.to(device) * 2 - 1  # Data on the GPU (mapped to (-1, 1))
...         y = y.to(device)
...         noise = torch.randn_like(x)
...         timesteps = torch.randint(0, 999, (x.shape[0],)).long().to(device)
...         noisy_x = noise_scheduler.add_noise(x, noise, timesteps)

...         # Get the model prediction
...         pred = net(noisy_x, timesteps, y)  # Note that we pass in the labels y

...         # Calculate the loss
...         loss = loss_fn(pred, noise)  # How close is the output to the noise

...         # Backprop and update the params:
...         opt.zero_grad()
...         loss.backward()
...         opt.step()

...         # Store the loss for later
...         losses.append(loss.item())

...     # Print out the average of the last 100 loss values to get an idea of progress:
...     avg_loss = sum(losses[-100:]) / 100
...     print(f"Finished epoch {epoch}. Average of the last 100 loss values: {avg_loss:05f}")

>>> # View the loss curve
>>> plt.plot(losses)
Finished epoch 0. Average of the last 100 loss values: 0.052451

训练结束后,我们可以采样一些图像,输入不同的标签作为我们的条件

>>> # @markdown Sampling some different digits:

>>> # Prepare random x to start from, plus some desired labels y
>>> x = torch.randn(80, 1, 28, 28).to(device)
>>> y = torch.tensor([[i] * 8 for i in range(10)]).flatten().to(device)

>>> # Sampling loop
>>> for i, t in tqdm(enumerate(noise_scheduler.timesteps)):

...     # Get model pred
...     with torch.no_grad():
...         residual = net(x, t, y)  # Again, note that we pass in our labels y

...     # Update sample with step
...     x = noise_scheduler.step(residual, t, x).prev_sample

>>> # Show the results
>>> fig, ax = plt.subplots(1, 1, figsize=(12, 12))
>>> ax.imshow(torchvision.utils.make_grid(x.detach().cpu().clip(-1, 1), nrow=8)[0], cmap="Greys")

就这样!我们现在可以控制生成哪些图像了。

希望您喜欢这个示例。与往常一样,欢迎在 Discord 中提问。

# Exercise (optional): Try this with FashionMNIST. Tweak the learning rate, batch size and number of epochs.
# Can you get some decent-looking fashion images with less training time than the example above?
< > 在 GitHub 上更新