speechbrain.lobes.models.resepformer 模块

Resource-Efficient Sepformer 的库。

作者
  • Cem Subakan 2022

摘要

MemLSTM

SkiM 的 Mem-LSTM --

ResourceEfficientSeparationPipeline

用于 RE-SepFormer 和 SkiM 的 Resource Efficient Separation Pipeline

ResourceEfficientSeparator

Resource Efficient Source Separator 这是实现 RE-SepFormer 的类

SBRNNBlock

带输出层的 RNNBlock。

SBTransformerBlock_wnormandskip

SpeechBrain 实现的 Transformer encoder 的 Wrapper。

SegLSTM

SkiM 的 Segment-LSTM

参考

class speechbrain.lobes.models.resepformer.MemLSTM(hidden_size, dropout=0.0, bidirectional=False, mem_type='hc', norm_type='cln')[source]

基类: Module

SkiM 的 Mem-LSTM –

注意:这取自 ESPNet 工具包中 SkiM 的实现,并为与 SpeechBrain 兼容进行了修改。

参数:
  • hidden_size (int) – 隐藏状态的维度。

  • dropout (float) – dropout 比率。默认为 0。

  • bidirectional (bool) – LSTM 层是否是双向的。默认为 False。

  • mem_type (str) – ‘hc’、‘h’、‘c’ 或 ‘id’。这控制 SegLSTM 的隐藏状态(或细胞状态)是否会由 MemLSTM 处理。在 ‘id’ 模式下,隐藏状态和细胞状态都将原样返回。

  • norm_type (str) – ‘gln’, ‘cln’。这选择归一化类型,cln 用于因果实现。

示例

>>> x = (torch.randn(1, 5, 64), torch.randn(1, 5, 64))
>>> block = MemLSTM(64)
>>> x = block(x, 5)
>>> x[0].shape
torch.Size([1, 5, 64])
forward(hc, S)[source]

内存 RNN 的 forward 函数

参数:
  • hc (tuple) –

    (h, c),来自 SegLSTM 的隐藏状态和细胞状态的元组,h 和 c 的形状:(d, B*S, H)

    其中 d 是方向数

    B 是 batch size S 是分块数 H 是潜在维度

  • S (int) – S 是分块数

返回:

ret_val – 内存 RNN 的输出

返回类型:

torch.Tensor

class speechbrain.lobes.models.resepformer.SegLSTM(input_size, hidden_size, dropout=0.0, bidirectional=False, norm_type='cLN')[source]

基类: Module

SkiM 的 Segment-LSTM

注意:这取自 ESPNet 工具包中 SkiM 的实现,并为与 SpeechBrain 兼容进行了修改。

参数:
  • input_size (int,) – 输入特征的维度。输入形状应为 (batch, seq_len, input_size)。

  • hidden_size (int,) – 隐藏状态的维度。

  • dropout (float,) – dropout 比率。默认为 0。

  • bidirectional (bool,) – LSTM 层是否是双向的。默认为 False。

  • norm_type (str) – gln, cln 中的一种。这选择归一化类型,cln 用于因果实现。

示例

>>> x = torch.randn(3, 20, 64)
>>> hc = None
>>> seglstm = SegLSTM(64, 64)
>>> y = seglstm(x, hc)
>>> y[0].shape
torch.Size([3, 20, 64])
forward(input, hc)[source]

Segment LSTM 的 forward 函数

参数:
  • input (torch.Tensor) –

    形状 [B*S, T, H],其中 B 是 batch size

    S 是分块数 T 是分块大小 H 是潜在维度

  • hc (tuple) –

    来自 SegLSTM 的隐藏状态和细胞状态的元组,h 和 c 的形状:(d, B*S, H)

    其中 d 是方向数

    B 是 batch size S 是分块数 H 是潜在维度

返回:

  • output (torch.Tensor) – Segment LSTM 的输出

  • (h, c) (tuple) – 与 hc 输入相同

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

基类: Module

带输出层的 RNNBlock。

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

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

  • num_layers (int) – RNN 层的数量。

  • outsize (int) – 线性层输出的维度数量

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

  • dropout (float) – Dropout 比率

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

示例

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

返回转换后的输出。

参数:

x (torch.Tensor) –

[B, L, N],其中 B = Batch size,

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

返回:

out – 转换后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.resepformer.SBTransformerBlock_wnormandskip(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', causal=False, use_norm=True, use_skip=True, norm_type='gln')[source]

基类: Module

SpeechBrain 实现的 Transformer encoder 的 Wrapper。

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

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

  • nhead (int) – Attention 头数量。

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

  • input_shape (tuple) – 输入形状。

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

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

  • dropout (float) – Dropout 率。

  • activation (str) – 激活函数。

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

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

  • attention_type (str) – Attention 类型,默认为 “regularMHA”

  • causal (bool) – 是否掩盖未来信息,默认为 False

  • use_norm (bool) – 是否在块中包含归一化。

  • use_skip (bool) – 是否在块中添加跳跃连接。

  • norm_type (str) – “cln”, “gln” 中的一种

示例

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

返回转换后的输出。

参数:

x (torch.Tensor) –

Tensor 形状 [B, L, N],其中 B = Batch size,

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

返回:

out – 转换后的输出。

返回类型:

torch.Tensor

class speechbrain.lobes.models.resepformer.ResourceEfficientSeparationPipeline(input_size, hidden_size, output_size, dropout=0.0, num_blocks=2, segment_size=20, bidirectional=True, mem_type='av', norm_type='gln', seg_model=None, mem_model=None)[source]

基类: Module

用于 RE-SepFormer 和 SkiM 的 Resource Efficient Separation Pipeline

注意:此实现是 ESPNET 中 SkiM 实现的通用化

参数:
  • input_size (int) – 输入特征的维度。输入形状应为 (batch, length, input_size)

  • hidden_size (int) – 隐藏状态的维度。

  • output_size (int) – 输出大小的维度。

  • dropout (float) – Dropout 比率。默认为 0。

  • num_blocks (int) – 基本 SkiM 块的数量

  • segment_size (int) – 用于分割长特征的分段大小

  • bidirectional (bool) – RNN 层是否是双向的。

  • mem_type (str) – ‘hc’、‘h’、‘c’、‘id’、‘av’ 或 None。这控制是否使用内存表示来确保段之间的连续性。在 ‘av’ 模式下,汇总状态是通过简单地对每个段的时间维度求平均计算得出的。在 ‘id’ 模式下,隐藏状态和细胞状态都将原样返回。当 mem_type 为 None 时,内存模型将被移除。

  • norm_type (str) – gln 或 cln 中的一种。cln 用于因果实现。

  • seg_model (class) – 处理段内元素的模型

  • mem_model (class) – 确保段之间连续性的内存模型

示例

>>> x = torch.randn(10, 100, 64)
>>> seg_mdl = SBTransformerBlock_wnormandskip(1, 64, 8)
>>> mem_mdl = SBTransformerBlock_wnormandskip(1, 64, 8)
>>> resepf_pipeline = ResourceEfficientSeparationPipeline(64, 64, 128, seg_model=seg_mdl, mem_model=mem_mdl)
>>> out = resepf_pipeline.forward(x)
>>> out.shape
torch.Size([10, 100, 128])
forward(input)[source]

ResourceEfficientSeparationPipeline 的 forward 函数

这接收一个大小为 [B, (S*K), D] 的 tensor

参数:

input (torch.Tensor) –

Tensor 形状 [B, (S*K), D],其中 B = Batch size,

S = 分块数量 K = 分块大小 D = 特征数量

返回:

output – 分离后的 tensor。

返回类型:

torch.Tensor

class speechbrain.lobes.models.resepformer.ResourceEfficientSeparator(input_dim: int, causal: bool = True, num_spk: int = 2, nonlinear: str = 'relu', layer: int = 3, unit: int = 512, segment_size: int = 20, dropout: float = 0.0, mem_type: str = 'hc', seg_model=None, mem_model=None)[source]

基类: Module

Resource Efficient Source Separator 这是实现 RE-SepFormer 的类

参数:
  • input_dim (int) – 输入特征维度

  • causal (bool) – 系统是否是因果的。

  • num_spk (int) – 目标说话人数量。

  • nonlinear (class) – 用于掩码估计的非线性函数,从 ‘relu’、‘tanh’、‘sigmoid’ 中选择

  • layer (int) – 块的数量。RE-SepFormer 默认为 2。

  • unit (int) – 隐藏状态的维度。

  • segment_size (int) – 用于分割长特征的分块大小

  • dropout (float) – dropout 比率。默认为 0。

  • mem_type (str) – ‘hc’、‘h’、‘c’、‘id’、‘av’ 或 None。这控制是否使用内存表示来确保段之间的连续性。在 ‘av’ 模式下,汇总状态是通过简单地对每个段的时间维度求平均计算得出的。在 ‘id’ 模式下,隐藏状态和细胞状态都将原样返回。当 mem_type 为 None 时,内存模型将被移除。

  • seg_model (class) – 处理段内元素的模型

  • mem_model (class) – 确保段之间连续性的内存模型

示例

>>> x = torch.randn(10, 64, 100)
>>> seg_mdl = SBTransformerBlock_wnormandskip(1, 64, 8)
>>> mem_mdl = SBTransformerBlock_wnormandskip(1, 64, 8)
>>> resepformer = ResourceEfficientSeparator(64, num_spk=3, mem_type='av', seg_model=seg_mdl, mem_model=mem_mdl)
>>> out = resepformer.forward(x)
>>> out.shape
torch.Size([3, 10, 64, 100])
forward(inpt: Tensor)[source]

Forward

参数:

inpt (torch.Tensor) – 编码特征 [B, T, N]

返回:

mask_tensor

返回类型:

torch.Tensor