Exemplo n.º 1
0
 def __init__(self,
              num_chars,
              num_speakers,
              r=5,
              linear_dim=1025,
              mel_dim=80,
              memory_size=5,
              attn_win=False,
              attn_norm="sigmoid",
              prenet_type="original",
              prenet_dropout=True,
              forward_attn=False,
              trans_agent=False,
              forward_attn_mask=False,
              location_attn=True,
              separate_stopnet=True):
     super(Tacotron, self).__init__()
     self.r = r
     self.mel_dim = mel_dim
     self.linear_dim = linear_dim
     self.embedding = nn.Embedding(num_chars, 256)
     self.embedding.weight.data.normal_(0, 0.3)
     if num_speakers > 1:
         self.speaker_embedding = nn.Embedding(num_speakers, 256)
         self.speaker_embedding.weight.data.normal_(0, 0.3)
     self.encoder = Encoder(256)
     self.decoder = Decoder(256, mel_dim, r, memory_size, attn_win,
                            attn_norm, prenet_type, prenet_dropout,
                            forward_attn, trans_agent, forward_attn_mask,
                            location_attn, separate_stopnet)
     self.postnet = PostCBHG(mel_dim)
     self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
                                  linear_dim)
Exemplo n.º 2
0
 def __init__(self,
              num_chars,
              num_speakers,
              r=5,
              postnet_output_dim=1025,
              decoder_output_dim=80,
              memory_size=5,
              attn_type='original',
              attn_win=False,
              gst=False,
              attn_norm="sigmoid",
              prenet_type="original",
              prenet_dropout=True,
              forward_attn=False,
              trans_agent=False,
              forward_attn_mask=False,
              location_attn=True,
              attn_K=5,
              separate_stopnet=True,
              bidirectional_decoder=False):
     super(Tacotron, self).__init__()
     self.r = r
     self.decoder_output_dim = decoder_output_dim
     self.postnet_output_dim = postnet_output_dim
     self.gst = gst
     self.num_speakers = num_speakers
     self.bidirectional_decoder = bidirectional_decoder
     decoder_dim = 512 if num_speakers > 1 else 256
     encoder_dim = 512 if num_speakers > 1 else 256
     proj_speaker_dim = 80 if num_speakers > 1 else 0
     # embedding layer
     self.embedding = nn.Embedding(num_chars, 256)
     self.embedding.weight.data.normal_(0, 0.3)
     # boilerplate model
     self.encoder = Encoder(encoder_dim)
     self.decoder = Decoder(decoder_dim, decoder_output_dim, r, memory_size,
                            attn_type, attn_win, attn_norm, prenet_type,
                            prenet_dropout, forward_attn, trans_agent,
                            forward_attn_mask, location_attn, attn_K,
                            separate_stopnet, proj_speaker_dim)
     if self.bidirectional_decoder:
         self.decoder_backward = copy.deepcopy(self.decoder)
     self.postnet = PostCBHG(decoder_output_dim)
     self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
                                  postnet_output_dim)
     # speaker embedding layers
     if num_speakers > 1:
         self.speaker_embedding = nn.Embedding(num_speakers, 256)
         self.speaker_embedding.weight.data.normal_(0, 0.3)
         self.speaker_project_mel = nn.Sequential(
             nn.Linear(256, proj_speaker_dim), nn.Tanh())
         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)
Exemplo n.º 3
0
    def test_in_out():
        layer = Decoder(in_features=256,
                        memory_dim=80,
                        r=2,
                        memory_size=4,
                        attn_windowing=False,
                        attn_norm="sigmoid",
                        prenet_type='original',
                        prenet_dropout=True,
                        forward_attn=True,
                        trans_agent=True,
                        forward_attn_mask=True,
                        location_attn=True,
                        separate_stopnet=True)
        dummy_input = T.rand(4, 8, 256)
        dummy_memory = T.rand(4, 2, 80)

        output, alignment, stop_tokens = layer(dummy_input,
                                               dummy_memory,
                                               mask=None)

        assert output.shape[0] == 4
        assert output.shape[1] == 1, "size not {}".format(output.shape[1])
        assert output.shape[2] == 80 * 2, "size not {}".format(output.shape[2])
        assert stop_tokens.shape[0] == 4
