-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwavegan.py
More file actions
202 lines (167 loc) · 5.35 KB
/
wavegan.py
File metadata and controls
202 lines (167 loc) · 5.35 KB
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
import tensorflow as tf
def conv1d_transpose(
inputs,
filters,
kernel_width,
stride=4,
padding='same',
upsample='zeros'):
if upsample == 'zeros':
return tf.layers.conv2d_transpose(
tf.expand_dims(inputs, axis=1),
filters,
(1, kernel_width),
strides=(1, stride),
padding='same'
)[:, 0]
elif upsample == 'nn':
batch_size = tf.shape(inputs)[0]
_, w, nch = inputs.get_shape().as_list()
x = inputs
x = tf.expand_dims(x, axis=1)
x = tf.image.resize_nearest_neighbor(x, [1, w * stride])
x = x[:, 0]
return tf.layers.conv1d(
x,
filters,
kernel_width,
1,
padding='same')
else:
raise NotImplementedError
"""
Input: [None, 100]
Output: [None, 16384, 1]
"""
def WaveGANGenerator(
z,
kernel_len=25,
dim=64,
use_batchnorm=False,
upsample='zeros',
train=False):
batch_size = tf.shape(z)[0]
if use_batchnorm:
batchnorm = lambda x: tf.layers.batch_normalization(x, training=train)
else:
batchnorm = lambda x: x
# FC and reshape for convolution
# [100] -> [16, 1024]
output = z
with tf.variable_scope('z_project'):
output = tf.layers.dense(output, 4 * 4 * dim * 16)
output = tf.reshape(output, [batch_size, 16, dim * 16])
output = batchnorm(output)
output = tf.nn.relu(output)
# Layer 0
# [16, 1024] -> [64, 512]
with tf.variable_scope('upconv_0'):
output = conv1d_transpose(output, dim * 8, kernel_len, 4, upsample=upsample)
output = batchnorm(output)
output = tf.nn.relu(output)
# Layer 1
# [64, 512] -> [256, 256]
with tf.variable_scope('upconv_1'):
output = conv1d_transpose(output, dim * 4, kernel_len, 4, upsample=upsample)
output = batchnorm(output)
output = tf.nn.relu(output)
# Layer 2
# [256, 256] -> [1024, 128]
with tf.variable_scope('upconv_2'):
output = conv1d_transpose(output, dim * 2, kernel_len, 4, upsample=upsample)
output = batchnorm(output)
output = tf.nn.relu(output)
# Layer 3
# [1024, 128] -> [4096, 64]
with tf.variable_scope('upconv_3'):
output = conv1d_transpose(output, dim, kernel_len, 4, upsample=upsample)
output = batchnorm(output)
output = tf.nn.relu(output)
# Layer 4
# [4096, 64] -> [16384, 1]
with tf.variable_scope('upconv_4'):
output = conv1d_transpose(output, 1, kernel_len, 4, upsample=upsample)
output = tf.nn.tanh(output)
# Automatically update batchnorm moving averages every time G is used during training
if train and use_batchnorm:
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
if len(update_ops) != 10:
raise Exception('Other update ops found in graph')
with tf.control_dependencies(update_ops):
output = tf.identity(output)
return output
def lrelu(inputs, alpha=0.2):
return tf.maximum(alpha * inputs, inputs)
def apply_phaseshuffle(x, rad, pad_type='reflect'):
b, x_len, nch = x.get_shape().as_list()
phase = tf.random_uniform([], minval=-rad, maxval=rad + 1, dtype=tf.int32)
pad_l = tf.maximum(phase, 0)
pad_r = tf.maximum(-phase, 0)
phase_start = pad_r
x = tf.pad(x, [[0, 0], [pad_l, pad_r], [0, 0]], mode=pad_type)
x = x[:, phase_start:phase_start+x_len]
x.set_shape([b, x_len, nch])
return x
"""
Input: [None, 16384, 1]
Output: [None] (linear output)
"""
def WaveGANDiscriminator(
x,
kernel_len=25,
dim=64,
use_batchnorm=False,
phaseshuffle_rad=0,
strides=4):
batch_size = tf.shape(x)[0]
if use_batchnorm:
batchnorm = lambda x: tf.layers.batch_normalization(x, training=True)
else:
batchnorm = lambda x: x
if phaseshuffle_rad > 0:
phaseshuffle = lambda x: apply_phaseshuffle(x, phaseshuffle_rad)
else:
phaseshuffle = lambda x: x
# Layer 0
# [16384, 1] -> [4096, 64]
output = x
with tf.variable_scope('downconv_0') as vvvv:
output = tf.layers.conv1d(output, dim, kernel_len, strides, padding='SAME')
output = lrelu(output)
output = phaseshuffle(output)
# Layer 1
# [4096, 64] -> [1024, 128]
with tf.variable_scope('downconv_1'):
output = tf.layers.conv1d(output, dim * 2, kernel_len, strides, padding='SAME')
output = batchnorm(output)
output = lrelu(output)
output = phaseshuffle(output)
# Layer 2
# [1024, 128] -> [256, 256]
with tf.variable_scope('downconv_2'):
output = tf.layers.conv1d(output, dim * 4, kernel_len, strides, padding='SAME')
output = batchnorm(output)
output = lrelu(output)
output = phaseshuffle(output)
# Layer 3
# [256, 256] -> [64, 512]
with tf.variable_scope('downconv_3'):
output = tf.layers.conv1d(output, dim * 8, kernel_len, strides, padding='SAME')
output = batchnorm(output)
output = lrelu(output)
output = phaseshuffle(output)
# Layer 4
# [64, 512] -> [16, 1024]
with tf.variable_scope('downconv_4'):
output = tf.layers.conv1d(output, dim * 16, kernel_len, strides, padding='SAME')
output = batchnorm(output)
output = lrelu(output)
# Flatten
# output = tf.reshape(output, [batch_size, 4 * 4 * dim * 16])
output = tf.reshape(output, [batch_size, output.shape[1]*output.shape[2]])
# Connect to single logit
# with tf.variable_scope('output'):
# output = tf.layers.dense(output, 1)[:, 0]
# output = tf.layers.dense(output, 1)
# Don't need to aggregate batchnorm update ops like we do for the generator because we only use the discriminator for training
return output