speechbrain.augment.time_domain 模块

时域序列数据增强类

本模块包含用于增强时域序列数据的类。它对于在训练期间提高神经模型的鲁棒性特别有用。可用的数据失真包括添加噪声、应用混响、调整播放速度等。所有类都实现为 torch.nn.Module,支持端到端可微分和梯度反向传播。

作者:- Peter Plantinga (2020) - Mirco Ravanelli (2023)

摘要

AddNoise

此类将噪声信号加性地组合到输入信号中。

AddReverb

此类将音频信号与脉冲响应进行卷积。

ChannelDrop

此函数在多通道输入波形中随机丢弃通道。

ChannelSwap

此函数随机交换 N 个通道。

CutCat

此函数将批次中包含的时间序列片段(在时间上长度相等)组合起来。

DoClip

此函数通过截断输入张量来模仿音频削波。

DropBitResolution

此类将 float32 张量转换为较低分辨率的张量(例如 int16、int8、float16),然后将其转换回 float32。

DropChunk

此类丢弃输入信号的一部分。

DropFreq

此类从信号中丢弃一个随机频率。

FastDropChunk

此类丢弃输入信号的一部分。

RandAmp

此函数将信号乘以随机幅度。

Resample

此类使用基于 sinc 插值的 torchaudio 重采样器 对音频进行重采样。

SignFlip

翻转信号的符号。

SpeedPerturb

稍微加快或减慢音频信号的速度。

函数

pink_noise_like

创建粉红噪声序列(也称为 1/f 噪声)。

参考

class speechbrain.augment.time_domain.AddNoise(csv_file=None, csv_keys=None, sorting='random', num_workers=0, snr_low=0, snr_high=0, pad_noise=False, start_index=None, normalize=False, noise_funct=<built-in method randn_like of type object>, replacements={}, noise_sample_rate=16000, clean_sample_rate=16000)[源码]

基类:Module

此类将噪声信号加性地组合到输入信号中。

参数:
  • csv_file (str) – 包含噪声音频文件位置的 csv 文件名。如果未提供,将使用白噪声。

  • csv_keys (list, None, optional) – 默认值:None。应指定噪声数据的一个数据条目。如果为 None,则假定 csv 文件只有一个数据条目。

  • sorting (str) – 迭代 csv 文件的顺序,选项包括:random, original, ascending, 和 descending。

  • num_workers (int) – DataLoader 中的工作进程数(参见 PyTorch DataLoader 文档)。

  • snr_low (int) – 以分贝为单位的混合比低端。

  • snr_high (int) – 以分贝为单位的混合比高端。

  • pad_noise (bool) – 如果为 True,将复制比对应干净信号短的噪声信号,以便覆盖整个干净信号。否则,不填充噪声。

  • start_index (int) – 从噪声波形的哪个索引开始。默认情况下,在 [0, len(noise) - len(waveforms)] 中选择一个随机索引。

  • normalize (bool) – 如果为 True,输出超出 [-1,1] 的带噪信号将被归一化到 [-1,1]。

  • noise_funct (funct object) – 用于抽取噪声样本的函数。如果未提供包含带噪序列的 csv 文件,则启用此功能。默认情况下,使用 torch.randn_like(用于采样白噪声)。通常,它必须是一个函数,接受原始波形作为输入,并返回一个张量,其中包含要添加的相应噪声(例如,参见 pink_noise_like)。

  • replacements (dict) – 在 csv 文件中进行的一组字符串替换。每次找到键时,它将被替换为对应的值。

  • noise_sample_rate (int) – 噪声音频信号的采样率,以便在需要时将噪声重采样到干净信号的采样率。

  • clean_sample_rate (int) – 干净音频信号的采样率,以便在需要时将噪声重采样到干净信号的采样率。

示例

>>> import pytest
>>> from speechbrain.dataio.dataio import read_audio
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> clean = signal.unsqueeze(0) # [batch, time, channels]
>>> noisifier = AddNoise('tests/samples/annotation/noise.csv',
...                     replacements={'noise_folder': 'tests/samples/noise'})
>>> noisy = noisifier(clean, torch.ones(1))
forward(waveforms, lengths)[源码]
参数:
  • waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

  • lengths (torch.Tensor) – 形状应为一维,[batch]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量。

