TTS/models/tacotron.py

48 lines
1.8 KiB
Python
Raw Normal View History

2018-01-22 14:59:41 +00:00
# coding: utf-8
import torch
from torch import nn
from math import sqrt
from utils.text.symbols import symbols, phonemes
from layers.tacotron import Prenet, Encoder, Decoder, PostCBHG
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,
embedding_dim=256,
linear_dim=1025,
mel_dim=80,
r=5,
padding_idx=None,
attn_windowing=False):
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
2018-08-02 14:34:17 +00:00
self.embedding = nn.Embedding(
len(phonemes), embedding_dim, padding_idx=padding_idx)
print(" | > Number of characters : {}".format(len(phonemes)))
std = sqrt(2.0 / (len(phonemes) + embedding_dim))
val = sqrt(3.0) * std # uniform bounds for std
self.embedding.weight.data.uniform_(-val, val)
2018-01-22 14:59:41 +00:00
self.encoder = Encoder(embedding_dim)
self.decoder = Decoder(256, mel_dim, r, attn_windowing)
self.postnet = PostCBHG(mel_dim)
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
2018-08-10 15:43:45 +00:00
def forward(self, characters, mel_specs=None, mask=None):
2018-01-22 14:59:41 +00:00
B = characters.size(0)
inputs = self.embedding(characters)
2018-04-03 10:24:57 +00:00
# batch x time x dim
encoder_outputs = self.encoder(inputs)
2018-04-03 10:24:57 +00:00
# batch x time x dim*r
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
# Reshape
2018-04-03 10:24:57 +00:00
# batch x time x dim
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)
return mel_outputs, linear_outputs, alignments, stop_tokens