Exemplo n.º 4
0
    def test_in_out(self):
        layer = Decoder(in_features=256, memory_dim=80, r=2)
        dummy_input = T.autograd.Variable(T.rand(4, 8, 256))
        dummy_memory = T.autograd.Variable(T.rand(4, 2, 80))

        output, alignment = layer(dummy_input, dummy_memory)

        assert output.shape[0] == 4
        assert output.shape[1] == 1, "size not {}".format(output.shape[1])
        assert output.shape[2] == 80 * 2, "size not {}".format(output.shape[2])
Exemplo n.º 5
0
    def test_in_out(self):
        layer = Decoder(in_features=128, memory_dim=32, r=5)
        dummy_input = T.autograd.Variable(T.rand(4, 8, 128))
        dummy_memory = T.autograd.Variable(T.rand(4, 120, 32))

        print(layer)
        output, alignment = layer(dummy_input, dummy_memory)
        print(output.shape)
        assert output.shape[0] == 4
        assert output.shape[1] == 120 / 5
        assert output.shape[2] == 32 * 5
Exemplo n.º 6
0
 def __init__(self,
              num_chars,
              linear_dim=1025,
              mel_dim=80,
              r=5,
              padding_idx=None,
              memory_size=5,
              attn_win=False,
              attn_norm="sigmoid"):
     super(Tacotron, self).__init__()
     self.r = r
     self.mel_dim = mel_dim
     self.linear_dim = linear_dim
     self.embedding = nn.Embedding(num_chars, 256, padding_idx=padding_idx)
     self.embedding.weight.data.normal_(0, 0.3)
     self.encoder = Encoder(256)
     self.decoder = Decoder(256, mel_dim, r, memory_size, attn_win,
                            attn_norm)
     self.postnet = PostCBHG(mel_dim)
     self.last_linear = nn.Sequential(
         nn.Linear(self.postnet.cbhg.gru_features * 2, linear_dim),
         nn.Sigmoid())
Exemplo n.º 7
0
    def test_in_out(self):
        layer = Decoder(in_features=256, memory_dim=80, r=2)
        dummy_input = T.rand(4, 8, 256)
        dummy_memory = T.rand(4, 2, 80)

        output, alignment, stop_tokens = layer(dummy_input, dummy_memory)

        assert output.shape[0] == 4
        assert output.shape[1] == 1, "size not {}".format(output.shape[1])
        assert output.shape[2] == 80 * 2, "size not {}".format(output.shape[2])
        assert stop_tokens.shape[0] == 4
        assert stop_tokens.max() <= 1.0
        assert stop_tokens.min() >= 0
Exemplo n.º 8
0
 def __init__(self, embedding_dim=256, linear_dim=1025, mel_dim=80,
              r=5, padding_idx=None):
     super(Tacotron, self).__init__()
     self.r = r
     self.mel_dim = mel_dim
     self.linear_dim = linear_dim
     self.embedding = nn.Embedding(len(symbols), embedding_dim,
                                   padding_idx=padding_idx)
     print(" | > Embedding dim : {}".format(len(symbols)))
     self.embedding.weight.data.normal_(0, 0.3)
     self.encoder = Encoder(embedding_dim)
     self.decoder = Decoder(256, mel_dim, r)
     self.postnet = CBHG(mel_dim, K=8, projections=[256, mel_dim])
     self.last_linear = nn.Linear(mel_dim * 2, linear_dim)
