图像生成:扩散模型
参考资料:
CVPR 2023 Tutorial: Denoising Diffusion Models: A Generative Learning Big Bang
CVPR 2024 Tutorial: Diffusion-based Video Generative Models
【较真系列】讲人话- Diffusion Model 全解(原理+代码+公式)
Denoising Diffusion Models 在图像中的应用
Diffusion Model 的结构
-
基于 U-Net 结构:被广泛用于 text-to-image Diffusion Model 中
- Imagen
- Stable Diffusion
- eDiff-I

-
基于 Transformer 结构:将图像分割为 patch 后作为 tokens 输入至 Transformer 中
- Scalable Diffusion Models with Transformers
- One Transformer Fits All Distributions in Multi-Modal Diffusion at Scale
- Simple Diffusion: End-to-end Diffusion for High Resolution Images

使用 Diffusion Model 对图像进行编辑和定制
目前有三种常见的引导(Guidance)方法:
- RGB Pixel Guidance
- Text Guidance
- Reference Image Guidance
RGB Pixel Guidance
ICLR 2022, SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations
Given an input image with user guide in a form of manipulating RGB pixels, SDEdit first adds noise to the input, then subsequently denoises the resulting image through the SDE prior to increase its realism.
通过用户在原图上给出一些引导,比如 RGB 像素的涂鸦(stroke painting),甚至可以不给定原图,直接纯手工绘制一个涂鸦画作为输入,模型首先对输入添加噪声,最后通过随机微分方程的先验增加图片的真实性,最终根据输入的带有引导信息的图像生成对应的结果。

Synthesizing images from strokes with SDEdit. The blue dots illustrate the editing process of our method. The green and blue contour plots represent the distributions of images and stroke paintings, respectively. Given a stroke painting, we first perturb it with Gaussian noise and progressively remove the noise by simulating the reverse SDE. This process gradually projects an unrealistic stroke painting to the manifold of natural images.
下图中的蓝点代表本文的编辑过程,绿色分布代表真实图像的分布,蓝色分布代表 stroke paintings 的分布。
当模型首先被输入 stroke painting 后,使用高斯噪声进行扰动,然后通过模拟反向随机微分方程逐步移除噪声。这一过程逐步将不真实的 stroke painting 投影到自然、真实图像的分布中。

Text Guidance
ICLR 2023, DiffEdit: Diffusion-based semantic image editing with mask guidance
论文引入一个掩码生成模块,该模块确定图像的哪一部分应该被编辑,然后只对掩码部分执行基于文本的扩散。
首先用户输入参考图像以及两个查询文本和参考文本,查询文本 Query 是参考图像的标题或用于描述图像,参考文本 R 用于描述想要替换的效果。
掩码生成模块首先为输入图像添加噪声,并进行两次去噪,一次通过参考文本 R 进行,一次通过查询文本 Q 进行,并根据去噪结果的差异推导出参考图像中的掩码区域。

CVPR 2023, Imagic: Text-Based Real Image Editing with Diffusion Models
模型接受真实图像(参考图像)和目标图像文本描述 作为输入。
-
模型首先对目标文本进行编码,得到初始嵌入表示 ,然后优化 对原始图像进行重构,得到优化后的目标文本嵌入表示 。
目标文本首先通过一个文本编码器,该编码器输出其对应的文本嵌入 ,其中 是给定目标文本中的 token 数量,而 是 token 的嵌入维度。固定生成扩散模型 的参 数,并使用重构损失来优化目标文本嵌入 :
其中 , 是输入图像 的噪声版本, 是预训练的扩散模型权重。这使输入的图像尽可能接近的文本嵌入表示,即只更新 Text Embedding 的参数,从而得到 。
这种邻近性使得在嵌入空间中进行有意义的线性插值成为可能,而对于远距离的嵌入表示来说并不表现出线性行为。
此时使用的是低分辨率版本的 Diffusion Model,分辨率 ,训练设定为 100 次迭代。
-
由于第一步进行的 Text Embedding Optimization 步数较少,优化后的 并不能直接与输入图像对齐。
因此第二步固定优化后的目标文本嵌入表示 ,仍使用第一步中的 Reconstrcution Loss 对第一步使用的 分辨率的预训练 Diffusion Model 进行微调,设定为 1500 次迭代。
同时,为了捕获原始图像中的细节,保持高保真度,文章还会使用初始的 Text Embedding 层,即 以及原始图像通过 Reconstruction Loss 再 fine-tune 一个从 到 的超分辨率 Diffusion Model,设定为 1500 次迭代。
-
使用初始目标文本嵌入表示 插值优化后的目标文本表示 ,插值方式如下:
插值得到的文本嵌入表示分别通过 的基础 Diffusion Model、从 到 的超分辨率 Diffusion Model,以及最终的从 到 的超分辨率 Diffusion Model 得到最终的目标图像。

InstructPix2Pix: Learning to Follow Image Editing Instructions
主要方法
Our method consists of two parts: generating an image editing dataset, and training a diffusion model on that dataset. (a) We first use a finetuned GPT-3 to generate instructions and edited captions. (b) We then use StableDiffusion [52] in combination with Prompt-to-Prompt [17] to generate pairs of images from pairs of captions. We use this procedure to create a dataset (c) of over 450,000 training examples. (d) Finally, our InstructPix2Pix diffusion model is trained on our generated data to edit images from instructions. At inference time, our model generalizes to edit real images from human-written instructions.
论文使用现有的 LLMs 工具生成训练数据集,再使用上述生成的数据微调 Stable Diffusion Model,最终得到可以根据指令 caption 进行图像编辑的 Diffusion Model。
-
训练数据生成阶段:
- 首先使用 Input Caption 描述一张图像,并使用 GPT-3 通过指令 Instruction 生成修改过后的图像描述 Edited Caption,如 Input Caption “一张女孩骑在马上的图片”通过指令 Instruction “让她骑在龙上”得到 Edited Caption “一张女孩骑在龙上的图片”
- 使用 Stable Diffusion 以及 Prompt2Prompt 通过 Input Caption 以及 Edited Caption 生成一组图像对
- 最终得到图像对以及指令 Instruction 组成一组训练数据
-
使用上述训练数据微调 Stable Diffusion:
首先,对于一张图像 ,我们将其通过编码器 转换为对应的潜变量 。然后,通过一系列的时间步长 ,我们将噪声逐步加入到潜变量 中,得到带有噪声的潜变量 。这里的噪声水平随着时间步长的增加而增大。
接下来,我们训练一个网络 ,使其能够在给定图像条件 和文本指令条件 的情况下,预测出在当前时间步长 , 所加入的噪声。
为了达到这一目标,我们需要最小化以下目标函数 Latent Diffusion Objective: