-
Notifications
You must be signed in to change notification settings - Fork 671
/
gan_64x64.py
631 lines (492 loc) · 27.4 KB
/
gan_64x64.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
import os, sys
sys.path.append(os.getcwd())
import time
import functools
import numpy as np
import tensorflow as tf
import sklearn.datasets
import tflib as lib
import tflib.ops.linear
import tflib.ops.conv2d
import tflib.ops.batchnorm
import tflib.ops.deconv2d
import tflib.save_images
import tflib.small_imagenet
import tflib.ops.layernorm
import tflib.plot
# Download 64x64 ImageNet at http://image-net.org/small/download.php and
# fill in the path to the extracted files here!
DATA_DIR = ''
if len(DATA_DIR) == 0:
raise Exception('Please specify path to data directory in gan_64x64.py!')
MODE = 'wgan-gp' # dcgan, wgan, wgan-gp, lsgan
DIM = 64 # Model dimensionality
CRITIC_ITERS = 5 # How many iterations to train the critic for
N_GPUS = 1 # Number of GPUs
BATCH_SIZE = 64 # Batch size. Must be a multiple of N_GPUS
ITERS = 200000 # How many iterations to train for
LAMBDA = 10 # Gradient penalty lambda hyperparameter
OUTPUT_DIM = 64*64*3 # Number of pixels in each iamge
lib.print_model_settings(locals().copy())
def GeneratorAndDiscriminator():
"""
Choose which generator and discriminator architecture to use by
uncommenting one of these lines.
"""
# For actually generating decent samples, use this one
return GoodGenerator, GoodDiscriminator
# Baseline (G: DCGAN, D: DCGAN)
# return DCGANGenerator, DCGANDiscriminator
# No BN and constant number of filts in G
# return WGANPaper_CrippledDCGANGenerator, DCGANDiscriminator
# 512-dim 4-layer ReLU MLP G
# return FCGenerator, DCGANDiscriminator
# No normalization anywhere
# return functools.partial(DCGANGenerator, bn=False), functools.partial(DCGANDiscriminator, bn=False)
# Gated multiplicative nonlinearities everywhere
# return MultiplicativeDCGANGenerator, MultiplicativeDCGANDiscriminator
# tanh nonlinearities everywhere
# return functools.partial(DCGANGenerator, bn=True, nonlinearity=tf.tanh), \
# functools.partial(DCGANDiscriminator, bn=True, nonlinearity=tf.tanh)
# 101-layer ResNet G and D
# return ResnetGenerator, ResnetDiscriminator
raise Exception('You must choose an architecture!')
DEVICES = ['/gpu:{}'.format(i) for i in xrange(N_GPUS)]
def LeakyReLU(x, alpha=0.2):
return tf.maximum(alpha*x, x)
def ReLULayer(name, n_in, n_out, inputs):
output = lib.ops.linear.Linear(name+'.Linear', n_in, n_out, inputs, initialization='he')
return tf.nn.relu(output)
def LeakyReLULayer(name, n_in, n_out, inputs):
output = lib.ops.linear.Linear(name+'.Linear', n_in, n_out, inputs, initialization='he')
return LeakyReLU(output)
def Normalize(name, axes, inputs):
if ('Discriminator' in name) and (MODE == 'wgan-gp'):
if axes != [0,2,3]:
raise Exception('Layernorm over non-standard axes is unsupported')
return lib.ops.layernorm.Layernorm(name,[1,2,3],inputs)
else:
return lib.ops.batchnorm.Batchnorm(name,axes,inputs,fused=True)
def pixcnn_gated_nonlinearity(a, b):
return tf.sigmoid(a) * tf.tanh(b)
def SubpixelConv2D(*args, **kwargs):
kwargs['output_dim'] = 4*kwargs['output_dim']
output = lib.ops.conv2d.Conv2D(*args, **kwargs)
output = tf.transpose(output, [0,2,3,1])
output = tf.depth_to_space(output, 2)
output = tf.transpose(output, [0,3,1,2])
return output
def ConvMeanPool(name, input_dim, output_dim, filter_size, inputs, he_init=True, biases=True):
output = lib.ops.conv2d.Conv2D(name, input_dim, output_dim, filter_size, inputs, he_init=he_init, biases=biases)
output = tf.add_n([output[:,:,::2,::2], output[:,:,1::2,::2], output[:,:,::2,1::2], output[:,:,1::2,1::2]]) / 4.
return output
def MeanPoolConv(name, input_dim, output_dim, filter_size, inputs, he_init=True, biases=True):
output = inputs
output = tf.add_n([output[:,:,::2,::2], output[:,:,1::2,::2], output[:,:,::2,1::2], output[:,:,1::2,1::2]]) / 4.
output = lib.ops.conv2d.Conv2D(name, input_dim, output_dim, filter_size, output, he_init=he_init, biases=biases)
return output
def UpsampleConv(name, input_dim, output_dim, filter_size, inputs, he_init=True, biases=True):
output = inputs
output = tf.concat([output, output, output, output], axis=1)
output = tf.transpose(output, [0,2,3,1])
output = tf.depth_to_space(output, 2)
output = tf.transpose(output, [0,3,1,2])
output = lib.ops.conv2d.Conv2D(name, input_dim, output_dim, filter_size, output, he_init=he_init, biases=biases)
return output
def BottleneckResidualBlock(name, input_dim, output_dim, filter_size, inputs, resample=None, he_init=True):
"""
resample: None, 'down', or 'up'
"""
if resample=='down':
conv_shortcut = functools.partial(lib.ops.conv2d.Conv2D, stride=2)
conv_1 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim, output_dim=input_dim/2)
conv_1b = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim/2, output_dim=output_dim/2, stride=2)
conv_2 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=output_dim/2, output_dim=output_dim)
elif resample=='up':
conv_shortcut = SubpixelConv2D
conv_1 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim, output_dim=input_dim/2)
conv_1b = functools.partial(lib.ops.deconv2d.Deconv2D, input_dim=input_dim/2, output_dim=output_dim/2)
conv_2 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=output_dim/2, output_dim=output_dim)
elif resample==None:
conv_shortcut = lib.ops.conv2d.Conv2D
conv_1 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim, output_dim=input_dim/2)
conv_1b = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim/2, output_dim=output_dim/2)
conv_2 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim/2, output_dim=output_dim)
else:
raise Exception('invalid resample value')
if output_dim==input_dim and resample==None:
shortcut = inputs # Identity skip-connection
else:
shortcut = conv_shortcut(name+'.Shortcut', input_dim=input_dim, output_dim=output_dim, filter_size=1,
he_init=False, biases=True, inputs=inputs)
output = inputs
output = tf.nn.relu(output)
output = conv_1(name+'.Conv1', filter_size=1, inputs=output, he_init=he_init)
output = tf.nn.relu(output)
output = conv_1b(name+'.Conv1B', filter_size=filter_size, inputs=output, he_init=he_init)
output = tf.nn.relu(output)
output = conv_2(name+'.Conv2', filter_size=1, inputs=output, he_init=he_init, biases=False)
output = Normalize(name+'.BN', [0,2,3], output)
return shortcut + (0.3*output)
def ResidualBlock(name, input_dim, output_dim, filter_size, inputs, resample=None, he_init=True):
"""
resample: None, 'down', or 'up'
"""
if resample=='down':
conv_shortcut = MeanPoolConv
conv_1 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim, output_dim=input_dim)
conv_2 = functools.partial(ConvMeanPool, input_dim=input_dim, output_dim=output_dim)
elif resample=='up':
conv_shortcut = UpsampleConv
conv_1 = functools.partial(UpsampleConv, input_dim=input_dim, output_dim=output_dim)
conv_2 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=output_dim, output_dim=output_dim)
elif resample==None:
conv_shortcut = lib.ops.conv2d.Conv2D
conv_1 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim, output_dim=input_dim)
conv_2 = functools.partial(lib.ops.conv2d.Conv2D, input_dim=input_dim, output_dim=output_dim)
else:
raise Exception('invalid resample value')
if output_dim==input_dim and resample==None:
shortcut = inputs # Identity skip-connection
else:
shortcut = conv_shortcut(name+'.Shortcut', input_dim=input_dim, output_dim=output_dim, filter_size=1,
he_init=False, biases=True, inputs=inputs)
output = inputs
output = Normalize(name+'.BN1', [0,2,3], output)
output = tf.nn.relu(output)
output = conv_1(name+'.Conv1', filter_size=filter_size, inputs=output, he_init=he_init, biases=False)
output = Normalize(name+'.BN2', [0,2,3], output)
output = tf.nn.relu(output)
output = conv_2(name+'.Conv2', filter_size=filter_size, inputs=output, he_init=he_init)
return shortcut + output
# ! Generators
def GoodGenerator(n_samples, noise=None, dim=DIM, nonlinearity=tf.nn.relu):
if noise is None:
noise = tf.random_normal([n_samples, 128])
output = lib.ops.linear.Linear('Generator.Input', 128, 4*4*8*dim, noise)
output = tf.reshape(output, [-1, 8*dim, 4, 4])
output = ResidualBlock('Generator.Res1', 8*dim, 8*dim, 3, output, resample='up')
output = ResidualBlock('Generator.Res2', 8*dim, 4*dim, 3, output, resample='up')
output = ResidualBlock('Generator.Res3', 4*dim, 2*dim, 3, output, resample='up')
output = ResidualBlock('Generator.Res4', 2*dim, 1*dim, 3, output, resample='up')
output = Normalize('Generator.OutputN', [0,2,3], output)
output = tf.nn.relu(output)
output = lib.ops.conv2d.Conv2D('Generator.Output', 1*dim, 3, 3, output)
output = tf.tanh(output)
return tf.reshape(output, [-1, OUTPUT_DIM])
def FCGenerator(n_samples, noise=None, FC_DIM=512):
if noise is None:
noise = tf.random_normal([n_samples, 128])
output = ReLULayer('Generator.1', 128, FC_DIM, noise)
output = ReLULayer('Generator.2', FC_DIM, FC_DIM, output)
output = ReLULayer('Generator.3', FC_DIM, FC_DIM, output)
output = ReLULayer('Generator.4', FC_DIM, FC_DIM, output)
output = lib.ops.linear.Linear('Generator.Out', FC_DIM, OUTPUT_DIM, output)
output = tf.tanh(output)
return output
def DCGANGenerator(n_samples, noise=None, dim=DIM, bn=True, nonlinearity=tf.nn.relu):
lib.ops.conv2d.set_weights_stdev(0.02)
lib.ops.deconv2d.set_weights_stdev(0.02)
lib.ops.linear.set_weights_stdev(0.02)
if noise is None:
noise = tf.random_normal([n_samples, 128])
output = lib.ops.linear.Linear('Generator.Input', 128, 4*4*8*dim, noise)
output = tf.reshape(output, [-1, 8*dim, 4, 4])
if bn:
output = Normalize('Generator.BN1', [0,2,3], output)
output = nonlinearity(output)
output = lib.ops.deconv2d.Deconv2D('Generator.2', 8*dim, 4*dim, 5, output)
if bn:
output = Normalize('Generator.BN2', [0,2,3], output)
output = nonlinearity(output)
output = lib.ops.deconv2d.Deconv2D('Generator.3', 4*dim, 2*dim, 5, output)
if bn:
output = Normalize('Generator.BN3', [0,2,3], output)
output = nonlinearity(output)
output = lib.ops.deconv2d.Deconv2D('Generator.4', 2*dim, dim, 5, output)
if bn:
output = Normalize('Generator.BN4', [0,2,3], output)
output = nonlinearity(output)
output = lib.ops.deconv2d.Deconv2D('Generator.5', dim, 3, 5, output)
output = tf.tanh(output)
lib.ops.conv2d.unset_weights_stdev()
lib.ops.deconv2d.unset_weights_stdev()
lib.ops.linear.unset_weights_stdev()
return tf.reshape(output, [-1, OUTPUT_DIM])
def WGANPaper_CrippledDCGANGenerator(n_samples, noise=None, dim=DIM):
if noise is None:
noise = tf.random_normal([n_samples, 128])
output = lib.ops.linear.Linear('Generator.Input', 128, 4*4*dim, noise)
output = tf.nn.relu(output)
output = tf.reshape(output, [-1, dim, 4, 4])
output = lib.ops.deconv2d.Deconv2D('Generator.2', dim, dim, 5, output)
output = tf.nn.relu(output)
output = lib.ops.deconv2d.Deconv2D('Generator.3', dim, dim, 5, output)
output = tf.nn.relu(output)
output = lib.ops.deconv2d.Deconv2D('Generator.4', dim, dim, 5, output)
output = tf.nn.relu(output)
output = lib.ops.deconv2d.Deconv2D('Generator.5', dim, 3, 5, output)
output = tf.tanh(output)
return tf.reshape(output, [-1, OUTPUT_DIM])
def ResnetGenerator(n_samples, noise=None, dim=DIM):
if noise is None:
noise = tf.random_normal([n_samples, 128])
output = lib.ops.linear.Linear('Generator.Input', 128, 4*4*8*dim, noise)
output = tf.reshape(output, [-1, 8*dim, 4, 4])
for i in xrange(6):
output = BottleneckResidualBlock('Generator.4x4_{}'.format(i), 8*dim, 8*dim, 3, output, resample=None)
output = BottleneckResidualBlock('Generator.Up1', 8*dim, 4*dim, 3, output, resample='up')
for i in xrange(6):
output = BottleneckResidualBlock('Generator.8x8_{}'.format(i), 4*dim, 4*dim, 3, output, resample=None)
output = BottleneckResidualBlock('Generator.Up2', 4*dim, 2*dim, 3, output, resample='up')
for i in xrange(6):
output = BottleneckResidualBlock('Generator.16x16_{}'.format(i), 2*dim, 2*dim, 3, output, resample=None)
output = BottleneckResidualBlock('Generator.Up3', 2*dim, 1*dim, 3, output, resample='up')
for i in xrange(6):
output = BottleneckResidualBlock('Generator.32x32_{}'.format(i), 1*dim, 1*dim, 3, output, resample=None)
output = BottleneckResidualBlock('Generator.Up4', 1*dim, dim/2, 3, output, resample='up')
for i in xrange(5):
output = BottleneckResidualBlock('Generator.64x64_{}'.format(i), dim/2, dim/2, 3, output, resample=None)
output = lib.ops.conv2d.Conv2D('Generator.Out', dim/2, 3, 1, output, he_init=False)
output = tf.tanh(output / 5.)
return tf.reshape(output, [-1, OUTPUT_DIM])
def MultiplicativeDCGANGenerator(n_samples, noise=None, dim=DIM, bn=True):
if noise is None:
noise = tf.random_normal([n_samples, 128])
output = lib.ops.linear.Linear('Generator.Input', 128, 4*4*8*dim*2, noise)
output = tf.reshape(output, [-1, 8*dim*2, 4, 4])
if bn:
output = Normalize('Generator.BN1', [0,2,3], output)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = lib.ops.deconv2d.Deconv2D('Generator.2', 8*dim, 4*dim*2, 5, output)
if bn:
output = Normalize('Generator.BN2', [0,2,3], output)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = lib.ops.deconv2d.Deconv2D('Generator.3', 4*dim, 2*dim*2, 5, output)
if bn:
output = Normalize('Generator.BN3', [0,2,3], output)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = lib.ops.deconv2d.Deconv2D('Generator.4', 2*dim, dim*2, 5, output)
if bn:
output = Normalize('Generator.BN4', [0,2,3], output)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = lib.ops.deconv2d.Deconv2D('Generator.5', dim, 3, 5, output)
output = tf.tanh(output)
return tf.reshape(output, [-1, OUTPUT_DIM])
# ! Discriminators
def GoodDiscriminator(inputs, dim=DIM):
output = tf.reshape(inputs, [-1, 3, 64, 64])
output = lib.ops.conv2d.Conv2D('Discriminator.Input', 3, dim, 3, output, he_init=False)
output = ResidualBlock('Discriminator.Res1', dim, 2*dim, 3, output, resample='down')
output = ResidualBlock('Discriminator.Res2', 2*dim, 4*dim, 3, output, resample='down')
output = ResidualBlock('Discriminator.Res3', 4*dim, 8*dim, 3, output, resample='down')
output = ResidualBlock('Discriminator.Res4', 8*dim, 8*dim, 3, output, resample='down')
output = tf.reshape(output, [-1, 4*4*8*dim])
output = lib.ops.linear.Linear('Discriminator.Output', 4*4*8*dim, 1, output)
return tf.reshape(output, [-1])
def MultiplicativeDCGANDiscriminator(inputs, dim=DIM, bn=True):
output = tf.reshape(inputs, [-1, 3, 64, 64])
output = lib.ops.conv2d.Conv2D('Discriminator.1', 3, dim*2, 5, output, stride=2)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = lib.ops.conv2d.Conv2D('Discriminator.2', dim, 2*dim*2, 5, output, stride=2)
if bn:
output = Normalize('Discriminator.BN2', [0,2,3], output)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = lib.ops.conv2d.Conv2D('Discriminator.3', 2*dim, 4*dim*2, 5, output, stride=2)
if bn:
output = Normalize('Discriminator.BN3', [0,2,3], output)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = lib.ops.conv2d.Conv2D('Discriminator.4', 4*dim, 8*dim*2, 5, output, stride=2)
if bn:
output = Normalize('Discriminator.BN4', [0,2,3], output)
output = pixcnn_gated_nonlinearity(output[:,::2], output[:,1::2])
output = tf.reshape(output, [-1, 4*4*8*dim])
output = lib.ops.linear.Linear('Discriminator.Output', 4*4*8*dim, 1, output)
return tf.reshape(output, [-1])
def ResnetDiscriminator(inputs, dim=DIM):
output = tf.reshape(inputs, [-1, 3, 64, 64])
output = lib.ops.conv2d.Conv2D('Discriminator.In', 3, dim/2, 1, output, he_init=False)
for i in xrange(5):
output = BottleneckResidualBlock('Discriminator.64x64_{}'.format(i), dim/2, dim/2, 3, output, resample=None)
output = BottleneckResidualBlock('Discriminator.Down1', dim/2, dim*1, 3, output, resample='down')
for i in xrange(6):
output = BottleneckResidualBlock('Discriminator.32x32_{}'.format(i), dim*1, dim*1, 3, output, resample=None)
output = BottleneckResidualBlock('Discriminator.Down2', dim*1, dim*2, 3, output, resample='down')
for i in xrange(6):
output = BottleneckResidualBlock('Discriminator.16x16_{}'.format(i), dim*2, dim*2, 3, output, resample=None)
output = BottleneckResidualBlock('Discriminator.Down3', dim*2, dim*4, 3, output, resample='down')
for i in xrange(6):
output = BottleneckResidualBlock('Discriminator.8x8_{}'.format(i), dim*4, dim*4, 3, output, resample=None)
output = BottleneckResidualBlock('Discriminator.Down4', dim*4, dim*8, 3, output, resample='down')
for i in xrange(6):
output = BottleneckResidualBlock('Discriminator.4x4_{}'.format(i), dim*8, dim*8, 3, output, resample=None)
output = tf.reshape(output, [-1, 4*4*8*dim])
output = lib.ops.linear.Linear('Discriminator.Output', 4*4*8*dim, 1, output)
return tf.reshape(output / 5., [-1])
def FCDiscriminator(inputs, FC_DIM=512, n_layers=3):
output = LeakyReLULayer('Discriminator.Input', OUTPUT_DIM, FC_DIM, inputs)
for i in xrange(n_layers):
output = LeakyReLULayer('Discriminator.{}'.format(i), FC_DIM, FC_DIM, output)
output = lib.ops.linear.Linear('Discriminator.Out', FC_DIM, 1, output)
return tf.reshape(output, [-1])
def DCGANDiscriminator(inputs, dim=DIM, bn=True, nonlinearity=LeakyReLU):
output = tf.reshape(inputs, [-1, 3, 64, 64])
lib.ops.conv2d.set_weights_stdev(0.02)
lib.ops.deconv2d.set_weights_stdev(0.02)
lib.ops.linear.set_weights_stdev(0.02)
output = lib.ops.conv2d.Conv2D('Discriminator.1', 3, dim, 5, output, stride=2)
output = nonlinearity(output)
output = lib.ops.conv2d.Conv2D('Discriminator.2', dim, 2*dim, 5, output, stride=2)
if bn:
output = Normalize('Discriminator.BN2', [0,2,3], output)
output = nonlinearity(output)
output = lib.ops.conv2d.Conv2D('Discriminator.3', 2*dim, 4*dim, 5, output, stride=2)
if bn:
output = Normalize('Discriminator.BN3', [0,2,3], output)
output = nonlinearity(output)
output = lib.ops.conv2d.Conv2D('Discriminator.4', 4*dim, 8*dim, 5, output, stride=2)
if bn:
output = Normalize('Discriminator.BN4', [0,2,3], output)
output = nonlinearity(output)
output = tf.reshape(output, [-1, 4*4*8*dim])
output = lib.ops.linear.Linear('Discriminator.Output', 4*4*8*dim, 1, output)
lib.ops.conv2d.unset_weights_stdev()
lib.ops.deconv2d.unset_weights_stdev()
lib.ops.linear.unset_weights_stdev()
return tf.reshape(output, [-1])
Generator, Discriminator = GeneratorAndDiscriminator()
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as session:
all_real_data_conv = tf.placeholder(tf.int32, shape=[BATCH_SIZE, 3, 64, 64])
if tf.__version__.startswith('1.'):
split_real_data_conv = tf.split(all_real_data_conv, len(DEVICES))
else:
split_real_data_conv = tf.split(0, len(DEVICES), all_real_data_conv)
gen_costs, disc_costs = [],[]
for device_index, (device, real_data_conv) in enumerate(zip(DEVICES, split_real_data_conv)):
with tf.device(device):
real_data = tf.reshape(2*((tf.cast(real_data_conv, tf.float32)/255.)-.5), [BATCH_SIZE/len(DEVICES), OUTPUT_DIM])
fake_data = Generator(BATCH_SIZE/len(DEVICES))
disc_real = Discriminator(real_data)
disc_fake = Discriminator(fake_data)
if MODE == 'wgan':
gen_cost = -tf.reduce_mean(disc_fake)
disc_cost = tf.reduce_mean(disc_fake) - tf.reduce_mean(disc_real)
elif MODE == 'wgan-gp':
gen_cost = -tf.reduce_mean(disc_fake)
disc_cost = tf.reduce_mean(disc_fake) - tf.reduce_mean(disc_real)
alpha = tf.random_uniform(
shape=[BATCH_SIZE/len(DEVICES),1],
minval=0.,
maxval=1.
)
differences = fake_data - real_data
interpolates = real_data + (alpha*differences)
gradients = tf.gradients(Discriminator(interpolates), [interpolates])[0]
slopes = tf.sqrt(tf.reduce_sum(tf.square(gradients), reduction_indices=[1]))
gradient_penalty = tf.reduce_mean((slopes-1.)**2)
disc_cost += LAMBDA*gradient_penalty
elif MODE == 'dcgan':
try: # tf pre-1.0 (bottom) vs 1.0 (top)
gen_cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=disc_fake,
labels=tf.ones_like(disc_fake)))
disc_cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=disc_fake,
labels=tf.zeros_like(disc_fake)))
disc_cost += tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=disc_real,
labels=tf.ones_like(disc_real)))
except Exception as e:
gen_cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(disc_fake, tf.ones_like(disc_fake)))
disc_cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(disc_fake, tf.zeros_like(disc_fake)))
disc_cost += tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(disc_real, tf.ones_like(disc_real)))
disc_cost /= 2.
elif MODE == 'lsgan':
gen_cost = tf.reduce_mean((disc_fake - 1)**2)
disc_cost = (tf.reduce_mean((disc_real - 1)**2) + tf.reduce_mean((disc_fake - 0)**2))/2.
else:
raise Exception()
gen_costs.append(gen_cost)
disc_costs.append(disc_cost)
gen_cost = tf.add_n(gen_costs) / len(DEVICES)
disc_cost = tf.add_n(disc_costs) / len(DEVICES)
if MODE == 'wgan':
gen_train_op = tf.train.RMSPropOptimizer(learning_rate=5e-5).minimize(gen_cost,
var_list=lib.params_with_name('Generator'), colocate_gradients_with_ops=True)
disc_train_op = tf.train.RMSPropOptimizer(learning_rate=5e-5).minimize(disc_cost,
var_list=lib.params_with_name('Discriminator.'), colocate_gradients_with_ops=True)
clip_ops = []
for var in lib.params_with_name('Discriminator'):
clip_bounds = [-.01, .01]
clip_ops.append(tf.assign(var, tf.clip_by_value(var, clip_bounds[0], clip_bounds[1])))
clip_disc_weights = tf.group(*clip_ops)
elif MODE == 'wgan-gp':
gen_train_op = tf.train.AdamOptimizer(learning_rate=1e-4, beta1=0., beta2=0.9).minimize(gen_cost,
var_list=lib.params_with_name('Generator'), colocate_gradients_with_ops=True)
disc_train_op = tf.train.AdamOptimizer(learning_rate=1e-4, beta1=0., beta2=0.9).minimize(disc_cost,
var_list=lib.params_with_name('Discriminator.'), colocate_gradients_with_ops=True)
elif MODE == 'dcgan':
gen_train_op = tf.train.AdamOptimizer(learning_rate=2e-4, beta1=0.5).minimize(gen_cost,
var_list=lib.params_with_name('Generator'), colocate_gradients_with_ops=True)
disc_train_op = tf.train.AdamOptimizer(learning_rate=2e-4, beta1=0.5).minimize(disc_cost,
var_list=lib.params_with_name('Discriminator.'), colocate_gradients_with_ops=True)
elif MODE == 'lsgan':
gen_train_op = tf.train.RMSPropOptimizer(learning_rate=1e-4).minimize(gen_cost,
var_list=lib.params_with_name('Generator'), colocate_gradients_with_ops=True)
disc_train_op = tf.train.RMSPropOptimizer(learning_rate=1e-4).minimize(disc_cost,
var_list=lib.params_with_name('Discriminator.'), colocate_gradients_with_ops=True)
else:
raise Exception()
# For generating samples
fixed_noise = tf.constant(np.random.normal(size=(BATCH_SIZE, 128)).astype('float32'))
all_fixed_noise_samples = []
for device_index, device in enumerate(DEVICES):
n_samples = BATCH_SIZE / len(DEVICES)
all_fixed_noise_samples.append(Generator(n_samples, noise=fixed_noise[device_index*n_samples:(device_index+1)*n_samples]))
if tf.__version__.startswith('1.'):
all_fixed_noise_samples = tf.concat(all_fixed_noise_samples, axis=0)
else:
all_fixed_noise_samples = tf.concat(0, all_fixed_noise_samples)
def generate_image(iteration):
samples = session.run(all_fixed_noise_samples)
samples = ((samples+1.)*(255.99/2)).astype('int32')
lib.save_images.save_images(samples.reshape((BATCH_SIZE, 3, 64, 64)), 'samples_{}.png'.format(iteration))
# Dataset iterator
train_gen, dev_gen = lib.small_imagenet.load(BATCH_SIZE, data_dir=DATA_DIR)
def inf_train_gen():
while True:
for (images,) in train_gen():
yield images
# Save a batch of ground-truth samples
_x = inf_train_gen().next()
_x_r = session.run(real_data, feed_dict={real_data_conv: _x[:BATCH_SIZE/N_GPUS]})
_x_r = ((_x_r+1.)*(255.99/2)).astype('int32')
lib.save_images.save_images(_x_r.reshape((BATCH_SIZE/N_GPUS, 3, 64, 64)), 'samples_groundtruth.png')
# Train loop
session.run(tf.initialize_all_variables())
gen = inf_train_gen()
for iteration in xrange(ITERS):
start_time = time.time()
# Train generator
if iteration > 0:
_ = session.run(gen_train_op)
# Train critic
if (MODE == 'dcgan') or (MODE == 'lsgan'):
disc_iters = 1
else:
disc_iters = CRITIC_ITERS
for i in xrange(disc_iters):
_data = gen.next()
_disc_cost, _ = session.run([disc_cost, disc_train_op], feed_dict={all_real_data_conv: _data})
if MODE == 'wgan':
_ = session.run([clip_disc_weights])
lib.plot.plot('train disc cost', _disc_cost)
lib.plot.plot('time', time.time() - start_time)
if iteration % 200 == 199:
t = time.time()
dev_disc_costs = []
for (images,) in dev_gen():
_dev_disc_cost = session.run(disc_cost, feed_dict={all_real_data_conv: images})
dev_disc_costs.append(_dev_disc_cost)
lib.plot.plot('dev disc cost', np.mean(dev_disc_costs))
generate_image(iteration)
if (iteration < 5) or (iteration % 200 == 199):
lib.plot.flush()
lib.plot.tick()