speechbrain.lobes.models.dual_path 模块

支持双路径语音分离的库。

作者
  • Cem Subakan 2020

  • Mirco Ravanelli 2020

  • Samuele Cornell 2020

  • Mirko Bronzi 2020

  • Jianyuan Zhong 2020

摘要

CumulativeLayerNorm

计算累积层归一化。

DPTNetBlock

DPT Net 块。

解码器

由 ConvTranspose1d 组成的解码器层。

Dual_Computation_Block

用于双路径处理的计算块。

Dual_Path_Model

双路径模型,是 dualpathrnn、sepformer、dptnet 的基础。

编码器

卷积编码器层。

FastTransformerBlock

此块用于实现具有高效注意力的快速 Transformer 模型。

GlobalLayerNorm

计算全局层归一化。

IdentityBlock

此块用于在 Dual_path 块中实现恒等变换。

PyTorchPositionalEncoding

PyTorch Transformer 的位置编码器。

PytorchTransformerBlock

使用 PyTorch Transformer 块的封装器。

SBConformerEncoderBlock

SpeechBrain 实现的 ConformerEncoder 的封装器。

SBRNNBlock

用于双路径管道的 RNNBlock。

SBTransformerBlock

SpeechBrain 实现的 Transformer 编码器的封装器。

SepformerWrapper

sepformer 模型的封装器,它结合了 Encoder、Masknet 和 Decoder https://arxiv.org/abs/2010.13154

函数

select_norm

仅用于选择归一化类型的封装器。

参考

class speechbrain.lobes.models.dual_path.GlobalLayerNorm(dim, shape, eps=1e-08, elementwise_affine=True)[source]

基类: Module

计算全局层归一化。

参数:
  • dim ((intlisttorch.Size)) – 预期输入大小的输入形状。

  • shape (tuple) – 预期的输入形状。

  • eps (float) – 添加到分母中的值,用于数值稳定性。

  • elementwise_affine (bool) – 布尔值,当设置为 True 时,此模块具有可学习的逐元素仿射参数,初始化为全一(权重)和全零(偏置)。

示例

>>> x = torch.randn(5, 10, 20)
>>> GLN = GlobalLayerNorm(10, 3)
>>> x_norm = GLN(x)
forward(x)[source]

返回归一化的 Tensor。

参数:

x (torch.Tensor) – 大小为 [N, C, K, S] 或 [N, C, L] 的 Tensor。

返回:

out – 归一化后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.CumulativeLayerNorm(dim, elementwise_affine=True, eps=1e-08)[source]

基类: LayerNorm

计算累积层归一化。

参数:
  • dim (int) – 要进行归一化的维度。

  • elementwise_affine (bool) – 可学习的逐元素仿射参数。

  • eps (float) – 用于防止溢出的一个很小的值。

示例

>>> x = torch.randn(5, 10, 20)
>>> CLN = CumulativeLayerNorm(10)
>>> x_norm = CLN(x)
forward(x)[source]

返回归一化的 Tensor。

参数:

x (torch.Tensor) – 大小为 [N, C, K, S] 或 [N, C, L] 的 torch.Tensor

返回:

out – 归一化后的输出。

返回类型:

torch.Tensor

speechbrain.lobes.models.dual_path.select_norm(norm, dim, shape, eps=1e-08)[source]

仅用于选择归一化类型的封装器。

class speechbrain.lobes.models.dual_path.Encoder(kernel_size=2, out_channels=64, in_channels=1)[source]

基类: Module

卷积编码器层。

参数:
  • kernel_size (int) – 滤波器长度。

  • out_channels (int) – 输出通道数。

  • in_channels (int) – 输入通道数。

示例

>>> x = torch.randn(2, 1000)
>>> encoder = Encoder(kernel_size=4, out_channels=64)
>>> h = encoder(x)
>>> h.shape
torch.Size([2, 64, 499])
forward(x)[source]

返回编码后的输出。

参数:

x (torch.Tensor) – 输入张量,维度为 [B, L]。

