原文:The Illustrated Stable Diffusion

作者:Jay Alammar

形式一:

text to image

形式二:

text + image to image

Stable Diffusion

文本到图片生成,整体结构,

其中,

Text Encoder

Text Encoder: Transformer 模型,CLIP 模型的文本编码器,输入文本,输出表示了文本中每个词的向量表示. 如,77 token embeddings vectors, each in 768 dimensions.

Image Generator

然后,将向量表示输入到图像生成器,如:

Image Generator 包括两个阶段

[1] - Image information creator

该阶段是 SD 的核心.

在该阶段,需要迭代很多次,以生成图像信息(image information). 即,SD 中的 steps 参数,一般默认是 50 或100.

Image information creator 一般是在在图像信息空间,或者潜空间中进行. 相比于像素空间,速度更快. 其主要是由 UNet 网络和 Scheduling 算法组成.

[2] - Image Decoder

Image Decoder 将信息构建器中的信息解码为图像,在得到最终图片前,其仅需要运行一次.

总述

Stable Diffusion 主要包括三个组成:

  • CLIP Text, 用于文本编码

    输入:text
    输出:77 token embeddings vectors, each in 768 dim.
  • UNet + Scheduler,用于在信息空间(潜空间)渐进地处理/扩散信息.

    输入:text embeddings 和 noise tensor
    输出:处理后的 information array
  • Autoencoder Decoder,用于将 information array 解码为图片.

    输入:处理后的 information array, 维度:(4, 64, 64)
    输出:图片,维度为(3, 512, 512). # (rgb, width, height)
  • 如图:

Schedulers

https://huggingface.co/docs/diffusers/using-diffusers/schedulers

Diffusion models 往往简化了从 noise 样本到 less noisy 样本的 forward 过程,而 Schedulers 定义了整个 denoising process, 如:

  • denoising steps
  • Stochastic or deterministic,随机或确定性
  • 用于寻找 denoised sample 的算法

Schedules 是相当复杂的,通常是 denoising speed 和 denoising quality. 很难量化评估哪种 scheduler 更好.

Diffusion 介绍

Diffusion 是在 image information creator 中进行的.

给定文本的 token 表示,以及一个随机初始化的 image information array,即noise tensor,会输出用于图像解码的 information array.

如图,

该过程是 step-by-step 的. 每一步都新增加更多的相关信息. 为了可视化过程,以一个随机 latents array 为例,观察其被转化为视觉噪声的过程.

Diffusion 在多个 steps 都会发生,每一个 step 处理一个输入 latents array,得到更能理解输入文本的另一个 latents array.

再如,潜变量集合,来观察每一 step 被添加的信息:

整个过程如:

这里,第二部到第四步出现了一些有趣的事情,好像是将轮廓从噪声中浮现出来.

Diffusion 的工作原理

Diffusion models 生成图像的中心思想,依赖于强大的计算机视觉模型. 给定足够大的数据集,模型可以学习到复杂操作.

Diffusion models 是这样来处理图像生成问题的,

假设有一张图片,生成一些噪声noise,并将其添加到图像上,如图,

其可以认为是一个训练样本. 类似的,可以创建更多的训练样本,用于训练 image generator model. 如图,

上图示例了从图像(0, no noise)到完全噪声(4, total noise). 可以很容易的控制添加到图像的噪声. 如,可以划分为 10 steps,对数据集中的每张图像创建10个训练样本.

基于构建的训练数据集,就可以训练 noise predictor. 一个好的 noise predictor 能够在特定配置下,真实的生成图像.

一次训练 step 如,

到这一步,图片就彻底变成了噪声.

这里,假设将这些样本训练一个CV模型. 给定 step 步数和图片,期望预测在前一步被添加了多少噪声.

虽然此示例显示了从图像到总噪声的两个步骤,但可以控制向图像添加更多噪声,因此可以将其分散到数十个步骤中,为训练数据集中的所有图像创建每个图像的数十个训练示例 .

有意思的是,当我们能够得到这样一个噪声预测模型时,我们就可以有效的画出图片,通过一处一定 step 的噪声.

逐步移除噪声以生成图像

训练得到的 noise predictor,可以根据 noisy 图像和 denoising step ,来能够预测 noise slice.

则,从图像中减去预测得到的 sampled noise,即可得到与模型训练的图像更接近的图像.

