speechbrain.nnet.CNN 模块

实现卷积神经网络的库。

作者
  • Mirco Ravanelli 2020

  • Jianyuan Zhong 2020

  • Cem Subakan 2021

  • Davide Borra 2021

  • Andreas Nautsch 2022

  • Sarthak Yadav 2022

摘要

Conv1d

此函数实现一维卷积。

Conv2d

此函数实现二维卷积。

ConvTranspose1d

此类使用 speechbrain 实现一维转置卷积。

DepthwiseSeparableConv1d

此类实现深度可分离一维卷积。

DepthwiseSeparableConv2d

此类实现深度可分离二维卷积。

GaborConv1d

此类实现了来自 的一维 Gabor 卷积

SincConv

此函数实现 SincConv (SincNet)。

函数

get_padding_elem

此函数计算零填充所需的元素数量。

get_padding_elem_transposed

此函数计算转置卷积所需的填充大小

参考

class speechbrain.nnet.CNN.SincConv(out_channels, kernel_size, input_shape=None, in_channels=None, stride=1, dilation=1, padding='same', padding_mode='reflect', sample_rate=16000, min_low_hz=50, min_band_hz=50)[source]

基类: Module

此函数实现 SincConv (SincNet)。

M. Ravanelli, Y. Bengio, “Speaker Recognition from raw waveform with SincNet”, in Proc. of SLT 2018 (https://arxiv.org/abs/1808.00158)

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

  • kernel_size (int) – 卷积滤波器的核大小。

  • input_shape (tuple) – 输入的形状。也可以使用 in_channels

  • in_channels (int) – 输入通道数。也可以使用 input_shape

  • stride (int) – 卷积滤波器的步长因子。当步长因子 > 1 时,进行时间抽取。

  • dilation (int) – 卷积滤波器的膨胀因子。

  • padding (str) – (same, valid, causal)。如果为“valid”,则不进行填充。如果为“same”且步长为 1,则输出形状与输入形状相同。“causal”产生因果(膨胀)卷积。

  • padding_mode (str) – 此标志指定填充类型。更多信息请参阅 torch.nn 文档。

  • sample_rate (int) – 输入信号的采样率。仅用于 sinc_conv。

  • min_low_hz (float) – 滤波器最低可能频率(赫兹)。仅用于 sinc_conv。

  • min_band_hz (float) – 滤波器带宽的最低可能值(赫兹)。

示例

>>> inp_tensor = torch.rand([10, 16000])
>>> conv = SincConv(input_shape=inp_tensor.shape, out_channels=25, kernel_size=11)
>>> out_tensor = conv(inp_tensor)
>>> out_tensor.shape
torch.Size([10, 16000, 25])
forward(x)[source]

返回卷积的输出。

参数:

x (torch.Tensor (batch, time, channel)) – 要进行卷积的输入。期望为 2d 或 4d tensors。

返回:

wx – 卷积输出。

返回类型:

torch.Tensor

class speechbrain.nnet.CNN.Conv1d(out_channels, kernel_size, input_shape=None, in_channels=None, stride=1, dilation=1, padding='same', groups=1, bias=True, padding_mode='reflect', skip_transpose=False, weight_norm=False, conv_init=None, default_padding=0)[source]

基类: Module

此函数实现一维卷积。

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

  • kernel_size (int) – 卷积滤波器的核大小。

  • input_shape (tuple) – 输入的形状。也可以使用 in_channels

  • in_channels (int) – 输入通道数。也可以使用 input_shape

  • stride (int) – 卷积滤波器的步长因子。当步长因子 > 1 时,进行时间抽取。

  • dilation (int) – 卷积滤波器的膨胀因子。

  • padding (str) – (same, valid, causal)。如果为“valid”,则不进行填充。如果为“same”且步长为 1,则输出形状与输入形状相同。“causal”产生因果(膨胀)卷积。

  • groups (int) – 从输入通道到输出通道的阻塞连接数。

  • bias (bool) – 是否为卷积操作添加偏置项。

  • padding_mode (str) – 此标志指定填充类型。更多信息请参阅 torch.nn 文档。

  • skip_transpose (bool) – 如果为 False,则使用 speechbrain 的 batch x time x channel 约定。如果为 True,则使用 batch x channel x time 约定。

  • weight_norm (bool) – 如果为 True,则使用权重归一化,在推理时通过 self.remove_weight_norm() 移除。

  • conv_init (str) – 卷积网络的权重初始化方法。

  • default_padding (str or int) – 这设置了 pytorch Conv1d 后端将使用的默认填充模式。

示例

>>> inp_tensor = torch.rand([10, 40, 16])
>>> cnn_1d = Conv1d(
...     input_shape=inp_tensor.shape, out_channels=8, kernel_size=5
... )
>>> out_tensor = cnn_1d(inp_tensor)
>>> out_tensor.shape
torch.Size([10, 40, 8])
forward(x)[source]

返回卷积的输出。

参数:

x (torch.Tensor (batch, time, channel)) – 要进行卷积的输入。期望为 2d 或 4d tensors。

返回:

wx – 卷积输出。

返回类型:

torch.Tensor

remove_weight_norm()[source]

如果在训练期间使用了权重归一化,则在推理时移除它。

class speechbrain.nnet.CNN.Conv2d(out_channels, kernel_size, input_shape=None, in_channels=None, stride=(1, 1), dilation=(1, 1), padding='same', groups=1, bias=True, padding_mode='reflect', max_norm=None, swap=False, skip_transpose=False, weight_norm=False, conv_init=None)[source]

基类: Module

此函数实现二维卷积。

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

  • kernel_size (tuple) – 二维卷积滤波器在时间和频率轴上的核大小。

  • input_shape (tuple) – 输入的形状。也可以使用 in_channels

  • in_channels (int) – 输入通道数。也可以使用 input_shape

  • stride (int) – 二维卷积滤波器在时间和频率轴上的步长因子。

  • dilation (int) – 二维卷积滤波器在时间和频率轴上的膨胀因子。

  • padding (str) – (same, valid, causal)。如果为“valid”,则不进行填充。如果为“same”且步长为 1,则输出形状与输入形状相同。如果为“causal”,则插入适当填充以模拟第一空间维度上的因果卷积。(对于 skip_transpose=False 和 skip_transpose=True,空间维度 1 都是维度 3)

  • groups (int) – 此选项指定卷积组。更多信息请参阅 torch.nn 文档。

  • bias (bool) – 如果为 True,则使用加性偏置 b。

  • padding_mode (str) – 此标志指定填充类型。更多信息请参阅 torch.nn 文档。

  • max_norm (float) – 核的最大范数。

  • swap (bool) – 如果为 True,则使用 (B, C, W, H) 格式进行卷积。如果为 False,则使用 (B, H, W, C) 格式进行卷积。仅在 skip_transpose 为 False 时有效。

  • skip_transpose (bool) – 如果为 False,则使用 speechbrain 的 batch x spatial.dim2 x spatial.dim1 x channel 约定。如果为 True,则使用 batch x channel x spatial.dim1 x spatial.dim2 约定。

  • weight_norm (bool) – 如果为 True,则使用权重归一化,在推理时通过 self.remove_weight_norm() 移除。

  • conv_init (str) – 卷积网络的权重初始化方法。

示例

>>> inp_tensor = torch.rand([10, 40, 16, 8])
>>> cnn_2d = Conv2d(
...     input_shape=inp_tensor.shape, out_channels=5, kernel_size=(7, 3)
... )
>>> out_tensor = cnn_2d(inp_tensor)
>>> out_tensor.shape
torch.Size([10, 40, 16, 5])
forward(x)[source]

返回卷积的输出。

参数:

x (torch.Tensor (batch, time, channel)) – 要进行卷积的输入。期望为 2d 或 4d tensors。

返回:

x – 卷积的输出。

返回类型:

torch.Tensor

remove_weight_norm()[source]

如果在训练期间使用了权重归一化,则在推理时移除它。

class speechbrain.nnet.CNN.ConvTranspose1d(out_channels, kernel_size, input_shape=None, in_channels=None, stride=1, dilation=1, padding=0, output_padding=0, groups=1, bias=True, skip_transpose=False, weight_norm=False)[source]

基类: Module

此类使用 speechbrain 实现一维转置卷积。转置卷积通常用于上采样。

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

  • kernel_size (int) – 卷积滤波器的核大小。

  • input_shape (tuple) – 输入的形状。也可以使用 in_channels

  • in_channels (int) – 输入通道数。也可以使用 input_shape

  • stride (int) – 卷积滤波器的步长因子。当步长因子 > 1 时,在时间上进行上采样。

  • dilation (int) – 卷积滤波器的膨胀因子。

  • padding (str or int) – 为了获得目标输出维度,我们建议适当调整核大小和填充。我们还支持以下函数,以便对填充和相应的输出维度进行一些控制。如果为“valid”,则不应用填充;如果为“same”,则推断填充量,使输出大小尽可能接近输入大小。请注意,对于某些 kernel_size / stride 组合,无法获得完全相同的大小,但我们返回最接近的大小。如果为“factor”,则推断填充量,使输出大小最接近 inputsize*stride。请注意,对于某些 kernel_size / stride 组合,无法获得完全相同的大小,但我们返回最接近的大小。如果输入整数值,则使用自定义填充。

  • output_padding (int,) – 添加到输出形状一侧的额外大小

  • groups (int) – 从输入通道到输出通道的阻塞连接数。默认值: 1

  • bias (bool) – 如果为 True,则为输出添加可学习的偏置项

  • skip_transpose (bool) – 如果为 False,则使用 speechbrain 的 batch x time x channel 约定。如果为 True,则使用 batch x channel x time 约定。

  • weight_norm (bool) – 如果为 True,则使用权重归一化,在推理时通过 self.remove_weight_norm() 移除。

示例

>>> from speechbrain.nnet.CNN import Conv1d, ConvTranspose1d
>>> inp_tensor = torch.rand([10, 12, 40]) #[batch, time, fea]
>>> convtranspose_1d = ConvTranspose1d(
...     input_shape=inp_tensor.shape, out_channels=8, kernel_size=3, stride=2
... )
>>> out_tensor = convtranspose_1d(inp_tensor)
>>> out_tensor.shape
torch.Size([10, 25, 8])
>>> # Combination of Conv1d and ConvTranspose1d
>>> from speechbrain.nnet.CNN import Conv1d, ConvTranspose1d
>>> signal = torch.tensor([1,100])
>>> signal = torch.rand([1,100]) #[batch, time]
>>> conv1d = Conv1d(input_shape=signal.shape, out_channels=1, kernel_size=3, stride=2)
>>> conv_out = conv1d(signal)
>>> conv_t = ConvTranspose1d(input_shape=conv_out.shape, out_channels=1, kernel_size=3, stride=2, padding=1)
>>> signal_rec = conv_t(conv_out, output_size=[100])
>>> signal_rec.shape
torch.Size([1, 100])
>>> signal = torch.rand([1,115]) #[batch, time]
>>> conv_t = ConvTranspose1d(input_shape=signal.shape, out_channels=1, kernel_size=3, stride=2, padding='same')
>>> signal_rec = conv_t(signal)
>>> signal_rec.shape
torch.Size([1, 115])
>>> signal = torch.rand([1,115]) #[batch, time]
>>> conv_t = ConvTranspose1d(input_shape=signal.shape, out_channels=1, kernel_size=7, stride=2, padding='valid')
>>> signal_rec = conv_t(signal)
>>> signal_rec.shape
torch.Size([1, 235])
>>> signal = torch.rand([1,115]) #[batch, time]
>>> conv_t = ConvTranspose1d(input_shape=signal.shape, out_channels=1, kernel_size=7, stride=2, padding='factor')
>>> signal_rec = conv_t(signal)
>>> signal_rec.shape
torch.Size([1, 231])
>>> signal = torch.rand([1,115]) #[batch, time]
>>> conv_t = ConvTranspose1d(input_shape=signal.shape, out_channels=1, kernel_size=3, stride=2, padding=10)
>>> signal_rec = conv_t(signal)
>>> signal_rec.shape
torch.Size([1, 211])
forward(x, output_size=None)[source]

返回卷积的输出。

参数:
  • x (torch.Tensor (batch, time, channel)) – 要进行卷积的输入。期望为 2d 或 4d tensors。

  • output_size (int) – 输出的大小

返回:

x – 卷积输出

返回类型:

torch.Tensor

remove_weight_norm()[source]

如果在训练期间使用了权重归一化,则在推理时移除它。

class speechbrain.nnet.CNN.DepthwiseSeparableConv1d(out_channels, kernel_size, input_shape, stride=1, dilation=1, padding='same', bias=True)[source]

基类: Module

此类实现深度可分离一维卷积。

首先,对输入应用逐通道卷积,然后进行逐点卷积以将输入投影到输出

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

  • kernel_size (int) – 卷积滤波器的核大小。

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

  • stride (int) – 卷积滤波器的步长因子。当步长因子 > 1 时,进行时间抽取。

  • dilation (int) – 卷积滤波器的膨胀因子。

  • padding (str) – (same, valid, causal)。如果为“valid”,则不进行填充。如果为“same”且步长为 1,则输出形状与输入形状相同。“causal”产生因果(膨胀)卷积。

  • bias (bool) – 如果为 True,则使用加性偏置 b。

示例

>>> inp = torch.randn([8, 120, 40])
>>> conv = DepthwiseSeparableConv1d(256, 3, input_shape=inp.shape)
>>> out = conv(inp)
>>> out.shape
torch.Size([8, 120, 256])
forward(x)[source]

返回卷积的输出。

参数:

x (torch.Tensor (batch, time, channel)) – 要进行卷积的输入。期望为 3d tensors。

返回类型:

卷积输出。

class speechbrain.nnet.CNN.DepthwiseSeparableConv2d(out_channels, kernel_size, input_shape, stride=(1, 1), dilation=(1, 1), padding='same', bias=True)[source]

基类: Module

此类实现深度可分离二维卷积。

首先,对输入应用逐通道卷积,然后进行逐点卷积以将输入投影到输出

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

  • kernel_size (int) – 卷积滤波器的核大小。

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

  • stride (int) – 卷积滤波器的步长因子。当步长因子 > 1 时,进行时间抽取。

  • dilation (int) – 卷积滤波器的膨胀因子。

  • padding (str) – (same, valid, causal)。如果为“valid”,则不进行填充。如果为“same”且步长为 1,则输出形状与输入形状相同。“causal”产生因果(膨胀)卷积。

  • bias (bool) – 如果为 True,则使用加性偏置 b。

示例

>>> inp = torch.randn([8, 120, 40, 1])
>>> conv = DepthwiseSeparableConv2d(256, (3, 3), input_shape=inp.shape)
>>> out = conv(inp)
>>> out.shape
torch.Size([8, 120, 40, 256])
forward(x)[source]

返回卷积的输出。

参数:

x (torch.Tensor (batch, time, channel)) – 要进行卷积的输入。期望为 3d tensors。

返回:

out – 卷积输出。

返回类型:

torch.Tensor

class speechbrain.nnet.CNN.GaborConv1d(out_channels, kernel_size, stride, input_shape=None, in_channels=None, padding='same', padding_mode='constant', sample_rate=16000, min_freq=60.0, max_freq=None, n_fft=512, normalize_energy=False, bias=False, sort_filters=False, use_legacy_complex=False, skip_transpose=False)[source]

基类: Module

此类实现了来自 的一维 Gabor 卷积

Neil Zeghidour, Olivier Teboul, Félix de Chaumont Quitry & Marco Tagliasacchi, “LEAF: A LEARNABLE FRONTEND FOR AUDIO CLASSIFICATION”, in Proc. of ICLR 2021 (https://arxiv.org/abs/2101.08596)

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

  • kernel_size (int) – 卷积滤波器的核大小。

  • stride (int) – 卷积滤波器的步长因子。当步长因子 > 1 时,进行时间抽取。

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

  • in_channels (int) – 输入中期望的通道数。

  • padding (str) – (same, valid)。如果为“valid”,则不进行填充。如果为“same”且步长为 1,则输出形状与输入形状相同。

  • padding_mode (str) – 此标志指定填充类型。更多信息请参阅 torch.nn 文档。

  • sample_rate (int,) – 输入信号的采样率。仅用于 sinc_conv。

  • min_freq (float) – 滤波器最低可能频率(赫兹)

  • max_freq (float) – 滤波器最高可能频率(赫兹)

  • n_fft (int) – 用于初始化的 FFT 桶数

  • normalize_energy (bool) – 是否在初始化时归一化能量。默认为 False

  • bias (bool) – 如果为 True,则使用加性偏置 b。

  • sort_filters (bool) – 是否按中心频率对滤波器进行排序。默认为 False

  • use_legacy_complex (bool) – 如果为 False,则使用 torch.complex64 数据类型作为 Gabor 脉冲响应。如果为 True,则在两个实值 tensors 上执行计算。

  • skip_transpose (bool) – 如果为 False,则使用 speechbrain 的 batch x time x channel 约定。如果为 True,则使用 batch x channel x time 约定。

示例

>>> inp_tensor = torch.rand([10, 8000])
>>> # 401 corresponds to a window of 25 ms at 16000 kHz
>>> gabor_conv = GaborConv1d(
...     40, kernel_size=401, stride=1, in_channels=1
... )
>>> #
>>> out_tensor = gabor_conv(inp_tensor)
>>> out_tensor.shape
torch.Size([10, 8000, 40])
forward(x)[source]

返回 Gabor 卷积的输出。

参数:

x (torch.Tensor (batch, time, channel)) – 要进行卷积的输入。

返回:

x – Gabor 卷积的输出

返回类型:

torch.Tensor

speechbrain.nnet.CNN.get_padding_elem(L_in: int, stride: int, kernel_size: int, dilation: int)[source]

此函数计算零填充所需的元素数量。

参数:
  • L_in (int)

  • stride (int)

  • kernel_size (int)

  • dilation (int)

返回:

padding – 要添加的填充大小

返回类型:

int

speechbrain.nnet.CNN.get_padding_elem_transposed(L_out: int, L_in: int, stride: int, kernel_size: int, dilation: int, output_padding: int)[source]

此函数计算转置卷积所需的填充大小

参数:
  • L_out (int)

  • L_in (int)

  • stride (int)

  • kernel_size (int)

  • dilation (int)

  • output_padding (int)

返回:

padding – 要应用的填充大小

返回类型:

int