-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavegan.py
834 lines (709 loc) · 30.5 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
# -*- coding: utf-8 -*-
"""Untitled
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1vCgJZeXLT9X1Q0fdXlvVjPGXmaL85muX
"""
# coding: utf-8
from __future__ import with_statement, print_function, absolute_import
import math
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from torch.distributions import Normal
import torchvision
import torchsummary
#@title wavenet_vocoder code
#wavenet_vocoder/conv.py
class extConv1d(nn.Conv1d):
"""Extended nn.Conv1d for incremental dilated convolutions
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.clear_buffer()
self._linearized_weight = None
self.register_backward_hook(self._clear_linearized_weight)
def incremental_forward(self, input):
# input: (B, T, C)
if self.training:
raise RuntimeError('incremental_forward only supports eval mode')
# run forward pre hooks (e.g., weight norm)
for hook in self._forward_pre_hooks.values():
hook(self, input)
# reshape weight
weight = self._get_linearized_weight()
kw = self.kernel_size[0]
dilation = self.dilation[0]
bsz = input.size(0) # input: bsz x len x dim
if kw > 1:
input = input.data
if self.input_buffer is None:
self.input_buffer = input.new(bsz, kw + (kw - 1) * (dilation - 1), input.size(2))
self.input_buffer.zero_()
else:
# shift buffer
self.input_buffer[:, :-1, :] = self.input_buffer[:, 1:, :].clone()
# append next input
self.input_buffer[:, -1, :] = input[:, -1, :]
input = self.input_buffer
if dilation > 1:
input = input[:, 0::dilation, :].contiguous()
output = F.linear(input.view(bsz, -1), weight, self.bias)
return output.view(bsz, 1, -1)
def clear_buffer(self):
self.input_buffer = None
def _get_linearized_weight(self):
if self._linearized_weight is None:
kw = self.kernel_size[0]
# nn.Conv1d
if self.weight.size() == (self.out_channels, self.in_channels, kw):
weight = self.weight.transpose(1, 2).contiguous()
else:
# fairseq.modules.conv_tbc.ConvTBC
weight = self.weight.transpose(2, 1).transpose(1, 0).contiguous()
assert weight.size() == (self.out_channels, kw, self.in_channels)
self._linearized_weight = weight.view(self.out_channels, -1)
return self._linearized_weight
def _clear_linearized_weight(self, *args):
self._linearized_weight = None
#wavenet_vocoder/modules.py
def Conv1d(in_channels, out_channels, kernel_size, dropout=0, **kwargs):
m = extConv1d(in_channels, out_channels, kernel_size, **kwargs)
nn.init.kaiming_normal_(m.weight, nonlinearity="relu")
if m.bias is not None:
nn.init.constant_(m.bias, 0)
return nn.utils.weight_norm(m)
def Embedding(num_embeddings, embedding_dim, padding_idx, std=0.01):
m = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)
m.weight.data.normal_(0, std)
return m
def _conv1x1_forward(conv, x, is_incremental):
"""Conv1x1 forward
"""
if is_incremental:
x = conv.incremental_forward(x)
else:
x = conv(x)
return x
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, causal=True,
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:
# no future time stamps available
if causal:
padding = (kernel_size - 1) * dilation
else:
padding = (kernel_size - 1) // 2 * dilation
self.causal = causal
self.conv = Conv1d(residual_channels, gate_channels, kernel_size,
padding=padding, dilation=dilation,
bias=bias, *args, **kwargs)
# local conditioning
if cin_channels > 0:
self.conv1x1c = Conv1d(cin_channels, gate_channels, kernel_size=1, padding=0, dilation=1, bias=False)
else:
self.conv1x1c = None
# global conditioning
if gin_channels > 0:
self.conv1x1g = Conv1d(gin_channels, gate_channels, kernel_size=1, padding=0, dilation=1, bias=False)
else:
self.conv1x1g = None
# conv output is split into two groups
gate_out_channels = gate_channels // 2
self.conv1x1_out = Conv1d(gate_out_channels, residual_channels, kernel_size=1, padding=0, dilation=1, bias=bias)
self.conv1x1_skip = Conv1d(gate_out_channels, skip_out_channels, kernel_size=1, padding=0, dilation=1, bias=bias)
def forward(self, x, c=None, g=None):
return self._forward(x, c, g, False)
def incremental_forward(self, x, c=None, g=None):
return self._forward(x, c, g, True)
def _forward(self, x, c, g, is_incremental):
"""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
is_incremental (Bool) : Whether incremental mode or not
Returns:
Tensor: output
"""
residual = x
x = F.dropout(x, p=self.dropout, training=self.training)
if is_incremental:
splitdim = -1
x = self.conv.incremental_forward(x)
else:
splitdim = 1
x = self.conv(x)
# remove future time steps
x = x[:, :, :residual.size(-1)] if self.causal else x
a, b = x.split(x.size(splitdim) // 2, dim=splitdim)
# local conditioning
if c is not None:
assert self.conv1x1c is not None
c = _conv1x1_forward(self.conv1x1c, c, is_incremental)
ca, cb = c.split(c.size(splitdim) // 2, dim=splitdim)
a, b = a + ca, b + cb
# global conditioning
if g is not None:
assert self.conv1x1g is not None
g = _conv1x1_forward(self.conv1x1g, g, is_incremental)
ga, gb = g.split(g.size(splitdim) // 2, dim=splitdim)
a, b = a + ga, b + gb
x = torch.tanh(a) * torch.sigmoid(b)
# For skip connection
s = _conv1x1_forward(self.conv1x1_skip, x, is_incremental)
# For residual connection
x = _conv1x1_forward(self.conv1x1_out, x, is_incremental)
x = (x + residual) * math.sqrt(0.5)
return x, s
def clear_buffer(self):
for c in [self.conv, self.conv1x1_out, self.conv1x1_skip,
self.conv1x1c, self.conv1x1g]:
if c is not None:
c.clear_buffer()
#wavenet_vocoder/mixture.py
def log_sum_exp(x):
""" numerically stable log_sum_exp implementation that prevents overflow """
# TF ordering
axis = len(x.size()) - 1
m, _ = torch.max(x, dim=axis)
m2, _ = torch.max(x, dim=axis, keepdim=True)
return m + torch.log(torch.sum(torch.exp(x - m2), dim=axis))
def discretized_mix_logistic_loss(y_hat, y, num_classes=256, log_scale_min=-7.0, reduce=True):
"""Discretized mixture of logistic distributions loss
Note that it is assumed that input is scaled to [-1, 1].
Args:
y_hat (Tensor): Predicted output (B x C x T)
y (Tensor): Target (B x T x 1).
num_classes (int): Number of classes
log_scale_min (float): Log scale minimum value
reduce (bool): If True, the losses are averaged or summed for each
minibatch.
Returns
Tensor: loss
"""
assert y_hat.dim() == 3
assert y_hat.size(1) % 3 == 0
nr_mix = y_hat.size(1) // 3
# (B x T x C)
y_hat = y_hat.transpose(1, 2)
# unpack parameters. (B, T, num_mixtures) x 3
logit_probs = y_hat[:, :, :nr_mix]
means = y_hat[:, :, nr_mix:2 * nr_mix]
log_scales = torch.clamp(y_hat[:, :, 2 * nr_mix:3 * nr_mix], min=log_scale_min)
# B x T x 1 -> B x T x num_mixtures
y = y.expand_as(means)
centered_y = y - means
inv_stdv = torch.exp(-log_scales)
plus_in = inv_stdv * (centered_y + 1. / (num_classes - 1))
cdf_plus = torch.sigmoid(plus_in)
min_in = inv_stdv * (centered_y - 1. / (num_classes - 1))
cdf_min = torch.sigmoid(min_in)
# log probability for edge case of 0 (before scaling)
# equivalent: torch.log(torch.sigmoid(plus_in))
log_cdf_plus = plus_in - F.softplus(plus_in)
# log probability for edge case of 255 (before scaling)
# equivalent: (1 - torch.sigmoid(min_in)).log()
log_one_minus_cdf_min = -F.softplus(min_in)
# probability for all other cases
cdf_delta = cdf_plus - cdf_min
mid_in = inv_stdv * centered_y
# log probability in the center of the bin, to be used in extreme cases
# (not actually used in our code)
log_pdf_mid = mid_in - log_scales - 2. * F.softplus(mid_in)
# tf equivalent
"""
log_probs = tf.where(x < -0.999, log_cdf_plus,
tf.where(x > 0.999, log_one_minus_cdf_min,
tf.where(cdf_delta > 1e-5,
tf.log(tf.maximum(cdf_delta, 1e-12)),
log_pdf_mid - np.log(127.5))))
"""
# TODO: cdf_delta <= 1e-5 actually can happen. How can we choose the value
# for num_classes=65536 case? 1e-7? not sure..
inner_inner_cond = (cdf_delta > 1e-5).float()
inner_inner_out = inner_inner_cond * \
torch.log(torch.clamp(cdf_delta, min=1e-12)) + \
(1. - inner_inner_cond) * (log_pdf_mid - np.log((num_classes - 1) / 2))
inner_cond = (y > 0.999).float()
inner_out = inner_cond * log_one_minus_cdf_min + (1. - inner_cond) * inner_inner_out
cond = (y < -0.999).float()
log_probs = cond * log_cdf_plus + (1. - cond) * inner_out
log_probs = log_probs + F.log_softmax(logit_probs, -1)
if reduce:
return -torch.sum(log_sum_exp(log_probs))
else:
return -log_sum_exp(log_probs).unsqueeze(-1)
def to_one_hot(tensor, n, fill_with=1.):
# we perform one hot encore with respect to the last axis
one_hot = torch.FloatTensor(tensor.size() + (n,)).zero_()
if tensor.is_cuda:
one_hot = one_hot.cuda()
one_hot.scatter_(len(tensor.size()), tensor.unsqueeze(-1), fill_with)
return one_hot
def sample_from_discretized_mix_logistic(y, log_scale_min=-7.0, clamp_log_scale=False):
"""
Sample from discretized mixture of logistic distributions
Args:
y (Tensor): B x C x T
log_scale_min (float): Log scale minimum value
Returns:
Tensor: sample in range of [-1, 1].
"""
assert y.size(1) % 3 == 0
nr_mix = y.size(1) // 3
# B x T x C
y = y.transpose(1, 2)
logit_probs = y[:, :, :nr_mix]
# sample mixture indicator from softmax
temp = logit_probs.data.new(logit_probs.size()).uniform_(1e-5, 1.0 - 1e-5)
temp = logit_probs.data - torch.log(- torch.log(temp))
_, argmax = temp.max(dim=-1)
# (B, T) -> (B, T, nr_mix)
one_hot = to_one_hot(argmax, nr_mix)
# select logistic parameters
means = torch.sum(y[:, :, nr_mix:2 * nr_mix] * one_hot, dim=-1)
log_scales = torch.sum(y[:, :, 2 * nr_mix:3 * nr_mix] * one_hot, dim=-1)
if clamp_log_scale:
log_scales = torch.clamp(log_scales, min=log_scale_min)
# sample from logistic & clip to interval
# we don't actually round to the nearest 8bit value when sampling
u = means.data.new(means.size()).uniform_(1e-5, 1.0 - 1e-5)
x = means + torch.exp(log_scales) * (torch.log(u) - torch.log(1. - u))
x = torch.clamp(torch.clamp(x, min=-1.), max=1.)
return x
def sample_from_mix_gaussian(y, log_scale_min=-7.0):
"""
Sample from (discretized) mixture of gaussian distributions
Args:
y (Tensor): B x C x T
log_scale_min (float): Log scale minimum value
Returns:
Tensor: sample in range of [-1, 1].
"""
C = y.size(1)
if C == 2:
nr_mix = 1
else:
assert y.size(1) % 3 == 0
nr_mix = y.size(1) // 3
# B x T x C
y = y.transpose(1, 2)
if C == 2:
logit_probs = None
else:
logit_probs = y[:, :, :nr_mix]
if nr_mix > 1:
# sample mixture indicator from softmax
temp = logit_probs.data.new(logit_probs.size()).uniform_(1e-5, 1.0 - 1e-5)
temp = logit_probs.data - torch.log(- torch.log(temp))
_, argmax = temp.max(dim=-1)
# (B, T) -> (B, T, nr_mix)
one_hot = to_one_hot(argmax, nr_mix)
# Select means and log scales
means = torch.sum(y[:, :, nr_mix:2 * nr_mix] * one_hot, dim=-1)
log_scales = torch.sum(y[:, :, 2 * nr_mix:3 * nr_mix] * one_hot, dim=-1)
else:
if C == 2:
means, log_scales = y[:, :, 0], y[:, :, 1]
elif C == 3:
means, log_scales = y[:, :, 1], y[:, :, 2]
else:
assert False, "shouldn't happen"
scales = torch.exp(log_scales)
dist = Normal(loc=means, scale=scales)
x = dist.sample()
x = torch.clamp(x, min=-1.0, max=1.0)
return x
#wavenet_vocoder/upsample.py
class Stretch2d(nn.Module):
def __init__(self, x_scale, y_scale, mode="nearest"):
super(Stretch2d, self).__init__()
self.x_scale = x_scale
self.y_scale = y_scale
self.mode = mode
def forward(self, x):
return F.interpolate(
x, scale_factor=(self.y_scale, self.x_scale), mode=self.mode)
def _get_activation(upsample_activation):
nonlinear = getattr(nn, upsample_activation)
return nonlinear
class UpsampleNetwork(nn.Module):
def __init__(self, upsample_scales, upsample_activation="none",
upsample_activation_params={}, mode="nearest",
freq_axis_kernel_size=1, cin_pad=0, cin_channels=80):
super(UpsampleNetwork, self).__init__()
self.up_layers = nn.ModuleList()
total_scale = np.prod(upsample_scales)
self.indent = cin_pad * total_scale
for scale in upsample_scales:
freq_axis_padding = (freq_axis_kernel_size - 1) // 2
k_size = (freq_axis_kernel_size, scale * 2 + 1)
padding = (freq_axis_padding, scale)
stretch = Stretch2d(scale, 1, mode)
conv = nn.Conv2d(1, 1, kernel_size=k_size, padding=padding, bias=False)
conv.weight.data.fill_(1. / np.prod(k_size))
conv = nn.utils.weight_norm(conv)
self.up_layers.append(stretch)
self.up_layers.append(conv)
if upsample_activation != "none":
nonlinear = _get_activation(upsample_activation)
self.up_layers.append(nonlinear(**upsample_activation_params))
def forward(self, c):
"""
Args:
c : B x C x T
"""
# B x 1 x C x T
c = c.unsqueeze(1)
for f in self.up_layers:
c = f(c)
# B x C x T
c = c.squeeze(1)
if self.indent > 0:
c = c[:, :, self.indent:-self.indent]
return c
class ConvInUpsampleNetwork(nn.Module):
def __init__(self, upsample_scales, upsample_activation="none",
upsample_activation_params={}, mode="nearest",
freq_axis_kernel_size=1, cin_pad=0,
cin_channels=80):
super(ConvInUpsampleNetwork, self).__init__()
# To capture wide-context information in conditional features
# meaningless if cin_pad == 0
ks = 2 * cin_pad + 1
self.conv_in = nn.Conv1d(cin_channels, cin_channels, kernel_size=ks, bias=False)
self.upsample = UpsampleNetwork(
upsample_scales, upsample_activation, upsample_activation_params,
mode, freq_axis_kernel_size, cin_pad=0, cin_channels=cin_channels)
def forward(self, c):
c_up = self.upsample(self.conv_in(c))
return c_up
#wavenet_vocoder/wavenet.py
def _expand_global_features(B, T, g, bct=True):
"""Expand global conditioning features to all time steps
Args:
B (int): Batch size.
T (int): Time length.
g (Tensor): Global features, (B x C) or (B x C x 1).
bct (bool) : returns (B x C x T) if True, otherwise (B x T x C)
Returns:
Tensor: B x C x T or B x T x C or None
"""
if g is None:
return None
g = g.unsqueeze(-1) if g.dim() == 2 else g
if bct:
g_bct = g.expand(B, -1, T)
return g_bct.contiguous()
else:
g_btc = g.expand(B, -1, T).transpose(1, 2)
return g_btc.contiguous()
def receptive_field_size(total_layers, num_cycles, kernel_size, dilation=lambda x: 2**x):
"""Compute receptive field size
Args:
total_layers (int): total layers
num_cycles (int): cycles
kernel_size (int): kernel size
dilation (lambda): lambda to compute dilation factor. ``lambda x : 1``
to disable dilated convolution.
Returns:
int: receptive field size in sample
"""
assert total_layers % num_cycles == 0
layers_per_cycle = total_layers // num_cycles
dilations = [dilation(i % layers_per_cycle) for i in range(total_layers)]
return (kernel_size - 1) * sum(dilations) + 1
#@title Wavenet class
class WaveNet(nn.Module):
"""The WaveNet model that supports local and global conditioning.
Args:
out_channels (int): Output channels. If input_type is mu-law quantized
one-hot vecror. this must equal to the quantize channels. Other wise
num_mixtures x 3 (pi, mu, log_scale).
layers (int): Number of total layers
stacks (int): Number of dilation cycles
residual_channels (int): Residual input / output channels
gate_channels (int): Gated activation channels.
skip_out_channels (int): Skip connection channels.
kernel_size (int): Kernel size of convolution layers.
dropout (float): Dropout probability.
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.
n_speakers (int): Number of speakers. Used only if global conditioning
is enabled.
upsample_conditional_features (bool): Whether upsampling local
conditioning features by transposed convolution layers or not.
upsample_scales (list): List of upsample scale.
``np.prod(upsample_scales)`` must equal to hop size. Used only if
upsample_conditional_features is enabled.
freq_axis_kernel_size (int): Freq-axis kernel_size for transposed
convolution layers for upsampling. If you only care about time-axis
upsampling, set this to 1.
scalar_input (Bool): If True, scalar input ([-1, 1]) is expected, otherwise
quantized one-hot vector is expected.
use_speaker_embedding (Bool): Use speaker embedding or Not. Set to False
if you want to disable embedding layer and use external features
directly.
"""
def __init__(self, out_channels=256, layers=20, stacks=2,
residual_channels=512,
gate_channels=512,
skip_out_channels=512,
kernel_size=3, dropout=1 - 0.95,
cin_channels=-1, gin_channels=-1, n_speakers=None,
upsample_conditional_features=False,
upsample_net="ConvInUpsampleNetwork",
upsample_params={"upsample_scales": [4, 4, 4, 4]},
scalar_input=False,
use_speaker_embedding=False,
output_distribution="Logistic",
cin_pad=0,
):
super(WaveNet, self).__init__()
self.scalar_input = scalar_input
self.out_channels = out_channels
self.cin_channels = cin_channels
self.output_distribution = output_distribution
assert layers % stacks == 0
layers_per_stack = layers // stacks
if scalar_input:
self.first_conv = Conv1d(1, residual_channels, kernel_size=1, padding=0, dilation=1, bias=True)
else:
self.first_conv = Conv1d(out_channels, residual_channels, kernel_size=1, padding=0, dilation=1, bias=True)
self.conv_layers = nn.ModuleList()
for layer in range(layers):
dilation = 2**(layer % layers_per_stack)
conv = ResidualConv1dGLU(
residual_channels, gate_channels,
kernel_size=kernel_size,
skip_out_channels=skip_out_channels,
bias=True, # magenda uses bias, but musyoku doesn't
dilation=dilation, dropout=dropout,
cin_channels=cin_channels,
gin_channels=gin_channels)
self.conv_layers.append(conv)
self.last_conv_layers = nn.ModuleList([
nn.ReLU(inplace=True),
Conv1d(skip_out_channels, skip_out_channels, kernel_size=1, padding=0, dilation=1, bias=True),
nn.ReLU(inplace=True),
Conv1d(skip_out_channels, out_channels, kernel_size=1, padding=0, dilation=1, bias=True),
])
if gin_channels > 0 and use_speaker_embedding:
assert n_speakers is not None
self.embed_speakers = Embedding(
n_speakers, gin_channels, padding_idx=None, std=0.1)
else:
self.embed_speakers = None
# Upsample conv net
if upsample_conditional_features:
self.upsample_net = getattr(upsample, upsample_net)(**upsample_params)
else:
self.upsample_net = None
self.receptive_field = receptive_field_size(layers, stacks, kernel_size)
def has_speaker_embedding(self):
return self.embed_speakers is not None
def local_conditioning_enabled(self):
return self.cin_channels > 0
def forward(self, x, c=None, g=None, softmax=False):
"""Forward step
Args:
x (Tensor): One-hot encoded audio signal, shape (B x C x T)
c (Tensor): Local conditioning features,
shape (B x cin_channels x T)
g (Tensor): Global conditioning features,
shape (B x gin_channels x 1) or speaker Ids of shape (B x 1).
Note that ``self.use_speaker_embedding`` must be False when you
want to disable embedding layer and use external features
directly (e.g., one-hot vector).
Also type of input tensor must be FloatTensor, not LongTensor
in case of ``self.use_speaker_embedding`` equals False.
softmax (bool): Whether applies softmax or not.
Returns:
Tensor: output, shape B x out_channels x T
"""
print(x.size())
B, _, T = x.size()
if g is not None:
if self.embed_speakers is not None:
# (B x 1) -> (B x 1 x gin_channels)
g = self.embed_speakers(g.view(B, -1))
# (B x gin_channels x 1)
g = g.transpose(1, 2)
assert g.dim() == 3
# Expand global conditioning features to all time steps
g_bct = _expand_global_features(B, T, g, bct=True)
if c is not None and self.upsample_net is not None:
c = self.upsample_net(c)
assert c.size(-1) == x.size(-1)
# Feed data to network
x = self.first_conv(x)
skips = 0
for f in self.conv_layers:
x, h = f(x, c, g_bct)
skips += h
skips *= math.sqrt(1.0 / len(self.conv_layers))
x = skips
for f in self.last_conv_layers:
x = f(x)
x = F.softmax(x, dim=1) if softmax else x
return x
def incremental_forward(self, initial_input=None, c=None, g=None,
T=100, test_inputs=None,
tqdm=lambda x: x, softmax=True, quantize=True,
log_scale_min=-50.0):
"""Incremental forward step
Due to linearized convolutions, inputs of shape (B x C x T) are reshaped
to (B x T x C) internally and fed to the network for each time step.
Input of each time step will be of shape (B x 1 x C).
Args:
initial_input (Tensor): Initial decoder input, (B x C x 1)
c (Tensor): Local conditioning features, shape (B x C' x T)
g (Tensor): Global conditioning features, shape (B x C'' or B x C''x 1)
T (int): Number of time steps to generate.
test_inputs (Tensor): Teacher forcing inputs (for debugging)
tqdm (lamda) : tqdm
softmax (bool) : Whether applies softmax or not
quantize (bool): Whether quantize softmax output before feeding the
network output to input for the next time step. TODO: rename
log_scale_min (float): Log scale minimum value.
Returns:
Tensor: Generated one-hot encoded samples. B x C x T
or scaler vector B x 1 x T
"""
self.clear_buffer()
B = 1
# Note: shape should be **(B x T x C)**, not (B x C x T) opposed to
# batch forward due to linealized convolution
if test_inputs is not None:
if self.scalar_input:
if test_inputs.size(1) == 1:
test_inputs = test_inputs.transpose(1, 2).contiguous()
else:
if test_inputs.size(1) == self.out_channels:
test_inputs = test_inputs.transpose(1, 2).contiguous()
B = test_inputs.size(0)
if T is None:
T = test_inputs.size(1)
else:
T = max(T, test_inputs.size(1))
# cast to int in case of numpy.int64...
T = int(T)
# Global conditioning
if g is not None:
if self.embed_speakers is not None:
g = self.embed_speakers(g.view(B, -1))
# (B x gin_channels, 1)
g = g.transpose(1, 2)
assert g.dim() == 3
g_btc = _expand_global_features(B, T, g, bct=False)
# Local conditioning
if c is not None:
B = c.shape[0]
if self.upsample_net is not None:
c = self.upsample_net(c)
assert c.size(-1) == T
if c.size(-1) == T:
c = c.transpose(1, 2).contiguous()
outputs = []
if initial_input is None:
if self.scalar_input:
initial_input = torch.zeros(B, 1, 1)
else:
initial_input = torch.zeros(B, 1, self.out_channels)
initial_input[:, :, 127] = 1 # TODO: is this ok?
# https://github.com/pytorch/pytorch/issues/584#issuecomment-275169567
if next(self.parameters()).is_cuda:
initial_input = initial_input.cuda()
else:
if initial_input.size(1) == self.out_channels:
initial_input = initial_input.transpose(1, 2).contiguous()
current_input = initial_input
for t in tqdm(range(T)):
if test_inputs is not None and t < test_inputs.size(1):
current_input = test_inputs[:, t, :].unsqueeze(1)
else:
if t > 0:
current_input = outputs[-1]
# Conditioning features for single time step
ct = None if c is None else c[:, t, :].unsqueeze(1)
gt = None if g is None else g_btc[:, t, :].unsqueeze(1)
x = current_input
x = self.first_conv.incremental_forward(x)
skips = 0
for f in self.conv_layers:
x, h = f.incremental_forward(x, ct, gt)
skips += h
skips *= math.sqrt(1.0 / len(self.conv_layers))
x = skips
for f in self.last_conv_layers:
try:
x = f.incremental_forward(x)
except AttributeError:
x = f(x)
# Generate next input by sampling
if self.scalar_input:
if self.output_distribution == "Logistic":
x = sample_from_discretized_mix_logistic(
x.view(B, -1, 1), log_scale_min=log_scale_min)
elif self.output_distribution == "Normal":
x = sample_from_mix_gaussian(
x.view(B, -1, 1), log_scale_min=log_scale_min)
else:
assert False
else:
x = F.softmax(x.view(B, -1), dim=1) if softmax else x.view(B, -1)
if quantize:
dist = torch.distributions.OneHotCategorical(x)
x = dist.sample()
outputs += [x.data]
# T x B x C
outputs = torch.stack(outputs)
# B x C x T
outputs = outputs.transpose(0, 1).transpose(1, 2).contiguous()
self.clear_buffer()
return outputs
def clear_buffer(self):
self.first_conv.clear_buffer()
for f in self.conv_layers:
f.clear_buffer()
for f in self.last_conv_layers:
try:
f.clear_buffer()
except AttributeError:
pass
def make_generation_fast_(self):
def remove_weight_norm(m):
try:
nn.utils.remove_weight_norm(m)
except ValueError: # this module didn't have weight norm
return
self.apply(remove_weight_norm)
wn = WaveNet()
torchsummary.summary(wn, input_size=(256,100))