speechbrain.nnet.unet 模块
用于扩散模型的UNet模型实现
改编自OpenAI引导扩散,略有修改 并增加了额外功能 https://github.com/openai/guided-diffusion
MIT许可证
版权所有 (c) 2021 OpenAI
特此授予任何获得本软件及相关文档文件(“软件”)副本的人免费许可,允许其在不受限制的情况下处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本,并允许向其提供软件的人员这样做,但须遵守以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“原样”提供,不提供任何形式的明示或暗示保证,包括但不限于适销性、特定用途适用性和非侵权性的保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他原因引起的,还是与本软件或使用或其他交易有关的。
- Authors
阿尔乔姆·普洛日尼科夫 2022
摘要
类:
一个注意力块,允许空间位置相互关注。 |
|
二维注意力池化 |
|
带有注意力和时间步嵌入的半UNet模型。 |
|
一个带有可选卷积的下采样层。 |
|
一个包装模块,用于为下采样因子应用必要的填充 |
|
一个简单的模块,用于计算嵌入向量在指定维度上的投影 |
|
带有注意力和时间步嵌入的半UNet模型。 |
|
一个执行QKV注意力并以不同顺序拆分的模块。 |
|
一个可以可选地改变通道数的残差块。 |
|
任何模块,其中 forward() 将时间步嵌入作为第二个参数。 |
|
一个顺序模块,将时间步嵌入作为额外输入传递给支持它的子模块。 |
|
完整的UNet模型,带有注意力和时间步嵌入。 |
|
一个基于UNet的变分自编码器(VAE)的便利类 - 在构建潜在扩散模型时非常有用 |
|
一个带有可选卷积的上采样层。 |
函数:
创建一个1D、2D或3D的平均池化模块。 |
|
构建一个用于嵌入投影的嵌入模块字典 |
|
创建一个1D、2D或3D卷积模块。 |
|
将模块的参数归零并返回它。 |
|
创建正弦时间步嵌入。 |
参考
- speechbrain.nnet.unet.fixup(module, use_fixup_init=True)[source]
将模块的参数归零并返回它。
- Parameters:
模块 (torch.nn.Module) – 一个模块
use_fixup_init (bool) – 是否将参数归零。如果设置为false,该函数将不执行任何操作
- Return type:
固定模块
- class speechbrain.nnet.unet.AttentionPool2d(spatial_dim: int, embed_dim: int, num_heads_channels: int, output_dim: int = None)[source]
基础:
Module二维注意力池化
改编自CLIP:https://github.com/openai/CLIP/blob/main/clip/model.py
- Parameters:
Example
>>> attn_pool = AttentionPool2d( ... spatial_dim=64, ... embed_dim=16, ... num_heads_channels=2, ... output_dim=4 ... ) >>> x = torch.randn(4, 1, 64, 64) >>> x_pool = attn_pool(x) >>> x_pool.shape torch.Size([4, 4])
- class speechbrain.nnet.unet.TimestepBlock(*args, **kwargs)[source]
基础:
Module任何模块,其中forward()将时间步嵌入作为第二个参数。
- class speechbrain.nnet.unet.TimestepEmbedSequential(*args: Module)[source]
- class speechbrain.nnet.unet.TimestepEmbedSequential(arg: OrderedDict[str, Module])
基础类:
Sequential,TimestepBlock一个顺序模块,将时间步嵌入传递给支持它作为额外输入的子模块。
Example
>>> from speechbrain.nnet.linear import Linear >>> class MyBlock(TimestepBlock): ... def __init__(self, input_size, output_size, emb_size): ... super().__init__() ... self.lin = Linear( ... n_neurons=output_size, ... input_size=input_size ... ) ... self.emb_proj = Linear( ... n_neurons=output_size, ... input_size=emb_size, ... ) ... def forward(self, x, emb): ... return self.lin(x) + self.emb_proj(emb) >>> tes = TimestepEmbedSequential( ... MyBlock(128, 64, 16), ... Linear( ... n_neurons=32, ... input_size=64 ... ) ... ) >>> x = torch.randn(4, 10, 128) >>> emb = torch.randn(4, 10, 16) >>> out = tes(x, emb) >>> out.shape torch.Size([4, 10, 32])
- class speechbrain.nnet.unet.Upsample(channels, use_conv, dims=2, out_channels=None)[source]
基础:
Module一个带有可选卷积的上采样层。
- Parameters:
Example
>>> ups = Upsample(channels=4, use_conv=True, dims=2, out_channels=8) >>> x = torch.randn(8, 4, 32, 32) >>> x_up = ups(x) >>> x_up.shape torch.Size([8, 8, 64, 64])
- class speechbrain.nnet.unet.Downsample(channels, use_conv, dims=2, out_channels=None)[source]
基础:
Module一个带有可选卷积的下采样层。
- Parameters:
Example
>>> ups = Downsample(channels=4, use_conv=True, dims=2, out_channels=8) >>> x = torch.randn(8, 4, 32, 32) >>> x_up = ups(x) >>> x_up.shape torch.Size([8, 8, 16, 16])
- class speechbrain.nnet.unet.ResBlock(channels, emb_channels, dropout, out_channels=None, use_conv=False, dims=2, up=False, down=False, norm_num_groups=32, use_fixup_init=True)[source]
基础类:
TimestepBlock一个可以可选地改变通道数量的残差块。
- Parameters:
channels (int) – 输入通道的数量。
emb_channels (int) – 时间步嵌入通道的数量。
dropout (float) – dropout的比率。
out_channels (int) – 如果指定,表示输出通道的数量。
use_conv (bool) – 如果为True并且指定了out_channels,则使用空间卷积而不是较小的1x1卷积来改变跳跃连接中的通道。
dims (int) – 确定信号是1D、2D还是3D。
up (bool) – 如果为True,则使用此块进行上采样。
down (bool) – 如果为True,则使用此块进行下采样。
norm_num_groups (int) – 用于组归一化的组数
use_fixup_init (bool) – 是否使用FixUp初始化
Example
>>> res = ResBlock( ... channels=4, ... emb_channels=8, ... dropout=0.1, ... norm_num_groups=2, ... use_conv=True, ... ) >>> x = torch.randn(2, 4, 32, 32) >>> emb = torch.randn(2, 8) >>> res_out = res(x, emb) >>> res_out.shape torch.Size([2, 4, 32, 32])
- class speechbrain.nnet.unet.AttentionBlock(channels, num_heads=1, num_head_channels=-1, norm_num_groups=32, use_fixup_init=True)[source]
基础:
Module一个允许空间位置相互关注的注意力块。 最初是从这里移植的,但已适应N维情况。 https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/models/unet.py#L66.
- Parameters:
Example
>>> attn = AttentionBlock( ... channels=8, ... num_heads=4, ... num_head_channels=4, ... norm_num_groups=2 ... ) >>> x = torch.randn(4, 8, 16, 16) >>> out = attn(x) >>> out.shape torch.Size([4, 8, 16, 16])
- class speechbrain.nnet.unet.QKVAttention(n_heads)[source]
基础:
Module一个执行QKV注意力并以不同顺序拆分的模块。
- Parameters:
n_heads (int) – 注意力头的数量。
Example
>>> attn = QKVAttention(4) >>> n = 4 >>> c = 8 >>> h = 64 >>> w = 16 >>> qkv = torch.randn(4, (3 * h * c), w) >>> out = attn(qkv) >>> out.shape torch.Size([4, 512, 16])
- speechbrain.nnet.unet.build_emb_proj(emb_config, proj_dim=None, use_emb=None)[source]
构建一个用于嵌入投影的嵌入模块字典
- class speechbrain.nnet.unet.UNetModel(in_channels, model_channels, out_channels, num_res_blocks, attention_resolutions, dropout=0, channel_mult=(1, 2, 4, 8), conv_resample=True, dims=2, emb_dim=None, cond_emb=None, use_cond_emb=None, num_heads=1, num_head_channels=-1, num_heads_upsample=-1, norm_num_groups=32, resblock_updown=False, use_fixup_init=True)[source]
基础:
Module带有注意力和时间步嵌入的完整UNet模型。
- Parameters:
in_channels (int) – 输入 torch.Tensor 的通道数。
model_channels (int) – 模型的基础通道数。
out_channels (int) – 输出 torch.Tensor 中的通道数。
num_res_blocks (int) – 每个下采样层的残差块数量。
attention_resolutions (int) – 一个下采样率的集合,在这些下采样率下将进行注意力操作。可以是一个集合、列表或元组。例如,如果这个集合包含4,那么在4倍下采样时,将使用注意力机制。
dropout (float) – 丢弃概率。
channel_mult (int) – UNet每一层的通道乘数。
conv_resample (bool) – 如果为True,则使用学习的卷积进行上采样和下采样
dims (int) – 确定信号是1D、2D还是3D。
emb_dim (int) – 时间嵌入维度(默认为 model_channels * 4)
cond_emb (dict) –
模型将基于的条件嵌入
示例: {
- ”speaker”: {
“emb_dim”: 256
}, “label”: {
”emb_dim”: 12
}
}
use_cond_emb (dict) –
一个字典,其键对应于cond_emb中的键,值对应于布尔值,用于打开和关闭嵌入。这在结合hparams文件使用时非常有用,可以通过简单的开关来打开和关闭嵌入。
示例: {“speaker”: False, “label”: True}
num_heads (int) – 每个注意力层中的注意力头数量。
num_head_channels (int) – 如果指定,则忽略 num_heads 并改为使用每个注意力头的固定通道宽度。
num_heads_upsample (int) – 与num_heads一起工作,为上采样设置不同数量的头。已弃用。
norm_num_groups (int) – 规范中的组数,默认为32
resblock_updown (bool) – 使用残差块进行上/下采样。
use_fixup_init (bool) – 是否使用FixUp初始化
Example
>>> model = UNetModel( ... in_channels=3, ... model_channels=32, ... out_channels=1, ... num_res_blocks=1, ... attention_resolutions=[1] ... ) >>> x = torch.randn(4, 3, 16, 32) >>> ts = torch.tensor([10, 100, 50, 25]) >>> out = model(x, ts) >>> out.shape torch.Size([4, 1, 16, 32])
- class speechbrain.nnet.unet.EncoderUNetModel(in_channels, model_channels, out_channels, num_res_blocks, attention_resolutions, dropout=0, channel_mult=(1, 2, 4, 8), conv_resample=True, dims=2, num_heads=1, num_head_channels=-1, num_heads_upsample=-1, norm_num_groups=32, resblock_updown=False, pool=None, attention_pool_dim=None, out_kernel_size=3, use_fixup_init=True)[source]
基础:
Module带有注意力和时间步嵌入的半UNet模型。 有关用法,请参见UNetModel。
- Parameters:
in_channels (int) – 输入 torch.Tensor 的通道数。
model_channels (int) – 模型的基础通道数。
out_channels (int) – 输出 torch.Tensor 中的通道数。
num_res_blocks (int) – 每个下采样层的残差块数量。
attention_resolutions (int) – 一个下采样率的集合,在这些下采样率下将进行注意力操作。可以是一个集合、列表或元组。例如,如果这个集合包含4,那么在4倍下采样时,将使用注意力机制。
dropout (float) – 丢弃概率。
channel_mult (int) – UNet每一层的通道乘数。
conv_resample (bool) – 如果为True,则使用学习的卷积进行上采样和下采样
dims (int) – 确定信号是1D、2D还是3D。
num_heads (int) – 每个注意力层中的注意力头数量。
num_head_channels (int) – 如果指定,则忽略 num_heads 并改为使用每个注意力头的固定通道宽度。
num_heads_upsample (int) – 与 num_heads 一起工作,为上采样设置不同数量的头。已弃用。
norm_num_groups (int) – 规范中的组数,默认为32。
resblock_updown (bool) – 使用残差块进行上/下采样。
pool (str) – 使用的池化类型,其中之一: [“adaptive”, “attention”, “spatial”, “spatial_v2”].
attention_pool_dim (int) – 应用注意力池化的维度。
out_kernel_size (int) – 输出卷积的核大小
use_fixup_init (bool) – 是否使用FixUp初始化
Example
>>> model = EncoderUNetModel( ... in_channels=3, ... model_channels=32, ... out_channels=1, ... num_res_blocks=1, ... attention_resolutions=[1] ... ) >>> x = torch.randn(4, 3, 16, 32) >>> ts = torch.tensor([10, 100, 50, 25]) >>> out = model(x, ts) >>> out.shape torch.Size([4, 1, 2, 4])
- class speechbrain.nnet.unet.EmbeddingProjection(emb_dim, proj_dim)[source]
基础:
Module一个简单的模块,用于计算嵌入向量在指定维度上的投影
Example
>>> mod_emb_proj = EmbeddingProjection(emb_dim=16, proj_dim=64) >>> emb = torch.randn(4, 16) >>> emb_proj = mod_emb_proj(emb) >>> emb_proj.shape torch.Size([4, 64])
- class speechbrain.nnet.unet.DecoderUNetModel(in_channels, model_channels, out_channels, num_res_blocks, attention_resolutions, dropout=0, channel_mult=(1, 2, 4, 8), conv_resample=True, dims=2, num_heads=1, num_head_channels=-1, num_heads_upsample=-1, resblock_updown=False, norm_num_groups=32, out_kernel_size=3, use_fixup_init=True)[source]
基础:
Module带有注意力和时间步嵌入的半UNet模型。 有关用法,请参见UNet。
- Parameters:
in_channels (int) – 输入 torch.Tensor 的通道数。
model_channels (int) – 模型的基础通道数。
out_channels (int) – 输出 torch.Tensor 中的通道数。
num_res_blocks (int) – 每个下采样层的残差块数量。
attention_resolutions (int) – 一个下采样率的集合,在这些下采样率下将进行注意力操作。可以是一个集合、列表或元组。例如,如果这个集合包含4,那么在4倍下采样时,将使用注意力机制。
dropout (float) – 丢弃概率。
channel_mult (int) – UNet每一层的通道乘数。
conv_resample (bool) – 如果为True,则使用学习的卷积进行上采样和下采样
dims (int) – 确定信号是1D、2D还是3D。
num_heads (int) – 每个注意力层中的注意力头数量。
num_head_channels (int) –
- 如果指定,忽略 num_heads 并改为使用
每个注意力头的固定通道宽度。
num_heads_upsample (int) –
- 与num_heads一起工作以设置不同的数量
用于上采样的头数。已弃用。
resblock_updown (bool) – 使用残差块进行上/下采样。
norm_num_groups (int) – 在norm中使用的组数,默认为32
out_kernel_size (int) – 输出内核大小,默认为3
use_fixup_init (bool) – 是否使用FixUp初始化
Example
>>> model = DecoderUNetModel( ... in_channels=1, ... model_channels=32, ... out_channels=3, ... num_res_blocks=1, ... attention_resolutions=[1] ... ) >>> x = torch.randn(4, 1, 2, 4) >>> ts = torch.tensor([10, 100, 50, 25]) >>> out = model(x, ts) >>> out.shape torch.Size([4, 3, 16, 32])
- class speechbrain.nnet.unet.DownsamplingPadding(factor, len_dim=2, dims=None)[source]
基础:
Module一个包装模块,用于为下采样因子应用必要的填充
Example
>>> padding = DownsamplingPadding(factor=4, dims=[1, 2], len_dim=1) >>> x = torch.randn(4, 7, 14) >>> length = torch.tensor([1., 0.8, 1., 0.7]) >>> x, length_new = padding(x, length) >>> x.shape torch.Size([4, 8, 16]) >>> length_new tensor([0.8750, 0.7000, 0.8750, 0.6125])
- class speechbrain.nnet.unet.UNetNormalizingAutoencoder(in_channels, model_channels, encoder_out_channels, latent_channels, encoder_num_res_blocks, encoder_attention_resolutions, decoder_num_res_blocks, decoder_attention_resolutions, dropout=0, channel_mult=(1, 2, 4, 8), dims=2, num_heads=1, num_head_channels=-1, num_heads_upsample=-1, norm_num_groups=32, resblock_updown=False, out_kernel_size=3, len_dim=2, out_mask_value=0.0, latent_mask_value=0.0, use_fixup_norm=False, downsampling_padding=None)[source]
-
一个用于基于UNet的变分自编码器(VAE)的便利类 - 在构建潜在扩散模型时非常有用
- Parameters:
in_channels (int) – 输入通道的数量
model_channels (int) – UNet编码器和解码器的卷积层中的通道数
encoder_out_channels (int) – 编码器将输出的通道数
latent_channels (int) – 潜在空间中的通道数
encoder_num_res_blocks (int) – 编码器中残差块的数量
encoder_attention_resolutions (list) – 在编码器中应用注意力层的分辨率
decoder_num_res_blocks (int) – 解码器中残差块的数量
decoder_attention_resolutions (list) – 在编码器中应用注意力层的分辨率
dropout (float) – 丢弃概率
channel_mult (tuple) – 每层的通道乘数
dims (int) – 使用的卷积维度(1、2 或 3)
num_heads (int) – 注意力头的数量
num_head_channels (int) – 注意力头中的通道数
num_heads_upsample (int) – 上采样头的数量
norm_num_groups (int) – 规范组的数量,默认为32
resblock_updown (bool) – 是否使用残差块进行上采样和下采样
out_kernel_size (int) – 输出卷积层的核大小(如果适用)
len_dim (int) – 输出的大小。
out_mask_value (float) – 在屏蔽输出时填充的值。
latent_mask_value (float) – 当屏蔽潜在变量时要填充的值。
use_fixup_norm (bool) – 是否使用FixUp归一化
downsampling_padding (int) – 在下采样中应用的填充量,默认值为 2 ** len(channel_mult)
Example
>>> unet_ae = UNetNormalizingAutoencoder( ... in_channels=1, ... model_channels=4, ... encoder_out_channels=16, ... latent_channels=3, ... encoder_num_res_blocks=1, ... encoder_attention_resolutions=[], ... decoder_num_res_blocks=1, ... decoder_attention_resolutions=[], ... norm_num_groups=2, ... ) >>> x = torch.randn(4, 1, 32, 32) >>> x_enc = unet_ae.encode(x) >>> x_enc.shape torch.Size([4, 3, 4, 4]) >>> x_dec = unet_ae.decode(x_enc) >>> x_dec.shape torch.Size([4, 1, 32, 32])