TTS/models/tacotron.py

49 lines
1.6 KiB
Python
Raw Normal View History

2018-01-22 14:59:41 +00:00
# coding: utf-8
import torch
from torch.autograd import Variable
from torch import nn
from TTS.utils.text.symbols import symbols
from TTS.layers.tacotron import Prenet, Encoder, Decoder, CBHG
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):
def __init__(self, embedding_dim=256, linear_dim=1025, mel_dim=80,
2018-03-19 15:26:16 +00:00
freq_dim=1025, r=5, padding_idx=None):
2018-01-22 14:59:41 +00:00
super(Tacotron, self).__init__()
self.mel_dim = mel_dim
self.linear_dim = linear_dim
self.embedding = nn.Embedding(len(symbols), embedding_dim,
padding_idx=padding_idx)
2018-02-01 16:26:40 +00:00
print(" | > Embedding dim : {}".format(len(symbols)))
2018-01-22 14:59:41 +00:00
# Trying smaller std
self.embedding.weight.data.normal_(0, 0.3)
self.encoder = Encoder(embedding_dim)
2018-02-13 09:45:52 +00:00
self.decoder = Decoder(256, mel_dim, r)
2018-01-22 14:59:41 +00:00
self.postnet = CBHG(mel_dim, K=8, projections=[256, mel_dim])
self.last_linear = nn.Linear(mel_dim * 2, freq_dim)
2018-03-19 15:26:16 +00:00
def forward(self, characters, mel_specs=None):
2018-01-22 14:59:41 +00:00
B = characters.size(0)
inputs = self.embedding(characters)
# (B, T', in_dim)
encoder_outputs = self.encoder(inputs)
2018-01-22 14:59:41 +00:00
# (B, T', mel_dim*r)
mel_outputs, alignments = self.decoder(
2018-02-23 16:35:53 +00:00
encoder_outputs, mel_specs, input_lengths=input_lengths)
2018-01-22 14:59:41 +00:00
# Post net processing below
# Reshape
# (B, T, mel_dim)
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