返回:

x – 编码后的张量,维度为 [B, N, T_out]。其中 B = 批量大小 (Batchsize)

L = 时间点数 (Number of timepoints) N = 滤波器数量 (Number of filters) T_out = 编码器输出的时间点数 (Number of timepoints at the output of the encoder)

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.Decoder(*args, **kwargs)[source]

基类: ConvTranspose1d

由 ConvTranspose1d 组成的解码器层。

参数:
  • *args (tuple)

  • **kwargs (dict) – 传递给 nn.ConvTranspose1d 的参数。

示例

>>> x = torch.randn(2, 100, 1000)
>>> decoder = Decoder(kernel_size=4, in_channels=100, out_channels=1)
>>> h = decoder(x)
>>> h.shape
torch.Size([2, 1003])
forward(x)[source]

返回解码后的输出。

参数:

x (torch.Tensor) –

输入张量,维度为 [B, N, L]。
其中,B = 批量大小 (Batchsize),

N = 滤波器数量 L = 时间点数

返回:

out – 解码后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.IdentityBlock[source]

基类: object

此块用于在 Dual_path 块中实现恒等变换。

参数:

**kwargs (dict) – 参数被忽略。

示例

>>> x = torch.randn(10, 100)
>>> IB = IdentityBlock()
>>> xhat = IB(x)
class speechbrain.lobes.models.dual_path.FastTransformerBlock(attention_type, out_channels, num_layers=6, nhead=8, d_ffn=1024, dropout=0, activation='relu', reformer_bucket_size=32)[source]

基类: Module

此块用于实现具有高效注意力的快速 Transformer 模型。

实现基于 https://fast-transformers.github.io/

参数:
  • attention_type (str) – 指定注意力类型。详情请查阅 https://fast-transformers.github.io/

  • out_channels (int) – 表示的维度。

  • num_layers (int) – 层数。

  • nhead (int) – 注意力头的数量。

  • d_ffn (int) – 位置前馈网络的维度。

  • dropout (float) – Dropout 丢弃率。

  • activation (str) – 激活函数。

  • reformer_bucket_size (int) – reformer 的 bucket size。

示例

# >>> x = torch.randn(10, 100, 64) # >>> block = FastTransformerBlock('linear', 64) # >>> x = block(x) # >>> x.shape # torch.Size([10, 100, 64])

forward(x)[source]

返回变换后的输入。

参数:

x (torch.Tensor) –

张量形状 [B, L, N]。其中,B = 批量大小 (Batchsize),

N = 滤波器数量 L = 时间点数

返回:

out – 变换后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.PyTorchPositionalEncoding(d_model, dropout=0.1, max_len=5000)[source]

基类: Module

PyTorch Transformer 的位置编码器。

参数:
  • d_model (int) – 表示维度。

  • dropout (float) – Dropout 丢弃概率。

  • max_len (int) – 最大序列长度。

示例

>>> x = torch.randn(10, 100, 64)
>>> enc = PyTorchPositionalEncoding(64)
>>> x = enc(x)
forward(x)[source]

返回编码后的输出。

参数:

x (torch.Tensor) –

张量形状 [B, L, N],其中,B = 批量大小 (Batchsize),

N = 滤波器数量 L = 时间点数

返回:

out – 编码后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.PytorchTransformerBlock(out_channels, num_layers=6, nhead=8, d_ffn=2048, dropout=0.1, activation='relu', use_positional_encoding=True)[source]

基类: Module

使用 PyTorch Transformer 块的封装器。

参数:
  • out_channels (int) – 表示的维度。

  • num_layers (int) – 层数。

  • nhead (int) – 注意力头的数量。

  • d_ffn (int) – 位置前馈网络的维度。

  • dropout (float) – Dropout 丢弃率。

  • activation (str) – 激活函数。

  • use_positional_encoding (bool) – 如果为 True,则使用位置编码。

示例

