1. 环境准备
进入案例官网, 本体验的所有代码都来自mindspore官网:
MindSporehttps://www.mindspore.cn/tutorials/application/zh-CN/r2.1/generative/diffusion.html点击”在ModelArts平台运行“
进入modelArts平台
免费的CPU 2核4GB
有多种选择:
2. Diffusion扩散模型介绍
本文对Diffusion扩散模型的理解是基于DDPM, 而DDPM已经在(无)条件图像/音频/视频生成领域取得了较多显著的成果,现有的比较受欢迎的的例子包括由OpenAI主导的GLIDE和DALL-E 2、由海德堡大学主导的潜在扩散和由Google Brain主导的图像生成。
将Diffusion与其他生成模型(如Normalizing Flows、GAN或VAE)进行比较,它们都将噪声从一些简单分布转换为数据样本,Diffusion也是从纯噪声开始通过一个神经网络学习逐步去噪,最终得到一个实际图像。 Diffusion基于马尔科夫链, 对于图像的处理包括以下两个过程:
正向扩散过程 q:它逐渐将高斯噪声添加到图像中,直到最终得到纯噪声
反向去噪的扩散过程 pθ:通过训练神经网络从纯噪声开始逐渐对图像去噪,直到最终得到一个实际的图像
如图所示:X0-Xt的过程是Diffusion的前向过程, 每一步的随机过程为: , Xt-X0的过程为Diffusion的逆向过程,每一步的随机过程为:
Diffusion 前向过程
Diffusion 前向过程就是图片加噪声的过程, 也叫做马尔科夫过程:
Diffusion 逆向过程
Diffusion 逆向过程是去噪过程,通过神经网络学习并表示 pθ(xt−1|xt) 的过程就是Diffusion 逆向去噪的核心,公式是:
3. 体验实验
实验开始之前请确保安装并导入所需的库(假设您已经安装了MindSpore、download、dataset、matplotlib以及tqdm)
不过此次体验用的是官网modelarts直接用mindspore_diffusion.ipynb运行,不需要再次安装
点击左上角的运行即可
构建Diffusion模型
定义了一些帮助函数和类,这些函数和类将在实现神经网络时使用。
def rearrange(head, inputs):
b, hc, x, y = inputs.shape
c = hc // head
return inputs.reshape((b, head, c, x * y))
def rsqrt(x):
res = ops.sqrt(x)
return ops.inv(res)
def randn_like(x, dtype=None):
if dtype is None:
dtype = x.dtype
res = ops.standard_normal(x.shape).astype(dtype)
return res
def randn(shape, dtype=None):
if dtype is None:
dtype = ms.float32
res = ops.standard_normal(shape).astype(dtype)
return res
def randint(low, high, size, dtype=ms.int32):
res = ops.uniform(size, Tensor(low, dtype), Tensor(high, dtype), dtype=dtype)
return res
def exists(x):
return x is not None
def default(val, d):
if exists(val):
return val
return d() if callable(d) else d
def _check_dtype(d1, d2):
if ms.float32 in (d1, d2):
return ms.float32
if d1 == d2:
return d1
raise ValueError('dtype is not supported.')
class Residual(nn.Cell):
def __init__(self, fn):
super().__init__()
self.fn = fn
def construct(self, x, *args, **kwargs):
return self.fn(x, *args, **kwargs) + x