-
Notifications
You must be signed in to change notification settings - Fork 45
/
generator.py
270 lines (238 loc) · 9.24 KB
/
generator.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
""" borrow from https://github.com/Sytronik/denoising-wavenet-pytorch/blob/master/model/dwavenet.py.
"""
import math
import torch
from torch import nn
from torch.nn import functional as F
class Postnet(torch.nn.Module):
"""Postnet module for Spectrogram prediction network.
This is a module of Postnet in Spectrogram prediction network,
which described in `Natural TTS Synthesis by
Conditioning WaveNet on Mel Spectrogram Predictions`_.
The Postnet predicts refines the predicted
Mel-filterbank of the decoder,
which helps to compensate the detail sturcture of spectrogram.
.. _`Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions`:
https://arxiv.org/abs/1712.05884
"""
def __init__(
self,
idim: int,
odim: int,
n_layers: int = 5,
n_chans: int = 512,
n_filts: int = 5,
dropout_rate: float = 0.5,
use_batch_norm: bool = True,
):
"""Initialize postnet module.
Args:
idim (int): Dimension of the inputs.
odim (int): Dimension of the outputs.
n_layers (int, optional): The number of layers.
n_filts (int, optional): The number of filter size.
n_units (int, optional): The number of filter channels.
use_batch_norm (bool, optional): Whether to use batch normalization..
dropout_rate (float, optional): Dropout rate..
"""
super(Postnet, self).__init__()
self.postnet = torch.nn.ModuleList()
for layer in range(n_layers - 1):
ichans = odim if layer == 0 else n_chans
ochans = odim if layer == n_layers - 1 else n_chans
if use_batch_norm:
self.postnet += [
torch.nn.Sequential(
torch.nn.Conv1d(
ichans,
ochans,
n_filts,
stride=1,
padding=(n_filts - 1) // 2,
bias=False,
),
torch.nn.BatchNorm1d(ochans),
torch.nn.Tanh(),
torch.nn.Dropout(dropout_rate),
)
]
else:
self.postnet += [
torch.nn.Sequential(
torch.nn.Conv1d(
ichans,
ochans,
n_filts,
stride=1,
padding=(n_filts - 1) // 2,
bias=False,
),
torch.nn.Tanh(),
torch.nn.Dropout(dropout_rate),
)
]
ichans = n_chans if n_layers != 1 else odim
if use_batch_norm:
self.postnet += [
torch.nn.Sequential(
torch.nn.Conv1d(
ichans,
odim,
n_filts,
stride=1,
padding=(n_filts - 1) // 2,
bias=False,
),
torch.nn.BatchNorm1d(odim),
torch.nn.Dropout(dropout_rate),
)
]
else:
self.postnet += [
torch.nn.Sequential(
torch.nn.Conv1d(
ichans,
odim,
n_filts,
stride=1,
padding=(n_filts - 1) // 2,
bias=False,
),
torch.nn.Dropout(dropout_rate),
)
]
def forward(self, xs):
"""Calculate forward propagation.
Args:
xs (Tensor): Batch of the sequences of padded input tensors (B, idim, Tmax).
Returns:
Tensor: Batch of padded output tensor. (B, odim, Tmax).
"""
for postnet in self.postnet:
xs = postnet(xs)
return xs
class ResidualConv1dGLU(nn.Module):
"""Residual dilated conv1d + Gated linear unit
Args:
residual_channels (int): Residual input / output channels
gate_channels (int): Gated activation channels.
kernel_size (int): Kernel size of convolution layers.
skip_out_channels (int): Skip connection channels. If None, set to same
as ``residual_channels``.
cin_channels (int): Local conditioning channels. If negative value is
set, local conditioning is disabled.
gin_channels (int): Global conditioning channels. If negative value is
set, global conditioning is disabled.
dropout (float): Dropout probability.
padding (int): Padding for convolution layers. If None, proper padding
is computed depends on dilation and kernel_size.
dilation (int): Dilation factor.
"""
def __init__(self, residual_channels, gate_channels, kernel_size,
skip_out_channels=None,
cin_channels=-1, gin_channels=-1,
dropout=1 - 0.95, padding=None, dilation=1,
bias=True, *args, **kwargs):
super(ResidualConv1dGLU, self).__init__()
self.dropout = dropout
if skip_out_channels is None:
skip_out_channels = residual_channels
if padding is None:
padding = (kernel_size - 1) // 2 * dilation # For non causal dilated convolution
self.conv = nn.Conv1d(residual_channels, gate_channels, kernel_size,
padding=padding, dilation=dilation,
bias=bias, *args, **kwargs)
# conv output is split into two groups
gate_out_channels = gate_channels // 2
self.conv1x1_out = nn.Conv1d(gate_out_channels, residual_channels, 1, bias=bias)
self.conv1x1_skip = nn.Conv1d(gate_out_channels, skip_out_channels, 1, bias=bias)
def forward(self, x):
"""Forward
Args:
x (Tensor): B x C x T
c (Tensor): B x C x T, Local conditioning features
g (Tensor): B x C x T, Expanded global conditioning features
Returns:
Tensor: output
"""
residual = x
x = F.dropout(x, p=self.dropout, training=self.training)
splitdim = 1
x = self.conv(x)
a, b = x.split(x.size(splitdim) // 2, dim=splitdim)
x = torch.tanh(a) * torch.sigmoid(b)
# For skip connection
s = self.conv1x1_skip(x)
# For residual connection
x = self.conv1x1_out(x)
x = (x + residual) * math.sqrt(0.5)
return x, s
class Generator(nn.Module):
def __init__(self, in_channels, out_channels=1, bias=False,
num_layers=20, num_stacks=2, kernel_size=3,
residual_channels=128, gate_channels=128, skip_out_channels=128,
postnet_layers=12, postnet_filts=32, use_batch_norm=False, postnet_dropout_rate=0.5):
super().__init__()
assert num_layers % num_stacks == 0
num_layers_per_stack = num_layers // num_stacks
# in_channels is 1 for RAW waveform otherwise quantize classes
self.first_conv = nn.Conv1d(in_channels, residual_channels, 3, padding=1, bias=bias)
self.conv_layers = nn.ModuleList()
for n_layer in range(num_layers):
dilation = 2**(n_layer % num_layers_per_stack)
conv = ResidualConv1dGLU(
residual_channels, gate_channels,
skip_out_channels=skip_out_channels,
kernel_size=kernel_size,
bias=bias,
dilation=dilation,
dropout=1 - 0.95,
)
self.conv_layers.append(conv)
self.last_conv_layers = nn.Sequential(
nn.ReLU(True),
nn.Conv1d(skip_out_channels, skip_out_channels, 1, bias=True),
nn.ReLU(True),
nn.Conv1d(skip_out_channels, out_channels, 1, bias=True),
)
self.postnet = (
None
if postnet_layers == 0
else Postnet(
idim=in_channels,
odim=out_channels,
n_layers=postnet_layers,
n_chans=residual_channels,
n_filts=postnet_filts,
use_batch_norm=use_batch_norm,
dropout_rate=postnet_dropout_rate,
)
)
def forward(self, x, with_postnet=False):
x = self.first_conv(x)
skips = 0
for conv in self.conv_layers:
x, h = conv(x)
skips += h
skips *= math.sqrt(1.0 / len(self.conv_layers))
x = skips
x = self.last_conv_layers(x)
if not with_postnet:
return x, None
else:
after_x = self.postnet(x)
return x, after_x
def generator_loss(disc_outputs):
loss = 0
gen_losses = []
for dg in disc_outputs:
l = torch.mean((1-dg)**2)
gen_losses.append(l)
loss += l
return loss, gen_losses
if __name__ == '__main__':
model = Generator(1)
x = torch.ones([2, 1, 16000])
y = model(x)
print("Shape of y", y.shape)
assert x.shape[-1] == y.shape[-1]