File size: 7,780 Bytes
7ce5feb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

from utils import ConvNorm, LinearNorm
from torch.nn.parameter import Parameter



class GroupNorm_Mask(nn.Module):
    def __init__(self, num_groups, num_channels, eps=1e-5, affine=True):
        super().__init__()
        
        self.num_groups = num_groups
        self.num_channels = num_channels
        self.eps = eps
        self.affine = affine
        if self.affine:
            self.weight = Parameter(torch.Tensor(num_channels))
            self.bias = Parameter(torch.Tensor(num_channels))
        else:
            self.register_parameter('weight', None)
            self.register_parameter('bias', None)
        self.reset_parameters()

    def reset_parameters(self):
        if self.affine:
            nn.init.ones_(self.weight)
            nn.init.zeros_(self.bias)
    
    def forward(self, x, mask):
        B, C, L = x.size()
        assert C % self.num_groups == 0
        
        x = x.view(B, self.num_groups, C//self.num_groups, L)
        mask = mask.view(B, 1, 1, L)
        x = x * mask
        
        mean = x.mean(dim=2, keepdim=True).sum(dim=3, keepdim=True) / mask.sum(dim=3, keepdim=True)
        var = (((x - mean)**2)*mask).mean(dim=2, keepdim=True).sum(dim=3, keepdim=True) / mask.sum(dim=3, keepdim=True)
        
        x = (x - mean) / (var + self.eps).sqrt()
        
        x = x.view(B, C, L)
        
        return x * self.weight.view(1,-1,1) + self.bias.view(1,-1,1)
    
    
    
class M43_Sequential(nn.Sequential):
    def forward(self, inputs, mask):
        inputs = self._modules['0'](inputs)
        inputs = self._modules['1'](inputs, mask)
        return inputs
    


class Encoder(nn.Module):
    """Encoder module:
    """
    def __init__(self, hparams):
        super(Encoder, self).__init__()
        
        self.dim_freq = hparams.dim_freq_sea
        self.dim_enc = hparams.dim_enc_sea
        self.chs_grp = hparams.chs_grp
        self.dim_neck = hparams.dim_neck_sea
        
        convolutions = []        
        for i in range(5):
            conv_layer = M43_Sequential(
                ConvNorm(self.dim_freq if i==0 else self.dim_enc,
                         self.dim_enc,
                         kernel_size=1, stride=1,
                         padding=0,
                         dilation=1, w_init_gain='relu'),
                GroupNorm_Mask(self.dim_enc//self.chs_grp, self.dim_enc))
            convolutions.append(conv_layer)
             
        conv_layer = M43_Sequential(
                ConvNorm(self.dim_enc,
                         128,
                         kernel_size=1, stride=1,
                         padding=0,
                         dilation=1, w_init_gain='relu'),
                GroupNorm_Mask(128//self.chs_grp, 128))
        convolutions.append(conv_layer)   
        
        conv_layer = M43_Sequential(
                ConvNorm(128,
                         32,
                         kernel_size=1, stride=1,
                         padding=0,
                         dilation=1, w_init_gain='relu'),
                GroupNorm_Mask(32//self.chs_grp, 32))
        convolutions.append(conv_layer)           
        
        conv_layer = M43_Sequential(
                ConvNorm(32,
                         self.dim_neck,
                         kernel_size=1, stride=1,
                         padding=0,
                         dilation=1, w_init_gain='relu'),
                GroupNorm_Mask(1, self.dim_neck))
        convolutions.append(conv_layer)   
            
        self.convolutions = nn.ModuleList(convolutions)
        

    def forward(self, x, mask):
                
        for conv in self.convolutions:
            x = F.relu(conv(x, mask))
            
        codes = x.permute(0, 2, 1) * mask.unsqueeze(-1)

        return codes
      
      
        
class Decoder(nn.Module):
    """Decoder module:
    """
    def __init__(self, hparams):
        super(Decoder, self).__init__()
        self.dim_enc = hparams.dim_enc_sea
        self.dim_emb = hparams.dim_spk
        self.dim_freq = hparams.dim_freq_sp
        self.dim_neck = hparams.dim_neck_sea
        
        self.lstm = nn.LSTM(self.dim_neck+self.dim_emb, 
                            1024, 3, batch_first=True)
        
        self.linear_projection = LinearNorm(1024, self.dim_freq)

    def forward(self, x):
        
        outputs = self.lstm(x)[0]
        
        decoder_output = self.linear_projection(outputs)

        return decoder_output   
    

    
    
class Generator(nn.Module):
    """Generator network."""
    def __init__(self, hparams):
        super(Generator, self).__init__()
        
        self.encoder = Encoder(hparams)
        self.decoder = Decoder(hparams)

    def forward(self, x, c_trg):
        
        x = x.transpose(2,1)
        codes = self.encoder(x)
        
        encoder_outputs = torch.cat((codes, 
                                     c_trg.unsqueeze(1).expand(-1,x.size(-1),-1)), dim=-1)
        mel_outputs = self.decoder(encoder_outputs)
        
        return mel_outputs
    
    def encode(self, x, mask):
        x = x.transpose(2,1)
        codes = self.encoder(x, mask)
        return codes
    
    def decode(self, codes, c_trg):
        encoder_outputs = torch.cat((codes, 
                                     c_trg.unsqueeze(1).expand(-1,codes.size(1),-1)), dim=-1)
        mel_outputs = self.decoder(encoder_outputs)
        
        return mel_outputs
    
    
    
class Encoder_2(nn.Module):
    """Encoder module:
    """
    def __init__(self, hparams):
        super().__init__()
        
        self.dim_freq = hparams.dim_freq_sea
        self.dim_enc = hparams.dim_enc_sea
        self.chs_grp = hparams.chs_grp
        self.dim_neck = hparams.dim_neck_sea
        
        convolutions = []        
        for i in range(5):
            conv_layer = M43_Sequential(
                ConvNorm(self.dim_freq if i==0 else self.dim_enc,
                         self.dim_enc,
                         kernel_size=5, stride=1,
                         padding=2,
                         dilation=1, w_init_gain='relu'),
                GroupNorm_Mask(self.dim_enc//self.chs_grp, self.dim_enc))
            convolutions.append(conv_layer)
             
        conv_layer = M43_Sequential(
                ConvNorm(self.dim_enc,
                         128,
                         kernel_size=5, stride=1,
                         padding=2,
                         dilation=1, w_init_gain='relu'),
                GroupNorm_Mask(128//self.chs_grp, 128))
        convolutions.append(conv_layer)   
        
        conv_layer = M43_Sequential(
                ConvNorm(128,
                         32,
                         kernel_size=5, stride=1,
                         padding=2,
                         dilation=1, w_init_gain='relu'),
                GroupNorm_Mask(32//self.chs_grp, 32))
        convolutions.append(conv_layer)           
        
        conv_layer = M43_Sequential(
                ConvNorm(32,
                         self.dim_neck,
                         kernel_size=5, stride=1,
                         padding=2,
                         dilation=1, w_init_gain='linear'),
                GroupNorm_Mask(1, self.dim_neck))
        convolutions.append(conv_layer)   
            
        self.convolutions = nn.ModuleList(convolutions)
        

    def forward(self, x, mask):
                
        for i in range(len(self.convolutions)-1):
            x = F.relu(self.convolutions[i](x, mask))
            
        x = self.convolutions[-1](x, mask)    
            
        codes = x.permute(0, 2, 1) * mask.unsqueeze(-1)

        return codes