-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscriminator.py
284 lines (240 loc) · 7.73 KB
/
discriminator.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from params import *
class WaveGANDiscriminator(nn.Module):
'''Discriminator with structure similar to WaveGAN paper'''
def __init__(
self,
model_size=64,
shift_factor=2,
alpha=0.2,
dim_mul=32,
slice_len=SLICE_LEN,
use_batch_norm=False,
spectral_norm=False,
):
super(WaveGANDiscriminator, self).__init__()
assert slice_len in [65536] #leave as array for possibility of different lengths
self.model_size = model_size
self.use_batch_norm = use_batch_norm
self.dim_mul = dim_mul
self.slice_len = slice_len
conv_layers = [
Conv1d(
1,
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=shift_factor,
),
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 = self.slice_len
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):
for conv in self.conv_layers:
x = conv(x)
x = x.view(-1, self.fc_input_size)
if self.spectral_norm:
x = nn.utils.spectral_norm(x)
return self.fc1(x)
class Conv1d(nn.Module):
'''1d Convolutions for WaveGAN Discriminator'''
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.phase_shuffle = PhaseShuffle(shift_factor)
sequence = []
conv1d = nn.Conv1d(input_channels, output_channels, kernel_size, stride=stride, padding=padding)
sequence.append(conv1d)
if use_batch_norm:
sequence.append(nn.BatchNorm1d(output_channels))
sequence.append(nn.LeakyReLU(negative_slope=alpha))
if shift_factor > 0:
sequence.append(self.phase_shuffle)
if drop_prob > 0:
sequence.append(nn.Dropout2d(drop_prob))
self.layer = nn.Sequential(*sequence)
def forward(self, x):
return self.layer(x)
class PhaseShuffle(nn.Module):
'''
Phase Shuffle module, similar to implementation in WaveGAN paper.
Shifts the inputs at each layer slightly along the time axis so that
the discriminator does not learn to distinguish the exact phase of
real vs generated.
'''
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
k_list = (
torch.Tensor(x.shape[0]).random_(0, 2 * self.shift_factor + 1)
- self.shift_factor
)
k_list = k_list.numpy().astype(int)
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)
x_shuffle = x.clone()
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 TransGANDiscriminator(nn.Module):
'''Transformer GAN Discriminator, similar architecture to TransGAN paper, adapted to audio'''
def __init__(
self,
noise_dim=NOISE_DIM,
upsample=True,
dim_mul=32,
model_size=64,
slice_len=SLICE_LEN,
use_batch_norm=True,
):
super().__init__()
self.model_size = model_size
self.dim_mul = dim_mul
self.use_batch_norm = use_batch_norm
self.slice_len = slice_len
transformer_layers = [
Transformer1dLayer(
1,
self.model_size
),
Transformer1dLayer(
self.model_size,
self.model_size * 2
),
Transformer1dLayer(
self.model_size * 2,
self.model_size * 4
),
Transformer1dLayer(
self.model_size * 4,
self.model_size * 8
),
Transformer1dLayer(
self.model_size * 8,
self.model_size * 16
),
Transformer1dLayer(
self.model_size * 16,
self.model_size * 32
)
]
self.transformer_list = nn.ModuleList(transformer_layers)
self.fc1 = nn.Linear(self.slice_len, 1)
self.bn1 = nn.BatchNorm1d(num_features=self.model_size * self.dim_mul)
def forward(self, x):
for conv in self.conv_layers:
x = conv(x)
x = x.view(-1, self.fc_input_size)
if self.use_batch_norm:
x = self.bn1(x)
x = self.fc1(x)
return x
class Transformer1dLayer(nn.Module):
'''Single layer for TransGAN'''
def __init__(
self,
in_channels,
out_channels,
kernel_size=25,
use_batch_norm=True,
n_head=8,
alpha=0.2,
):
super().__init__()
self.kernel_size = kernel_size
self.use_batch_norm = use_batch_norm
self.in_channels = in_channels
self.out_channels = out_channels
self.n_head = n_head
self.alpha = alpha
encoder_layer = nn.TransformerEncoderLayer(self.in_channels, self.n_head)
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=6)
def forward(self, x):
x = self.transformer(x)
if self.use_batch_norm:
batch_norm = nn.BatchNorm1d(self.out_channels)
x = batch_norm(x)
x = nn.LeakyReLU(negative_slope=self.alpha)(x)
B, H, W = x.shape
x = x.view(B, -1, self.out_channels)
return x