-
Notifications
You must be signed in to change notification settings - Fork 18
/
gan.py
executable file
·1572 lines (1331 loc) · 66.6 KB
/
gan.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2017 Max Planck Society
# Distributed under the BSD-3 Software license,
# (See accompanying file ./LICENSE.txt or copy at
# https://opensource.org/licenses/BSD-3-Clause)
"""This class implements Generative Adversarial Networks training.
"""
import logging
import tensorflow as tf
import utils
from utils import ProgressBar
from utils import TQDM
import numpy as np
import ops
from metrics import Metrics
class Gan(object):
"""A base class for running individual GANs.
This class announces all the necessary bits for running individual
GAN trainers. It is assumed that a GAN trainer should receive the
data points and the corresponding weights, which are used for
importance sampling of minibatches during the training. All the
methods should be implemented in the subclasses.
"""
def __init__(self, opts, data, weights):
# Create a new session with session.graph = default graph
self._session = tf.Session()
self._trained = False
self._data = data
self._data_weights = np.copy(weights)
# Latent noise sampled ones to apply G while training
self._noise_for_plots = utils.generate_noise(opts, 500)
# Placeholders
self._real_points_ph = None
self._fake_points_ph = None
self._noise_ph = None
self._inv_target_ph = None
# Main operations
self._G = None # Generator function
self._d_loss = None # Loss of discriminator
self._g_loss = None # Loss of generator
self._c_loss = None # Loss of mixture discriminator
self._c_training = None # Outputs of the mixture discriminator on data
self._inv_loss = None
self._inv_loss_per_point = None
# Variables
self._inv_z = None
# Optimizers
self._g_optim = None
self._d_optim = None
self._c_optim = None
self._inv_optim = None
with self._session.as_default(), self._session.graph.as_default():
logging.debug('Building the graph...')
self._build_model_internal(opts)
if opts['inverse_metric']:
assert opts['dataset'] in ('mnist', 'mnist3', 'guitars'),\
'Invertion currently supported only for mnist, mnist3, guitars'
logging.debug('Adding inversion ops to the graph...')
self._add_inversion_ops(opts)
# Make sure AdamOptimizer, if used in the Graph, is defined before
# calling global_variables_initializer().
init = tf.global_variables_initializer()
self._session.run(init)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
# Cleaning the whole default Graph
logging.debug('Cleaning the graph...')
tf.reset_default_graph()
logging.debug('Closing the session...')
# Finishing the session
self._session.close()
def train(self, opts):
"""Train a GAN model.
"""
with self._session.as_default(), self._session.graph.as_default():
self._train_internal(opts)
self._trained = True
def sample(self, opts, num=100):
"""Sample points from the trained GAN model.
"""
assert self._trained, 'Can not sample from the un-trained GAN'
with self._session.as_default(), self._session.graph.as_default():
return self._sample_internal(opts, num)
def train_mixture_discriminator(self, opts, fake_images):
"""Train classifier separating true data from points in fake_images.
Return:
prob_real: probabilities of the points from training data being the
real points according to the trained mixture classifier.
Numpy vector of shape (self._data.num_points,)
prob_fake: probabilities of the points from fake_images being the
real points according to the trained mixture classifier.
Numpy vector of shape (len(fake_images),)
"""
with self._session.as_default(), self._session.graph.as_default():
return self._train_mixture_discriminator_internal(opts, fake_images)
def invert_points(self, opts, images):
"""Invert the learned generator function for every image in images.
Args:
images: numpy array of shape [num_points] + data_shape
"""
assert self._trained, 'Can not invert, not trained yet.'
assert len(images) == opts['inverse_num'],\
'Currently inversion works only for fixed number of images'
with self._session.as_default(), self._session.graph.as_default():
target_ph = self._inv_target_ph
z = self._inv_z
loss_per_point = self._inv_loss_per_point
optim = self._inv_optim
norms = self._inv_norms
val_list = []
err_per_point_list = []
z_list = []
norms_list = []
for _start in xrange(5):
inv_vars = tf.get_collection(
tf.GraphKeys.GLOBAL_VARIABLES, scope="inversion")
# Initialize z and optimizer's variables randomly
self._session.run(tf.variables_initializer(inv_vars))
prev_val = 100.
check_every = 100
steps = 1
while True:
# Stopping criterion: relative improvement of the maximal
# per point mse gets smaller than a threshold
self._session.run(
optim, feed_dict={target_ph:images})
if steps % check_every == 0:
err_per_point = loss_per_point.eval(
feed_dict={target_ph:images})
err_max = np.max(err_per_point)
err = np.mean(err_per_point)
logging.debug('Init %02d, steps %d, loss %f, max mse %f' %\
(_start, steps, err, err_max))
relative_improvement = np.abs(prev_val - err) / prev_val
if relative_improvement < 1e-3 or steps > 10000:
val_list.append(err)
err_per_point_list.append(err_per_point)
z_list.append(self._session.run(z))
norms_list.append(self._session.run(norms))
break
prev_val = err
steps += 1
# Choose the run where we got the best (i.e. minimal) maximal
# per point mse
best_id = sorted(zip(val_list, range(len(val_list))))[0][1]
best_err_per_point = err_per_point_list[best_id]
best_z = z_list[best_id]
best_norms = norms_list[best_id]
best_reconstructions = self._G.eval(
feed_dict={self._noise_ph:best_z,
self._is_training_ph:False})
return best_reconstructions, best_z, best_err_per_point, best_norms
def _add_inversion_ops(self, opts):
data_shape = self._data.data_shape
with tf.variable_scope("inversion"):
target_ph = tf.placeholder(
tf.float32, [None] + list(data_shape),
name='target_ph')
z = tf.get_variable(
"inverted", [opts['inverse_num'], opts['latent_space_dim']],
tf.float32, tf.random_normal_initializer(stddev=1.))
reconstructed_images = self.generator(
opts, z, is_training=False, reuse=True)
with tf.variable_scope("inversion"):
loss_per_point = tf.reduce_mean(
tf.square(tf.subtract(reconstructed_images, target_ph)),
axis=[1, 2, 3])
loss = tf.reduce_mean(loss_per_point)
norms = tf.reduce_sum(tf.square(z), axis=[1])
optim = tf.train.AdamOptimizer(0.01, 0.9)
optim = optim.minimize(loss, var_list=[z])
self._inv_target_ph = target_ph
self._inv_z = z
self._inv_optim = optim
self._inv_loss = loss
self._inv_loss_per_point = loss_per_point
self._inv_norms = norms
def _run_batch(self, opts, operation, placeholder, feed,
placeholder2=None, feed2=None):
"""Wrapper around session.run to process huge data.
It is asumed that (a) first dimension of placeholder enumerates
separate points, and (b) that operation is independently applied
to every point, i.e. we can split it point-wisely and then merge
the results. The second placeholder is meant either for is_train
flag for batch-norm or probabilities of dropout.
TODO: write util function which will be called both from this method
and MNIST classification evaluation as well.
"""
assert len(feed.shape) > 0, 'Empry feed.'
num_points = feed.shape[0]
batch_size = opts['tf_run_batch_size']
batches_num = int(np.ceil((num_points + 0.) / batch_size))
result = []
for idx in xrange(batches_num):
if idx == batches_num - 1:
if feed2 is None:
res = self._session.run(
operation,
feed_dict={placeholder: feed[idx * batch_size:]})
else:
res = self._session.run(
operation,
feed_dict={placeholder: feed[idx * batch_size:],
placeholder2: feed2})
else:
if feed2 is None:
res = self._session.run(
operation,
feed_dict={placeholder: feed[idx * batch_size:
(idx + 1) * batch_size]})
else:
res = self._session.run(
operation,
feed_dict={placeholder: feed[idx * batch_size:
(idx + 1) * batch_size],
placeholder2: feed2})
if len(res.shape) == 1:
# convert (n,) vector to (n,1) array
res = np.reshape(res, [-1, 1])
result.append(res)
result = np.vstack(result)
assert len(result) == num_points
return result
def _build_model_internal(self, opts):
"""Build a TensorFlow graph with all the necessary ops.
"""
assert False, 'Gan base class has no build_model method defined.'
def _train_internal(self, opts):
assert False, 'Gan base class has no train method defined.'
def _sample_internal(self, opts, num):
assert False, 'Gan base class has no sample method defined.'
def _train_mixture_discriminator_internal(self, opts, fake_images):
assert False, 'Gan base class has no mixture discriminator method defined.'
class ToyGan(Gan):
"""A simple GAN implementation, suitable for toy datasets.
"""
def generator(self, opts, noise, reuse=False):
"""Generator function, suitable for simple toy experiments.
Args:
noise: [num_points, dim] array, where dim is dimensionality of the
latent noise space.
Returns:
[num_points, dim1, dim2, dim3] array, where the first coordinate
indexes the points, which all are of the shape (dim1, dim2, dim3).
"""
output_shape = self._data.data_shape
num_filters = opts['g_num_filters']
with tf.variable_scope("GENERATOR", reuse=reuse):
h0 = ops.linear(opts, noise, num_filters, 'h0_lin')
h0 = tf.nn.relu(h0)
h1 = ops.linear(opts, h0, num_filters, 'h1_lin')
h1 = tf.nn.relu(h1)
h2 = ops.linear(opts, h1, np.prod(output_shape), 'h2_lin')
h2 = tf.reshape(h2, [-1] + list(output_shape))
if opts['input_normalize_sym']:
return tf.nn.tanh(h2)
else:
return h2
def discriminator(self, opts, input_,
prefix='DISCRIMINATOR', reuse=False):
"""Discriminator function, suitable for simple toy experiments.
"""
shape = input_.get_shape().as_list()
num_filters = opts['d_num_filters']
assert len(shape) > 0, 'No inputs to discriminate.'
with tf.variable_scope(prefix, reuse=reuse):
h0 = ops.linear(opts, input_, num_filters, 'h0_lin')
h0 = tf.nn.relu(h0)
h1 = ops.linear(opts, h0, num_filters, 'h1_lin')
h1 = tf.nn.relu(h1)
h2 = ops.linear(opts, h1, 1, 'h2_lin')
return h2
def _build_model_internal(self, opts):
"""Build the Graph corresponding to GAN implementation.
"""
data_shape = self._data.data_shape
# Placeholders
real_points_ph = tf.placeholder(
tf.float32, [None] + list(data_shape), name='real_points_ph')
fake_points_ph = tf.placeholder(
tf.float32, [None] + list(data_shape), name='fake_points_ph')
noise_ph = tf.placeholder(
tf.float32, [None] + [opts['latent_space_dim']], name='noise_ph')
# Operations
G = self.generator(opts, noise_ph)
d_logits_real = self.discriminator(opts, real_points_ph)
d_logits_fake = self.discriminator(opts, G, reuse=True)
c_logits_real = self.discriminator(
opts, real_points_ph, prefix='CLASSIFIER')
c_logits_fake = self.discriminator(
opts, fake_points_ph, prefix='CLASSIFIER', reuse=True)
c_training = tf.nn.sigmoid(
self.discriminator(opts, real_points_ph, prefix='CLASSIFIER', reuse=True))
d_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_real, labels=tf.ones_like(d_logits_real)))
d_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_fake, labels=tf.zeros_like(d_logits_fake)))
d_loss = d_loss_real + d_loss_fake
g_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_fake, labels=tf.ones_like(d_logits_fake)))
c_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=c_logits_real, labels=tf.ones_like(c_logits_real)))
c_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=c_logits_fake, labels=tf.zeros_like(c_logits_fake)))
c_loss = c_loss_real + c_loss_fake
t_vars = tf.trainable_variables()
d_vars = [var for var in t_vars if 'DISCRIMINATOR/' in var.name]
g_vars = [var for var in t_vars if 'GENERATOR/' in var.name]
d_optim = ops.optimizer(opts, 'd').minimize(d_loss, var_list=d_vars)
g_optim = ops.optimizer(opts, 'g').minimize(g_loss, var_list=g_vars)
c_vars = [var for var in t_vars if 'CLASSIFIER/' in var.name]
c_optim = ops.optimizer(opts).minimize(c_loss, var_list=c_vars)
self._real_points_ph = real_points_ph
self._fake_points_ph = fake_points_ph
self._noise_ph = noise_ph
self._G = G
self._d_loss = d_loss
self._g_loss = g_loss
self._c_loss = c_loss
self._c_training = c_training
self._g_optim = g_optim
self._d_optim = d_optim
self._c_optim = c_optim
def _train_internal(self, opts):
"""Train a GAN model.
"""
batches_num = self._data.num_points / opts['batch_size']
train_size = self._data.num_points
counter = 0
logging.debug('Training GAN')
for _epoch in xrange(opts["gan_epoch_num"]):
for _idx in xrange(batches_num):
data_ids = np.random.choice(train_size, opts['batch_size'],
replace=False, p=self._data_weights)
batch_images = self._data.data[data_ids].astype(np.float)
batch_noise = utils.generate_noise(opts, opts['batch_size'])
# Update discriminator parameters
for _iter in xrange(opts['d_steps']):
_ = self._session.run(
self._d_optim,
feed_dict={self._real_points_ph: batch_images,
self._noise_ph: batch_noise})
# Update generator parameters
for _iter in xrange(opts['g_steps']):
_ = self._session.run(
self._g_optim, feed_dict={self._noise_ph: batch_noise})
counter += 1
if opts['verbose'] and counter % opts['plot_every'] == 0:
metrics = Metrics()
points_to_plot = self._run_batch(
opts, self._G, self._noise_ph,
self._noise_for_plots[0:320])
data_ids = np.random.choice(train_size, 320,
replace=False,
p=self._data_weights)
metrics.make_plots(
opts, counter,
self._data.data[data_ids],
points_to_plot,
prefix='sample_e%04d_mb%05d_' % (_epoch, _idx))
def _sample_internal(self, opts, num):
"""Sample from the trained GAN model.
"""
noise = utils.generate_noise(opts, num)
sample = self._run_batch(opts, self._G, self._noise_ph, noise)
# sample = self._session.run(
# self._G, feed_dict={self._noise_ph: noise})
return sample
def _train_mixture_discriminator_internal(self, opts, fake_images):
"""Train a classifier separating true data from points in fake_images.
"""
batches_num = self._data.num_points / opts['batch_size']
logging.debug('Training a mixture discriminator')
for epoch in xrange(opts["mixture_c_epoch_num"]):
for idx in xrange(batches_num):
ids = np.random.choice(len(fake_images), opts['batch_size'],
replace=False)
batch_fake_images = fake_images[ids]
ids = np.random.choice(self._data.num_points, opts['batch_size'],
replace=False)
batch_real_images = self._data.data[ids]
_ = self._session.run(
self._c_optim,
feed_dict={self._real_points_ph: batch_real_images,
self._fake_points_ph: batch_fake_images})
res = self._run_batch(
opts, self._c_training,
self._real_points_ph, self._data.data)
return res, None
class ToyUnrolledGan(ToyGan):
"""A simple GAN implementation, suitable for toy datasets.
"""
def __init__(self, opts, data, weights):
# Losses of the copied discriminator network
self._d_loss_cp = None
self._d_optim_cp = None
# Rolling back ops (assign variable values fo true
# to copied discriminator network)
self._roll_back = None
Gan.__init__(self, opts, data, weights)
# Architecture used in unrolled gan paper
def generator(self, opts, noise, reuse=False):
"""Generator function, suitable for simple toy experiments.
Args:
noise: [num_points, dim] array, where dim is dimensionality of the
latent noise space.
Returns:
[num_points, dim1, dim2, dim3] array, where the first coordinate
indexes the points, which all are of the shape (dim1, dim2, dim3).
"""
output_shape = self._data.data_shape
num_filters = opts['g_num_filters']
with tf.variable_scope("GENERATOR", reuse=reuse):
h0 = ops.linear(opts, noise, num_filters, 'h0_lin')
h0 = tf.nn.tanh(h0)
h1 = ops.linear(opts, h0, num_filters, 'h1_lin')
h1 = tf.nn.tanh(h1)
h2 = ops.linear(opts, h1, np.prod(output_shape), 'h2_lin')
h2 = tf.reshape(h2, [-1] + list(output_shape))
if opts['input_normalize_sym']:
return tf.nn.tanh(h2)
else:
return h2
def discriminator(self, opts, input_,
prefix='DISCRIMINATOR', reuse=False):
"""Discriminator function, suitable for simple toy experiments.
"""
shape = input_.get_shape().as_list()
num_filters = opts['d_num_filters']
assert len(shape) > 0, 'No inputs to discriminate.'
with tf.variable_scope(prefix, reuse=reuse):
h0 = ops.linear(opts, input_, num_filters, 'h0_lin')
h0 = tf.nn.tanh(h0)
h1 = ops.linear(opts, h0, num_filters, 'h1_lin')
h1 = tf.nn.tanh(h1)
h2 = ops.linear(opts, h1, 1, 'h2_lin')
return h2
def _build_model_internal(self, opts):
"""Build the Graph corresponding to GAN implementation.
"""
data_shape = self._data.data_shape
# Placeholders
real_points_ph = tf.placeholder(
tf.float32, [None] + list(data_shape), name='real_points_ph')
fake_points_ph = tf.placeholder(
tf.float32, [None] + list(data_shape), name='fake_points_ph')
noise_ph = tf.placeholder(
tf.float32, [None] + [opts['latent_space_dim']], name='noise_ph')
# Operations
G = self.generator(opts, noise_ph)
d_logits_real = self.discriminator(opts, real_points_ph)
d_logits_fake = self.discriminator(opts, G, reuse=True)
# Disccriminator copy for the unrolling steps
d_logits_real_cp = self.discriminator(
opts, real_points_ph, prefix='DISCRIMINATOR_CP')
d_logits_fake_cp = self.discriminator(
opts, G, prefix='DISCRIMINATOR_CP', reuse=True)
c_logits_real = self.discriminator(
opts, real_points_ph, prefix='CLASSIFIER')
c_logits_fake = self.discriminator(
opts, fake_points_ph, prefix='CLASSIFIER', reuse=True)
c_training = tf.nn.sigmoid(
self.discriminator(opts, real_points_ph, prefix='CLASSIFIER', reuse=True))
d_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_real, labels=tf.ones_like(d_logits_real)))
d_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_fake, labels=tf.zeros_like(d_logits_fake)))
d_loss = d_loss_real + d_loss_fake
d_loss_real_cp = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_real_cp, labels=tf.ones_like(d_logits_real_cp)))
d_loss_fake_cp = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_fake_cp,
labels=tf.zeros_like(d_logits_fake_cp)))
d_loss_cp = d_loss_real_cp + d_loss_fake_cp
if opts['objective'] == 'JS':
g_loss = - d_loss_cp
elif opts['objective'] == 'JS_modified':
g_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_fake_cp, labels=tf.ones_like(d_logits_fake_cp)))
else:
assert False, 'No objective %r implemented' % opts['objective']
c_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=c_logits_real, labels=tf.ones_like(c_logits_real)))
c_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=c_logits_fake, labels=tf.zeros_like(c_logits_fake)))
c_loss = c_loss_real + c_loss_fake
t_vars = tf.trainable_variables()
d_vars = [var for var in t_vars if 'DISCRIMINATOR/' in var.name]
d_vars_cp = [var for var in t_vars if 'DISCRIMINATOR_CP/' in var.name]
c_vars = [var for var in t_vars if 'CLASSIFIER/' in var.name]
g_vars = [var for var in t_vars if 'GENERATOR/' in var.name]
# Ops to roll back the variable values of discriminator_cp
# Will be executed each time before the unrolling steps
with tf.variable_scope('assign'):
roll_back = []
for var, var_cp in zip(d_vars, d_vars_cp):
roll_back.append(tf.assign(var_cp, var))
d_optim = ops.optimizer(opts, 'd').minimize(d_loss, var_list=d_vars)
d_optim_cp = ops.optimizer(opts, 'd').minimize(
d_loss_cp,
var_list=d_vars_cp)
c_optim = ops.optimizer(opts).minimize(c_loss, var_list=c_vars)
g_optim = ops.optimizer(opts, 'g').minimize(g_loss, var_list=g_vars)
# writer = tf.summary.FileWriter(opts['work_dir']+'/tensorboard', self._session.graph)
self._real_points_ph = real_points_ph
self._fake_points_ph = fake_points_ph
self._noise_ph = noise_ph
self._G = G
self._roll_back = roll_back
self._d_loss = d_loss
self._d_loss_cp = d_loss_cp
self._g_loss = g_loss
self._c_loss = c_loss
self._c_training = c_training
self._g_optim = g_optim
self._d_optim = d_optim
self._d_optim_cp = d_optim_cp
self._c_optim = c_optim
logging.debug("Building Graph Done.")
def _train_internal(self, opts):
"""Train a GAN model.
"""
batches_num = self._data.num_points / opts['batch_size']
train_size = self._data.num_points
counter = 0
logging.debug('Training GAN')
for _epoch in xrange(opts["gan_epoch_num"]):
for _idx in TQDM(opts, xrange(batches_num),
desc='Epoch %2d/%2d' %\
(_epoch+1, opts["gan_epoch_num"])):
data_ids = np.random.choice(train_size, opts['batch_size'],
replace=False, p=self._data_weights)
batch_images = self._data.data[data_ids].astype(np.float)
batch_noise = utils.generate_noise(opts, opts['batch_size'])
# Update discriminator parameters
for _iter in xrange(opts['d_steps']):
_ = self._session.run(
self._d_optim,
feed_dict={self._real_points_ph: batch_images,
self._noise_ph: batch_noise})
# Roll back discriminator_cp's variables
self._session.run(self._roll_back)
# Unrolling steps
for _iter in xrange(opts['unrolling_steps']):
self._session.run(
self._d_optim_cp,
feed_dict={self._real_points_ph: batch_images,
self._noise_ph: batch_noise})
# Update generator parameters
for _iter in xrange(opts['g_steps']):
_ = self._session.run(
self._g_optim, feed_dict={self._noise_ph: batch_noise})
counter += 1
if opts['verbose'] and counter % opts['plot_every'] == 0:
metrics = Metrics()
points_to_plot = self._run_batch(
opts, self._G, self._noise_ph,
self._noise_for_plots[0:320])
data_ids = np.random.choice(train_size, 320,
replace=False,
p=self._data_weights)
metrics.make_plots(
opts, counter,
self._data.data[data_ids],
points_to_plot,
prefix='sample_e%04d_mb%05d_' % (_epoch, _idx))
class ImageGan(Gan):
"""A simple GAN implementation, suitable for pictures.
"""
def __init__(self, opts, data, weights):
# One more placeholder for batch norm
self._is_training_ph = None
Gan.__init__(self, opts, data, weights)
def generator(self, opts, noise, is_training, reuse=False):
"""Generator function, suitable for simple picture experiments.
Args:
noise: [num_points, dim] array, where dim is dimensionality of the
latent noise space.
is_training: bool, defines whether to use batch_norm in the train
or test mode.
Returns:
[num_points, dim1, dim2, dim3] array, where the first coordinate
indexes the points, which all are of the shape (dim1, dim2, dim3).
"""
output_shape = self._data.data_shape # (dim1, dim2, dim3)
# Computing the number of noise vectors on-the-go
dim1 = tf.shape(noise)[0]
num_filters = opts['g_num_filters']
with tf.variable_scope("GENERATOR", reuse=reuse):
height = output_shape[0] / 4
width = output_shape[1] / 4
h0 = ops.linear(opts, noise, num_filters * height * width,
scope='h0_lin')
h0 = tf.reshape(h0, [-1, height, width, num_filters])
h0 = ops.batch_norm(opts, h0, is_training, reuse, scope='bn_layer1')
# h0 = tf.nn.relu(h0)
h0 = ops.lrelu(h0)
_out_shape = [dim1, height * 2, width * 2, num_filters / 2]
# for 28 x 28 does 7 x 7 --> 14 x 14
h1 = ops.deconv2d(opts, h0, _out_shape, scope='h1_deconv')
h1 = ops.batch_norm(opts, h1, is_training, reuse, scope='bn_layer2')
# h1 = tf.nn.relu(h1)
h1 = ops.lrelu(h1)
_out_shape = [dim1, height * 4, width * 4, num_filters / 4]
# for 28 x 28 does 14 x 14 --> 28 x 28
h2 = ops.deconv2d(opts, h1, _out_shape, scope='h2_deconv')
h2 = ops.batch_norm(opts, h2, is_training, reuse, scope='bn_layer3')
# h2 = tf.nn.relu(h2)
h2 = ops.lrelu(h2)
_out_shape = [dim1] + list(output_shape)
# data_shape[0] x data_shape[1] x ? -> data_shape
h3 = ops.deconv2d(opts, h2, _out_shape,
d_h=1, d_w=1, scope='h3_deconv')
h3 = ops.batch_norm(opts, h3, is_training, reuse, scope='bn_layer4')
if opts['input_normalize_sym']:
return tf.nn.tanh(h3)
else:
return tf.nn.sigmoid(h3)
def discriminator(self, opts, input_, is_training,
prefix='DISCRIMINATOR', reuse=False):
"""Discriminator function, suitable for simple toy experiments.
"""
num_filters = opts['d_num_filters']
with tf.variable_scope(prefix, reuse=reuse):
h0 = ops.conv2d(opts, input_, num_filters, scope='h0_conv')
h0 = ops.batch_norm(opts, h0, is_training, reuse, scope='bn_layer1')
h0 = ops.lrelu(h0)
h1 = ops.conv2d(opts, h0, num_filters * 2, scope='h1_conv')
h1 = ops.batch_norm(opts, h1, is_training, reuse, scope='bn_layer2')
h1 = ops.lrelu(h1)
h2 = ops.conv2d(opts, h1, num_filters * 4, scope='h2_conv')
h2 = ops.batch_norm(opts, h2, is_training, reuse, scope='bn_layer3')
h2 = ops.lrelu(h2)
h3 = ops.linear(opts, h2, 1, scope='h3_lin')
return h3
def _build_model_internal(self, opts):
"""Build the Graph corresponding to GAN implementation.
"""
data_shape = self._data.data_shape
# Placeholders
real_points_ph = tf.placeholder(
tf.float32, [None] + list(data_shape), name='real_points_ph')
fake_points_ph = tf.placeholder(
tf.float32, [None] + list(data_shape), name='fake_points_ph')
noise_ph = tf.placeholder(
tf.float32, [None] + [opts['latent_space_dim']], name='noise_ph')
is_training_ph = tf.placeholder(tf.bool, name='is_train_ph')
# Operations
G = self.generator(opts, noise_ph, is_training_ph)
# We use conv2d_transpose in the generator, which results in the
# output tensor of undefined shapes. However, we statically know
# the shape of the generator output, which is [-1, dim1, dim2, dim3]
# where (dim1, dim2, dim3) is given by self._data.data_shape
G.set_shape([None] + list(self._data.data_shape))
d_logits_real = self.discriminator(opts, real_points_ph, is_training_ph)
d_logits_fake = self.discriminator(opts, G, is_training_ph, reuse=True)
c_logits_real = self.discriminator(
opts, real_points_ph, is_training_ph, prefix='CLASSIFIER')
c_logits_fake = self.discriminator(
opts, fake_points_ph, is_training_ph, prefix='CLASSIFIER', reuse=True)
c_training = tf.nn.sigmoid(
self.discriminator(opts, real_points_ph, is_training_ph,
prefix='CLASSIFIER', reuse=True))
d_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_real, labels=tf.ones_like(d_logits_real)))
d_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_fake, labels=tf.zeros_like(d_logits_fake)))
d_loss = d_loss_real + d_loss_fake
g_loss = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=d_logits_fake, labels=tf.ones_like(d_logits_fake)))
c_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=c_logits_real, labels=tf.ones_like(c_logits_real)))
c_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(
logits=c_logits_fake, labels=tf.zeros_like(c_logits_fake)))
c_loss = c_loss_real + c_loss_fake
t_vars = tf.trainable_variables()
d_vars = [var for var in t_vars if 'DISCRIMINATOR/' in var.name]
g_vars = [var for var in t_vars if 'GENERATOR/' in var.name]
d_optim = ops.optimizer(opts, 'd').minimize(d_loss, var_list=d_vars)
g_optim = ops.optimizer(opts, 'g').minimize(g_loss, var_list=g_vars)
# d_optim_op = ops.optimizer(opts, 'd')
# g_optim_op = ops.optimizer(opts, 'g')
# def debug_grads(grad, var):
# _grad = tf.Print(
# grad, # grads_and_vars,
# [tf.global_norm([grad])],
# 'Global grad norm of %s: ' % var.name)
# return _grad, var
# d_grads_and_vars = [debug_grads(grad, var) for (grad, var) in \
# d_optim_op.compute_gradients(d_loss, var_list=d_vars)]
# g_grads_and_vars = [debug_grads(grad, var) for (grad, var) in \
# g_optim_op.compute_gradients(g_loss, var_list=g_vars)]
# d_optim = d_optim_op.apply_gradients(d_grads_and_vars)
# g_optim = g_optim_op.apply_gradients(g_grads_and_vars)
c_vars = [var for var in t_vars if 'CLASSIFIER/' in var.name]
c_optim = ops.optimizer(opts).minimize(c_loss, var_list=c_vars)
self._real_points_ph = real_points_ph
self._fake_points_ph = fake_points_ph
self._noise_ph = noise_ph
self._is_training_ph = is_training_ph
self._G = G
self._d_loss = d_loss
self._g_loss = g_loss
self._c_loss = c_loss
self._c_training = c_training
self._g_optim = g_optim
self._d_optim = d_optim
self._c_optim = c_optim
logging.debug("Building Graph Done.")
def _train_internal(self, opts):
"""Train a GAN model.
"""
batches_num = self._data.num_points / opts['batch_size']
train_size = self._data.num_points
counter = 0
logging.debug('Training GAN')
for _epoch in xrange(opts["gan_epoch_num"]):
for _idx in xrange(batches_num):
# logging.debug('Step %d of %d' % (_idx, batches_num ) )
data_ids = np.random.choice(train_size, opts['batch_size'],
replace=False, p=self._data_weights)
batch_images = self._data.data[data_ids].astype(np.float)
batch_noise = utils.generate_noise(opts, opts['batch_size'])
# Update discriminator parameters
for _iter in xrange(opts['d_steps']):
_ = self._session.run(
self._d_optim,
feed_dict={self._real_points_ph: batch_images,
self._noise_ph: batch_noise,
self._is_training_ph: True})
# Update generator parameters
for _iter in xrange(opts['g_steps']):
_ = self._session.run(
self._g_optim,
feed_dict={self._noise_ph: batch_noise,
self._is_training_ph: True})
counter += 1
if opts['verbose'] and counter % opts['plot_every'] == 0:
logging.debug(
'Epoch: %d/%d, batch:%d/%d' % \
(_epoch+1, opts['gan_epoch_num'], _idx+1, batches_num))
metrics = Metrics()
points_to_plot = self._run_batch(
opts, self._G, self._noise_ph,
self._noise_for_plots[0:320],
self._is_training_ph, False)
metrics.make_plots(
opts,
counter,
None,
points_to_plot,
prefix='sample_e%04d_mb%05d_' % (_epoch, _idx))
if opts['early_stop'] > 0 and counter > opts['early_stop']:
break
def _sample_internal(self, opts, num):
"""Sample from the trained GAN model.
"""
noise = utils.generate_noise(opts, num)
sample = self._run_batch(
opts, self._G, self._noise_ph, noise,
self._is_training_ph, False)
# sample = self._session.run(
# self._G, feed_dict={self._noise_ph: noise})
return sample
def _train_mixture_discriminator_internal(self, opts, fake_images):
"""Train a classifier separating true data from points in fake_images.
"""
batches_num = self._data.num_points / opts['batch_size']
logging.debug('Training a mixture discriminator')
logging.debug('Using %d real points and %d fake ones' %\
(self._data.num_points, len(fake_images)))
for epoch in xrange(opts["mixture_c_epoch_num"]):
for idx in xrange(batches_num):
ids = np.random.choice(len(fake_images), opts['batch_size'],
replace=False)
batch_fake_images = fake_images[ids]
ids = np.random.choice(self._data.num_points, opts['batch_size'],
replace=False)
batch_real_images = self._data.data[ids]
_ = self._session.run(
self._c_optim,
feed_dict={self._real_points_ph: batch_real_images,
self._fake_points_ph: batch_fake_images,
self._is_training_ph: True})
# Evaluating trained classifier on real points
res = self._run_batch(
opts, self._c_training,
self._real_points_ph, self._data.data,
self._is_training_ph, False)
# Evaluating trained classifier on fake points
res_fake = self._run_batch(
opts, self._c_training,
self._real_points_ph, fake_images,
self._is_training_ph, False)
return res, res_fake
class MNISTLabelGan(ImageGan):
"""Architecture for MNIST from "Improved techniques for training GANs"
"""
def generator(self, opts, noise, is_training, reuse=False):
with tf.variable_scope("GENERATOR", reuse=reuse):
h0 = ops.linear(opts, noise, 100, scope='h0_lin')
h0 = ops.batch_norm(opts, h0, is_training, reuse, scope='bn_layer1', scale=False)
h0 = tf.nn.softplus(h0)
h1 = ops.linear(opts, h0, 100, scope='h1_lin')
h1 = ops.batch_norm(opts, h1, is_training, reuse, scope='bn_layer2', scale=False)
h1 = tf.nn.softplus(h1)
h2 = ops.linear(opts, h1, 28 * 28, scope='h2_lin')
# h2 = ops.batch_norm(opts, h2, is_training, reuse, scope='bn_layer3')
h2 = tf.reshape(h2, [-1, 28, 28, 1])
if opts['input_normalize_sym']:
return tf.nn.tanh(h2)
else:
return tf.nn.sigmoid(h2)
def discriminator(self, opts, input_, is_training,
prefix='DISCRIMINATOR', reuse=False):
shape = tf.shape(input_)
num = shape[0]
with tf.variable_scope(prefix, reuse=reuse):
h0 = input_
h0 = tf.add(h0, tf.random_normal(shape, stddev=0.3))
h0 = ops.linear(opts, h0, 1000, scope='h0_linear')