class speechbrain.augment.time_domain.AddReverb(csv_file, sorting='random', num_workers=0, rir_scale_factor=1.0, replacements={}, reverb_sample_rate=16000, clean_sample_rate=16000)[源码]

基类:Module

此类将音频信号与脉冲响应进行卷积。

参数:
  • csv_file (str) – 包含脉冲响应文件位置的 csv 文件名。

  • sorting (str) – 迭代 csv 文件的顺序,选项包括:random, original, ascending, 和 descending。

  • num_workers (int) – DataLoader 中的工作进程数(参见 PyTorch DataLoader 文档)。

  • rir_scale_factor (float) – 它压缩或扩展给定的脉冲响应。如果 0 < scale_factor < 1,脉冲响应被压缩(混响较少),而如果 scale_factor > 1,则脉冲响应被扩展(混响更多)。

  • replacements (dict) – 在 csv 文件中进行的一组字符串替换。每次找到键时,它将被替换为对应的值。

  • reverb_sample_rate (int) – 损坏信号(rir)的采样率,以便在需要时可以将其重采样到干净信号的采样率。

  • clean_sample_rate (int) – 干净信号的采样率,以便在卷积之前将损坏信号重采样到干净信号的采样率。

示例

>>> import pytest
>>> from speechbrain.dataio.dataio import read_audio
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> clean = signal.unsqueeze(0) # [batch, time, channels]
>>> reverb = AddReverb('tests/samples/annotation/RIRs.csv',
...                     replacements={'rir_folder': 'tests/samples/RIRs'})
>>> reverbed = reverb(clean)
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量。

class speechbrain.augment.time_domain.SpeedPerturb(orig_freq, speeds=[90, 100, 110], device='cpu')[源码]

基类:Module

稍微加快或减慢音频信号的速度。

以类似于原始速率的速率对音频信号进行重采样,以实现稍微慢或稍微快的信号。该技术在论文“Audio Augmentation for Speech Recognition”中有所阐述。

参数:
  • orig_freq (int) – 原始信号的频率。

  • speeds (list) – 信号应更改到的速度,以原始信号的百分比表示(即 speeds 除以 100 得到比例)。

  • device (str) – 用于重采样的设备。

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> perturbator = SpeedPerturb(orig_freq=16000, speeds=[90])
>>> clean = signal.unsqueeze(0)
>>> perturbed = perturbator(clean)
>>> clean.shape
torch.Size([1, 52173])
>>> perturbed.shape
torch.Size([1, 46956])
forward(waveform)[源码]
参数:

waveform (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的 torch.Tensor。

class speechbrain.augment.time_domain.Resample(orig_freq=16000, new_freq=16000, *args, **kwargs)[源码]

基类:Module

此类使用基于 sinc 插值的 torchaudio 重采样器 对音频进行重采样。

参数:

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> signal = signal.unsqueeze(0) # [batch, time, channels]
>>> resampler = Resample(orig_freq=16000, new_freq=8000)
>>> resampled = resampler(signal)
>>> signal.shape
torch.Size([1, 52173])
>>> resampled.shape
torch.Size([1, 26087])
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量。

class speechbrain.augment.time_domain.DropFreq(drop_freq_low=1e-14, drop_freq_high=1, drop_freq_count_low=1, drop_freq_count_high=3, drop_freq_width=0.05, epsilon=1e-12)[源码]

基类:Module

此类从信号中丢弃一个随机频率。

此类旨在教会模型依赖信号的所有部分,而不仅仅是少数几个频带。

参数:
  • drop_freq_low (float) – 可以丢弃的频率低端,表示为采样率/2 的分数。

  • drop_freq_high (float) – 可以丢弃的频率高端,表示为采样率/2 的分数。

  • drop_freq_count_low (int) – 可以丢弃的频率数量低端。

  • drop_freq_count_high (int) – 可以丢弃的频率数量高端。

  • drop_freq_width (float) – 要丢弃的频带宽度,表示为采样率/2 的分数。

  • epsilon (float) – 一个小的正值,用于防止诸如过滤 0 Hz、除以零或其他数值不稳定性等问题。此值设置了用于滤波的归一化频率的绝对最小值。默认值为 1e-12。

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> dropper = DropFreq()
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> dropped_signal = dropper(signal.unsqueeze(0))
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量。

class speechbrain.augment.time_domain.DropChunk(drop_length_low=100, drop_length_high=1000, drop_count_low=1, drop_count_high=3, drop_start=0, drop_end=None, noise_factor=0.0)[源码]

基类:Module

此类丢弃输入信号的一部分。

使用 DropChunk 作为增强策略有助于模型学习依赖信号的所有部分,因为它不能指望给定部分一定存在。

参数:
  • drop_length_low (int) – 将信号设置为零的长度低端,以采样点为单位。

  • drop_length_high (int) – 将信号设置为零的长度高端,以采样点为单位。

  • drop_count_low (int) – 信号可以被设置为零的次数低端。

  • drop_count_high (int) – 信号可以被设置为零的次数高端。

  • drop_start (int) – 允许丢弃的第一个索引。

  • drop_end (int) – 允许丢弃的最后一个索引。

  • noise_factor (float) – 相对于话语平均幅度用于缩放插入的白噪声的因子。1 保持平均幅度相同,而 0 插入全 0。

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> dropper = DropChunk(drop_start=100, drop_end=200, noise_factor=0.)
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> signal = signal.unsqueeze(0) # [batch, time, channels]
>>> length = torch.ones(1)
>>> dropped_signal = dropper(signal, length)
>>> float(dropped_signal[:, 150])
0.0
forward(waveforms, lengths)[源码]
参数:
  • waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

  • lengths (torch.Tensor) – 形状应为一维,[batch]

返回:

[batch, time, channels]

返回类型:

形状为 [batch, time]

class speechbrain.augment.time_domain.FastDropChunk(drop_length_low=100, drop_length_high=1000, drop_count_low=1, drop_count_high=10, drop_start=0, drop_end=None, n_masks=1000)[源码]

基类:Module

此类丢弃输入信号的一部分。与 DropChunk 的区别在于,在这种情况下,我们在第一次调用 forward 函数时预先计算丢弃掩码。对于所有其他调用,我们只打乱并应用它们。这使得代码更快,更适合于大型批次的数据增强。

它只能用于固定长度的序列。

参数:
  • drop_length_low (int) – 将信号设置为零的长度低端,以采样点为单位。

  • drop_length_high (int) – 将信号设置为零的长度高端,以采样点为单位。

  • drop_count_low (int) – 信号可以被设置为零的次数低端。

  • drop_count_high (int) – 信号可以被设置为零的次数高端。

  • drop_start (int) – 允许丢弃的第一个索引。

  • drop_end (int) – 允许丢弃的最后一个索引。

  • n_masks (int) – 预计算掩码的数量。

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> dropper = FastDropChunk(drop_start=100, drop_end=200)
>>> signal = torch.rand(10, 250, 22)
>>> dropped_signal = dropper(signal)
initialize_masks(waveforms)[源码]
waveformstorch.Tensor

形状应为 [batch, time][batch, time, channels]

`.
dropped_maskstorch.Tensor

大小为 [n_masks, time] 的张量,包含丢弃的块。丢弃的区域被设置为 0。

forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量

class speechbrain.augment.time_domain.DoClip(clip_low=0.5, clip_high=0.5)[源码]

基类:Module

此函数通过截断输入张量来模仿音频削波。首先,将波形归一化到 -1 到 1 之间。然后应用削波。最后,恢复原始幅度。

参数:
  • clip_low (float) – 用于削波信号的幅度低端。

  • clip_high (float) – 用于削波信号的幅度高端。

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> clipper = DoClip(clip_low=0.01, clip_high=0.01)
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> clipped_signal = clipper(signal.unsqueeze(0))
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量

class speechbrain.augment.time_domain.RandAmp(amp_low=0.5, amp_high=1.5)[源码]

基类:Module

此函数将信号乘以一个随机幅度。首先,将信号归一化到幅度在 -1 和 1 之间。然后将其乘以一个随机数。

参数:
  • amp_low (float) – 最小幅度乘数。

  • amp_high (float) – 最大幅度乘数。

示例

>>> from speechbrain.dataio.dataio import read_audio
>>> rand_amp = RandAmp(amp_low=0.25, amp_high=1.75)
>>> signal = read_audio('tests/samples/single-mic/example1.wav')
>>> output_signal = rand_amp(signal.unsqueeze(0))
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量

class speechbrain.augment.time_domain.ChannelDrop(drop_rate=0.1)[源码]

基类:Module

此函数在多通道输入波形中随机丢弃通道。

参数:

drop_rate (float) – 通道丢弃因子

示例

>>> signal = torch.rand(4, 256, 8)
>>> ch_drop = ChannelDrop(drop_rate=0.5)
>>> output_signal = ch_drop(signal)
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量

class speechbrain.augment.time_domain.ChannelSwap(min_swap=0, max_swap=0)[源码]

基类:Module

此函数随机交换 N 个通道。

参数:
  • min_swap (int) – 要交换的最小通道数。

  • max_swap (int) – 要交换的最大通道数。

示例

>>> signal = torch.rand(4, 256, 8)
>>> ch_swap = ChannelSwap()
>>> output_signal = ch_swap(signal)
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量

class speechbrain.augment.time_domain.CutCat(min_num_segments=2, max_num_segments=10)[源码]

基类:Module

此函数将批次中包含的时间序列片段(在时间上长度相等)组合起来。建议用于 EEG 信号,参见 https://doi.org/10.1016/j.neunet.2021.05.032

参数:
  • min_num_segments (int) – 要组合的最小片段数。

  • max_num_segments (int) – 要组合的最大片段数。默认值为 10。

示例

>>> signal = torch.ones((4, 256, 22)) * torch.arange(4).reshape((4, 1, 1,))
>>> cutcat =  CutCat()
>>> output_signal = cutcat(signal)
forward(waveforms)[源码]
参数:

waveforms (torch.Tensor) – 形状应为 [batch, time][batch, time, channels]

返回类型:

形状为 [batch, time][batch, time, channels] 的张量

speechbrain.augment.time_domain.pink_noise_like(waveforms, alpha_low=1.0, alpha_high=1.0, sample_rate=50)[源码]

创建粉红噪声序列(也称为 1/f)。粉红噪声是通过将白噪声序列的频谱乘以因子 (1/f^alpha) 获得的。alpha 因子控制频率域的衰减因子(alpha=0 添加白噪声,alpha>>0 添加低频噪声)。它在 alpha_low 和 alpha_high 之间随机采样。使用负 alpha 时,此函数生成蓝噪声。

参数:
  • waveforms (torch.Tensor) – 原始波形。仅用于推断形状。

  • alpha_low (float) – alpha 谱平滑因子的最小值。

  • alpha_high (float) – alpha 谱平滑因子的最大值。

  • sample_rate (float) – 原始信号的采样率。

返回:

pink_noise – 形状与输入张量相同的粉红噪声。

返回类型:

torch.Tensor

示例

>>> waveforms = torch.randn(4,257,10)
>>> noise = pink_noise_like(waveforms)
>>> noise.shape
torch.Size([4, 257, 10])
class speechbrain.augment.time_domain.DropBitResolution(target_dtype='random')[源码]

基类:Module

此类将 float32 张量转换为较低分辨率的张量(例如 int16、int8、float16),然后将其转换回 float32。此过程会丢失信息,可用于数据增强。

参数:

target_dtype: str

“int16”、“int8”、“float16”之一。如果为“random”,则位分辨率在上述选项中随机选择。

示例
>>> dropper = DropBitResolution()
>>> signal = torch.rand(4, 16000)
>>> signal_dropped = dropper(signal)
forward(float32_tensor)[源码]

参数:

float32_tensor: torch.Tensor

形状为 [batch, time][batch, time, channels] 的 float32 张量。

返回:

torch.Tensor

形状为 [batch, time][batch, time, channels] 的张量 (Float32)

class speechbrain.augment.time_domain.SignFlip(flip_prob=0.5)[源码]

基类:Module

翻转信号的符号。

此模块以给定概率对张量中的所有值取反。如果符号未翻转,则返回原始信号不变。此技术在论文“CADDA: Class-wise Automatic Differentiable Data Augmentation for EEG Signals”中有所阐述 https://arxiv.org/pdf/2106.13695

参数:

flip_prob (float) – 翻转信号符号的概率。默认值为 0.5。

示例

>>> import torch
>>> x = torch.tensor([1,2,3,4,5])
>>> flip = SignFlip(flip_prob=1) # 100% chance to flip sign
>>> flip(x)
tensor([-1, -2, -3, -4, -5])
forward(waveform)[源码]
参数:

waveform (torch.Tensor) – 代表波形的输入张量,形状不重要。

返回:

形状与输入张量相同,其中张量中所有值的符号以 flip_prob 的概率被翻转的输出张量。

返回类型:

torch.Tensor