【扩散模型学习1】Diffusion Model概念讲解

参考学习资料:扩散模型 - Diffusion Model【李宏毅2023】_哔哩哔哩_bilibili

相关论文&论文链接:

Denoising Diffusion Probabilistic Models (DDPM):https://arxiv.org/abs/2006.11239

Stable Diffusion:https://arxiv.org/abs/2112.10752

DALL-E series:https//arxiv.org/abs/2204.06125、https//arxiv.org/abs/2102.12092

Imagen:https://imagen.research.google/、https://arxiv.prg/abs/2205.11487


目录

1. Diffusion Model是如何运作的:

1.1. reverse process:

1.1.1. denoise模块

1.1.1.1. denoise模块介绍:

1.1.1.2. 如何训练noise predictor模块

1.2. Forward Process(Diffusion Process)

2. 文字引导生成图片的diffusion model(Text-to-Image)

2.1 基本工作原理

2.2 denoise模块结构

2.3 如何训练

3. DDPM(Denoising Diffusion Probabilistic Models)

4. 常见影像生成模型

4.1. 基本架构

4.1.1. 文字的encoder

4.1.1.1. 指标介绍

4.1.1.2. 实验结果

4.1.2. decoder

4.1.3. generation model

4.1.3.1. forward process

4.1.3.2. 训练noise predicter:

4.1.3.3. reverse process

4.2. stable diffusion

4.3. DALL-E series

4.4. Imagen


1. Diffusion Model是如何运作的:

1.1. reverse process:

输入一张充满噪声的图——denoise——输出一张noise少的图——denoise——输出一张noise更少的图——denoise——……——输出清晰的图片(如图1所示)

图1 REVERSE PROCESS
  • 每张图的大小一致

1.1.1. denoise模块

  • denoise的次数是事先定好的,通常会有一个编号
  • 同一个denoise的model反复使用,但是输入不同,输入是图片+noise的严重程度(数字越大,noise越多),如图2所示
图2 denoise输入和输出
1.1.1.1. denoise模块介绍:
图3 denoise模块结构

如图3所示:

①Noise Predicter:用来预测输入的图片里面的noise长什么样,输出一张Noise的图

输入的图片 - Noise图  = 输出denoise之后的结果

1.1.1.2. 如何训练noise predictor模块

思考:如图4所示,如何搞groundtruth???

图4 noise predicter训练需要的groundtruth从何而来

创造groundtruth:

如图5所示,清晰的图像——加噪声——有噪声的图像——加噪声——噪声更多的图像——加噪声——……——含很多噪声的图像

以上过程称为forward process/diffusion process

图5 创造gt
### 扩散模型代码实现与解析 扩散模型是一种强大的生成模型,在图像生成等领域取得了显著成果。下面将展示如何通过Python代码实现一个简单的扩散模型。 #### 1. 导入必要的库 为了构建扩散模型,需要导入一些常用的机器学习和深度学习库: ```python import torch from torch import nn, optim import torchvision.transforms as transforms from torchvision.datasets import MNIST from torch.utils.data import DataLoader import numpy as np import matplotlib.pyplot as plt ``` #### 2. 定义前向过程 (Forward Process) 在扩散模型中,前向过程是指逐渐增加噪声的过程。这一步骤对于训练至关重要[^1]。 ```python def forward_process(x_0, t, beta_max=0.02): """ 前向过程中加入高斯噪声 参数: x_0 : 初始输入数据 t : 时间步数 beta_max : 最大噪音强度 返回: xt : 加噪后的样本 noise : 添加的随机噪声 """ # 计算beta_t betas = torch.linspace(1e-4, beta_max, steps=t).to(device=x_0.device) # 获取alpha_bar_t alpha_bars = torch.cumprod(1 - betas, dim=0) # 随机采样标准正态分布作为噪声 noise = torch.randn_like(x_0) # 应用加权平均得到xt xt = ( torch.sqrt(alpha_bars[t]) * x_0 + torch.sqrt(1 - alpha_bars[t]) * noise ) return xt, noise ``` #### 3. 构建去噪网络结构 这里采用UNet架构来预测每一步应该去除多少噪声。该网络能够有效地捕捉到不同尺度下的特征信息[^2]。 ```python class UNet(nn.Module): def __init__(self): super().__init__() self.encoder_layers = nn.Sequential( ConvBlock(in_channels=1, out_channels=64), DownSample(), ConvBlock(in_channels=64, out_channels=128), DownSample(), ... ) ... def forward(self, x, timestep_embedding=None): """定义向前传播""" ... ``` 由于篇幅原因,上述`ConvBlock`, `DownSample`等组件的具体实现未完全给出,实际应用时需补充完整这些模块。 #### 4. 反向过程 (Reverse Process) 反向过程即是从纯噪声逐步恢复原始信号的关键环节。此阶段利用之前提到的UNet来进行迭代优化。 ```python @torch.no_grad() def reverse_process(model, shape=(1, 28, 28), T=1000, device='cpu'): img = torch.randn(shape, device=device) for i in reversed(range(T)): t = torch.full((shape[0], ), i, dtype=torch.long, device=device) pred_noise = model(img, t) beta_t = get_beta(i) alpha_t = 1 - beta_t alpha_cumprod_prev = ... if i>0 else 1. posterior_variance = beta_t * (1-alpha_cumprod_prev)/(1-get_alpha_cumprod(i)) mean_pred = ((img - beta_t*pred_noise/torch.sqrt(1.-get_alpha_cumprod(i))) / torch.sqrt(alpha_t)) + \ posterioir_variance * torch.randn_like(img)*i/T img = mean_pred.clamp(-1., 1.) return img.cpu().numpy()[0] ``` 以上展示了简化版的扩散模型编码方式及其核心算法逻辑。需要注意的是,真实场景下还需要考虑更多细节问题,比如更复杂的损失函数设计、超参数调整以及性能调优等方面的工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值