如果训练数据集是质量比较好的,比如,Stable Diffusion 训练所采用的 LAION Aesthetics,生成的图像也会比较好. 如果是在 logos 图像训练的,则会得到 logo-generating 模型.

综上,覆盖了 Denoising Diffusion Probabilistic Models 说描述的大部分 diffusion models 图像生成的知识. 而且不仅仅是 Stable Diffusion, 也是 Dall-E 2 和 Google’s Imagen 的主要组成.

不过,至此还没涉及任何使用 text 数据进行图像生成的部分. 仅可用于图像生成,但还不能控制生成图像的内容.

潜空间 Diffusion 以进行加速计算

Stable Diffusion 论文里提出不是在 pixel images 进行 diffusion process,而是在图像压缩空间. High-Resolution Image Synthesis with Latent Diffusion Models 称之为 Departure to Latent Space.

图像压缩是通过 Autoencoder 来实现的. Autoencoder 的 encoder 将图像压缩到 latent space,decoder 则用于将压缩信息重建为图像.

首先将原图进行压缩,自编码器

然后,即可在压缩的 latents 上进行 diffusion forward 处理. noise predictor 实际上是在 latent space 训练的,以用于预测 noise.

(采用 autoencoder 的 encoder)forward process 是关于如何生成数据以训练 noise predictor. 当训练得到 noise predictor 后,即可(采用 autoencoder 的 decoder)reverse process 来生成图像.

上图两个 flows 图,即是 LDM/Stable Diffusion 论文里的 Figure3. 如,

不同之处在于,Figure3 里包含 conditioning 部分,其是 text prompts 引导模型应该生成的图像.

Text Encoder:Transformer 语言模型

Transformer 语言模型用于自然语言理解,其以 text prompt 作为输入,并输出 token embeddings.

开源 Stable Diffusion 模型是采用 ClipText (GPT-based model),而论文是采用 BERT.

Imagen 论文里给出了不同语言模型的选择对比. 相比于较大的图像生成部分,较大的语言模型对生成图像质量影响更大.

Larger/better language models have a significant effect on the quality of image generation models. Source: Google Imagen paper by Saharia et. al.. Figure A.5.

StableDiffusionV2 采用的是 OpenCLIP. text models 的参数量为 354M,而 ClipText 的参数两是 63M.

https://stability.ai/blog/stable-diffusion-v2-release

CLIP 训练

CLIP 的训练基于图片和对应描述(captions) 的样本,数据量大约为 400 million,如:

实际上,CLIP 的训练数据是,网络上爬取的图片以及对应的 "alt" tags.

CLIP 是由 image encoder 和 text encoder 组成. 其训练过程可以简单看做是 image 和其 caption 的处理,分别采用 image encoder 和 text encoder 对其编码.

然后,采用 cosine similarity 来计算编码后 embeddings 的相似性. 开始训练时,相似性会比较低,即使 text 能够正确描述图像.

接着,更新两个 encoder 模型,迭代.

采用大的 batchsizes 对数据集重复迭代,最终可以得到 encoders 模型能够输出表征 image 和 caption 相似的 embeddings.

类似于 word2vec,训练过程也需要包含图像和captions 不匹配的 negative 样本,且模型需要能够给予较低的相似度.

文本信息送入图像生成过程

Feeding Text Information Into The Image Generation Process

为了将文本变成图像生成过程的一部分,需要调整 noise predictor,以适应文本作为输入.

因为数据集中包含编码后的文本,由于是在 latent space 的操作,输入图像和预测的 noise 也都需要在 latent space.

为了更深入的了解 text tokens 在 UNet 中的使用,继续看下 UNet 中的内部结构.

UNet Noise predictor (without text)

在没有文本输入时,diffusion UNet 的输入和输出,如图:

由图可知,

  • UNet 是用于对 latens array 进行转换的网络层.
  • 每个网络层对上一网络层的输出进行处理.
  • 某些输出(通过 residuall connections)传递到后面的网络层
  • timestep 被转换为 time stem embedding vector,以用于网络层中的处理.

UNet Noise predictor WITH text

在采用了文本输入时,diffusion UNet 的输入和输出,如图:

主要更动是,对 ResNet blocks 添加了 attention 层,以支持文本输入,

ResNet block 并未直接对文本进行处理. 但 attention layers 将 latents 中的 text 表示进行融合. 下一个 ResNet block 即可使用整合后的 text 信息.

Resources

Last modification:May 9th, 2023 at 10:54 am