speechbrain.lobes.models.HifiGAN 模块

HiFi-GAN 的神经网络模块:用于高效高保真语音合成的生成对抗网络

更多详情请见:https://arxiv.org/pdf/2010.05646.pdf, https://arxiv.org/abs/2406.10735

作者
  • Jarod Duret 2021

  • 王英志 2022

摘要

DiscriminatorLoss

创建判别器损失摘要

DiscriminatorP

HiFiGAN 周期性判别器 从输入波形中获取每 P 个值并应用一系列卷积。注意:如果周期为 2,则波形 = [1, 2, 3, 4, 5, 6 ...] --> [1, 3, 5 ...] --> 卷积 -> 分数,特征。

DiscriminatorS

HiFiGAN 尺度判别器。

GeneratorLoss

创建生成器损失摘要并为不同损失应用权重

HifiganDiscriminator

封装 MPD 和 MSD 的 HiFiGAN 判别器。

HifiganGenerator

具有多感受野融合 (MRF) 的 HiFiGAN 生成器

L1SpecLoss

HiFiGAN 论文 (https://arxiv.org/pdf/2010.05646.pdf) 中描述的频谱图 L1 损失。注意:与 L2 损失相比,L1 损失有助于学习细节。

MSEDLoss

均方判别器损失 训练判别器将真实样本分类为 1,将生成器合成的样本分类为 0。

MSEGLoss

均方生成器损失 训练生成器通过将样本质量更新为几乎等于 1 的值来欺骗判别器。

MelganFeatureLoss

计算特征匹配损失,这是一种学习到的相似性度量,通过判别器对真实样本和生成样本之间的特征差异来衡量(Larsen 等人,2016 年;Kumar 等人,2019 年)。

MultiPeriodDiscriminator

HiFiGAN 多周期判别器 (MPD) PeriodDiscriminator 的包装器,用于在不同周期应用它。建议将周期设置为素数,以减少每个判别器之间的重叠。

MultiScaleDiscriminator

HiFiGAN 多尺度判别器。

MultiScaleSTFTLoss

多尺度 STFT 损失。

ResBlock1

残差块类型 1,每个卷积块中有 3 个卷积层。

ResBlock2

残差块类型 2,每个卷积块中有 2 个卷积层。

STFTLoss

STFT 损失。

UnitHifiganGenerator

UnitHiFiGAN 生成器将离散语音 token 作为输入。

VariancePredictor

受 FastSpeech2 启发的方差预测器

函数

动态范围压缩

音频信号的动态范围压缩

梅尔频谱图

计算原始音频信号的 Mel 频谱图

处理时长

处理给定批次的 code,提取连续唯一元素及其相关特征。

stft

计算输入短重叠窗口的傅里叶变换

参考

speechbrain.lobes.models.HifiGAN.dynamic_range_compression(x, C=1, clip_val=1e-05)[source]

音频信号的动态范围压缩

speechbrain.lobes.models.HifiGAN.mel_spectogram(sample_rate, hop_length, win_length, n_fft, n_mels, f_min, f_max, power, normalized, norm, mel_scale, compression, audio)[source]

计算原始音频信号的 Mel 频谱图

参数:
  • sample_rate (int) – 音频信号的采样率。

  • hop_length (int) – STFT 窗口之间的跳长。

  • win_length (int) – 窗口大小。

  • n_fft (int) – FFT 大小。

  • n_mels (int) – 梅尔滤波器组数量。

  • f_min (float) – 最小频率。

  • f_max (float) – 最大频率。

  • power (float) – 幅度频谱图的指数。

  • normalized (bool) – 是否在 stft 后按幅度归一化。

  • norm (str or None) – 如果为 “slaney”,则将三角梅尔权重除以梅尔带的宽度。

  • mel_scale (str) – 使用的尺度:“htk” 或 “slaney”。

  • compression (bool) – 是否进行动态范围压缩。

  • audio (torch.tensor) – 输入音频信号。

返回类型:

梅尔频谱图

speechbrain.lobes.models.HifiGAN.process_duration(code, code_feat)[source]

处理给定批次的 code,提取连续唯一元素及其相关特征。

参数:
  • code (torch.Tensor (批次, 时间)) – code 索引张量。

  • code_feat (torch.Tensor (批次, 时间, 通道)) – code 特征张量。

返回:

  • uniq_code_feat_filtered (torch.Tensor (批次, 时间)) – 连续唯一 code 的特征。

  • mask (torch.Tensor (批次, 时间)) – 唯一 code 的填充掩码。

  • uniq_code_count (torch.Tensor (n)) – 唯一 code 的计数。

示例

>>> code = torch.IntTensor([[40, 18, 18, 10]])
>>> code_feat = torch.rand([1, 4, 128])
>>> out_tensor, mask, uniq_code = process_duration(code, code_feat)
>>> out_tensor.shape
torch.Size([1, 1, 128])
>>> mask.shape
torch.Size([1, 1])
>>> uniq_code.shape
torch.Size([1])
class speechbrain.lobes.models.HifiGAN.ResBlock1(channels, kernel_size=3, dilation=(1, 3, 5))[source]

基类:Module

残差块类型 1,每个卷积块中有 3 个卷积层。

参数:
  • channels (int) – 卷积层的隐藏通道数。

  • kernel_size (int) – 每层卷积滤波器的尺寸。

  • dilation (list) – 块中每个卷积层的扩张值列表。

forward(x)[source]

返回 ResBlock1 的输出

参数:

x (torch.Tensor (批次, 通道, 时间)) – 输入张量。

返回类型:

ResBlock 输出

remove_weight_norm()[source]

此函数在推理期间移除权重归一化。

class speechbrain.lobes.models.HifiGAN.ResBlock2(channels, kernel_size=3, dilation=(1, 3))[source]

基类:Module

残差块类型 2,每个卷积块中有 2 个卷积层。

参数:
  • channels (int) – 卷积层的隐藏通道数。

  • kernel_size (int) – 每层卷积滤波器的尺寸。

  • dilation (list) – 块中每个卷积层的扩张值列表。

forward(x)[source]

返回 ResBlock1 的输出

参数:

x (torch.Tensor (批次, 通道, 时间)) – 输入张量。

返回类型:

ResBlock 输出

remove_weight_norm()[source]

此函数在推理期间移除权重归一化。

class speechbrain.lobes.models.HifiGAN.HifiganGenerator(in_channels, out_channels, resblock_type, resblock_dilation_sizes, resblock_kernel_sizes, upsample_kernel_sizes, upsample_initial_channel, upsample_factors, inference_padding=5, cond_channels=0, conv_post_bias=True)[source]

基类:Module

具有多感受野融合 (MRF) 的 HiFiGAN 生成器

参数:
  • in_channels (int) – 输入张量通道数。

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

  • resblock_type (str) – ResBlock 类型。‘1’ 或 ‘2’。

  • resblock_dilation_sizes (List[List[int]]) – ResBlock 中每层的膨胀值列表。

  • resblock_kernel_sizes (List[int]) – 每个 ResBlock 的核大小列表。

  • upsample_kernel_sizes (List[int]) – 每个转置卷积的核大小列表。

  • upsample_initial_channel (int) – 第一个上采样层的通道数。每个连续的上采样层此值都会除以 2。

  • upsample_factors (List[int]) – 每个上采样层的上采样因子(步长)。

  • inference_padding (int) – 推理时应用于输入的常量填充。默认为 5。

  • cond_channels (int) – 如果提供,在 forward 的开始添加一个卷积层。

  • conv_post_bias (bool) – 是否在最终卷积层添加偏置项。

示例

>>> inp_tensor = torch.rand([4, 80, 33])
>>> hifigan_generator= HifiganGenerator(
...    in_channels = 80,
...    out_channels = 1,
...    resblock_type = "1",
...    resblock_dilation_sizes = [[1, 3, 5], [1, 3, 5], [1, 3, 5]],
...    resblock_kernel_sizes = [3, 7, 11],
...    upsample_kernel_sizes = [16, 16, 4, 4],
...    upsample_initial_channel = 512,
...    upsample_factors = [8, 8, 2, 2],
... )
>>> out_tensor = hifigan_generator(inp_tensor)
>>> out_tensor.shape
torch.Size([4, 1, 8448])
forward(x, g=None)[source]
参数:
  • x (torch.Tensor (batch, channel, time)) – 特征输入张量。

  • g (torch.Tensor (batch, 1, time)) – 全局条件输入张量。

返回类型:

生成器的输出

remove_weight_norm()[source]

此函数在推理期间移除权重归一化。

inference(c, padding=True)[source]

推理函数执行填充并运行 forward 方法。

参数:
  • c (torch.Tensor (batch, channel, time)) – 特征输入张量。

  • padding (bool) – 在 forward 之前是否对张量进行填充。

返回类型:

生成器的输出

class speechbrain.lobes.models.HifiGAN.VariancePredictor(encoder_embed_dim, var_pred_hidden_dim, var_pred_kernel_size, var_pred_dropout)[source]

基类:Module

受 FastSpeech2 启发的方差预测器

参数:
  • encoder_embed_dim (int) – 输入张量通道数。

  • var_pred_hidden_dim (int) – 卷积层的隐藏通道大小。

  • var_pred_kernel_size (int) – 每层卷积核的大小。

  • var_pred_dropout (float) – 每层的 dropout 概率。

示例

>>> inp_tensor = torch.rand([4, 80, 128])
>>> duration_predictor = VariancePredictor(
...    encoder_embed_dim = 128,
...    var_pred_hidden_dim = 128,
...    var_pred_kernel_size = 3,
...    var_pred_dropout = 0.5,
... )
>>> out_tensor = duration_predictor (inp_tensor)
>>> out_tensor.shape
torch.Size([4, 80])
forward(x)[source]
参数:

x (torch.Tensor (batch, channel, time)) – 特征输入张量。

返回类型:

方差预测器的输出

class speechbrain.lobes.models.HifiGAN.UnitHifiganGenerator(in_channels, out_channels, resblock_type, resblock_dilation_sizes, resblock_kernel_sizes, upsample_kernel_sizes, upsample_initial_channel, upsample_factors, inference_padding=5, cond_channels=0, conv_post_bias=True, vocab_size=100, embedding_dim=128, attn_dim=128, duration_predictor=False, var_pred_hidden_dim=128, var_pred_kernel_size=3, var_pred_dropout=0.5, multi_speaker=False, normalize_speaker_embeddings=False, skip_token_embedding=False, pooling_type='attention')[source]

基类: HifiganGenerator

UnitHiFiGAN 生成器接收离散语音 token 作为输入。该生成器适用于支持码率可伸缩性训练。更多详情请参阅:https://arxiv.org/abs/2406.10735

参数:
  • in_channels (int) – 输入张量通道数。

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

  • resblock_type (str) – ResBlock 类型。‘1’ 或 ‘2’。

  • resblock_dilation_sizes (List[List[int]]) – ResBlock 中每层的膨胀值列表。

  • resblock_kernel_sizes (List[int]) – 每个 ResBlock 的核大小列表。

  • upsample_kernel_sizes (List[int]) – 每个转置卷积的核大小列表。

  • upsample_initial_channel (int) – 第一个上采样层的通道数。每个连续的上采样层此值都会除以 2。

  • upsample_factors (List[int]) – 每个上采样层的上采样因子(步长)。

  • inference_padding (int) – 推理时应用于输入的常量填充。默认为 5。

  • cond_channels (int) – 是否在前面添加卷积层

  • conv_post_bias (bool) – 是否在最后一个卷积层添加偏置

  • vocab_size (int) – 嵌入字典的大小。

  • embedding_dim (int) – 每个嵌入向量的大小。

  • attn_dim (int) – 注意力维度的大小。

  • duration_predictor (bool) – 启用时长预测模块。

  • var_pred_hidden_dim (int) – 时长预测器卷积层的隐藏通道大小。

  • var_pred_kernel_size (int) – 时长预测器每层卷积核的大小。

  • var_pred_dropout (float) – 时长预测器每层的 dropout 概率。

  • multi_speaker (bool) – 启用多说话人训练。

  • normalize_speaker_embeddings (bool) – 启用说话人嵌入归一化。

  • skip_token_embedding (bool) – 在连续输入情况下是否跳过嵌入层。

  • pooling_type (str, optional) – 使用的池化类型。必须是 ["attention", "sum", "none"] 之一。对于可伸缩声码器,默认为 "attention"。

示例

>>> inp_tensor = torch.randint(0, 100, (4, 10, 1))
>>> unit_hifigan_generator= UnitHifiganGenerator(
...    in_channels = 128,
...    out_channels = 1,
...    resblock_type = "1",
...    resblock_dilation_sizes = [[1, 3, 5], [1, 3, 5], [1, 3, 5]],
...    resblock_kernel_sizes = [3, 7, 11],
...    upsample_kernel_sizes = [11, 8, 8, 4, 4],
...    upsample_initial_channel = 512,
...    upsample_factors = [5, 4, 4, 2, 2],
...    vocab_size = 100,
...    embedding_dim = 128,
...    duration_predictor = True,
...    var_pred_hidden_dim = 128,
...    var_pred_kernel_size = 3,
...    var_pred_dropout = 0.5,
... )
>>> out_tensor, _ = unit_hifigan_generator(inp_tensor)
>>> out_tensor.shape
torch.Size([4, 1, 3200])
forward(x, g=None, spk=None)[source]
参数:
  • x (torch.Tensor (batch, time, channel)) – 特征输入张量。

  • g (torch.Tensor (batch, 1, time)) – 全局条件输入张量。

  • spk (torch.Tensor) – 说话人嵌入。

返回类型:

生成器输出

inference(x, spk=None)[source]

推理函数执行时长预测并运行 forward 方法。

参数:
  • x (torch.Tensor (batch, time, channel)) – 特征输入张量。

  • spk (torch.Tensor) – 说话人嵌入。

返回类型:

生成器输出

class speechbrain.lobes.models.HifiGAN.DiscriminatorP(period, kernel_size=5, stride=3)[source]

基类:Module

HiFiGAN 周期判别器。从输入波形中每隔 P 个值取一个值,并应用卷积堆栈。注意:

如果 period 是 2,波形 = [1, 2, 3, 4, 5, 6 …] –> [1, 3, 5 … ] –> 卷积 –> 分数,特征

参数:
  • period (int) – 每隔 period 取一个新值

  • kernel_size (int) – 卷积堆栈的 1-d 核大小

  • stride (int) – 卷积堆栈的步长

forward(x)[source]
参数:

x (torch.Tensor (batch, 1, time)) – 输入波形。

返回类型:

分数和特征

class speechbrain.lobes.models.HifiGAN.MultiPeriodDiscriminator[source]

基类:Module

HiFiGAN 多周期判别器 (MPD),是 PeriodDiscriminator 的封装,用于在不同周期应用。建议周期使用质数,以减少各个判别器之间的重叠。

forward(x)[source]

返回多周期判别器的分数和特征。

参数:

x (torch.Tensor (batch, 1, time)) – 输入波形。

返回类型:

分数和特征

class speechbrain.lobes.models.HifiGAN.DiscriminatorS(use_spectral_norm=False)[source]

基类:Module

HiFiGAN 尺度判别器。它类似于 MelganDiscriminator,但在论文中解释了一种特定的架构。这里没有使用 SpeechBrain CNN 封装器,因为谱范数(spectral_norm)不常使用。

参数:

use_spectral_norm (bool) – 如果为 True,则切换到谱范数而不是权重范数。

forward(x)[source]
参数:

x (torch.Tensor (batch, 1, time)) – 输入波形。

返回类型:

分数和特征

class speechbrain.lobes.models.HifiGAN.MultiScaleDiscriminator[source]

基类:Module

HiFiGAN 多尺度判别器。类似于 MultiScaleMelganDiscriminator,但根据论文专门为 HiFiGAN 定制。

forward(x)[source]
参数:

x (torch.Tensor (batch, 1, time)) – 输入波形。

返回类型:

分数和特征

class speechbrain.lobes.models.HifiGAN.HifiganDiscriminator[source]

基类:Module

封装 MPD 和 MSD 的 HiFiGAN 判别器。

示例

>>> inp_tensor = torch.rand([4, 1, 8192])
>>> hifigan_discriminator= HifiganDiscriminator()
>>> scores, feats = hifigan_discriminator(inp_tensor)
>>> len(scores)
8
>>> len(feats)
8
forward(x)[source]

返回来自每个判别器中每一层的特征列表的列表。

参数:

x (torch.Tensor) – 输入波形。

返回类型:

来自每个判别器层的特征

speechbrain.lobes.models.HifiGAN.stft(x, n_fft, hop_length, win_length, window_fn='hann_window')[source]

计算输入短重叠窗口的傅里叶变换

class speechbrain.lobes.models.HifiGAN.STFTLoss(n_fft, hop_length, win_length)[source]

基类:Module

STFT 损失。输入生成的和真实的波形被转换为频谱图,并与 L1 损失和谱收敛损失进行比较。它源自 ParallelWaveGAN 论文 https://arxiv.org/pdf/1910.11480.pdf

参数:
  • n_fft (int) – 傅里叶变换大小。

  • hop_length (int) – 相邻滑动窗口帧之间的距离。

  • win_length (int) – 窗口帧和 STFT 滤波器的大小。

forward(y_hat, y)[source]

返回幅度损失和谱收敛损失

参数:
  • y_hat (torch.tensor) – 生成的波形张量

  • y (torch.tensor) – 真实波形张量

返回类型:

幅度损失和谱收敛损失

class speechbrain.lobes.models.HifiGAN.MultiScaleSTFTLoss(n_ffts=(1024, 2048, 512), hop_lengths=(120, 240, 50), win_lengths=(600, 1200, 240))[source]

基类:Module

多尺度 STFT 损失。输入生成的和真实的波形被转换为频谱图,并与 L1 损失和谱收敛损失进行比较。它源自 ParallelWaveGAN 论文 https://arxiv.org/pdf/1910.11480.pdf

forward(y_hat, y)[source]

返回多尺度幅度损失和谱收敛损失

参数:
  • y_hat (torch.tensor) – 生成的波形张量

  • y (torch.tensor) – 真实波形张量

返回类型:

幅度损失和谱收敛损失

class speechbrain.lobes.models.HifiGAN.L1SpecLoss(sample_rate=22050, hop_length=256, win_length=24, n_mel_channels=80, n_fft=1024, n_stft=513, mel_fmin=0.0, mel_fmax=8000.0, mel_normalized=False, power=1.0, norm='slaney', mel_scale='slaney', dynamic_range_compression=True)[source]

基类:Module

HiFiGAN 论文 (https://arxiv.org/pdf/2010.05646.pdf) 中描述的频谱图 L1 损失。注意:与 L2 损失相比,L1 损失有助于学习细节。

参数:
  • sample_rate (int) – 音频信号的采样率。

  • hop_length (int) – STFT 窗口之间的跳长。

  • win_length (int) – 窗口大小。

  • n_mel_channels (int) – mel 滤波器组数量。

  • n_fft (int) – FFT 大小。

  • n_stft (int) – STFT 大小。

  • mel_fmin (float) – 最小频率。

  • mel_fmax (float) – 最大频率。

  • mel_normalized (bool) – STFT 后是否按幅度进行归一化。

  • power (float) – 幅度频谱图的指数。

  • norm (str or None) – 如果为 “slaney”,则将三角梅尔权重除以梅尔带的宽度。

  • mel_scale (str) – 使用的尺度:“htk” 或 “slaney”。

  • dynamic_range_compression (bool) – 是否进行动态范围压缩。

forward(y_hat, y)[source]

返回频谱图上的 L1 损失。

参数:
  • y_hat (torch.tensor) – 生成的波形张量

  • y (torch.tensor) – 真实波形张量

返回类型:

L1 损失

class speechbrain.lobes.models.HifiGAN.MSEGLoss(*args, **kwargs)[source]

基类:Module

均方生成器损失 训练生成器通过将样本质量更新为几乎等于 1 的值来欺骗判别器。

forward(score_fake)[source]

返回生成器 GAN 损失

参数:

score_fake (list) – 生成波形的判别器分数 D(G(s))

返回类型:

生成器损失

class speechbrain.lobes.models.HifiGAN.MelganFeatureLoss[source]

基类:Module

计算特征匹配损失,这是一种学习到的相似性度量,通过判别器对真实样本和生成样本之间的特征差异来衡量(Larsen 等人,2016 年;Kumar 等人,2019 年)。

forward(fake_feats, real_feats)[source]

返回特征匹配损失

参数:
  • fake_feats (list) – 生成波形的判别器特征

  • real_feats (list) – 真实波形的判别器特征

返回类型:

特征匹配损失

class speechbrain.lobes.models.HifiGAN.MSEDLoss[source]

基类:Module

均方判别器损失 训练判别器将真实样本分类为 1,将生成器合成的样本分类为 0。

forward(score_fake, score_real)[source]

返回判别器 GAN 损失

参数:
  • score_fake (list) – 生成波形的判别器分数

  • score_real (list) – 真实波形的判别器分数

返回类型:

判别器损失

class speechbrain.lobes.models.HifiGAN.GeneratorLoss(stft_loss=None, stft_loss_weight=0, mseg_loss=None, mseg_loss_weight=0, feat_match_loss=None, feat_match_loss_weight=0, l1_spec_loss=None, l1_spec_loss_weight=0, mseg_dur_loss=None, mseg_dur_loss_weight=0)[source]

基类:Module

创建生成器损失摘要并为不同损失应用权重

参数:
  • stft_loss (object) – stft 损失对象

  • stft_loss_weight (float) – STFT 损失权重

  • mseg_loss (object) – mseg 损失对象

  • mseg_loss_weight (float) – mseg 损失权重

  • feat_match_loss (object) – 特征匹配损失对象

  • feat_match_loss_weight (float) – 特征匹配损失权重

  • l1_spec_loss (object) – L1 频谱图损失对象

  • l1_spec_loss_weight (float) – L1 频谱图损失权重

  • mseg_dur_loss (object) – mseg 时长损失对象

  • mseg_dur_loss_weight (float) – mseg 时长损失权重

forward(stage, y_hat=None, y=None, scores_fake=None, feats_fake=None, feats_real=None, log_dur_pred=None, log_dur=None)[source]

返回生成器损失字典并应用权重

参数:
  • stage (speechbrain.Stage) – 训练、验证或测试阶段

  • y_hat (torch.tensor) – 生成的波形张量

  • y (torch.tensor) – 真实波形张量

  • scores_fake (list) – 生成波形的判别器分数

  • feats_fake (list) – 生成波形的判别器特征

  • feats_real (list) – 真实波形的判别器特征

  • log_dur_pred (torch.Tensor) – 用于时长损失的预测时长

  • log_dur (torch.Tensor) – 用于时长损失的真实时长

返回类型:

生成器损失字典

class speechbrain.lobes.models.HifiGAN.DiscriminatorLoss(msed_loss=None)[source]

基类:Module

创建判别器损失摘要

参数:

msed_loss (object) – MSE 判别器损失对象

forward(scores_fake, scores_real)[source]

返回判别器损失字典

参数:
  • scores_fake (list) – 生成波形的判别器分数

  • scores_real (list) – 真实波形的判别器分数

返回类型:

判别器损失字典