>>> x = torch.randn(10, 100, 64)
>>> block = PytorchTransformerBlock(64)
>>> x = block(x)
>>> x.shape
torch.Size([10, 100, 64])
forward(x)[source]

返回变换后的输出。

参数:

x (torch.Tensor) –

张量形状 [B, L, N],其中,B = 批量大小 (Batchsize),

N = 滤波器数量 L = 时间点数

返回:

out – 变换后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.SBTransformerBlock(num_layers, d_model, nhead, d_ffn=2048, input_shape=None, kdim=None, vdim=None, dropout=0.1, activation='relu', use_positional_encoding=False, norm_before=False, attention_type='regularMHA')[source]

基类: Module

SpeechBrain 实现的 Transformer 编码器的封装器。

参数:
  • num_layers (int) – 层数。

  • d_model (int) – 表示的维度。

  • nhead (int) – 注意力头的数量。

  • d_ffn (int) – 位置前馈网络的维度。

  • input_shape (tuple) – 输入的形状。

  • kdim (int) – 键的维度(可选)。

  • vdim (int) – 值的维度(可选)。

  • dropout (float) – Dropout 概率。

  • activation (str) – 激活函数。

  • use_positional_encoding (bool) – 如果为 True,则使用位置编码。

  • norm_before (bool) – 在变换前使用归一化。

  • attention_type (str) – 使用的注意力类型,默认为 "regularMHA"

示例

>>> x = torch.randn(10, 100, 64)
>>> block = SBTransformerBlock(1, 64, 8)
>>> x = block(x)
>>> x.shape
torch.Size([10, 100, 64])
forward(x)[source]

返回变换后的输出。

参数:

x (torch.Tensor) –

张量形状 [B, L, N],其中,B = 批量大小 (Batchsize),

L = 时间点数 N = 滤波器数量

返回:

out – 变换后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.SBRNNBlock(input_size, hidden_channels, num_layers, rnn_type='LSTM', dropout=0, bidirectional=True)[source]

基类: Module

用于双路径管道的 RNNBlock。

参数:
  • input_size (int) – 输入特征的维度。

  • hidden_channels (int) – RNN 潜在层的维度。

  • num_layers (int) – RNN 层数。

  • rnn_type (str) – RNN 单元格类型。

  • dropout (float) – Dropout 概率

  • bidirectional (bool) – 如果为 True,则为双向。

示例

>>> x = torch.randn(10, 100, 64)
>>> rnn = SBRNNBlock(64, 100, 1, bidirectional=True)
>>> x = rnn(x)
>>> x.shape
torch.Size([10, 100, 200])
forward(x)[source]

返回变换后的输出。

参数:

x (torch.Tensor) –

[B, L, N] 其中,B = 批量大小 (Batchsize),

N = 滤波器数量 L = 时间点数

返回:

out – 变换后的输出。

返回类型:

torch.Tensor

speechbrain.lobes.models.dual_path.DPTNetBlock(d_model, nhead, dim_feedforward=256, dropout=0, activation='relu')[源代码]

基类: Module

DPT Net 块。

参数:
  • d_model (int) – 输入中预期的特征数量(必需)。

  • nhead (int) – 多头注意力模型中的头数(必需)。

  • dim_feedforward (int) – 前馈网络模型的维度(默认为 2048)。

  • dropout (float) – Dropout 值(默认为 0.1)。

  • activation (str) – 中间层的激活函数,relu 或 gelu(默认为 relu)。

示例

>>> encoder_layer = DPTNetBlock(d_model=512, nhead=8)
>>> src = torch.rand(10, 100, 512)
>>> out = encoder_layer(src)
>>> out.shape
torch.Size([10, 100, 512])
forward(src)[source]

将输入通过编码器层。

参数:

src (torch.Tensor) –

张量形状 [B, L, N],其中,B = 批量大小 (Batchsize),

N = 滤波器数量 L = 时间点数

返回类型:

编码后的输出。

class speechbrain.lobes.models.dual_path.Dual_Computation_Block(intra_mdl, inter_mdl, out_channels, norm='ln', skip_around_intra=True, linear_layer_after_inter_intra=True)[source]

