-
Notifications
You must be signed in to change notification settings - Fork 1
/
wavegan.py
192 lines (151 loc) · 6.56 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
class Transpose1dLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, padding=11, upsample=None, output_padding=1):
super(Transpose1dLayer, self).__init__()
self.upsample = upsample
self.upsample_layer = torch.nn.Upsample(scale_factor=upsample)
reflection_pad = kernel_size // 2
self.reflection_pad = nn.ConstantPad1d(reflection_pad, value=0)
self.conv1d = torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride)
self.Conv1dTrans = nn.ConvTranspose1d(in_channels, out_channels, kernel_size, stride, padding, output_padding)
def forward(self, x):
if self.upsample:
return self.conv1d(self.reflection_pad(self.upsample_layer(x)))
else:
return self.Conv1dTrans(x)
class WaveGANGenerator(nn.Module):
def __init__(self, model_size=64, ngpus=1, num_channels=1,
latent_dim=100, post_proc_filt_len=512,
verbose=False, upsample=True):
super(WaveGANGenerator, self).__init__()
self.ngpus = ngpus
self.model_size = model_size # d
self.num_channels = num_channels # c
self.latent_di = latent_dim
self.post_proc_filt_len = post_proc_filt_len
self.verbose = verbose
# "Dense" is the same meaning as fully connection.
self.fc1 = nn.Linear(latent_dim, 256 * model_size)
stride = 4
if upsample:
stride = 1
upsample = 4
self.deconv_1 = Transpose1dLayer(16 * model_size, 8 * model_size, 25, stride, upsample=upsample)
self.deconv_2 = Transpose1dLayer(8 * model_size, 4 * model_size, 25, stride, upsample=upsample)
self.deconv_3 = Transpose1dLayer(4 * model_size, 2 * model_size, 25, stride, upsample=upsample)
self.deconv_4 = Transpose1dLayer(2 * model_size, model_size, 25, stride, upsample=upsample)
self.deconv_5 = Transpose1dLayer(model_size, num_channels, 25, stride, upsample=upsample)
if post_proc_filt_len:
self.ppfilter1 = nn.Conv1d(num_channels, num_channels, post_proc_filt_len)
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):
x = self.fc1(x).view(-1, 16 * self.model_size, 16)
x = F.relu(x)
if self.verbose:
print(x.shape)
x = F.relu(self.deconv_1(x))
if self.verbose:
print(x.shape)
x = F.relu(self.deconv_2(x))
if self.verbose:
print(x.shape)
x = F.relu(self.deconv_3(x))
if self.verbose:
print(x.shape)
x = F.relu(self.deconv_4(x))
if self.verbose:
print(x.shape)
output = F.tanh(self.deconv_5(x))
return output
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 PhaseRemove(nn.Module):
def __init__(self):
super(PhaseRemove, self).__init__()
def forward(self, x):
pass
class WaveGANDiscriminator(nn.Module):
def __init__(self, model_size=64, ngpus=1, num_channels=1, shift_factor=2,
alpha=0.2, verbose=False):
super(WaveGANDiscriminator, self).__init__()
self.model_size = model_size # d
self.ngpus = ngpus
self.num_channels = num_channels # c
self.shift_factor = shift_factor # n
self.alpha = alpha
self.verbose = verbose
self.conv1 = nn.Conv1d(num_channels, model_size, 25, stride=4, padding=11)
self.conv2 = nn.Conv1d(model_size, 2 * model_size, 25, stride=4, padding=11)
self.conv3 = nn.Conv1d(2 * model_size, 4 * model_size, 25, stride=4, padding=11)
self.conv4 = nn.Conv1d(4 * model_size, 8 * model_size, 25, stride=4, padding=11)
self.conv5 = nn.Conv1d(8 * model_size, 16 * model_size, 25, stride=4, padding=11)
self.ps1 = PhaseShuffle(shift_factor)
self.ps2 = PhaseShuffle(shift_factor)
self.ps3 = PhaseShuffle(shift_factor)
self.ps4 = PhaseShuffle(shift_factor)
self.fc1 = nn.Linear(256 * model_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):
x = F.leaky_relu(self.conv1(x), negative_slope=self.alpha)
if self.verbose:
print(x.shape)
x = self.ps1(x)
x = F.leaky_relu(self.conv2(x), negative_slope=self.alpha)
if self.verbose:
print(x.shape)
x = self.ps2(x)
x = F.leaky_relu(self.conv3(x), negative_slope=self.alpha)
if self.verbose:
print(x.shape)
x = self.ps3(x)
x = F.leaky_relu(self.conv4(x), negative_slope=self.alpha)
if self.verbose:
print(x.shape)
x = self.ps4(x)
x = F.leaky_relu(self.conv5(x), negative_slope=self.alpha)
if self.verbose:
print(x.shape)
x = x.view(-1, 256 * self.model_size)
if self.verbose:
print(x.shape)
return self.fc1(x)