Exemplo n.º 9
0
class Tacotron(nn.Module):
    def __init__(self,
                 num_chars,
                 linear_dim=1025,
                 mel_dim=80,
                 r=5,
                 padding_idx=None,
                 memory_size=5,
                 attn_win=False,
                 attn_norm="sigmoid"):
        super(Tacotron, self).__init__()
        self.r = r
        self.mel_dim = mel_dim
        self.linear_dim = linear_dim
        self.embedding = nn.Embedding(num_chars, 256, padding_idx=padding_idx)
        self.embedding.weight.data.normal_(0, 0.3)
        self.encoder = Encoder(256)
        self.decoder = Decoder(256, mel_dim, r, memory_size, attn_win,
                               attn_norm)
        self.postnet = PostCBHG(mel_dim)
        self.last_linear = nn.Sequential(
            nn.Linear(self.postnet.cbhg.gru_features * 2, linear_dim),
            nn.Sigmoid())

    def forward(self, characters, text_lengths, mel_specs):
        B = characters.size(0)
        mask = sequence_mask(text_lengths).to(characters.device)
        inputs = self.embedding(characters)
        encoder_outputs = self.encoder(inputs)
        mel_outputs, alignments, stop_tokens = self.decoder(
            encoder_outputs, mel_specs, mask)
        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

    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
Exemplo n.º 10
0
 def __init__(self,
              embedding_dim=256,
              linear_dim=1025,
              mel_dim=80,
              r=5,
              padding_idx=None):
     super(Tacotron, self).__init__()
     self.r = r
     self.mel_dim = mel_dim
     self.linear_dim = linear_dim
     self.embedding = nn.Embedding(
         len(symbols), embedding_dim, padding_idx=padding_idx)
     print(" | > Number of characters : {}".format(len(symbols)))
     self.embedding.weight.data.normal_(0, 0.3)
     self.encoder = Encoder(embedding_dim)
     self.decoder = Decoder(256, mel_dim, r)
     self.postnet = PostCBHG(mel_dim)
     self.last_linear = nn.Sequential(
         nn.Linear(self.postnet.cbhg.gru_features * 2, linear_dim),
         nn.Sigmoid())
Exemplo n.º 11
0
class Tacotron(nn.Module):
    def __init__(self,
                 num_chars,
                 num_speakers,
                 r=5,
                 linear_dim=1025,
                 mel_dim=80,
                 memory_size=5,
                 attn_win=False,
                 gst=False,
                 attn_norm="sigmoid",
                 prenet_type="original",
                 prenet_dropout=True,
                 forward_attn=False,
                 trans_agent=False,
                 forward_attn_mask=False,
                 location_attn=True,
                 separate_stopnet=True):
        super(Tacotron, self).__init__()
        self.r = r
        self.mel_dim = mel_dim
        self.linear_dim = linear_dim
        self.gst = gst
        self.num_speakers = num_speakers
        self.embedding = nn.Embedding(num_chars, 256)
        self.embedding.weight.data.normal_(0, 0.3)
        decoder_dim = 512 if num_speakers > 1 else 256
        encoder_dim = 512 if num_speakers > 1 else 256
        proj_speaker_dim = 80 if num_speakers > 1 else 0
        # boilerplate model
        self.encoder = Encoder(encoder_dim)
        self.decoder = Decoder(decoder_dim, mel_dim, r, memory_size, attn_win,
                               attn_norm, prenet_type, prenet_dropout,
                               forward_attn, trans_agent, forward_attn_mask,
                               location_attn, separate_stopnet,
                               proj_speaker_dim)
        self.postnet = PostCBHG(mel_dim)
        self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
                                     linear_dim)
        # speaker embedding layers
        if num_speakers > 1:
            self.speaker_embedding = nn.Embedding(num_speakers, 256)
            self.speaker_embedding.weight.data.normal_(0, 0.3)
            self.speaker_project_mel = nn.Sequential(
                nn.Linear(256, proj_speaker_dim), nn.Tanh())
            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)

    def _init_states(self):
        self.speaker_embeddings = None
        self.speaker_embeddings_projected = None

    def compute_speaker_embedding(self, speaker_ids):
        if hasattr(self, "speaker_embedding") and speaker_ids is None:
            raise RuntimeError(
                " [!] Model has speaker embedding layer but speaker_id is not provided"
            )
        if hasattr(self, "speaker_embedding") and speaker_ids is not None:
            self.speaker_embeddings = self._compute_speaker_embedding(
                speaker_ids)
            self.speaker_embeddings_projected = self.speaker_project_mel(
                self.speaker_embeddings).squeeze(1)

    def compute_gst(self, inputs, mel_specs):
        gst_outputs = self.gst_layer(mel_specs)
        inputs = self._add_speaker_embedding(inputs, gst_outputs)
        return inputs

    def forward(self, characters, text_lengths, mel_specs, speaker_ids=None):
        B = characters.size(0)
        mask = sequence_mask(text_lengths).to(characters.device)
        inputs = self.embedding(characters)
        self._init_states()
        self.compute_speaker_embedding(speaker_ids)
        if self.num_speakers > 1:
            inputs = self._concat_speaker_embedding(inputs,
                                                    self.speaker_embeddings)
        encoder_outputs = self.encoder(inputs)
        if self.gst:
            encoder_outputs = self.compute_gst(encoder_outputs, mel_specs)
        if self.num_speakers > 1:
            encoder_outputs = self._concat_speaker_embedding(
                encoder_outputs, self.speaker_embeddings)
        mel_outputs, alignments, stop_tokens = self.decoder(
            encoder_outputs, mel_specs, mask,
            self.speaker_embeddings_projected)
        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

    def inference(self, characters, speaker_ids=None, style_mel=None):
        B = characters.size(0)
        inputs = self.embedding(characters)
        self._init_states()
        self.compute_speaker_embedding(speaker_ids)
        if self.num_speakers > 1:
            inputs = self._concat_speaker_embedding(inputs,
                                                    self.speaker_embeddings)
        encoder_outputs = self.encoder(inputs)
        if self.gst and style_mel is not None:
            encoder_outputs = self.compute_gst(encoder_outputs, style_mel)
        if self.num_speakers > 1:
            encoder_outputs = self._concat_speaker_embedding(
                encoder_outputs, self.speaker_embeddings)
        mel_outputs, alignments, stop_tokens = self.decoder.inference(
            encoder_outputs, self.speaker_embeddings_projected)
        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

    def _compute_speaker_embedding(self, speaker_ids):
        speaker_embeddings = self.speaker_embedding(speaker_ids)
        return speaker_embeddings.unsqueeze_(1)

    @staticmethod
    def _add_speaker_embedding(outputs, speaker_embeddings):
        speaker_embeddings_ = speaker_embeddings.expand(
            outputs.size(0), outputs.size(1), -1)
        outputs = outputs + speaker_embeddings_
        return outputs

    @staticmethod
    def _concat_speaker_embedding(outputs, speaker_embeddings):
        speaker_embeddings_ = speaker_embeddings.expand(
            outputs.size(0), outputs.size(1), -1)
        outputs = torch.cat([outputs, speaker_embeddings_], dim=-1)
        return outputs