基类: Module

用于双路径处理的计算块。

参数:
  • intra_mdl (torch.nn.module) – 用于在块内进行处理的模型。

  • inter_mdl (torch.nn.module) – 用于在块间进行处理的模型。

  • out_channels (int) – inter/intra 模型的维度。

  • norm (str) – 归一化类型。

  • skip_around_intra (bool) – 是否围绕 intra 层使用跳跃连接。

  • linear_layer_after_inter_intra (bool) – 在 inter 或 intra 之后是否使用线性层。

示例

>>> intra_block = SBTransformerBlock(1, 64, 8)
>>> inter_block = SBTransformerBlock(1, 64, 8)
>>> dual_comp_block = Dual_Computation_Block(intra_block, inter_block, 64)
>>> x = torch.randn(10, 64, 100, 10)
>>> x = dual_comp_block(x)
>>> x.shape
torch.Size([10, 64, 100, 10])
forward(x)[source]

返回输出张量。

参数:

x (torch.Tensor) – 输入张量,维度为 [B, N, K, S]。

返回:

out – 输出张量,维度为 [B, N, K, S]。其中,B = 批量大小 (Batchsize),

N = 滤波器数量 K = 每个块中的时间点数 S = 块的数量

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.Dual_Path_Model(in_channels, out_channels, intra_model, inter_model, num_layers=1, norm='ln', K=200, num_spks=2, skip_around_intra=True, linear_layer_after_inter_intra=True, use_global_pos_enc=False, max_length=20000)[source]

基类: Module

双路径模型,是 dualpathrnn、sepformer、dptnet 的基础。

参数:
  • in_channels (int) – 编码器输出的通道数。

  • out_channels (int) – 将输入到 intra 和 inter 块的通道数。

  • intra_model (torch.nn.module) – 用于在块内进行处理的模型。

  • inter_model (torch.nn.module) – 用于在块间进行处理的模型,

  • num_layers (int) – Dual Computation Block 的层数。

  • norm (str) – 归一化类型。

  • K (int) – 块长度。

  • num_spks (int) – 源(说话人)的数量。

  • skip_around_intra (bool) – 围绕 intra 的跳跃连接。

  • linear_layer_after_inter_intra (bool) – 在 inter 和 intra 之后是否使用线性层。

  • use_global_pos_enc (bool) – 全局位置编码。

  • max_length (int) – 最大序列长度。

示例

>>> intra_block = SBTransformerBlock(1, 64, 8)
>>> inter_block = SBTransformerBlock(1, 64, 8)
>>> dual_path_model = Dual_Path_Model(64, 64, intra_block, inter_block, num_spks=2)
>>> x = torch.randn(10, 64, 2000)
>>> x = dual_path_model(x)
>>> x.shape
torch.Size([2, 10, 64, 2000])
forward(x)[source]

返回输出张量。

参数:

x (torch.Tensor) – 输入张量,维度为 [B, N, L]。

返回:

out – 输出张量,维度为 [spks, B, N, L],其中 spks = 说话人数量

B = 批量大小 (Batchsize), N = 滤波器数量 L = 时间点数

返回类型:

torch.Tensor

class speechbrain.lobes.models.dual_path.SepformerWrapper(encoder_kernel_size=16, encoder_in_nchannels=1, encoder_out_nchannels=256, masknet_chunksize=250, masknet_numlayers=2, masknet_norm='ln', masknet_useextralinearlayer=False, masknet_extraskipconnection=True, masknet_numspks=2, intra_numlayers=8, inter_numlayers=8, intra_nhead=8, inter_nhead=8, intra_dffn=1024, inter_dffn=1024, intra_use_positional=True, inter_use_positional=True, intra_norm_before=True, inter_norm_before=True)[source]

基类: Module

sepformer 模型的封装器,它结合了 Encoder、Masknet 和 Decoder https://arxiv.org/abs/2010.13154

