speechbrain.lobes.models.Tacotron2 模块
用于 Tacotron2 端到端神经文本到语音 (TTS) 模型的神经网络模块
作者 * Georges Abous-Rjeili 2021 * Artem Ploujnikov 2021
摘要
类
Tacotron 注意力层。 |
|
一个带有 Xavier 初始化的 1D 卷积层 |
|
Tacotron 解码器 |
|
Tacotron2 编码器模块,由一系列 1-d 卷积组(默认 3 个)和一个双向 LSTM 组成 |
|
一个带有 Xavier 初始化的线性层 |
|
一个基于位置的注意力层,由一个经过 Xavier 初始化的卷积层和一个全连接层组成 |
|
Tacotron 损失的实现 |
|
|
|
Tacotron 后处理网络,由多个带有 Xavier 初始化和 tanh 激活的 1-d 卷积层以及批量归一化组成。 |
|
Tacotron 前处理网络模块,由指定数量的归一化(Xavier 初始化)线性层组成 |
|
Tactron2 文本到语音模型,基于 NVIDIA 的实现。 |
|
根据每步的帧数对模型输入和目标进行零填充 |
函数
音频信号的动态范围压缩 |
|
预训练合成器的推理钩子 |
|
计算原始音频信号的 MelSpectrogram |
参考
- class speechbrain.lobes.models.Tacotron2.LinearNorm(in_dim, out_dim, bias=True, w_init_gain='linear')[源代码]
继承自:
Module
一个带有 Xavier 初始化的线性层
- 参数:
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import LinearNorm >>> layer = LinearNorm(in_dim=5, out_dim=3) >>> x = torch.randn(3, 5) >>> y = layer(x) >>> y.shape torch.Size([3, 3])
- class speechbrain.lobes.models.Tacotron2.ConvNorm(in_channels, out_channels, kernel_size=1, stride=1, padding=None, dilation=1, bias=True, w_init_gain='linear')[源代码]
继承自:
Module
一个带有 Xavier 初始化的 1D 卷积层
- 参数:
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import ConvNorm >>> layer = ConvNorm(in_channels=10, out_channels=5, kernel_size=3) >>> x = torch.randn(3, 10, 5) >>> y = layer(x) >>> y.shape torch.Size([3, 5, 5])
- class speechbrain.lobes.models.Tacotron2.LocationLayer(attention_n_filters=32, attention_kernel_size=31, attention_dim=128)[源代码]
继承自:
Module
一个基于位置的注意力层,由一个经过 Xavier 初始化的卷积层和一个全连接层组成
- 参数:
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import LocationLayer >>> layer = LocationLayer() >>> attention_weights_cat = torch.randn(3, 2, 64) >>> processed_attention = layer(attention_weights_cat) >>> processed_attention.shape torch.Size([3, 64, 128])
- class speechbrain.lobes.models.Tacotron2.Attention(attention_rnn_dim=1024, embedding_dim=512, attention_dim=128, attention_location_n_filters=32, attention_location_kernel_size=31)[源代码]
继承自:
Module
Tacotron 注意力层。使用基于位置的注意力。
- 参数:
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import ( ... Attention) >>> from speechbrain.lobes.models.transformer.Transformer import ( ... get_mask_from_lengths) >>> layer = Attention() >>> attention_hidden_state = torch.randn(2, 1024) >>> memory = torch.randn(2, 173, 512) >>> processed_memory = torch.randn(2, 173, 128) >>> attention_weights_cat = torch.randn(2, 2, 173) >>> memory_lengths = torch.tensor([173, 91]) >>> mask = get_mask_from_lengths(memory_lengths) >>> attention_context, attention_weights = layer( ... attention_hidden_state, ... memory, ... processed_memory, ... attention_weights_cat, ... mask ... ) >>> attention_context.shape, attention_weights.shape (torch.Size([2, 512]), torch.Size([2, 173]))
- get_alignment_energies(query, processed_memory, attention_weights_cat)[源代码]
计算对齐能量
- 参数:
query (torch.Tensor) – 解码器输出 (批次, n_mel_channels * n_frames_per_step)
processed_memory (torch.Tensor) – 经过处理的编码器输出 (B, T_in, attention_dim)
attention_weights_cat (torch.Tensor) – 累积和先前的注意力权重 (B, 2, max_time)
- 返回:
alignment – (批次, max_time)
- 返回类型:
torch.Tensor
- forward(attention_hidden_state, memory, processed_memory, attention_weights_cat, mask)[源代码]
计算前向传播
- 参数:
attention_hidden_state (torch.Tensor) – 注意力 RNN 的最后一个输出
memory (torch.Tensor) – 编码器输出
processed_memory (torch.Tensor) – 经过处理的编码器输出
attention_weights_cat (torch.Tensor) – 先前和累积的注意力权重
mask (torch.Tensor) – 填充数据的二值掩码
- 返回:
result – 一个 (attention_context, attention_weights) 元组
- 返回类型:
- class speechbrain.lobes.models.Tacotron2.Prenet(in_dim=80, sizes=[256, 256], dropout=0.5)[源代码]
继承自:
Module
Tacotron 前处理网络模块,由指定数量的归一化(Xavier 初始化)线性层组成
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import Prenet >>> layer = Prenet() >>> x = torch.randn(862, 2, 80) >>> output = layer(x) >>> output.shape torch.Size([862, 2, 256])
- class speechbrain.lobes.models.Tacotron2.Postnet(n_mel_channels=80, postnet_embedding_dim=512, postnet_kernel_size=5, postnet_n_convolutions=5)[源代码]
继承自:
Module
Tacotron 后处理网络由多个带有 Xavier 初始化和 tanh 激活的 1-d 卷积层以及批量归一化组成。根据配置,后处理网络可以细化 MEL 频谱图或将其上采样到线性频谱图
- 参数:
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import Postnet >>> layer = Postnet() >>> x = torch.randn(2, 80, 861) >>> output = layer(x) >>> output.shape torch.Size([2, 80, 861])
- class speechbrain.lobes.models.Tacotron2.Encoder(encoder_n_convolutions=3, encoder_embedding_dim=512, encoder_kernel_size=5)[源代码]
继承自:
Module
Tacotron2 编码器模块,由一系列 1-d 卷积组(默认 3 个)和一个双向 LSTM 组成
- 参数:
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import Encoder >>> layer = Encoder() >>> x = torch.randn(2, 512, 128) >>> input_lengths = torch.tensor([128, 83]) >>> outputs = layer(x, input_lengths) >>> outputs.shape torch.Size([2, 128, 512])
- class speechbrain.lobes.models.Tacotron2.Decoder(n_mel_channels=80, n_frames_per_step=1, encoder_embedding_dim=512, attention_dim=128, attention_location_n_filters=32, attention_location_kernel_size=31, attention_rnn_dim=1024, decoder_rnn_dim=1024, prenet_dim=256, max_decoder_steps=1000, gate_threshold=0.5, p_attention_dropout=0.1, p_decoder_dropout=0.1, early_stopping=True)[源代码]
继承自:
Module
Tacotron 解码器
- 参数:
n_mel_channels (int) – MEL 频谱图的通道数
n_frames_per_step (int) – 解码器每一步的频谱图帧数
encoder_embedding_dim (int) – 编码器嵌入维度
attention_dim (int) – 注意力向量大小
attention_location_n_filters (int) – 基于位置的注意力中的滤波器数量
attention_location_kernel_size (int) – 基于位置的注意力的卷积核大小
attention_rnn_dim (int) – 注意力层的 RNN 维度
decoder_rnn_dim (int) – 编码器 RNN 维度
prenet_dim (int) – prenet 的维度(内部层和输出层)
max_decoder_steps (int) – 模型期望最长话语的最大解码器步数
gate_threshold (float) – 与解码器输出进行比较的固定阈值,任何高于此阈值的输出概率被认为是完成并停止生成,从而实现变长输出
p_attention_dropout (float) – 注意力层的 dropout 概率
p_decoder_dropout (float) – 解码器层的 dropout 概率
early_stopping (bool) – 是否提前停止训练。
示例
>>> import torch >>> from speechbrain.lobes.models.Tacotron2 import Decoder >>> layer = Decoder() >>> memory = torch.randn(2, 173, 512) >>> decoder_inputs = torch.randn(2, 80, 173) >>> memory_lengths = torch.tensor([173, 91]) >>> mel_outputs, gate_outputs, alignments = layer( ... memory, decoder_inputs, memory_lengths) >>> mel_outputs.shape, gate_outputs.shape, alignments.shape (torch.Size([2, 80, 173]), torch.Size([2, 173]), torch.Size([2, 173, 173]))
- get_go_frame(memory)[源代码]
获取全零帧作为第一个解码器输入
- 参数:
memory (torch.Tensor) – 解码器输出
- 返回:
decoder_input – 全零帧
- 返回类型:
torch.Tensor
- initialize_decoder_states(memory)[源代码]
初始化注意力 RNN 状态、解码器 RNN 状态、注意力权重、累积注意力权重、注意力上下文,存储内存并存储处理后的内存
- 参数:
memory (torch.Tensor) – 编码器输出
- 返回:
attention_hidden (torch.Tensor)
attention_cell (torch.Tensor)
decoder_hidden (torch.Tensor)
decoder_cell (torch.Tensor)
attention_weights (torch.Tensor)
attention_weights_cum (torch.Tensor)
attention_context (torch.Tensor)
processed_memory (torch.Tensor)
- mask (torch.Tensor)
预处理解码器输入,即 mel 输出
- 参数:
decoder_inputs (torch.Tensor) – 用于教师强制训练的输入,即 mel-specs
- 返回:
decoder_inputs – 经过处理的解码器输入
- 返回类型:
torch.Tensor
- 预处理解码器输出以进行输出
mel_outputs (torch.Tensor) – MEL 尺度频谱图输出
- 参数:
gate_outputs (torch.Tensor) – 门输出能量
alignments (torch.Tensor) – 对齐张量
使用存储状态、注意力和内存进行解码器步骤 :param decoder_input: 先前的 mel 输出 :type decoder_input: torch.Tensor :param attention_hidden: 注意力模块的隐藏状态 :type attention_hidden: torch.Tensor :param attention_cell: 注意力单元状态 :type attention_cell: torch.Tensor :param decoder_hidden: 解码器隐藏状态 :type decoder_hidden: torch.Tensor :param decoder_cell: 解码器单元状态 :type decoder_cell: torch.Tensor :param attention_weights: 注意力权重 :type attention_weights: torch.Tensor :param attention_weights_cum: 累积注意力权重 :type attention_weights_cum: torch.Tensor :param attention_context: 注意力上下文张量 :type attention_context: torch.Tensor :param memory: 内存张量 :type memory: torch.Tensor :param processed_memory: 经过处理的内存张量 :type processed_memory: torch.Tensor :param mask: :type mask: torch.Tensor
- 返回:
gate_outputs (torch.Tensor) – 门输出能量
alignments (torch.Tensor) – 对齐张量
使用存储状态、注意力和内存进行解码器步骤 :param decoder_input: 先前的 mel 输出 :type decoder_input: torch.Tensor :param attention_hidden: 注意力模块的隐藏状态 :type attention_hidden: torch.Tensor :param attention_cell: 注意力单元状态 :type attention_cell: torch.Tensor :param decoder_hidden: 解码器隐藏状态 :type decoder_hidden: torch.Tensor :param decoder_cell: 解码器单元状态 :type decoder_cell: torch.Tensor :param attention_weights: 注意力权重 :type attention_weights: torch.Tensor :param attention_weights_cum: 累积注意力权重 :type attention_weights_cum: torch.Tensor :param attention_context: 注意力上下文张量 :type attention_context: torch.Tensor :param memory: 内存张量 :type memory: torch.Tensor :param processed_memory: 经过处理的内存张量 :type processed_memory: torch.Tensor :param mask: :type mask: torch.Tensor
- decode(decoder_input, attention_hidden, attention_cell, decoder_hidden, decoder_cell, attention_weights, attention_weights_cum, attention_context, memory, processed_memory, mask)[源代码]
使用存储的状态、注意力和内存执行解码器步骤
- 返回:
mel_output (torch.Tensor) – MEL 尺度输出
gate_output (torch.Tensor) – 门输出能量
attention_weights (torch.Tensor) – 注意力权重
- forward(memory, decoder_inputs, memory_lengths)[源代码]
用于训练的解码器前向传播
- 参数:
memory (torch.Tensor) – 编码器输出
decoder_inputs (torch.Tensor) – 用于教师强制的解码器输入,即 mel-specs
memory_lengths (torch.Tensor) – 用于注意力掩码的编码器输出长度。
- 返回:
mel_outputs (torch.Tensor) – 解码器的 mel 输出
gate_outputs (torch.Tensor) – 解码器的门控输出
alignments (torch.Tensor) – 解码器的注意力权重序列
- class speechbrain.lobes.models.Tacotron2.Tacotron2(mask_padding=True, n_mel_channels=80, n_symbols=148, symbols_embedding_dim=512, encoder_kernel_size=5, encoder_n_convolutions=3, encoder_embedding_dim=512, attention_rnn_dim=1024, attention_dim=128, attention_location_n_filters=32, attention_location_kernel_size=31, n_frames_per_step=1, decoder_rnn_dim=1024, prenet_dim=256, max_decoder_steps=1000, gate_threshold=0.5, p_attention_dropout=0.1, p_decoder_dropout=0.1, postnet_embedding_dim=512, postnet_kernel_size=5, postnet_n_convolutions=5, decoder_no_early_stopping=False)[源代码]
继承自:
Module
Tactron2 文本到语音模型,基于 NVIDIA 的实现。
此类是模型的主要入口点,负责实例化所有子模块,这些子模块又管理各个神经网络层
简化结构:输入->词嵌入 ->编码器 ->注意力 ->解码器(+prenet) -> 后处理网络 ->输出
prenet(输入为解码器上一个时间步长)输出为与注意力输出连接的解码器输入
- 参数:
mask_padding (bool) – 是否对 tacotron 的填充输出进行掩码
n_mel_channels (int) – 用于构建频谱图的 mel 通道数
n_symbols (int=128) – textToSequence 中定义的已接受字符符号数
symbols_embedding_dim (int) – 馈入 nn.Embedding 的符号嵌入维度数
encoder_kernel_size (int) – 处理嵌入的卷积核大小
encoder_n_convolutions (int) – 编码器中的卷积层数
encoder_embedding_dim (int) – 编码器中的卷积核数量,这也是编码器中双向 LSTM 的维度
attention_rnn_dim (int) – 输入维度
attention_dim (int) – 注意力中的隐藏表示数
attention_location_n_filters (int) – 注意力中 1-D 卷积滤波器的数量
attention_location_kernel_size (int) – 1-D 卷积滤波器的长度
n_frames_per_step (int=1) – 目前解码器仅支持每步生成 1 个 mel 帧。
decoder_rnn_dim (int) – 2 个单向堆叠 LSTM 单元的数量
prenet_dim (int) – 线性 prenet 层的维度
max_decoder_steps (int) – 解码器在停止前生成的最大步数/帧数
gate_threshold (int) – 截止级别,高于此值的任何输出概率都被认为是完成的并停止生成,从而获得变长输出
p_attention_dropout (float) – 注意力 dropout 概率
p_decoder_dropout (float) – 解码器 dropout 概率
postnet_embedding_dim (int) – 后处理网络滤波器数量
postnet_kernel_size (int) – 后处理网络卷积核的 1d 大小
postnet_n_convolutions (int) – 后处理网络中的卷积层数量
decoder_no_early_stopping (bool) – 与 gate_threshold 一起决定是否提前停止解码器。其逻辑逆值被馈送给解码器
示例
>>> import torch >>> _ = torch.manual_seed(213312) >>> from speechbrain.lobes.models.Tacotron2 import Tacotron2 >>> model = Tacotron2( ... mask_padding=True, ... n_mel_channels=80, ... n_symbols=148, ... symbols_embedding_dim=512, ... encoder_kernel_size=5, ... encoder_n_convolutions=3, ... encoder_embedding_dim=512, ... attention_rnn_dim=1024, ... attention_dim=128, ... attention_location_n_filters=32, ... attention_location_kernel_size=31, ... n_frames_per_step=1, ... decoder_rnn_dim=1024, ... prenet_dim=256, ... max_decoder_steps=32, ... gate_threshold=0.5, ... p_attention_dropout=0.1, ... p_decoder_dropout=0.1, ... postnet_embedding_dim=512, ... postnet_kernel_size=5, ... postnet_n_convolutions=5, ... decoder_no_early_stopping=False ... ) >>> _ = model.eval() >>> inputs = torch.tensor([ ... [13, 12, 31, 14, 19], ... [31, 16, 30, 31, 0], ... ]) >>> input_lengths = torch.tensor([5, 4]) >>> outputs, output_lengths, alignments = model.infer(inputs, input_lengths) >>> outputs.shape, output_lengths.shape, alignments.shape (torch.Size([2, 80, 1]), torch.Size([2]), torch.Size([2, 1, 5]))
- speechbrain.lobes.models.Tacotron2.LossStats
TacotronLoss
的别名
- class speechbrain.lobes.models.Tacotron2.Loss(guided_attention_sigma=None, gate_loss_weight=1.0, guided_attention_weight=1.0, guided_attention_scheduler=None, guided_attention_hard_stop=None)[源代码]
继承自:
Module
Tacotron 损失的实现
损失包括频谱图的 MSE 损失、BCE 门控损失以及(如果启用)引导注意力损失,该损失试图使注意力矩阵对角化
模块的输出是一个 LossStats 元组,其中包括总损失
- 参数:
示例
>>> import torch >>> _ = torch.manual_seed(42) >>> from speechbrain.lobes.models.Tacotron2 import Loss >>> loss = Loss(guided_attention_sigma=0.2) >>> mel_target = torch.randn(2, 80, 861) >>> gate_target = torch.randn(1722, 1) >>> mel_out = torch.randn(2, 80, 861) >>> mel_out_postnet = torch.randn(2, 80, 861) >>> gate_out = torch.randn(2, 861) >>> alignments = torch.randn(2, 861, 173) >>> targets = mel_target, gate_target >>> model_outputs = mel_out, mel_out_postnet, gate_out, alignments >>> input_lengths = torch.tensor([173, 91]) >>> target_lengths = torch.tensor([861, 438]) >>> loss(model_outputs, targets, input_lengths, target_lengths, 1) TacotronLoss(loss=tensor(4.8566), mel_loss=tensor(4.0097), gate_loss=tensor(0.8460), attn_loss=tensor(0.0010), attn_weight=tensor(1.))
- forward(model_output, targets, input_lengths, target_lengths, epoch)[source]
计算损失
- 参数:
- 返回:
result – 总损失 - 以及单独的损失(mel 和 gate)
- 返回类型:
LossStats
- get_attention_loss(alignments, input_lengths, target_lengths, epoch)[source]
计算注意力损失
- 参数:
alignments (torch.Tensor) – 来自模型的对齐矩阵
input_lengths (torch.Tensor) – 输入长度的 (batch, length) 张量
target_lengths (torch.Tensor) – 目标(谱图)长度的 (batch, length) 张量
epoch (int) – 当前的 epoch 数(用于引导注意力损失的调度)通常使用 StepScheduler
- 返回:
attn_loss – 注意力损失值
- 返回类型:
torch.Tensor
- class speechbrain.lobes.models.Tacotron2.TextMelCollate(n_frames_per_step=1)[source]
基类:
object
根据每步的帧数对模型输入和目标进行零填充
- 参数:
n_frames_per_step (int) – 每一步的输出帧数
- __call__(batch)[source]
从归一化文本和 mel-谱图整理训练批次
- 参数:
batch (list) – [text_normalized, mel_normalized]
- 返回:
text_padded (torch.Tensor)
input_lengths (torch.Tensor)
mel_padded (torch.Tensor)
gate_padded (torch.Tensor)
output_lengths (torch.Tensor)
len_x (torch.Tensor)
labels (torch.Tensor)
wavs (torch.Tensor)
- speechbrain.lobes.models.Tacotron2.dynamic_range_compression(x, C=1, clip_val=1e-05)[source]
音频信号的动态范围压缩
- speechbrain.lobes.models.Tacotron2.mel_spectogram(sample_rate, hop_length, win_length, n_fft, n_mels, f_min, f_max, power, normalized, norm, mel_scale, compression, audio)[source]
计算原始音频信号的 MelSpectrogram
- 参数:
sample_rate (int) – 音频信号的采样率。
hop_length (int) – STFT 窗口之间的跳跃长度。
win_length (int) – 窗口大小。
n_fft (int) – FFT 大小。
n_mels (int) – mel 滤波器组的数量。
f_min (float) – 最小频率。
f_max (float) – 最大频率。
power (float) – 幅度谱的指数。
normalized (bool) – 是否在 STFT 后按幅度进行归一化。
norm (str or None) – 如果是 “slaney”,则将三角形 mel 权重除以 mel 带宽
mel_scale (str) – 使用的尺度:“htk” 或 “slaney”。
compression (bool) – 是否进行动态范围压缩
audio (torch.Tensor) – 输入音频信号
- 返回:
mel – 计算出的 mel 谱图特征。
- 返回类型:
torch.Tensor