Exemplo n.º 12
0
class Tacotron(nn.Module):
    def __init__(self,
                 num_chars,
                 num_speakers,
                 r=5,
                 postnet_output_dim=1025,
                 decoder_output_dim=80,
                 memory_size=5,
                 attn_type='original',
                 attn_win=False,
                 gst=False,
                 attn_norm="sigmoid",
                 prenet_type="original",
                 prenet_dropout=True,
                 forward_attn=False,
                 trans_agent=False,
                 forward_attn_mask=False,
                 location_attn=True,
                 attn_K=5,
                 separate_stopnet=True,
                 bidirectional_decoder=False):
        super(Tacotron, self).__init__()
        self.r = r
        self.decoder_output_dim = decoder_output_dim
        self.postnet_output_dim = postnet_output_dim
        self.gst = gst
        self.num_speakers = num_speakers
        self.bidirectional_decoder = bidirectional_decoder
        decoder_dim = 512 if num_speakers > 1 else 256
        encoder_dim = 512 if num_speakers > 1 else 256
        proj_speaker_dim = 80 if num_speakers > 1 else 0
        # embedding layer
        self.embedding = nn.Embedding(num_chars, 256)
        self.embedding.weight.data.normal_(0, 0.3)
        # boilerplate model
        self.encoder = Encoder(encoder_dim)
        self.decoder = Decoder(decoder_dim, decoder_output_dim, r, memory_size,
                               attn_type, attn_win, attn_norm, prenet_type,
                               prenet_dropout, forward_attn, trans_agent,
                               forward_attn_mask, location_attn, attn_K,
                               separate_stopnet, proj_speaker_dim)
        if self.bidirectional_decoder:
            self.decoder_backward = copy.deepcopy(self.decoder)
        self.postnet = PostCBHG(decoder_output_dim)
        self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
                                     postnet_output_dim)
        # speaker embedding layers
        if num_speakers > 1:
            self.speaker_embedding = nn.Embedding(num_speakers, 256)
            self.speaker_embedding.weight.data.normal_(0, 0.3)
            self.speaker_project_mel = nn.Sequential(
                nn.Linear(256, proj_speaker_dim), nn.Tanh())
            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)

    def _init_states(self):
        self.speaker_embeddings = None
        self.speaker_embeddings_projected = None

    def compute_speaker_embedding(self, speaker_ids):
        if hasattr(self, "speaker_embedding") and speaker_ids is None:
            raise RuntimeError(
                " [!] Model has speaker embedding layer but speaker_id is not provided"
            )
        if hasattr(self, "speaker_embedding") and speaker_ids is not None:
            self.speaker_embeddings = self._compute_speaker_embedding(
                speaker_ids)
            self.speaker_embeddings_projected = self.speaker_project_mel(
                self.speaker_embeddings).squeeze(1)

    def compute_gst(self, inputs, mel_specs):
        gst_outputs = self.gst_layer(mel_specs)
        inputs = self._add_speaker_embedding(inputs, gst_outputs)
        return inputs

    def forward(self, characters, text_lengths, mel_specs, speaker_ids=None):
        """
        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()
        mask = sequence_mask(text_lengths).to(characters.device)
        # B x T_in x embed_dim
        inputs = self.embedding(characters)
        # B x speaker_embed_dim
        self.compute_speaker_embedding(speaker_ids)
        if self.num_speakers > 1:
            # B x T_in x embed_dim + speaker_embed_dim
            inputs = self._concat_speaker_embedding(inputs,
                                                    self.speaker_embeddings)
        # B x T_in x encoder_dim
        encoder_outputs = self.encoder(inputs)
        if self.gst:
            # B x gst_dim
            encoder_outputs = self.compute_gst(encoder_outputs, mel_specs)
        if self.num_speakers > 1:
            encoder_outputs = self._concat_speaker_embedding(
                encoder_outputs, self.speaker_embeddings)
        # decoder_outputs: B x decoder_dim x T_out
        # alignments: B x T_in x encoder_dim
        # stop_tokens: B x T_in
        decoder_outputs, alignments, stop_tokens = self.decoder(
            encoder_outputs, mel_specs, mask,
            self.speaker_embeddings_projected)
        # B x T_out x decoder_dim
        postnet_outputs = self.postnet(decoder_outputs)
        # B x T_out x posnet_dim
        postnet_outputs = self.last_linear(postnet_outputs)
        # B x T_out x decoder_dim
        decoder_outputs = decoder_outputs.transpose(1, 2).contiguous()
        if self.bidirectional_decoder:
            decoder_outputs_backward, alignments_backward = self._backward_inference(
                mel_specs, encoder_outputs, mask)
            return decoder_outputs, postnet_outputs, alignments, stop_tokens, decoder_outputs_backward, alignments_backward
        return decoder_outputs, postnet_outputs, alignments, stop_tokens

    def inference(self, characters, speaker_ids=None, style_mel=None):
        inputs = self.embedding(characters)
        self._init_states()
        self.compute_speaker_embedding(speaker_ids)
        if self.num_speakers > 1:
            inputs = self._concat_speaker_embedding(inputs,
                                                    self.speaker_embeddings)
        encoder_outputs = self.encoder(inputs)
        if self.gst and style_mel is not None:
            encoder_outputs = self.compute_gst(encoder_outputs, style_mel)
        if self.num_speakers > 1:
            encoder_outputs = self._concat_speaker_embedding(
                encoder_outputs, self.speaker_embeddings)
        decoder_outputs, alignments, stop_tokens = self.decoder.inference(
            encoder_outputs, self.speaker_embeddings_projected)
        postnet_outputs = self.postnet(decoder_outputs)
        postnet_outputs = self.last_linear(postnet_outputs)
        decoder_outputs = decoder_outputs.transpose(1, 2)
        return decoder_outputs, postnet_outputs, alignments, stop_tokens

    def _backward_inference(self, mel_specs, encoder_outputs, mask):
        decoder_outputs_b, alignments_b, _ = self.decoder_backward(
            encoder_outputs, torch.flip(mel_specs, dims=(1, )), mask,
            self.speaker_embeddings_projected)
        decoder_outputs_b = decoder_outputs_b.transpose(1, 2).contiguous()
        return decoder_outputs_b, alignments_b

    def _compute_speaker_embedding(self, speaker_ids):
        speaker_embeddings = self.speaker_embedding(speaker_ids)
        return speaker_embeddings.unsqueeze_(1)

    @staticmethod
    def _add_speaker_embedding(outputs, speaker_embeddings):
        speaker_embeddings_ = speaker_embeddings.expand(
            outputs.size(0), outputs.size(1), -1)
        outputs = outputs + speaker_embeddings_
        return outputs

    @staticmethod
    def _concat_speaker_embedding(outputs, speaker_embeddings):
        speaker_embeddings_ = speaker_embeddings.expand(
            outputs.size(0), outputs.size(1), -1)
        outputs = torch.cat([outputs, speaker_embeddings_], dim=-1)
        return outputs
Exemplo n.º 13
0
class Tacotron(nn.Module):
    def __init__(self,
                 num_chars,
                 num_speakers,
                 r=5,
                 linear_dim=1025,
                 mel_dim=80,
                 memory_size=5,
                 attn_win=False,
                 attn_norm="sigmoid",
                 prenet_type="original",
                 prenet_dropout=True,
                 forward_attn=False,
                 trans_agent=False,
                 forward_attn_mask=False,
                 location_attn=True,
                 separate_stopnet=True):
        super(Tacotron, self).__init__()
        self.r = r
        self.mel_dim = mel_dim
        self.linear_dim = linear_dim
        self.embedding = nn.Embedding(num_chars, 256)
        self.embedding.weight.data.normal_(0, 0.3)
        if num_speakers > 1:
            self.speaker_embedding = nn.Embedding(num_speakers, 256)
            self.speaker_embedding.weight.data.normal_(0, 0.3)
        self.encoder = Encoder(256)
        self.decoder = Decoder(256, mel_dim, r, memory_size, attn_win,
                               attn_norm, prenet_type, prenet_dropout,
                               forward_attn, trans_agent, forward_attn_mask,
                               location_attn, separate_stopnet)
        self.postnet = PostCBHG(mel_dim)
        self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
                                     linear_dim)

    def forward(self, characters, text_lengths, mel_specs, speaker_ids=None):
        B = characters.size(0)
        mask = sequence_mask(text_lengths).to(characters.device)
        inputs = self.embedding(characters)
        encoder_outputs = self.encoder(inputs)
        encoder_outputs = self._add_speaker_embedding(encoder_outputs,
                                                      speaker_ids)
        mel_outputs, alignments, stop_tokens = self.decoder(
            encoder_outputs, mel_specs, mask)
        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

    def inference(self, characters, speaker_ids=None):
        B = characters.size(0)
        inputs = self.embedding(characters)
        encoder_outputs = self.encoder(inputs)
        encoder_outputs = self._add_speaker_embedding(encoder_outputs,
                                                      speaker_ids)
        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

    def _add_speaker_embedding(self, encoder_outputs, speaker_ids):
        if hasattr(self, "speaker_embedding") and speaker_ids is None:
            raise RuntimeError(
                " [!] Model has speaker embedding layer but speaker_id is not provided"
            )
        if hasattr(self, "speaker_embedding") and speaker_ids is not None:
            speaker_embeddings = self.speaker_embedding(speaker_ids)

            speaker_embeddings.unsqueeze_(1)
            speaker_embeddings = speaker_embeddings.expand(
                encoder_outputs.size(0), encoder_outputs.size(1), -1)
            encoder_outputs = encoder_outputs + speaker_embeddings
        return encoder_outputs
Exemplo n.º 14
0
class Tacotron(TacotronAbstract):
    def __init__(self,
                 num_chars,
                 num_speakers,
                 r=5,
                 postnet_output_dim=1025,
                 decoder_output_dim=80,
                 attn_type='original',
                 attn_win=False,
                 attn_norm="sigmoid",
                 prenet_type="original",
                 prenet_dropout=True,
                 forward_attn=False,
                 trans_agent=False,
                 forward_attn_mask=False,
                 location_attn=True,
                 attn_K=5,
                 separate_stopnet=True,
                 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
        proj_speaker_dim = 80 if num_speakers > 1 else 0
        # base model layers
        self.embedding = nn.Embedding(num_chars, 256, padding_idx=0)
        self.embedding.weight.data.normal_(0, 0.3)
        self.encoder = Encoder(encoder_in_features)
        self.decoder = Decoder(decoder_in_features, decoder_output_dim, r,
                               memory_size, attn_type, attn_win, attn_norm,
                               prenet_type, prenet_dropout, forward_attn,
                               trans_agent, forward_attn_mask, location_attn,
                               attn_K, separate_stopnet, proj_speaker_dim)
        self.postnet = PostCBHG(decoder_output_dim)
        self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
                                     postnet_output_dim)
        # speaker embedding layers
        if num_speakers > 1:
            self.speaker_embedding = nn.Embedding(num_speakers, speaker_embedding_dim)
            self.speaker_embedding.weight.data.normal_(0, 0.3)
            self.speaker_project_mel = nn.Sequential(
                nn.Linear(speaker_embedding_dim, proj_speaker_dim), nn.Tanh())
            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)
        # backward pass decoder
        if self.bidirectional_decoder:
            self._init_backward_decoder()
        # setup DDC
        if self.double_decoder_consistency:
            self.coarse_decoder = Decoder(
                decoder_in_features, decoder_output_dim, ddc_r, memory_size,
                attn_type, attn_win, attn_norm, prenet_type, prenet_dropout,
                forward_attn, trans_agent, forward_attn_mask, location_attn,
                attn_K, separate_stopnet, proj_speaker_dim)


    def forward(self, characters, text_lengths, mel_specs, mel_lengths=None, speaker_ids=None):
        """
        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()
        input_mask, output_mask = self.compute_masks(text_lengths, mel_lengths)
        # B x T_in x embed_dim
        inputs = self.embedding(characters)
        # B x speaker_embed_dim
        if speaker_ids is not None:
            self.compute_speaker_embedding(speaker_ids)
        if self.num_speakers > 1:
            # B x T_in x embed_dim + speaker_embed_dim
            inputs = self._concat_speaker_embedding(inputs,
                                                    self.speaker_embeddings)
        # B x T_in x encoder_in_features
        encoder_outputs = self.encoder(inputs)
        # sequence masking
        encoder_outputs = encoder_outputs * input_mask.unsqueeze(2).expand_as(encoder_outputs)
        # global style token
        if self.gst:
            # B x gst_dim
            encoder_outputs = self.compute_gst(encoder_outputs, mel_specs)
        if self.num_speakers > 1:
            encoder_outputs = self._concat_speaker_embedding(
                encoder_outputs, self.speaker_embeddings)
        # decoder_outputs: B x decoder_in_features x T_out
        # alignments: B x T_in x encoder_in_features
        # stop_tokens: B x T_in
        decoder_outputs, alignments, stop_tokens = self.decoder(
            encoder_outputs, mel_specs, input_mask,
            self.speaker_embeddings_projected)
        # 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
        postnet_outputs = self.postnet(decoder_outputs)
        # sequence masking
        if output_mask is not None:
            postnet_outputs = postnet_outputs * output_mask.unsqueeze(2).expand_as(postnet_outputs)
        # B x T_out x posnet_dim
        postnet_outputs = self.last_linear(postnet_outputs)
        # B x T_out x decoder_in_features
        decoder_outputs = decoder_outputs.transpose(1, 2).contiguous()
        if self.bidirectional_decoder:
            decoder_outputs_backward, alignments_backward = self._backward_pass(mel_specs, encoder_outputs, input_mask)
            return decoder_outputs, postnet_outputs, alignments, stop_tokens, decoder_outputs_backward, alignments_backward
        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
        return decoder_outputs, postnet_outputs, alignments, stop_tokens

    @torch.no_grad()
    def inference(self, characters, speaker_ids=None, style_mel=None):
        inputs = self.embedding(characters)
        self._init_states()
        if speaker_ids is not None:
            self.compute_speaker_embedding(speaker_ids)
        if self.num_speakers > 1:
            inputs = self._concat_speaker_embedding(inputs,
                                                    self.speaker_embeddings)
        encoder_outputs = self.encoder(inputs)
        if self.gst and style_mel is not None:
            encoder_outputs = self.compute_gst(encoder_outputs, style_mel)
        if self.num_speakers > 1:
            encoder_outputs = self._concat_speaker_embedding(
                encoder_outputs, self.speaker_embeddings)
        decoder_outputs, alignments, stop_tokens = self.decoder.inference(
            encoder_outputs, self.speaker_embeddings_projected)
        postnet_outputs = self.postnet(decoder_outputs)
        postnet_outputs = self.last_linear(postnet_outputs)
        decoder_outputs = decoder_outputs.transpose(1, 2)
        return decoder_outputs, postnet_outputs, alignments, stop_tokens