参数:
  • encoder_kernel_size (int) – 编码器中使用的核大小。

  • encoder_in_nchannels (int) – 输入音频的通道数。

  • encoder_out_nchannels (int) – 编码器中使用的滤波器数量。同时,也是将输入到 intra 和 inter 块的通道数。

  • masknet_chunksize (int) – 将由 intra 块处理的块长度。

  • masknet_numlayers (int) – inter 和 intra 块组合的层数。

  • masknet_norm (str,) –

    在 masknet 中使用的归一化类型 应为 ‘ln’ – layernorm, ‘gln’ – globallayernorm 中的一种。

    ‘cln’ – cumulative layernorm, ‘bn’ – batchnorm – 更多详情请参阅上面的 select_norm 函数。

  • masknet_useextralinearlayer (bool) – 是否在 intra 和 inter 块的输出处使用线性层。

  • masknet_extraskipconnection (bool) – 这在 intra 块周围引入了额外的跳跃连接。

  • masknet_numspks (int) – 这决定了要估计的说话人数量。

  • intra_numlayers (int) – 这决定了 intra 块的层数。

  • inter_numlayers (int) – 这决定了 inter 块的层数。

  • intra_nhead (int) – 这决定了 intra 块中并行注意力头的数量。

  • inter_nhead (int) – 这决定了 inter 块中并行注意力头的数量。

  • intra_dffn (int) – inter 块中位置前馈模型中的维度数量。

  • inter_dffn (int) – intra 块中位置前馈模型中的维度数量。

  • intra_use_positional (bool) – 是否在 intra 块中使用位置编码。

  • inter_use_positional (bool) – 是否在 inter 块中使用位置编码。

  • intra_norm_before (bool) – 是否在 intra 块的变换前使用归一化。

  • inter_norm_before (bool) – 是否在 inter 块的变换前使用归一化。

示例

>>> model = SepformerWrapper()
>>> inp = torch.rand(1, 160)
>>> result = model.forward(inp)
>>> result.shape
torch.Size([1, 160, 2])
reset_layer_recursively(layer)[source]

重新初始化网络的参数。

forward(mix)[source]

处理输入张量 x 并返回输出张量。

class speechbrain.lobes.models.dual_path.SBConformerEncoderBlock(num_layers, d_model, nhead, d_ffn=2048, input_shape=None, kdim=None, vdim=None, dropout=0.1, activation='swish', kernel_size=31, bias=True, use_positional_encoding=True, attention_type='RelPosMHAXL')[source]

基类: Module

SpeechBrain 实现的 ConformerEncoder 的封装器。

参数:
  • num_layers (int) – 层数。

  • d_model (int) – 表示的维度。

  • nhead (int) – 注意力头的数量。

  • d_ffn (int) – 位置前馈网络的维度。

  • input_shape (tuple) – 输入的形状。

  • kdim (int) – 键的维度(可选)。

  • vdim (int) – 值的维度(可选)。

  • dropout (float) – Dropout 概率。

  • activation (str) – 激活函数。

  • kernel_size (int) – conformer 编码器中的核大小。

  • bias (bool) – 在 conformer 编码器的卷积部分是否使用偏置。

  • use_positional_encoding (bool) – 如果为 True,则使用位置编码。

  • attention_type (str) – 使用的注意力类型,默认为 “RelPosMHAXL”

示例

>>> x = torch.randn(10, 100, 64)
>>> block = SBConformerEncoderBlock(1, 64, 8)
>>> from speechbrain.lobes.models.transformer.Transformer import PositionalEncoding
>>> pos_enc = PositionalEncoding(64)
>>> pos_embs = pos_enc(torch.ones(1, 199, 64))
>>> x = block(x)
>>> x.shape
torch.Size([10, 100, 64])
forward(x)[source]

返回变换后的输出。

参数:

x (torch.Tensor) –

张量形状 [B, L, N],其中,B = 批量大小 (Batchsize),

L = 时间点数 N = 滤波器数量

返回类型:

变换后的输出