2018-01-22 14:59:41 +00:00
|
|
|
# coding: utf-8
|
|
|
|
import torch
|
|
|
|
from torch import nn
|
2019-01-09 15:00:57 +00:00
|
|
|
from math import sqrt
|
2018-08-08 10:34:44 +00:00
|
|
|
from layers.tacotron import Prenet, Encoder, Decoder, PostCBHG
|
2019-03-06 12:14:58 +00:00
|
|
|
from utils.generic_utils import sequence_mask
|
2018-01-22 14:59:41 +00:00
|
|
|
|
2018-02-13 16:08:23 +00:00
|
|
|
|
2018-01-22 14:59:41 +00:00
|
|
|
class Tacotron(nn.Module):
|
2018-08-02 14:34:17 +00:00
|
|
|
def __init__(self,
|
2019-01-21 13:52:40 +00:00
|
|
|
num_chars,
|
2018-08-02 14:34:17 +00:00
|
|
|
linear_dim=1025,
|
|
|
|
mel_dim=80,
|
|
|
|
r=5,
|
2019-03-05 12:25:50 +00:00
|
|
|
padding_idx=None,
|
2019-02-16 02:18:49 +00:00
|
|
|
memory_size=5,
|
2019-03-25 23:52:47 +00:00
|
|
|
attn_win=False,
|
2019-03-25 23:48:12 +00:00
|
|
|
attn_norm="sigmoid"):
|
2018-01-22 14:59:41 +00:00
|
|
|
super(Tacotron, self).__init__()
|
2018-03-22 19:34:16 +00:00
|
|
|
self.r = r
|
2018-01-22 14:59:41 +00:00
|
|
|
self.mel_dim = mel_dim
|
|
|
|
self.linear_dim = linear_dim
|
2019-03-05 12:25:50 +00:00
|
|
|
self.embedding = nn.Embedding(num_chars, 256, padding_idx=padding_idx)
|
2019-01-15 14:51:55 +00:00
|
|
|
self.embedding.weight.data.normal_(0, 0.3)
|
2019-03-05 12:25:50 +00:00
|
|
|
self.encoder = Encoder(256)
|
2019-03-25 23:52:47 +00:00
|
|
|
self.decoder = Decoder(256, mel_dim, r, memory_size, attn_win, attn_norm)
|
2018-08-08 10:34:44 +00:00
|
|
|
self.postnet = PostCBHG(mel_dim)
|
2018-09-06 13:27:15 +00:00
|
|
|
self.last_linear = nn.Sequential(
|
|
|
|
nn.Linear(self.postnet.cbhg.gru_features * 2, linear_dim),
|
|
|
|
nn.Sigmoid())
|
2018-01-22 14:59:41 +00:00
|
|
|
|
2019-03-06 12:43:29 +00:00
|
|
|
def forward(self, characters, text_lengths, mel_specs):
|
2018-01-22 14:59:41 +00:00
|
|
|
B = characters.size(0)
|
2019-03-06 12:14:58 +00:00
|
|
|
mask = sequence_mask(text_lengths).to(characters.device)
|
2018-01-22 14:59:41 +00:00
|
|
|
inputs = self.embedding(characters)
|
2018-02-04 16:25:00 +00:00
|
|
|
encoder_outputs = self.encoder(inputs)
|
2018-05-11 11:15:06 +00:00
|
|
|
mel_outputs, alignments, stop_tokens = self.decoder(
|
2018-08-10 15:43:45 +00:00
|
|
|
encoder_outputs, mel_specs, mask)
|
2018-01-22 14:59:41 +00:00
|
|
|
mel_outputs = mel_outputs.view(B, -1, self.mel_dim)
|
|
|
|
linear_outputs = self.postnet(mel_outputs)
|
|
|
|
linear_outputs = self.last_linear(linear_outputs)
|
2018-05-11 11:15:06 +00:00
|
|
|
return mel_outputs, linear_outputs, alignments, stop_tokens
|
2019-03-05 12:25:50 +00:00
|
|
|
|
|
|
|
def inference(self, characters):
|
|
|
|
B = characters.size(0)
|
|
|
|
inputs = self.embedding(characters)
|
|
|
|
encoder_outputs = self.encoder(inputs)
|
|
|
|
mel_outputs, alignments, stop_tokens = self.decoder.inference(
|
|
|
|
encoder_outputs)
|
|
|
|
mel_outputs = mel_outputs.view(B, -1, self.mel_dim)
|
|
|
|
linear_outputs = self.postnet(mel_outputs)
|
|
|
|
linear_outputs = self.last_linear(linear_outputs)
|
|
|
|
return mel_outputs, linear_outputs, alignments, stop_tokens
|