forked from mostafaelaraby/wavegan-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavegan.py
424 lines (382 loc) · 13.6 KB
/
wavegan.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""
wavegan-pytorch
https://github.com/mostafaelaraby/wavegan-pytorch/tree/master
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
#############################
# Model Params
#############################
# n_iterations = 100000
# use_batchnorm = False
# lr_g = 1e-4
# lr_d = 3e-4 # you can use with discriminator having a larger learning rate than generator instead of using n_critic updates ttur https://arxiv.org/abs/1706.08500
# beta1 = 0.5
# beta2 = 0.9
# decay_lr = False # used to linearly deay learning rate untill reaching 0 at iteration 100,000
# generator_batch_size_factor = 1 # in some cases we might try to update the generator with double batch size used in the discriminator https://arxiv.org/abs/1706.08500
# n_critic = 1 # update generator every n_critic steps if lr_g = lr_d the n_critic's default value is 5
# # gradient penalty regularization factor.
# validate=False
# p_coeff = 10
# batch_size = 10
noise_latent_dim = 100 # size of the sampling noise
# model_capacity_size = 32 # model capacity during training can be reduced to 32 for larger window length of 2 seconds and 4 seconds
class Transpose1dLayer(nn.Module):
def __init__(
self,
in_channels,
out_channels,
kernel_size, # 25
stride,
padding=11,
upsample=None,
output_padding=1,
use_batch_norm=False,
):
super(Transpose1dLayer, self).__init__()
self.upsample = upsample
reflection_pad = nn.ConstantPad1d(kernel_size // 2, value=0)
conv1d = nn.Conv1d(in_channels, out_channels, kernel_size, stride)
conv1d.weight.data.normal_(0.0, 0.02)
Conv1dTrans = nn.ConvTranspose1d(
in_channels, out_channels, kernel_size, stride, padding, output_padding
)
batch_norm = nn.BatchNorm1d(out_channels)
if self.upsample:
operation_list = [reflection_pad, conv1d]
else:
operation_list = [Conv1dTrans]
if use_batch_norm:
operation_list.append(batch_norm)
self.transpose_ops = nn.Sequential(*operation_list)
def forward(self, x):
# if upsample: nearest upsample, reflection_pad(ConstantPad1d), conv1d(Conv1d), batch_norm
# else: Conv1dTrans (ConvTranspose1d), batch_norm
if self.upsample:
# recommended by wavgan paper to use nearest upsampling
x = nn.functional.interpolate(x, scale_factor=self.upsample, mode="nearest")
return self.transpose_ops(x)
class Conv1D(nn.Module):
def __init__(
self,
input_channels,
output_channels,
kernel_size,
alpha=0.2,
shift_factor=2,
stride=4,
padding=11,
use_batch_norm=False,
drop_prob=0,
):
super(Conv1D, self).__init__()
self.conv1d = nn.Conv1d(
input_channels, output_channels, kernel_size, stride=stride, padding=padding
)
self.batch_norm = nn.BatchNorm1d(output_channels)
self.phase_shuffle = PhaseShuffle(shift_factor)
self.alpha = alpha
self.use_batch_norm = use_batch_norm
self.use_phase_shuffle = shift_factor == 0
self.use_drop = drop_prob > 0
self.dropout = nn.Dropout2d(drop_prob)
def forward(self, x):
x = self.conv1d(x)
if self.use_batch_norm:
x = self.batch_norm(x)
x = F.leaky_relu(x, negative_slope=self.alpha)
if self.use_phase_shuffle:
x = self.phase_shuffle(x)
if self.use_drop:
x = self.dropout(x)
return x
class PhaseShuffle(nn.Module):
"""
Performs phase shuffling, i.e. shifting feature axis of a 3D tensor
by a random integer in {-n, n} and performing reflection padding where
necessary.
"""
# Copied from https://github.com/jtcramer/wavegan/blob/master/wavegan.py#L8
def __init__(self, shift_factor):
super(PhaseShuffle, self).__init__()
self.shift_factor = shift_factor
def forward(self, x):
if self.shift_factor == 0:
return x
# uniform in (L, R)
k_list = (
torch.Tensor(x.shape[0]).random_(0, 2 * self.shift_factor + 1)
- self.shift_factor
)
k_list = k_list.numpy().astype(int)
# Combine sample indices into lists so that less shuffle operations
# need to be performed
k_map = {}
for idx, k in enumerate(k_list):
k = int(k)
if k not in k_map:
k_map[k] = []
k_map[k].append(idx)
# Make a copy of x for our output
x_shuffle = x.clone()
# Apply shuffle to each sample
for k, idxs in k_map.items():
if k > 0:
x_shuffle[idxs] = F.pad(x[idxs][..., :-k], (k, 0), mode="reflect")
else:
x_shuffle[idxs] = F.pad(x[idxs][..., -k:], (0, -k), mode="reflect")
assert x_shuffle.shape == x.shape, "{}, {}".format(x_shuffle.shape, x.shape)
return x_shuffle
class WaveGANGenerator(nn.Module):
def __init__(
self,
model_size=64,
ngpus=1,
num_channels=1,
verbose=False,
upsample=True,
slice_len=16384,
use_batch_norm=False,
):
super(WaveGANGenerator, self).__init__()
assert slice_len in [16384, 32768, 65536] # used to predict longer utterances
self.ngpus = ngpus
self.model_size = model_size # d
self.num_channels = num_channels # c
latent_dim = noise_latent_dim
self.verbose = verbose
self.use_batch_norm = use_batch_norm
self.dim_mul = 16 if slice_len == 16384 else 32
self.fc1 = nn.Linear(latent_dim, 4 * 4 * model_size * self.dim_mul)
self.bn1 = nn.BatchNorm1d(num_features=model_size * self.dim_mul)
stride = 4
if upsample:
stride = 1
upsample = 4
deconv_layers = [
Transpose1dLayer(
self.dim_mul * model_size, # 1024
(self.dim_mul * model_size) // 2, # 512
25,
stride,
upsample=upsample,
use_batch_norm=use_batch_norm,
),
Transpose1dLayer(
(self.dim_mul * model_size) // 2, # 512
(self.dim_mul * model_size) // 4, # 256
25,
stride,
upsample=upsample,
use_batch_norm=use_batch_norm,
),
Transpose1dLayer(
(self.dim_mul * model_size) // 4, # 256
(self.dim_mul * model_size) // 8, # 128
25,
stride,
upsample=upsample,
use_batch_norm=use_batch_norm,
),
Transpose1dLayer(
(self.dim_mul * model_size) // 8,
(self.dim_mul * model_size) // 16,
25,
stride,
upsample=upsample,
use_batch_norm=use_batch_norm,
),
]
if slice_len == 16384:
deconv_layers.append(
Transpose1dLayer(
(self.dim_mul * model_size) // 16,
num_channels,
25,
stride,
upsample=upsample,
)
)
elif slice_len == 32768:
deconv_layers += [
Transpose1dLayer(
(self.dim_mul * model_size) // 16,
model_size,
25,
stride,
upsample=upsample,
use_batch_norm=use_batch_norm,
),
Transpose1dLayer(model_size, num_channels, 25, 2, upsample=upsample),
]
elif slice_len == 65536:
deconv_layers += [
Transpose1dLayer(
(self.dim_mul * model_size) // 16,
model_size,
25,
stride,
upsample=upsample,
use_batch_norm=use_batch_norm,
),
Transpose1dLayer(
model_size, num_channels, 25, stride, upsample=upsample
),
]
else:
raise ValueError("slice_len {} value is not supported".format(slice_len))
self.deconv_list = nn.ModuleList(deconv_layers)
for m in self.modules():
if isinstance(m, nn.ConvTranspose1d) or isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight.data)
def forward(self, x, labels=None):
# b x 100 -> b x 16384 -> b x 1024 x 16
x = self.fc1(x).view(-1, self.dim_mul * self.model_size, 16)
if self.use_batch_norm:
x = self.bn1(x)
x = F.relu(x)
if self.verbose:
print(x.shape) # b x 1024 x 16
for deconv in self.deconv_list[:-1]:
x = F.relu(deconv(x))
if self.verbose:
print(x.shape) # b x 512 x 64, b x 256 x 256, b x 128 x 1024, b x 64 x 4096
output = torch.tanh(self.deconv_list[-1](x)) # 1 ~ -1 사이 raw waveform 생성.
if self.verbose:
print(output.shape) # b x 1 x 16384
return output
class WaveGANDiscriminator(nn.Module):
def __init__(
self,
model_size=64,
ngpus=1,
num_channels=1,
shift_factor=2,
alpha=0.2,
verbose=False,
slice_len=16384,
use_batch_norm=False,
):
super(WaveGANDiscriminator, self).__init__()
assert slice_len in [16384, 32768, 65536] # used to predict longer utterances
self.model_size = model_size # d
self.ngpus = ngpus
self.use_batch_norm = use_batch_norm
self.num_channels = num_channels # c
self.shift_factor = shift_factor # n
self.alpha = alpha
self.verbose = verbose
conv_layers = [
Conv1D(
num_channels,
model_size,
25,
stride=4,
padding=11,
use_batch_norm=use_batch_norm,
alpha=alpha,
shift_factor=shift_factor,
),
Conv1D(
model_size,
2 * model_size,
25,
stride=4,
padding=11,
use_batch_norm=use_batch_norm,
alpha=alpha,
shift_factor=shift_factor,
),
Conv1D(
2 * model_size,
4 * model_size,
25,
stride=4,
padding=11,
use_batch_norm=use_batch_norm,
alpha=alpha,
shift_factor=shift_factor,
),
Conv1D(
4 * model_size,
8 * model_size,
25,
stride=4,
padding=11,
use_batch_norm=use_batch_norm,
alpha=alpha,
shift_factor=shift_factor,
),
Conv1D(
8 * model_size,
16 * model_size,
25,
stride=4,
padding=11,
use_batch_norm=use_batch_norm,
alpha=alpha,
shift_factor=0 if slice_len == 16384 else shift_factor,
),
]
self.fc_input_size = 256 * model_size
if slice_len == 32768:
conv_layers.append(
Conv1D(
16 * model_size,
32 * model_size,
25,
stride=2,
padding=11,
use_batch_norm=use_batch_norm,
alpha=alpha,
shift_factor=0,
)
)
self.fc_input_size = 480 * model_size
elif slice_len == 65536:
conv_layers.append(
Conv1D(
16 * model_size,
32 * model_size,
25,
stride=4,
padding=11,
use_batch_norm=use_batch_norm,
alpha=alpha,
shift_factor=0,
)
)
self.fc_input_size = 512 * model_size
self.conv_layers = nn.ModuleList(conv_layers)
self.fc1 = nn.Linear(self.fc_input_size, 1)
for m in self.modules():
if isinstance(m, nn.Conv1d) or isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight.data)
def forward(self, x, labels=None):
# x: b x 1 x 16384
for conv in self.conv_layers:
x = conv(x)
if self.verbose:
print(x.shape) # b x 64 x 4096, b x 128 x 1024, b x 256 x 256, b x 512 x 64, b x 1024 x 16
x = x.view(-1, self.fc_input_size)
if self.verbose:
print(x.shape) # b x 16384
return self.fc1(x) # b x 1
if __name__ == "__main__":
from torch.autograd import Variable
for slice_len in [16384, 32768, 65536]:
G = WaveGANGenerator(
verbose=True, upsample=True, use_batch_norm=True, slice_len=slice_len
)
out = G(Variable(torch.randn(10, noise_latent_dim)))
print(out.shape)
assert out.shape == (10, 1, slice_len)
print("==========================")
D = WaveGANDiscriminator(verbose=True, use_batch_norm=True, slice_len=slice_len)
out2 = D(Variable(torch.randn(10, 1, slice_len)))
print(out2.shape)
assert out2.shape == (10, 1)
print("==========================")