-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathops.py
452 lines (338 loc) · 16.9 KB
/
ops.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
import tensorflow as tf
import tensorflow.contrib as tf_contrib
weight_init = tf_contrib.layers.variance_scaling_initializer() # kaming init for encoder / decoder
weight_regularizer = tf_contrib.layers.l2_regularizer(scale=0.0001)
##################################################################################
# Layer
##################################################################################
def conv(x, channels, kernel=4, stride=2, pad=0, pad_type='zero', use_bias=True, scope='conv'):
with tf.variable_scope(scope):
if scope.__contains__("discriminator") :
weight_init = tf.random_normal_initializer(mean=0.0, stddev=0.02)
else :
weight_init = tf_contrib.layers.variance_scaling_initializer()
if pad_type == 'zero' :
x = tf.pad(x, [[0, 0], [pad, pad], [pad, pad], [0, 0]])
if pad_type == 'reflect' :
x = tf.pad(x, [[0, 0], [pad, pad], [pad, pad], [0, 0]], mode='REFLECT')
x = tf.layers.conv2d(inputs=x, filters=channels,
kernel_size=kernel, kernel_initializer=weight_init,
kernel_regularizer=weight_regularizer,
strides=stride, use_bias=use_bias)
return x
def discriminator(inputs, reuse = False, scope_name = 'discriminator'):
# inputs has shape [batch_size, num_features, time]
# we need to add channel for 2D convolution [batch_size, num_features, time, 1]
inputs = tf.expand_dims(inputs, -1)
with tf.variable_scope(scope_name) as scope:
# Discriminator would be reused in CycleGAN
if reuse:
scope.reuse_variables()
else:
assert scope.reuse is False
h1 = conv2d_layer(inputs = inputs, filters = 128, kernel_size = [3, 3], strides = [1, 2], activation = None, name = 'h1_conv')
h1_gates = conv2d_layer(inputs = inputs, filters = 128, kernel_size = [3, 3], strides = [1, 2], activation = None, name = 'h1_conv_gates')
h1_glu = gated_linear_layer(inputs = h1, gates = h1_gates, name = 'h1_glu')
# Downsample
d1 = downsample2d_block(inputs = h1_glu, filters = 256, kernel_size = [3, 3], strides = [2, 2], name_prefix = 'downsample2d_block1_')
d2 = downsample2d_block(inputs = d1, filters = 512, kernel_size = [3, 3], strides = [2, 2], name_prefix = 'downsample2d_block2_')
d3 = downsample2d_block(inputs = d2, filters = 1024, kernel_size = [6, 3], strides = [1, 2], name_prefix = 'downsample2d_block3_')
# Output
o1 = tf.layers.dense(inputs = d3, units = 1, activation = tf.nn.sigmoid)
return o1
def generator_gatedcnn(inputs, reuse = False, scope_name = 'generator_gatedcnn'):
# inputs has shape [batch_size, num_features, time]
# we need to convert it to [batch_size, time, num_features] for 1D convolution
inputs = tf.transpose(inputs, perm = [0, 2, 1], name = 'input_transpose')
with tf.variable_scope(scope_name) as scope:
# Discriminator would be reused in CycleGAN
if reuse:
scope.reuse_variables()
else:
assert scope.reuse is False
h1 = conv1d_layer(inputs = inputs, filters = 128, kernel_size = 15, strides = 1, activation = None, name = 'h1_conv')
h1_gates = conv1d_layer(inputs = inputs, filters = 128, kernel_size = 15, strides = 1, activation = None, name = 'h1_conv_gates')
h1_glu = gated_linear_layer(inputs = h1, gates = h1_gates, name = 'h1_glu')
# Downsample
d1 = downsample1d_block(inputs = h1_glu, filters = 256, kernel_size = 5, strides = 2, name_prefix = 'downsample1d_block1_')
d2 = downsample1d_block(inputs = d1, filters = 512, kernel_size = 5, strides = 2, name_prefix = 'downsample1d_block2_')
# Residual blocks
r1 = residual1d_block(inputs = d2, filters = 1024, kernel_size = 3, strides = 1, name_prefix = 'residual1d_block1_')
r2 = residual1d_block(inputs = r1, filters = 1024, kernel_size = 3, strides = 1, name_prefix = 'residual1d_block2_')
r3 = residual1d_block(inputs = r2, filters = 1024, kernel_size = 3, strides = 1, name_prefix = 'residual1d_block3_')
r4 = residual1d_block(inputs = r3, filters = 1024, kernel_size = 3, strides = 1, name_prefix = 'residual1d_block4_')
r5 = residual1d_block(inputs = r4, filters = 1024, kernel_size = 3, strides = 1, name_prefix = 'residual1d_block5_')
r6 = residual1d_block(inputs = r5, filters = 1024, kernel_size = 3, strides = 1, name_prefix = 'residual1d_block6_')
# Upsample
u1 = upsample1d_block(inputs = r6, filters = 1024, kernel_size = 5, strides = 1, shuffle_size = 2, name_prefix = 'upsample1d_block1_')
u2 = upsample1d_block(inputs = u1, filters = 512, kernel_size = 5, strides = 1, shuffle_size = 2, name_prefix = 'upsample1d_block2_')
# Output
o1 = conv1d_layer(inputs = u2, filters = 24, kernel_size = 15, strides = 1, activation = None, name = 'o1_conv')
o2 = tf.transpose(o1, perm = [0, 2, 1], name = 'output_transpose')
return o2
def linear(x, units, use_bias=True, scope='linear'):
with tf.variable_scope(scope):
x = flatten(x)
x = tf.layers.dense(x, units=units, kernel_initializer=weight_init, kernel_regularizer=weight_regularizer, use_bias=use_bias)
return x
def flatten(x) :
return tf.layers.flatten(x)
##################################################################################
# Residual-block
##################################################################################
def resblock(x_init, channels, use_bias=True, scope='resblock'):
with tf.variable_scope(scope):
with tf.variable_scope('res1'):
x = conv(x_init, channels, kernel=3, stride=1, pad=1, pad_type='reflect', use_bias=use_bias)
x = instance_norm(x)
x = relu(x)
with tf.variable_scope('res2'):
x = conv(x, channels, kernel=3, stride=1, pad=1, pad_type='reflect', use_bias=use_bias)
x = instance_norm(x)
return x + x_init
# def adaptive_resblock(x_init, channels, mu, sigma, use_bias=True, name='adaptive_resblock') :
# with tf.variable_scope(scope):
# with tf.variable_scope('res1'):
# x = conv(x_init, channels, kernel=3, stride=1, pad=1, pad_type='reflect', use_bias=use_bias)
# x = adaptive_instance_norm(x, mu, sigma)
# x = relu(x)
# with tf.variable_scope('res2'):
# x = conv(x, channels, kernel=3, stride=1, pad=1, pad_type='reflect', use_bias=use_bias)
# x = adaptive_instance_norm(x, mu, sigma)
# return x + x_init
##################################################################################
# Sampling
##################################################################################
def down_sample(x) :
return tf.layers.average_pooling2d(x, pool_size=3, strides=2, padding='SAME')
def up_sample(x, scale_factor=2):
_, h, w, _ = x.get_shape().as_list()
new_size = [h * scale_factor, w * scale_factor]
return tf.image.resize_nearest_neighbor(x, size=new_size)
def adaptive_avg_pooling(x):
# global average pooling
gap = tf.reduce_mean(x, axis=1, keepdims=True)
return gap
##################################################################################
# Activation function
##################################################################################
def lrelu(x, alpha=0.01):
# pytorch alpha is 0.01
return tf.nn.leaky_relu(x, alpha)
def relu(x):
return tf.nn.relu(x)
def tanh(x):
return tf.tanh(x)
##################################################################################
# Normalization function
##################################################################################
def adaptive_instance_norm(content, gamma, beta, epsilon=1e-5):
# gamma, beta = style_mean, style_std from MLP
c_mean, c_var = tf.nn.moments(content, axes=[1], keep_dims=True)
c_std = tf.sqrt(c_var + epsilon)
return gamma * ((content - c_mean) / c_std) + beta
def instance_norm(x, scope='instance_norm'):
return tf_contrib.layers.instance_norm(x,
epsilon=1e-05,
center=True, scale=True,
scope=scope)
def layer_norm(x, scope='layer_norm') :
return tf_contrib.layers.layer_norm(x,
center=True, scale=True,
scope=scope)
##################################################################################
# Loss function
##################################################################################
"""
Author use LSGAN
For LSGAN, multiply each of G and D by 0.5.
However, MUNIT authors did not do this.
"""
def discriminator_loss(type, real, fake):
n_scale = len(real)
loss = []
real_loss = 0
fake_loss = 0
for i in range(n_scale) :
if type == 'lsgan' :
real_loss = tf.reduce_mean(tf.squared_difference(real[i], 1.0))
fake_loss = tf.reduce_mean(tf.square(fake[i]))
if type == 'gan' :
real_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(real[i]), logits=real[i]))
fake_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.zeros_like(fake[i]), logits=fake[i]))
loss.append(real_loss + fake_loss)
return sum(loss)
def generator_loss(type, fake):
n_scale = len(fake)
loss = []
fake_loss = 0
for i in range(n_scale) :
if type == 'lsgan' :
fake_loss = tf.reduce_mean(tf.squared_difference(fake[i], 1.0))
if type == 'gan' :
fake_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(fake[i]), logits=fake[i]))
loss.append(fake_loss)
return sum(loss)
def L1_loss(x, y):
loss = tf.reduce_mean(tf.abs(x - y))
return loss
########################################################################################################################
########################################################################################################################
def gated_linear_layer(inputs, gates, name=None):
activation = tf.multiply(x=inputs, y=tf.sigmoid(gates), name=name)
return activation
def instance_norm_layer(
inputs,
epsilon=1e-06,
activation_fn=None,
name=None):
instance_norm_layer = tf.contrib.layers.instance_norm(
inputs=inputs,
epsilon=epsilon,
activation_fn=activation_fn,
scope=name)
return instance_norm_layer
def layer_norm_layer(
inputs,
name=None):
layer_norm_layer = tf_contrib.layers.layer_norm(
inputs=inputs,
center=True,
scale=True,
scope=name)
return layer_norm_layer
def conv1d_layer(
inputs,
filters,
kernel_size,
strides=1,
padding='same',
activation=None,
name=None):
if name.__contains__("discriminator"):
weight_init = tf.random_normal_initializer(mean=0.0, stddev=0.02)
else:
weight_init = tf_contrib.layers.variance_scaling_initializer()
conv_layer = tf.layers.conv1d(
inputs=inputs,
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
activation=activation,
kernel_initializer=weight_init,
kernel_regularizer=weight_regularizer,
name=name)
return conv_layer
def conv2d_layer(
inputs,
filters,
kernel_size,
strides,
padding='same',
activation=None,
name=None):
if name.__contains__("discriminator"):
weight_init = tf.random_normal_initializer(mean=0.0, stddev=0.02)
else:
weight_init = tf_contrib.layers.variance_scaling_initializer()
conv_layer = tf.layers.conv2d(
inputs=inputs,
filters=filters,
kernel_size=kernel_size,
strides=strides,
padding=padding,
activation=activation,
kernel_initializer=weight_init,
kernel_regularizer=weight_regularizer,
name=name)
return conv_layer
def residual1d_block(
inputs,
filters=512,
kernel_size=3,
strides=1,
name_prefix='residule_block'):
h1 = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_conv')
h1_norm = instance_norm_layer(inputs=h1, name=name_prefix+'_h1_norm')
h1_gates = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_gates')
h1_norm_gates = instance_norm_layer(inputs=h1_gates, name=name_prefix+'_h1_norm_gates')
h1_glu = gated_linear_layer(inputs=h1_norm, gates=h1_norm_gates, name=name_prefix+'_h1_glu')
h2 = conv1d_layer(inputs=h1_glu, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h2_conv')
h2_norm = instance_norm_layer(inputs=h2, name=name_prefix+'_h2_norm')
h3 = inputs + h2_norm
return h3
def residual1d_block_adaptive(
inputs,
filters=512,
mu=0.0,
sigma=1.0,
kernel_size=3,
strides=1,
name_prefix='residule_block'):
h1 = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_conv')
h1_norm = adaptive_instance_norm(content=h1, gamma=mu, beta=sigma)
h1_gates = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_gates')
h1_norm_gates = adaptive_instance_norm(content=h1_gates, gamma=mu, beta=sigma)
h1_glu = gated_linear_layer(inputs=h1_norm, gates=h1_norm_gates, name=name_prefix+'_h1_glu')
h2 = conv1d_layer(inputs=h1_glu, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h2_conv')
h2_norm = adaptive_instance_norm(content=h2, gamma=mu, beta=sigma)
h3 = inputs + h2_norm
return h3
def downsample1d_block(
inputs,
filters,
kernel_size,
strides,
name_prefix='downsample1d_block'):
h1 = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_conv')
h1_norm = instance_norm_layer(inputs=h1, name=name_prefix+'_h1_norm')
h1_gates = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_gates')
h1_norm_gates = instance_norm_layer(inputs=h1_gates, name=name_prefix+'_h1_norm_gates')
h1_glu = gated_linear_layer(inputs=h1_norm, gates=h1_norm_gates, name=name_prefix+'_h1_glu')
return h1_glu
def downsample1d_block_withoutIN(
inputs,
filters,
kernel_size,
strides,
name_prefix='downsample1d_block_withoutIN'):
h1 = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_conv')
h1_gates = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_gates')
h1_glu = gated_linear_layer(inputs=h1, gates=h1_gates, name=name_prefix + 'h1_glu')
return h1_glu
def downsample2d_block(
inputs,
filters,
kernel_size,
strides,
name_prefix='downsample2d_block'):
h1 = conv2d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_conv')
h1_norm = instance_norm_layer(inputs=h1, name=name_prefix+'_h1_norm')
h1_gates = conv2d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_gates')
h1_norm_gates = instance_norm_layer(inputs=h1_gates, name=name_prefix+'_h1_norm_gates')
h1_glu = gated_linear_layer(inputs=h1_norm, gates=h1_norm_gates, name=name_prefix+'_h1_glu')
return h1_glu
def upsample1d_block(
inputs,
filters,
kernel_size,
strides,
shuffle_size=2,
name_prefix='upsample1d_block'):
h1 = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_conv')
h1_shuffle = pixel_shuffler(inputs=h1, shuffle_size=shuffle_size, name=name_prefix+'_h1_shuffle')
h1_norm = layer_norm_layer(inputs=h1_shuffle, name=name_prefix+'_h1_layer_norm')
h1_gates = conv1d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, name=name_prefix+'_h1_gates')
h1_shuffle_gates = pixel_shuffler(inputs=h1_gates, shuffle_size=shuffle_size, name=name_prefix+'_h1_shuffle_gates')
h1_norm_gates = layer_norm_layer(inputs=h1_shuffle_gates, name=name_prefix+'_h1_layer_norm_gates')
h1_glu = gated_linear_layer(inputs=h1_norm, gates=h1_norm_gates, name=name_prefix+'_h1_glu')
return h1_glu
def pixel_shuffler(inputs, shuffle_size=2, name=None):
n = tf.shape(inputs)[0]
w = tf.shape(inputs)[1]
c = inputs.get_shape().as_list()[2]
oc = c // shuffle_size
ow = w * shuffle_size
outputs = tf.reshape(tensor=inputs, shape=[n, ow, oc], name=name)
return outputs