speechbrain.nnet.diffusion 模块
去噪扩散的实现
https://arxiv.org/pdf/2006.11239.pdf
部分内容借鉴/受启发于 denoising-diffusion-pytorch https://github.com/lucidrains/denoising-diffusion-pytorch
- 作者
Artem Ploujnikov 2022
概要
类
经典去噪扩散概率模型 (DDPM) 的实现 |
|
基础扩散实现 |
|
DiffusionTrainSample(pred, noise, noisy_sample) |
|
添加普通高斯噪声 |
|
一个潜在扩散包装器。 |
|
LatentDiffusionTrainSample(diffusion, autoencoder) |
|
应用于填充样本的高斯噪声。 |
函数
返回一个时间步的随机样本作为 1-D 张量(仅一个维度) |
参考
- class speechbrain.nnet.diffusion.Diffuser(model, timesteps, noise=None)[source]
基类:
Module
基础扩散实现
- 参数:
- distort(x, timesteps=None)[source]
向一批数据添加噪声
- 参数:
x (torch.Tensor) – 原始数据样本
timesteps (torch.Tensor) – 一个 1-D 整数张量,其长度等于 x 中的批量数量,每个条目对应于该批次的时间步数。如果省略,将随机采样时间步。
- train_sample(x, timesteps=None, condition=None, **kwargs)[source]
创建一个用于训练循环的样本及对应的目标
- 参数:
x (torch.Tensor) – 原始数据样本
timesteps (torch.Tensor) – 一个 1-D 整数张量,其长度等于 x 中的批量数量,每个条目对应于该批次的时间步数。如果省略,将随机采样时间步。
condition (torch.Tensor) – 用于条件生成时的条件。在无条件生成时应省略。
**kwargs (dict) – 传递给底层模型的参数。
- 返回:
pred (torch.Tensor) – 模型输出 0 预测噪声
noise (torch.Tensor) – 应用的噪声
noisy_sample (torch.Tensor) – 应用噪声后的样本
- class speechbrain.nnet.diffusion.DenoisingDiffusion(model, timesteps=None, noise=None, beta_start=None, beta_end=None, sample_min=None, sample_max=None, show_progress=False)[source]
基类:
Diffuser
经典去噪扩散概率模型 (DDPM) 的实现
- 参数:
示例
>>> from speechbrain.nnet.unet import UNetModel >>> unet = UNetModel( ... in_channels=1, ... model_channels=16, ... norm_num_groups=4, ... out_channels=1, ... num_res_blocks=1, ... attention_resolutions=[] ... ) >>> diff = DenoisingDiffusion( ... model=unet, ... timesteps=5 ... ) >>> x = torch.randn(4, 1, 64, 64) >>> pred, noise, noisy_sample = diff.train_sample(x) >>> pred.shape torch.Size([4, 1, 64, 64]) >>> noise.shape torch.Size([4, 1, 64, 64]) >>> noisy_sample.shape torch.Size([4, 1, 64, 64]) >>> sample = diff.sample((2, 1, 64, 64)) >>> sample.shape torch.Size([2, 1, 64, 64])
- distort(x, noise=None, timesteps=None, **kwargs)[source]
在正向扩散过程中向样本添加噪声,
- 参数:
x (torch.Tensor) – 具有 2 个或更多维的数据样本,第一维表示批量
noise (torch.Tensor) – 要添加的噪声
timesteps (torch.Tensor) – 一个 1-D 整数张量,其长度等于 x 中的批量数量,每个条目对应于该批次的时间步数。如果省略,将随机采样时间步。
**kwargs (dict) – 传递给底层模型的参数。
- 返回:
result – 与 x 维度相同的张量
- 返回类型:
torch.Tensor
- class speechbrain.nnet.diffusion.LatentDiffusion(autoencoder, diffusion, latent_downsample_factor=None, latent_pad_dim=1)[source]
基类:
Module
一个潜在扩散包装器。潜在扩散是应用于潜在空间而非原始数据空间的去噪扩散。
- 参数:
autoencoder (speechbrain.nnet.autoencoders.Autoencoder) – 将原始空间转换为潜在空间的自编码器
diffusion (speechbrain.nnet.diffusion.Diffuser) – 扩散包装器
latent_downsample_factor (int) – 潜在空间维度需要能整除的因子。如果扩散包装器的底层模型基于 UNet 样式的架构,输入会被逐步缩小和放大两倍,则此参数很有用。
示例
>>> import torch >>> from torch import nn >>> from speechbrain.nnet.CNN import Conv2d >>> from speechbrain.nnet.autoencoders import NormalizingAutoencoder >>> from speechbrain.nnet.unet import UNetModel
设置一个简单的自编码器(真正的自编码器将是深度神经网络)
>>> ae_enc = Conv2d( ... kernel_size=3, ... stride=4, ... in_channels=1, ... out_channels=1, ... skip_transpose=True, ... ) >>> ae_dec = nn.ConvTranspose2d( ... kernel_size=3, ... stride=4, ... in_channels=1, ... out_channels=1, ... output_padding=1 ... ) >>> ae = NormalizingAutoencoder( ... encoder=ae_enc, ... decoder=ae_dec, ... )
构建一个具有 UNet 架构的扩散模型
>>> unet = UNetModel( ... in_channels=1, ... model_channels=16, ... norm_num_groups=4, ... out_channels=1, ... num_res_blocks=1, ... attention_resolutions=[] ... ) >>> diff = DenoisingDiffusion( ... model=unet, ... timesteps=5 ... ) >>> latent_diff = LatentDiffusion( ... autoencoder=ae, ... diffusion=diff, ... latent_downsample_factor=4, ... latent_pad_dim=2 ... ) >>> x = torch.randn(4, 1, 64, 64) >>> latent_sample = latent_diff.train_sample_latent(x) >>> diff_sample, ae_sample = latent_sample >>> pred, noise, noisy_sample = diff_sample >>> pred.shape torch.Size([4, 1, 16, 16]) >>> noise.shape torch.Size([4, 1, 16, 16]) >>> noisy_sample.shape torch.Size([4, 1, 16, 16]) >>> ae_sample.latent.shape torch.Size([4, 1, 16, 16])
创建一些样本(给定的形状应为潜在空间的形状)
>>> sample = latent_diff.sample((2, 1, 16, 16)) >>> sample.shape torch.Size([2, 1, 64, 64])
- train_sample(x, **kwargs)[source]
创建一个用于训练循环的样本及对应的目标
- 参数:
x (torch.Tensor) – 原始数据样本
**kwargs (dict) – 传递给底层模型的参数。
- 返回:
pred (torch.Tensor) – 模型输出 0 预测噪声
noise (torch.Tensor) – 应用的噪声
noisy_sample – 应用噪声后的样本
- train_sample_latent(x, **kwargs)[source]
返回带有自编码器输出的训练样本 - 可用于联合训练扩散模型和自编码器
- 参数:
x (torch.Tensor) – 原始数据样本
**kwargs (dict) – 传递给底层模型的参数。
- 返回:
训练样本。
- 返回类型:
- speechbrain.nnet.diffusion.sample_timesteps(x, num_timesteps)[source]
返回一个时间步的随机样本作为 1-D 张量(仅一个维度)
- 参数:
x (torch.Tensor) – 任意维度的样本张量
num_timesteps (int) – 时间步总数
- 返回类型:
时间戳的随机样本。
- class speechbrain.nnet.diffusion.LengthMaskedGaussianNoise(length_dim=1)[source]
基类:
Module
应用于填充样本的高斯噪声。不会向填充部分的位置添加噪声。
- 参数:
length_dim (int) – 长度适用的时间维度。