Exemplo n.º 15
0
 def __init__(self,
              num_chars,
              num_speakers,
              r=5,
              postnet_output_dim=1025,
              decoder_output_dim=80,
              attn_type='original',
              attn_win=False,
              attn_norm="sigmoid",
              prenet_type="original",
              prenet_dropout=True,
              forward_attn=False,
              trans_agent=False,
              forward_attn_mask=False,
              location_attn=True,
              attn_K=5,
              separate_stopnet=True,
              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
     proj_speaker_dim = 80 if num_speakers > 1 else 0
     # base model layers
     self.embedding = nn.Embedding(num_chars, 256, padding_idx=0)
     self.embedding.weight.data.normal_(0, 0.3)
     self.encoder = Encoder(encoder_in_features)
     self.decoder = Decoder(decoder_in_features, decoder_output_dim, r,
                            memory_size, attn_type, attn_win, attn_norm,
                            prenet_type, prenet_dropout, forward_attn,
                            trans_agent, forward_attn_mask, location_attn,
                            attn_K, separate_stopnet, proj_speaker_dim)
     self.postnet = PostCBHG(decoder_output_dim)
     self.last_linear = nn.Linear(self.postnet.cbhg.gru_features * 2,
                                  postnet_output_dim)
     # speaker embedding layers
     if num_speakers > 1:
         self.speaker_embedding = nn.Embedding(num_speakers, speaker_embedding_dim)
         self.speaker_embedding.weight.data.normal_(0, 0.3)
         self.speaker_project_mel = nn.Sequential(
             nn.Linear(speaker_embedding_dim, proj_speaker_dim), nn.Tanh())
         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)
     # backward pass decoder
     if self.bidirectional_decoder:
         self._init_backward_decoder()
     # setup DDC
     if self.double_decoder_consistency:
         self.coarse_decoder = Decoder(
             decoder_in_features, decoder_output_dim, ddc_r, memory_size,
             attn_type, attn_win, attn_norm, prenet_type, prenet_dropout,
             forward_attn, trans_agent, forward_attn_mask, location_attn,
             attn_K, separate_stopnet, proj_speaker_dim)