2018-01-22 14:59:41 +00:00
|
|
|
# coding: utf-8
|
2019-09-12 08:39:15 +00:00
|
|
|
import torch
|
2018-01-22 14:59:41 +00:00
|
|
|
from torch import nn
|
2020-06-04 12:28:16 +00:00
|
|
|
|
2019-09-24 14:18:48 +00:00
|
|
|
from TTS.layers.gst_layers import GST
|
2020-06-04 12:28:16 +00:00
|
|
|
from TTS.layers.tacotron import Decoder, Encoder, PostCBHG
|
|
|
|
from TTS.models.tacotron_abstract import TacotronAbstract
|
2018-01-22 14:59:41 +00:00
|
|
|
|
2018-02-13 16:08:23 +00:00
|
|
|
|
2020-06-04 12:28:16 +00:00
|
|
|
class Tacotron(TacotronAbstract):
|
2018-08-02 14:34:17 +00:00
|
|
|
def __init__(self,
|
2019-01-21 13:52:40 +00:00
|
|
|
num_chars,
|
2019-06-26 10:59:14 +00:00
|
|
|
num_speakers,
|
2019-05-27 12:40:28 +00:00
|
|
|
r=5,
|
2019-10-28 13:51:19 +00:00
|
|
|
postnet_output_dim=1025,
|
|
|
|
decoder_output_dim=80,
|
2019-10-31 15:24:09 +00:00
|
|
|
attn_type='original',
|
2019-03-25 23:52:47 +00:00
|
|
|
attn_win=False,
|
2019-05-17 14:15:43 +00:00
|
|
|
attn_norm="sigmoid",
|
2019-05-27 12:40:28 +00:00
|
|
|
prenet_type="original",
|
|
|
|
prenet_dropout=True,
|
|
|
|
forward_attn=False,
|
|
|
|
trans_agent=False,
|
2019-06-03 22:39:29 +00:00
|
|
|
forward_attn_mask=False,
|
2019-05-27 12:40:28 +00:00
|
|
|
location_attn=True,
|
2019-10-31 15:24:09 +00:00
|
|
|
attn_K=5,
|
2019-10-28 13:51:19 +00:00
|
|
|
separate_stopnet=True,
|
2020-06-04 12:28:16 +00:00
|
|
|
bidirectional_decoder=False,
|
|
|
|
double_decoder_consistency=False,
|
|
|
|
ddc_r=None,
|
|
|
|
gst=False,
|
|
|
|
memory_size=5):
|
|
|
|
super(Tacotron,
|
|
|
|
self).__init__(num_chars, num_speakers, r, postnet_output_dim,
|
|
|
|
decoder_output_dim, attn_type, attn_win,
|
|
|
|
attn_norm, prenet_type, prenet_dropout,
|
|
|
|
forward_attn, trans_agent, forward_attn_mask,
|
|
|
|
location_attn, attn_K, separate_stopnet,
|
|
|
|
bidirectional_decoder, double_decoder_consistency,
|
|
|
|
ddc_r, gst)
|
|
|
|
decoder_in_features = 512 if num_speakers > 1 else 256
|
|
|
|
encoder_in_features = 512 if num_speakers > 1 else 256
|
|
|
|
speaker_embedding_dim = 256
|
2019-09-12 08:39:15 +00:00
|
|
|
proj_speaker_dim = 80 if num_speakers > 1 else 0
|
2020-06-04 12:28:16 +00:00
|
|
|
# base model layers
|
2020-02-20 11:24:54 +00:00
|
|
|
self.embedding = nn.Embedding(num_chars, 256, padding_idx=0)
|
2019-10-28 13:51:19 +00:00
|
|
|
self.embedding.weight.data.normal_(0, 0.3)
|
2020-06-04 12:28:16 +00:00
|
|
|
self.encoder = Encoder(encoder_in_features)
|
|
|
|
self.decoder = Decoder(decoder_in_features, decoder_output_dim, r, memory_size, attn_type, attn_win,
|
2019-05-27 12:40:28 +00:00
|
|
|
attn_norm, prenet_type, prenet_dropout,
|
2019-06-03 22:39:29 +00:00
|
|
|
forward_attn, trans_agent, forward_attn_mask,
|
2019-10-31 15:24:09 +00:00
|
|
|
location_attn, attn_K, separate_stopnet,
|
2019-09-24 14:18:48 +00:00
|
|
|
proj_speaker_dim)
|
2019-10-28 13:51:19 +00:00
|
|
|
self.postnet = PostCBHG(decoder_output_dim)
|
2019-09-24 14:18:48 +00:00
|
|
|
self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
|
2019-10-28 13:51:19 +00:00
|
|
|
postnet_output_dim)
|
2019-09-24 14:18:48 +00:00
|
|
|
# speaker embedding layers
|
|
|
|
if num_speakers > 1:
|
2020-06-04 12:28:16 +00:00
|
|
|
self.speaker_embedding = nn.Embedding(num_speakers, speaker_embedding_dim)
|
2019-09-24 14:18:48 +00:00
|
|
|
self.speaker_embedding.weight.data.normal_(0, 0.3)
|
|
|
|
self.speaker_project_mel = nn.Sequential(
|
2020-06-04 12:28:16 +00:00
|
|
|
nn.Linear(speaker_embedding_dim, proj_speaker_dim), nn.Tanh())
|
2019-09-24 14:18:48 +00:00
|
|
|
self.speaker_embeddings = None
|
|
|
|
self.speaker_embeddings_projected = None
|
|
|
|
# global style token layers
|
|
|
|
if self.gst:
|
|
|
|
gst_embedding_dim = 256
|
|
|
|
self.gst_layer = GST(num_mel=80,
|
|
|
|
num_heads=4,
|
|
|
|
num_style_tokens=10,
|
|
|
|
embedding_dim=gst_embedding_dim)
|
2020-06-04 12:28:16 +00:00
|
|
|
# backward pass decoder
|
|
|
|
if self.bidirectional_decoder:
|
|
|
|
self._init_backward_decoder()
|
|
|
|
# setup DDC
|
|
|
|
if self.double_decoder_consistency:
|
|
|
|
self._init_coarse_decoder()
|
2019-09-12 08:39:15 +00:00
|
|
|
|
2019-09-24 14:18:48 +00:00
|
|
|
|
2020-06-04 12:28:16 +00:00
|
|
|
def forward(self, characters, text_lengths, mel_specs, mel_lengths=None, speaker_ids=None):
|
2019-10-28 13:51:19 +00:00
|
|
|
"""
|
|
|
|
Shapes:
|
|
|
|
- characters: B x T_in
|
|
|
|
- text_lengths: B
|
|
|
|
- mel_specs: B x T_out x D
|
|
|
|
- speaker_ids: B x 1
|
|
|
|
"""
|
|
|
|
self._init_states()
|
2020-06-04 12:28:16 +00:00
|
|
|
input_mask, output_mask = self.compute_masks(text_lengths, mel_lengths)
|
2019-10-28 13:51:19 +00:00
|
|
|
# B x T_in x embed_dim
|
2018-01-22 14:59:41 +00:00
|
|
|
inputs = self.embedding(characters)
|
2019-10-28 13:51:19 +00:00
|
|
|
# B x speaker_embed_dim
|
2020-06-04 12:28:16 +00:00
|
|
|
if speaker_ids is not None:
|
|
|
|
self.compute_speaker_embedding(speaker_ids)
|
2019-09-12 08:39:15 +00:00
|
|
|
if self.num_speakers > 1:
|
2019-10-28 13:51:19 +00:00
|
|
|
# B x T_in x embed_dim + speaker_embed_dim
|
2019-09-12 08:39:15 +00:00
|
|
|
inputs = self._concat_speaker_embedding(inputs,
|
2019-09-24 14:18:48 +00:00
|
|
|
self.speaker_embeddings)
|
2020-06-04 12:28:16 +00:00
|
|
|
# B x T_in x encoder_in_features
|
2018-02-04 16:25:00 +00:00
|
|
|
encoder_outputs = self.encoder(inputs)
|
2020-06-04 12:28:16 +00:00
|
|
|
# sequence masking
|
|
|
|
encoder_outputs = encoder_outputs * input_mask.unsqueeze(2).expand_as(encoder_outputs)
|
|
|
|
# global style token
|
2019-09-24 14:18:48 +00:00
|
|
|
if self.gst:
|
2019-10-28 13:51:19 +00:00
|
|
|
# B x gst_dim
|
2019-09-24 14:18:48 +00:00
|
|
|
encoder_outputs = self.compute_gst(encoder_outputs, mel_specs)
|
2019-09-12 08:39:15 +00:00
|
|
|
if self.num_speakers > 1:
|
2019-09-24 14:18:48 +00:00
|
|
|
encoder_outputs = self._concat_speaker_embedding(
|
|
|
|
encoder_outputs, self.speaker_embeddings)
|
2020-06-04 12:28:16 +00:00
|
|
|
# decoder_outputs: B x decoder_in_features x T_out
|
|
|
|
# alignments: B x T_in x encoder_in_features
|
2019-10-28 13:51:19 +00:00
|
|
|
# stop_tokens: B x T_in
|
|
|
|
decoder_outputs, alignments, stop_tokens = self.decoder(
|
2020-06-04 12:28:16 +00:00
|
|
|
encoder_outputs, mel_specs, input_mask,
|
2019-09-24 14:18:48 +00:00
|
|
|
self.speaker_embeddings_projected)
|
2020-06-04 12:28:16 +00:00
|
|
|
# sequence masking
|
|
|
|
if output_mask is not None:
|
|
|
|
decoder_outputs = decoder_outputs * output_mask.unsqueeze(1).expand_as(decoder_outputs)
|
|
|
|
# B x T_out x decoder_in_features
|
2019-10-28 13:51:19 +00:00
|
|
|
postnet_outputs = self.postnet(decoder_outputs)
|
2020-06-04 12:28:16 +00:00
|
|
|
# sequence masking
|
|
|
|
if output_mask is not None:
|
|
|
|
postnet_outputs = postnet_outputs * output_mask.unsqueeze(2).expand_as(postnet_outputs)
|
2019-10-28 13:51:19 +00:00
|
|
|
# B x T_out x posnet_dim
|
|
|
|
postnet_outputs = self.last_linear(postnet_outputs)
|
2020-06-04 12:28:16 +00:00
|
|
|
# B x T_out x decoder_in_features
|
2019-10-29 01:58:42 +00:00
|
|
|
decoder_outputs = decoder_outputs.transpose(1, 2).contiguous()
|
2019-10-28 13:51:19 +00:00
|
|
|
if self.bidirectional_decoder:
|
2020-06-04 12:28:16 +00:00
|
|
|
decoder_outputs_backward, alignments_backward = self._backward_pass(mel_specs, encoder_outputs, input_mask)
|
2019-10-28 13:51:19 +00:00
|
|
|
return decoder_outputs, postnet_outputs, alignments, stop_tokens, decoder_outputs_backward, alignments_backward
|
2020-06-04 12:28:16 +00:00
|
|
|
if self.double_decoder_consistency:
|
|
|
|
decoder_outputs_backward, alignments_backward = self._coarse_decoder_pass(mel_specs, encoder_outputs, alignments, input_mask)
|
|
|
|
return decoder_outputs, postnet_outputs, alignments, stop_tokens, decoder_outputs_backward, alignments_backward
|
2019-10-28 13:51:19 +00:00
|
|
|
return decoder_outputs, postnet_outputs, alignments, stop_tokens
|
2019-03-05 12:25:50 +00:00
|
|
|
|
2020-02-12 09:29:30 +00:00
|
|
|
@torch.no_grad()
|
2019-09-24 14:18:48 +00:00
|
|
|
def inference(self, characters, speaker_ids=None, style_mel=None):
|
2019-03-05 12:25:50 +00:00
|
|
|
inputs = self.embedding(characters)
|
2019-09-12 21:06:59 +00:00
|
|
|
self._init_states()
|
2020-06-04 12:28:16 +00:00
|
|
|
if speaker_ids is not None:
|
|
|
|
self.compute_speaker_embedding(speaker_ids)
|
2019-09-12 08:39:15 +00:00
|
|
|
if self.num_speakers > 1:
|
|
|
|
inputs = self._concat_speaker_embedding(inputs,
|
2019-09-24 14:18:48 +00:00
|
|
|
self.speaker_embeddings)
|
2019-10-29 13:28:49 +00:00
|
|
|
encoder_outputs = self.encoder(inputs)
|
2019-09-24 14:18:48 +00:00
|
|
|
if self.gst and style_mel is not None:
|
|
|
|
encoder_outputs = self.compute_gst(encoder_outputs, style_mel)
|
2019-09-12 08:39:15 +00:00
|
|
|
if self.num_speakers > 1:
|
2019-09-24 14:18:48 +00:00
|
|
|
encoder_outputs = self._concat_speaker_embedding(
|
|
|
|
encoder_outputs, self.speaker_embeddings)
|
2019-10-28 13:51:19 +00:00
|
|
|
decoder_outputs, alignments, stop_tokens = self.decoder.inference(
|
2019-09-12 08:39:15 +00:00
|
|
|
encoder_outputs, self.speaker_embeddings_projected)
|
2019-10-28 13:51:19 +00:00
|
|
|
postnet_outputs = self.postnet(decoder_outputs)
|
|
|
|
postnet_outputs = self.last_linear(postnet_outputs)
|
2019-10-29 01:58:42 +00:00
|
|
|
decoder_outputs = decoder_outputs.transpose(1, 2)
|
2019-10-28 13:51:19 +00:00
|
|
|
return decoder_outputs, postnet_outputs, alignments, stop_tokens
|