-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_functions.py
1416 lines (1128 loc) · 50 KB
/
base_functions.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 12 11:54:57 2020
"""
###############################################################################
"""
MODULES
"""
import tensorflow as tf
#from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import (Add,
BatchNormalization,
Concatenate,
Convolution2D,
Conv2DTranspose,
Lambda,
LayerNormalization,
LeakyReLU,
MaxPooling2D)
###############################################################################
"""
LIST OF FUNCTIONS
instance_noise_alpha
instance_noise_B_Bdomain
instance_noise_R_Bdomain
instance_noise_R_Rdomain
instance_noise_B_Rdomain
conv
tconv
add_common_layers
grouped_convolution
residual_block
one_one
make_dataset
down
up
KL_batchwise_loss
KL_variational_loss
reparameterize
wasserstein_loss
RandomWeightedAverage
L2_magnitude
generator_loss
encoder_loss
encoder_loss_var
critic_loss
"""
###############################################################################
"""
FUNCTION DETERMINES INSTANCE NOISE AMPLITUDE
"""
@tf.function
def instance_noise_alpha(epoch,
cutoff_epoch):
# if epoch < cutoff_epoch, add noise.
# else: do not add noise.
alpha = tf.cond(tf.less(epoch, # condition
cutoff_epoch),
lambda: tf.divide(epoch, # returned value if condition
cutoff_epoch),
lambda: tf.identity(1.0)) # returned value if not condition
return alpha
"""
FUNCTIONS FOR APPLYING INSTANCE NOISE
The purpose of instance noise is to force the inputs of C to have the same support throughout training, even at the beginning of training when G is not able to produce realistic outputs.
E does not need to be fed noisy inputs, since it should still be able to provide useful gradients even if the real and fake images do not have the same support. However, there is no additional cost to indiscriminately add noise to all images, even those only input into E. All models have at most some loss terms that they might be able to optimize during early training, if C is receiving noisy inputs but E is not, but this optimization would only be possible if G was already trained. We therefore add noise to all images.
Derivation:
1) Need to apply noise to same domain throughout entire problem, and
2) The domain to which it is applied should be the domain the critic operates on, since that is the domain where support needs to be the same between the real and fake submissions.
Applying noise to B when B is the input to C is not the same as applying noise to B when R is the input to C; likewise for applying noise to R.
Notation: B is an image, and R is the residual B - A, where A is the low-resolution downsampling of B. B' and R' are the noise-ified B and R, respectively.
if critic_input == 'B'
or critic_input == 'AB':
Applying noise to B:
R = B - A
-> B = R + A
B' = R' + A
= a * B + (1 - a) * noise
-> R' = a * (R + A) + (1 - a) * noise - A
= a * R + (1 - a) * (noise - A)
elif critic_input == 'R':
Alternatively, applying noise to R:
R' = B' - A
= a * R + (1 - a) * noise
-> B' = a * (B - A) + (1 - a) * noise + A
= a * B + (1 - a) * (noise + A)
"""
@tf.function
def instance_noise_B_Bdomain(B,
alpha):
"""
Args:
B: A high-resolution image (real or generated).
alpha: The fraction of B that goes into the output (the remainder is uniform noise).
Returns:
if alpha < 1:
B_noise: alpha * B + (1 - alpha) * noise
noise = random.uniform(-1,1)
else:
B
"""
B_noisy = tf.cond(tf.less(alpha,
1),
# if alpha < 1:
# return B, noise-ified in the B domain
lambda: alpha * B + \
(tf.identity(1.0) - alpha) * \
K.random_uniform(shape=tf.shape(B),
minval=-1,
maxval=1),
# else:
# return B with no noise-ification
lambda: B)
return B_noisy
@tf.function
def instance_noise_R_Bdomain(R,
A,
alpha):
"""
Args:
R: A residual, R = B - A (real or generated).
A: The conditioning image, in this case A = up(down(B)).
alpha: The noise fraction in the B domain.
Returns:
if alpha < 1:
R_noise: alpha * R + (1 - alpha) * (noise - A)
noise = random.uniform(-1,1)
else:
R
"""
R_noisy = tf.cond(tf.less(alpha,
1),
# if alpha < 1:
# return R, noise-ified in the B domain
lambda: alpha * R + \
(tf.identity(1.0) - alpha) * \
(K.random_uniform(shape=tf.shape(R),
minval=-1,
maxval=1) - A),
# else:
# return R with no noise-ification
lambda: R)
return R_noisy
@tf.function
def instance_noise_R_Rdomain(R,
alpha):
"""
Args:
R: An residual-domain image (real or generated).
alpha: The fraction of R that goes into the output (the remainder is uniform noise).
Returns:
if alpha < 1:
R_noise: alpha * R + (1 - alpha) * noise
noise = random.uniform(-1,1)
else:
R
"""
R_noisy = tf.cond(tf.less(alpha,
1),
# if alpha < 1:
# return R, noise-ified in the R domain
lambda: alpha * R + \
(tf.identity(1.0) - alpha) * \
K.random_uniform(shape=tf.shape(R),
minval=-1,
maxval=1),
# else:
# return R with no noise-ification
lambda: R)
return R_noisy
@tf.function
def instance_noise_B_Rdomain(B,
A,
alpha):
"""
Args:
B: An HRI-domin image (real or generated).
A: The conditioning image, in this case A = up(down(B)).
alpha: The noise fraction in the R domain.
Returns:
if alpha < 1:
B_noise = alpha * B + (1 - alpha) * (noise + A)
noise = random.uniform(-1,1)
else:
B
"""
B_noisy = tf.cond(tf.less(alpha,
1),
# if alpha < 1:
# return B, noise-ified in the R domain
lambda: alpha * B + \
(tf.identity(1.0) - alpha) * \
(K.random_uniform(shape=tf.shape(B),
minval=-1,
maxval=1) + A),
# else:
# return B with no noise-ification
lambda: B)
return B_noisy
###############################################################################
"""
CONVOLUTION BLOCK
"""
def conv(input_tensor,
n_filters,
kernel_size,
strides=(1, 1),
pool=False,
bn=False,
ln=False,
norm_axis=-1):
"""
Args:
input_tensor: the input tensor to the convolutional block (e.g., the output of the previous layer)
n_filters: the number of filters/kernels in the block
kernel_size: the size (integer if square, tuple if not) of the kernels
strides: stride length (integer or tuple)
pool: Boolean. Whether or not there is a max pooling layer after the convolution.
bn: Boolean. Whether or not there is a batch normalization layer after the activation function. *Only one of bn or ln should be True.*
ln: Boolean. Whether or not there is a layer normalization layer after the activation function. *Only one of bn or ln should be True.*
norm_axis: the axis over which batch normalization or layer normalization is performed.
Returns:
The tensor outputs of the following block:
input
2D convolution
[max pool]
LeakyReLU
[batch normalization]
[layer normalization]
output
where [...] indicates optional components.
"""
y = Convolution2D(filters=n_filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
kernel_initializer='he_normal')(input_tensor)
if pool:
y = MaxPooling2D(pool_size=(2, 2),
padding='same')(y)
y = LeakyReLU()(y)
if bn:
y = BatchNormalization(axis=norm_axis)(y)
if ln:
y = LayerNormalization(axis=norm_axis)(y)
return y
"""
TRANSPOSE CONVOLUTION BLOCK
"""
def tconv(input_tensor,
n_filters,
kernel_size,
strides=(1, 1),
bn=False,
ln=False,
norm_axis=-1):
"""
Args:
input_tensor: the input tensor to the convolutional block (e.g., the output of the previous layer)
n_filters: the number of filters/kernels in the block
kernel_size: the size (integer if square, tuple if not) of the kernels
strides: stride length (integer or tuple)
bn: Boolean. Whether or not there is a batch normalization layer after the activation function. *Only one of bn or ln should be True.*
ln: Boolean. Whether or not there is a layer normalization layer after the activation function. *Only one of bn or ln should be True.*
norm_axis: the axis over which batch normalization or layer normalization is performed.
Returns:
The tensor outputs of the following block:
input
2D convolution
LeakyReLU
[batch normalization]
[layer normalization]
output
where [...] indicates optional components.
"""
y = Conv2DTranspose(filters=n_filters,
kernel_size=kernel_size,
strides=strides,
padding='same')(input_tensor)
y = LeakyReLU()(y)
if bn:
y = BatchNormalization(axis=norm_axis)(y)
if ln:
y = LayerNormalization(axis=norm_axis)(y)
return y
"""
ResNeXt BLOCK
"""
"""
This code was adapted from
https://gist.github.com/mjdietzx/0cb95922aac14d446a6530f87b3a04ce
with the following changes:
1) ResNeXt blocks (see https://arxiv.org/pdf/1603.05027.pdf )
2) batch normalization -> ReLU
replaced by
LeakyReLU -> [batch OR layer OR no] normalization
"""
def add_common_layers(y,
bn=False,
ln=False,
norm_axis=-1):
"""
Args:
y: input tensor
bn: Boolean. Whether or not there is a batch normalization layer after the activation function. *Only one of bn or ln should be True.*
ln: Boolean. Whether or not there is a layer normalization layer after the activation function. *Only one of bn or ln should be True.*
norm_axis: the axis over which batch normalization or layer normalization is performed.
Returns:
The tensor outputs of the following block:
input
LeakyReLU
[batch normalization]
[layer normalization]
output
NOTE: Goes AT THE TOP of the stack to enable us to preserve the identity map. This also means that if this block is not used, activation and normalization must be added manually.
"""
y = LeakyReLU()(y)
if bn:
y = BatchNormalization(axis=norm_axis)(y)
if ln:
y = LayerNormalization(axis=norm_axis)(y)
return y
def grouped_convolution(y,
nb_channels,
_strides,
ksize,
cardinality):
"""
Args:
y: input tensor
nb_channels: total number of channels in the block, across all branches
_strides: strides of the convolution
ksize: size of the convolution kernels
cardinality: number of branches in the block
Returns:
The tensor outputs of the following stack:
input
split into a number of branches equal to cardinality
2D convolution on each branch
concatenate the branches back together
output
NOTE: nb_channels must be evenly divisible by cardinality (each branch has nb_channels/cardinality filters)
"""
# when `cardinality` == 1 this is just a standard convolution
if cardinality == 1:
return Convolution2D(nb_channels,
kernel_size=ksize,
strides=_strides,
padding='same')(y)
assert not nb_channels % cardinality
_d = nb_channels // cardinality
# in a grouped convolution layer, input and output channels are divided into `cardinality` groups, and convolutions are separately performed within each group
groups = []
for j in range(cardinality):
group = Lambda(lambda z: z[:, :, :, j * _d:j * _d + _d])(y)
groups.append(Convolution2D(_d,
kernel_size=ksize,
strides=_strides,
padding='same')(group))
# the grouped convolutional layer concatenates them as the outputs of the layer
# CANNOT use Concatenate here. concatenate is the FUNCTIONAL INTERFACE to Concatenate, and that's what we need here.
y = tf.keras.layers.concatenate(groups)
return y
def residual_block(y,
nb_channels_in,
nb_channels_out,
_strides=(1, 1),
_project_shortcut=False,
ksize=(4, 4),
cardinality=4,
bn=False,
ln=False,
norm_axis=-1):
"""
Args:
y: input tensor
nb_channels_in: reduced number of channels in the bottleneck/dimension reduction phase
nb_channels_out: number of channels in the output of the block
_strides: strides of the convolution
_project_shortcut: Boolean. ResNeXt blocks have an identity map that is added back into the output of the block; if the number of channels in the output is expected to differ from the number of channels in the input, or if the feature maps change size (e.g., if _strides != 1), the identity path must be transformed. if _project_shortcut, 1x1 convolutions will be performed on the identity path to map it to the appropriate shape.
ksize: size of the convolution kernels
cardinality: number of branches in the block
bn: Boolean. Whether or not there is a batch normalization layer after the activation function. *Only one of bn or ln should be True.*
ln: Boolean. Whether or not there is a layer normalization layer after the activation function. *Only one of bn or ln should be True.*
norm_axis: the axis over which batch normalization or layer normalization is performed.
Returns:
The tensor outputs of the following stack:
input
add_common_layers
2D convolution (1x1 kernels) <- bottleneck
add_common_layers
grouped_convolution
add_common_layers
2D convolution (1x1 kernels) <- map to desired number of channels
add to input (or projected input)
output
Two general rules (that we do not always follow for various reasons, including but not limited to parameter budget):
1) blocks producing spatial maps of the same size as their inputs have the same hyperparameters (cardinality, filter sizes, etc.)
2) each time a spatial map is downsampled by some factor, the width of the blocks (number of channels) is multiplied by the same factor.
"""
# This is the identity / shortcut path
shortcut = y
# we modify the residual building block as a bottleneck design to make the network more economical
y = add_common_layers(y,
bn,
ln,
norm_axis)
y = Convolution2D(nb_channels_in,
kernel_size=(1, 1),
strides=(1, 1),
padding='same')(y)
# ResNeXt (identical to ResNet when `cardinality` == 1)
y = add_common_layers(y,
bn,
ln,
norm_axis)
y = grouped_convolution(y,
nb_channels_in,
_strides=_strides,
ksize=ksize,
cardinality=cardinality)
# Map the aggregated branches to desired number of output channels
y = add_common_layers(y,
bn,
ln,
norm_axis)
y = Convolution2D(nb_channels_out,
kernel_size=(1, 1),
strides=(1, 1),
padding='same')(y)
# Add to the shortcut
# identity shortcuts used directly when the input and output are of the same dimensions
if _project_shortcut \
or _strides != (1, 1):
# when the dimensions increase projection shortcut is used to match dimensions (done by 1×1 convolutions)
# when the shortcuts go across feature maps of two sizes, they are performed with a stride of 2
shortcut = Convolution2D(nb_channels_out,
kernel_size=(1, 1),
strides=_strides,
padding='same')(shortcut)
# Add the shortcut and the transformation block
# Same as with the concatenate layer in grouped_convolution, we need the FUNCTIONAL INTERFACE with Add, which is add.
y = tf.keras.layers.add([shortcut, y])
return y
###############################################################################
"""
FUNCTION MAPS DATA TO RANGE [-1, 1]
"""
def one_one(x):
shape = x.shape
x_one_one = 2 * (x - x.min()) / (x.max() - x.min()) - 1
x_one_one = x_one_one.reshape(shape)
return x_one_one
###############################################################################
"""
FUNCTION FOR 2x2 AVERAGE POOL DOWNSAMPLING
AND
FUNCTION FOR 2x2 UPSAMPLING
"""
def down(img):
"""
Args:
img: an HxWxD image OR a batch of such images. In the latter case the inputs have an extra (batch) dimension.
Returns:
down(img): a 2x2 average-pooled downsampling of img -> an (H/2)x(W/2)xD image OR a btach of such images. In the latter case the inputs have an extra (batch) dimension.
Intended to be used with map().
"""
# if performing on individual examples with no batch dimension, need to expand dims to give a "batch size" of 1.
batch = True
#if len(tf.shape(img)) == 3:
if tf.shape(img).shape[0] == 3:
img = tf.expand_dims(img,
axis=0)
batch = False
batch_size = tf.shape(img)[0]
(M, N) = (tf.shape(img)[1], # img height
tf.shape(img)[2]) # img width
depth = tf.shape(img)[3]
(K, L) = (2, 2)
MK = M // K
NL = N // L
downsampled = img[:,
:MK*K,
:NL*L,
:]
downsampled = tf.reshape(downsampled,
shape=(batch_size,
MK, K,
NL, L,
depth))
downsampled = tf.reduce_mean(downsampled,
axis=(2,
4))
# if performing on individual examples, need to squeeze out the "batch" dimension.
if not batch:
downsampled = tf.squeeze(downsampled,
axis=0)
return downsampled
def up(img):
"""
Args:
img: an HxWxD image OR a batch of such images. In the latter case the inputs have an extra (batch) dimension.
Returns:
up(img): a 2x2-repeated upscaling of img -> a (2*H)x(2*W)xD image OR a batch of such images. In the latter case the inputs have an extra (batch) dimension.
Intended to be used with map().
"""
# if performing on individual examples with no batch dimension, need to expand dims to give a "batch size" of 1.
batch = True
#if len(tf.shape(img)) == 3:
if tf.shape(img).shape[0] == 3:
img = tf.expand_dims(img,
axis=0)
batch = False
# Upsample in H...
upscaled = tf.repeat(img,
repeats=2,
axis=1)
# ...and W.
upscaled = tf.repeat(upscaled,
repeats=2,
axis=2)
# if performing on individual examples, need to squeeze out the "batch" dimension.
if not batch:
upscaled = tf.squeeze(upscaled,
axis=0)
return upscaled
###############################################################################
"""
FUNCTIONS FOR LOADING, PARSING AND PREPROCESSING FASHION-MNIST FROM TFRECORDS FILES TO TENSORFLOW DATASET
"""
def make_dataset(filepath,
z_dim,
batch_size,
shuffle_buffer_size=None):
"""
Tensorflow input pipeline.
Args:
filepath: The filepath to the target tfrecords file, containing both images (x) and one-hot class labels (y). The x data has ALREADY:
(i) been converted to float,
(ii) been reshaped appropriately to feed into the network, and
(iii) been rescaled int [-1, 1] (the training and validation datasets have been rescaled independently from one another).
z_dim: The dimensionality of the latent space.
batch_size: The batch size in the resulting dataset.
shuffle_buffer_size: None if no shuffling is to be performed; otherwise, the size of the shuffle buffer. Fashion-MNIST is small enough that we can shuffle the entire dataset (there are 60000 examples in the training set and 10000 in the validation set).
Returns:
dataset: A ready-to-use TensorFlow Dataset (z, up(B2), B1), where
z: a z_dim-dimensional vector sampled from the standard normal
B2: 7x7 images created by downsampling fashion-MNIST images TWICE
B1: 14x14 images created by downsample fashion-MNIST images ONCE
up(): simple 2x2 upscaling
###########################################################################
Best practices for datasets too large to fit into memory, from https://github.com/tensorflow/tensorflow/issues/14857:
The Dataset.shuffle() implementation is designed for data that could be shuffled in memory (...) here's the usual approach we use when the data are too large to fit in memory:
1. Randomly shuffle the entire data once using a MapReduce/Spark/Beam/etc. job to create a set of roughly equal-sized files ("shards").
2. In each epoch:
i. Randomly shuffle the list of shard filenames, using Dataset.list_files(...).shuffle(num_shards).
ii. Use dataset.interleave(lambda filename: tf.data.TextLineDataset(filename), cycle_length=N) to mix together records from N different shards.
iii. Use dataset.shuffle(B) to shuffle the resulting dataset. Setting B might require some experimentation, but you will probably want to set it to some value larger than the number of records in a single shard.
"""
def load_fashion_MNIST(filepath):
"""
Args:
filepath: The filepath to the target tfrecordsfile.
Returns:
serialized_dataset: A Dataset object containing serialized image (x) and one-hot class label (y) data.
"""
# Create a Dataset from the TFRecord file.
serialized_dataset = tf.data.TFRecordDataset(filepath)
return serialized_dataset
@tf.function
def parse_example(serialized_example):
"""
Args:
serialized_example: A single serialized example from the serialized dataset obtained from load_fashion_MNIST.
Returns:
B0: The original 28x28x1 image (with depth channel). Class label data is discarded since we don't need it for now.
DOES NOT run on a batched dataset.
Intended to be used with map().
"""
features = {'img': tf.io.FixedLenFeature([],
tf.string),
'height': tf.io.FixedLenFeature([],
tf.int64),
'width': tf.io.FixedLenFeature([],
tf.int64),
'depth': tf.io.FixedLenFeature([],
tf.int64),
'label': tf.io.FixedLenFeature([],
tf.string)}
parsed_example = tf.io.parse_single_example(
serialized=serialized_example,
features=features)
# Get the image dimensions.
height = parsed_example['height']
width = parsed_example['width']
depth = parsed_example['depth']
# Get the raw byte string and convert it to a tensor object.
B0 = parsed_example['img']
B0 = tf.io.decode_raw(B0,
tf.float32)
# The tensor object is 1-dimensional, so reshape it using the image dimensions we obtained earlier.
B0 = tf.reshape(B0,
shape=(height,
width,
depth))
return B0
@tf.function
def get_images(B0):
"""
Args:
B0: The image outputted by parse_example.
Returns:
(B2, B1, B0), with:
B1 = down(B0)
B2 = down(B1)
"""
B1 = down(B0)
B2 = down(B1)
B0 = tf.cast(B0, tf.float32)
B1 = tf.cast(B1, tf.float32)
B2 = tf.cast(B2, tf.float32)
return (B2, B1, B0)
@tf.function
def get_latent_vectors(B2,
B1,
B0):
"""
Args:
B0: The original image.
B1 = down(B0)
B2 = down(B1)
Returns:
x = (z2, up(B2), B1)
"""
z2 = tf.random.normal((z_dim,),
dtype=tf.float32)
return (z2, up(B2), B1)
dataset = load_fashion_MNIST(filepath)
dataset = dataset.map(parse_example,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.map(get_images,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
# The B examples will not change from epoch to epoch so they can be cached
dataset = dataset.cache()
# The z examples WILL change from epoch to epoch so they must be generated after the cache.
dataset = dataset.map(get_latent_vectors,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
# Shuffle the dataset.
if shuffle_buffer_size:
dataset = dataset.shuffle(shuffle_buffer_size)
# Batch the dataset.
dataset = dataset.batch(batch_size)
# Prefetch the dataset.
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
return dataset
###############################################################################
"""
KL DIVERGENCE
"""
def KL_batchwise_loss(y_true,
y_pred):
"""
Args:
y_true: a dummy tensor. tf.keras loss functions must have a y_true, but it is not used to calculate the KL divergence loss
y_pred: Batch of z vectors with shape (batch_size, z_dim).
Returns:
The KL divergence from N(0,1) to the distribution represented by y_pred, evaluated once over the batch. Has a strict lower bound at 0 but no upper bound.
The KL divergence from a normal distribution p2 to another normal distribution p1 is (http://allisons.org/ll/MML/KL/Normal/ ):
KL(p1 || p2) = log(sigma_2 / sigma_1) +
[(mu_1 - mu_2)^2 + (sigma_1^2 - sigma_2^2)] / 2*sigma_2^2
When p2 is the standard normal distribution, this simplifies to:
KL(N(mu, sigma^2) || N(0, 1)) = -log(sigma) +
[mu^2 + sigma^2 - 1] / 2
NOTE: as in both tensorflow and numpy, log is log_e, the natural logarithm.
NOTE: this loss function assumes y_pred was sampled from some (non-standard) multivariate normal distribution N(mu,sigma^2).
"""
mu = K.mean(y_pred,
axis=0)
sigma = K.std(y_pred,
axis=0)
KL = -K.log(sigma) + \
0.5 * (K.square(mu) + \
K.square(sigma) - \
1)
KL = K.mean(KL,
axis=-1)
return KL
def KL_variational_loss(y_true,
y_pred):
"""
Args:
y_true: a dummy tensor. tf.keras loss functions must have a y_true, but it is not used to calculate the KL divergence loss
y_pred: outputs of variational encoder, with form (z_mu, z_logvar)
Returns:
The KL divergence from N(0,1) to N(mu,sigma^2), with mu=z_mu and sigma^2=exp[z_logvar]. Has a strict lower bound at 0 but no upper bound.
"""
z_mu = y_pred[0]
z_logvar = y_pred[1]
KL = 0.5 * (tf.math.square(z_mu) + \
tf.math.exp(z_logvar) - \
z_logvar - \
1)
KL = K.mean(KL)
return KL
###############################################################################
"""
REPARAMATERIZATION TRICK
Originally found in:
D.P. Kingma and M. Welling. Auto-encoding Variational Bayes, ICLR (2014).
The procedure is as follows:
1) Assume E(B) is a normal distribution (not necessarily standard normal).
2) Obtain mu and sigma via E(B) = (mu, logvar)
3) Sample the point cloud represented by E(B) via z ~ mu + sigma * N(0,1). This allows easier backpropagation because the reparameterized sampled z is differentiable with respect to mu and sigma, which are predicted deterministically by E.
4) Pass this newly sampled z through G to generate a fake image.
"""
def reparameterize(E_of_B):
"""
Args:
E_of_B: E(B) = (mu, log[sigma^2])
Returns:
z_sampled: mu + sigma * epsilon
epsilon = N(0,1)
"""
z_mu = E_of_B[0]
z_logvar = E_of_B[1]
batch_size = K.shape(z_mu)[0]
z_dim = K.shape(z_mu)[1]
epsilon = K.random_normal(shape=(batch_size,
z_dim))
z_sigma = K.exp(0.5 * z_logvar)
z_sampled = z_mu + z_sigma * epsilon
return z_sampled
###############################################################################
"""
WASSERSTEIN LOSS
"""
"""
Calculates the Wasserstein loss for a sample batch.
In a Wasserstein GAN, the output is linear (no activation function). The discriminator tries to make the distance between its prediction for real and fake samples as large as possible.
This can be done by labeling fake and real samples as -1 and 1, respectively.
The labels, y_true, have fixed values +/- 1. However, there is NO restriction on y_pred (and it is just a random scalar for a randomly initialized network). There is therefore no reason that y_pred should be fixed at +/- 1 or even in [-1, 1], although in practice it shouldn't be too far outside this range.
With this +/- 1 formulation (instead of the usual range [0, 1] for fake/real), the Wasserstein loss can be very simply calculated by multiplying the outputs and labels.
Note that as a result of this formulation the loss can and frequently will be negative.
"""
def wasserstein_loss(y_true,
y_pred):
"""
Args:
y_true: labels (either +1 for real or -1 for fake)
y_pred: the predicted critic score, more negative for "more likely to be fake," more real for "more likely to be real"
Returns:
Wasserstein loss.
NOTE: The labels, y_true, have fixed values of +/-1. However, there is no similar restriction on y_pred (and in fact, it is a random scalar for a randomly initialized C prior to training). y_pred is not fixed to +/-1 or even restricted to [-1,1], although in practice it should not be too far outside this range.
NOTE: The +/-1 convention for the score (as oposed to 0/1), the Wasserstein loss is just a simple product. This does mean that the loss can and frequently will be negative.
"""
return -K.mean(y_true * y_pred)
###############################################################################
@tf.function
def RandomWeightedAverage(tensor_1,
tensor_2):
"""
Args:
tensor_1: A TensorFlow tensor.
tensor_2: A TensorFlow tensor with the same shape as tensor_1.
Returns:
random_weighted_average: A randomly-weighted average of tensor_1 and tensor_2. (In geometric terms, this outputs a random point on the line between the two input points.)
"""
shape = K.shape(tensor_1)
weights = K.random_uniform(shape[:1],
0,
1)
for i in range(len(K.int_shape(tensor_1)) - 1):
weights = K.expand_dims(weights,