-
Notifications
You must be signed in to change notification settings - Fork 35
/
srez_model.py
1126 lines (837 loc) · 45.7 KB
/
srez_model.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
import numpy as np
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
class Model:
"""A neural network model.
Currently only supports a feedforward architecture."""
def __init__(self, name, features):
self.name = name
self.outputs = [features]
def _get_layer_str(self, layer=None):
if layer is None:
layer = self.get_num_layers()
return '%s_L%03d' % (self.name, layer+1)
def _get_num_inputs(self):
return int(self.get_output().get_shape()[-1])
def _glorot_initializer(self, prev_units, num_units, stddev_factor=1.0):
"""Initialization in the style of Glorot 2010.
stddev_factor should be 1.0 for linear activations, and 2.0 for ReLUs"""
stddev = np.sqrt(stddev_factor / np.sqrt(prev_units*num_units))
return tf.truncated_normal([prev_units, num_units],
mean=0.0, stddev=stddev)
def _glorot_initializer_conv2d(self, prev_units, num_units, mapsize, stddev_factor=1.0):
"""Initialization in the style of Glorot 2010.
stddev_factor should be 1.0 for linear activations, and 2.0 for ReLUs"""
stddev = np.sqrt(stddev_factor / (np.sqrt(prev_units*num_units)*mapsize*mapsize))
return tf.truncated_normal([mapsize, mapsize, prev_units, num_units],
mean=0.0, stddev=stddev)
def get_num_layers(self):
return len(self.outputs)
def add_batch_norm(self, scale=False):
"""Adds a batch normalization layer to this model.
See ArXiv 1502.03167v3 for details."""
# TBD: This appears to be very flaky, often raising InvalidArgumentError internally
with tf.variable_scope(self._get_layer_str()):
out = tf.contrib.layers.batch_norm(self.get_output(), scale=scale)
self.outputs.append(out)
return self
def add_flatten(self):
"""Transforms the output of this network to a 1D tensor"""
with tf.variable_scope(self._get_layer_str()):
batch_size = int(self.get_output().get_shape()[0])
out = tf.reshape(self.get_output(), [batch_size, -1])
self.outputs.append(out)
return self
def add_dense(self, num_units, stddev_factor=1.0):
"""Adds a dense linear layer to this model.
Uses Glorot 2010 initialization assuming linear activation."""
assert len(self.get_output().get_shape()) == 2, "Previous layer must be 2-dimensional (batch, channels)"
with tf.variable_scope(self._get_layer_str()):
prev_units = self._get_num_inputs()
# Weight term
initw = self._glorot_initializer(prev_units, num_units,
stddev_factor=stddev_factor)
weight = tf.get_variable('weight', initializer=initw)
# Bias term
initb = tf.constant(0.0, shape=[num_units])
bias = tf.get_variable('bias', initializer=initb)
# Output of this layer
out = tf.matmul(self.get_output(), weight) + bias
self.outputs.append(out)
return self
def add_sigmoid(self):
"""Adds a sigmoid (0,1) activation function layer to this model."""
with tf.variable_scope(self._get_layer_str()):
prev_units = self._get_num_inputs()
out = tf.nn.sigmoid(self.get_output())
self.outputs.append(out)
return self
def add_softmax(self):
"""Adds a softmax operation to this model"""
with tf.variable_scope(self._get_layer_str()):
this_input = tf.square(self.get_output())
reduction_indices = list(range(1, len(this_input.get_shape())))
acc = tf.reduce_sum(this_input, reduction_indices=reduction_indices, keep_dims=True)
out = this_input / (acc+FLAGS.epsilon)
#out = tf.verify_tensor_all_finite(out, "add_softmax failed; is sum equal to zero?")
self.outputs.append(out)
return self
def add_relu(self):
"""Adds a ReLU activation function to this model"""
with tf.variable_scope(self._get_layer_str()):
out = tf.nn.relu(self.get_output())
self.outputs.append(out)
return self
def add_elu(self):
"""Adds a ELU activation function to this model"""
with tf.variable_scope(self._get_layer_str()):
out = tf.nn.elu(self.get_output())
self.outputs.append(out)
return self
def add_lrelu(self, leak=.2):
"""Adds a leaky ReLU (LReLU) activation function to this model"""
with tf.variable_scope(self._get_layer_str()):
t1 = .5 * (1 + leak)
t2 = .5 * (1 - leak)
out = t1 * self.get_output() + \
t2 * tf.abs(self.get_output())
self.outputs.append(out)
return self
def add_conv2d(self, num_units, mapsize=1, stride=1, stddev_factor=1.0):
"""Adds a 2D convolutional layer."""
assert len(self.get_output().get_shape()) == 4 and "Previous layer must be 4-dimensional (batch, width, height, channels)"
with tf.variable_scope(self._get_layer_str()):
prev_units = self._get_num_inputs()
# Weight term and convolution
initw = self._glorot_initializer_conv2d(prev_units, num_units,
mapsize,
stddev_factor=stddev_factor)
weight = tf.get_variable('weight', initializer=initw)
out = tf.nn.conv2d(self.get_output(), weight,
strides=[1, stride, stride, 1],
padding='SAME')
# Bias term
initb = tf.constant(0.0, shape=[num_units])
bias = tf.get_variable('bias', initializer=initb)
out = tf.nn.bias_add(out, bias)
self.outputs.append(out)
return self
def add_conv2d_transpose(self, num_units, mapsize=1, stride=1, stddev_factor=1.0):
"""Adds a transposed 2D convolutional layer"""
assert len(self.get_output().get_shape()) == 4 and "Previous layer must be 4-dimensional (batch, width, height, channels)"
with tf.variable_scope(self._get_layer_str()):
prev_units = self._get_num_inputs()
# Weight term and convolution
initw = self._glorot_initializer_conv2d(prev_units, num_units,
mapsize,
stddev_factor=stddev_factor)
weight = tf.get_variable('weight', initializer=initw)
weight = tf.transpose(weight, perm=[0, 1, 3, 2])
prev_output = self.get_output()
output_shape = [FLAGS.batch_size,
int(prev_output.get_shape()[1]) * stride,
int(prev_output.get_shape()[2]) * stride,
num_units]
out = tf.nn.conv2d_transpose(self.get_output(), weight,
output_shape=output_shape,
strides=[1, stride, stride, 1],
padding='SAME')
# Bias term
initb = tf.constant(0.0, shape=[num_units])
bias = tf.get_variable('bias', initializer=initb)
out = tf.nn.bias_add(out, bias)
self.outputs.append(out)
return self
def add_residual_block(self, num_units, mapsize=3, num_layers=2, stddev_factor=1e-3):
"""Adds a residual block as per Arxiv 1512.03385, Figure 3"""
assert len(self.get_output().get_shape()) == 4 and "Previous layer must be 4-dimensional (batch, width, height, channels)"
# Add projection in series if needed prior to shortcut
if num_units != int(self.get_output().get_shape()[3]):
self.add_conv2d(num_units, mapsize=1, stride=1, stddev_factor=1.)
bypass = self.get_output()
# Residual block
for _ in range(num_layers):
self.add_batch_norm()
self.add_relu()
self.add_conv2d(num_units, mapsize=mapsize, stride=1, stddev_factor=stddev_factor)
self.add_sum(bypass)
return self
def add_bottleneck_residual_block(self, num_units, mapsize=3, stride=1, transpose=False):
"""Adds a bottleneck residual block as per Arxiv 1512.03385, Figure 3"""
assert len(self.get_output().get_shape()) == 4 and "Previous layer must be 4-dimensional (batch, width, height, channels)"
# Add projection in series if needed prior to shortcut
if num_units != int(self.get_output().get_shape()[3]) or stride != 1:
ms = 1 if stride == 1 else mapsize
#bypass.add_batch_norm() # TBD: Needed?
if transpose:
self.add_conv2d_transpose(num_units, mapsize=ms, stride=stride, stddev_factor=1.)
else:
self.add_conv2d(num_units, mapsize=ms, stride=stride, stddev_factor=1.)
bypass = self.get_output()
# Bottleneck residual block
self.add_batch_norm()
self.add_relu()
self.add_conv2d(num_units//4, mapsize=1, stride=1, stddev_factor=2.)
self.add_batch_norm()
self.add_relu()
if transpose:
self.add_conv2d_transpose(num_units//4,
mapsize=mapsize,
stride=1,
stddev_factor=2.)
else:
self.add_conv2d(num_units//4,
mapsize=mapsize,
stride=1,
stddev_factor=2.)
self.add_batch_norm()
self.add_relu()
self.add_conv2d(num_units, mapsize=1, stride=1, stddev_factor=2.)
self.add_sum(bypass)
return self
def add_sum(self, term):
"""Adds a layer that sums the top layer with the given term"""
with tf.variable_scope(self._get_layer_str()):
prev_shape = self.get_output().get_shape()
term_shape = term.get_shape()
#print("%s %s" % (prev_shape, term_shape))
assert prev_shape == term_shape and "Can't sum terms with a different size"
out = tf.add(self.get_output(), term)
self.outputs.append(out)
return self
def add_mean(self):
"""Adds a layer that averages the inputs from the previous layer"""
with tf.variable_scope(self._get_layer_str()):
prev_shape = self.get_output().get_shape()
reduction_indices = list(range(len(prev_shape)))
assert len(reduction_indices) > 2 and "Can't average a (batch, activation) tensor"
reduction_indices = reduction_indices[1:-1]
out = tf.reduce_mean(self.get_output(), reduction_indices=reduction_indices)
self.outputs.append(out)
return self
def add_upscale(self, size=None):
"""Adds a layer that upscales the output by 2x through nearest neighbor interpolation"""
prev_shape = self.get_output().get_shape()
if size is None:
size = [2 * int(s) for s in prev_shape[1:3]]
out = tf.image.resize_nearest_neighbor(self.get_output(), size)
self.outputs.append(out)
return self
def add_concat(self, layer_add):
last_layer = self.get_output()
prev_shape = last_layer.get_shape()
try:
out = tf.concat(axis = 3, values = [last_layer, layer_add])
self.outputs.append(out)
except:
print('fail to concat {0} and {1}'.format(last_layer, layer_add))
return self
def add_layer(self, layer_add):
self.outputs.append(layer_add)
return self
def get_output(self):
"""Returns the output from the topmost layer of the network"""
return self.outputs[-1]
def get_variable(self, layer, name):
"""Returns a variable given its layer and name.
The variable must already exist."""
scope = self._get_layer_str(layer)
collection = tf.get_collection(tf.GraphKeys.VARIABLES, scope=scope)
# TBD: Ugly!
for var in collection:
if var.name[:-2] == scope+'/'+name:
return var
return None
def get_all_layer_variables(self, layer):
"""Returns all variables in the given layer"""
scope = self._get_layer_str(layer)
return tf.get_collection(tf.GraphKeys.VARIABLES, scope=scope)
def _discriminator_model(sess, features, disc_input, layer_output_skip=5, hybrid_disc=0):
# update 05092017, hybrid_disc consider whether to use hybrid space for discriminator
# to study the kspace distribution/smoothness properties
# Fully convolutional model
mapsize = 3
layers = [8, 16, 32, 64]#[64, 128, 256, 512]
old_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
# get discriminator input
disc_hybird = 2 * disc_input - 1
print(hybrid_disc, 'discriminator input dimensions: {0}'.format(disc_hybird.get_shape()))
model = Model('DIS', disc_hybird)
# discriminator network structure
for layer in range(len(layers)):
nunits = layers[layer]
stddev_factor = 2.0
model.add_conv2d(nunits, mapsize=mapsize, stride=2, stddev_factor=stddev_factor)
model.add_batch_norm()
model.add_relu()
# Finalization a la "all convolutional net"
model.add_conv2d(nunits, mapsize=mapsize, stride=1, stddev_factor=stddev_factor)
model.add_batch_norm()
model.add_relu()
model.add_conv2d(nunits, mapsize=1, stride=1, stddev_factor=stddev_factor)
model.add_batch_norm()
model.add_relu()
# Linearly map to real/fake and return average score
# (softmax will be applied later)
model.add_conv2d(1, mapsize=1, stride=1, stddev_factor=stddev_factor)
model.add_mean()
new_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
disc_vars = list(set(new_vars) - set(old_vars))
#select output
output_layers = [model.outputs[0]] + model.outputs[1:-1][::layer_output_skip] + [model.outputs[-1]]
return model.get_output(), disc_vars, output_layers
def conv(batch_input, out_channels, stride=2, size_kernel=4):
with tf.variable_scope("conv"):
in_channels = batch_input.get_shape()[3]
filter = tf.get_variable("filter", [size_kernel, size_kernel, in_channels, out_channels], dtype=tf.float32, initializer=tf.random_normal_initializer(0, 0.02))
# [batch, in_height, in_width, in_channels], [filter_width, filter_height, in_channels, out_channels]
# => [batch, out_height, out_width, out_channels]
padded_input = tf.pad(batch_input, [[0, 0], [1, 1], [1, 1], [0, 0]], mode="CONSTANT")
conv = tf.nn.conv2d(padded_input, filter, [1, stride, stride, 1], padding="VALID")
return conv
def deconv(batch_input, out_channels, size_kernel=3):
with tf.variable_scope("deconv"):
batch, in_height, in_width, in_channels = [int(d) for d in batch_input.get_shape()]
filter = tf.get_variable("filter", [size_kernel, size_kernel, out_channels, in_channels], dtype=tf.float32, initializer=tf.random_normal_initializer(0, 0.02))
# [batch, in_height, in_width, in_channels], [filter_width, filter_height, out_channels, in_channels]
# => [batch, out_height, out_width, out_channels]
conv = tf.nn.conv2d_transpose(batch_input, filter, [batch, in_height * 2, in_width * 2, out_channels], [1, 2, 2, 1], padding="SAME")
return conv
def lrelu(x, a):
with tf.name_scope("lrelu"):
# adding these together creates the leak part and linear part
# then cancels them out by subtracting/adding an absolute value term
# leak: a*x/2 - a*abs(x)/2
# linear: x/2 + abs(x)/2
# this block looks like it has 2 inputs on the graph unless we do this
x = tf.identity(x)
return (0.5 * (1 + a)) * x + (0.5 * (1 - a)) * tf.abs(x)
def batchnorm(input):
with tf.variable_scope("batchnorm"):
# this block looks like it has 3 inputs on the graph unless we do this
input = tf.identity(input)
channels = input.get_shape()[3]
offset = tf.get_variable("offset", [channels], dtype=tf.float32, initializer=tf.zeros_initializer())
scale = tf.get_variable("scale", [channels], dtype=tf.float32, initializer=tf.random_normal_initializer(1.0, 0.02))
mean, variance = tf.nn.moments(input, axes=[0, 1, 2], keep_dims=False)
variance_epsilon = 1e-5
normalized = tf.nn.batch_normalization(input, mean, variance, offset, scale, variance_epsilon=variance_epsilon)
return normalized
def Fourier(x, separate_complex=True):
x = tf.cast(x, tf.complex64)
if separate_complex:
x_complex = x[:,:,:,0]+1j*x[:,:,:,1]
else:
x_complex = x
x_complex = tf.reshape(x_complex,x_complex.get_shape()[:3])
y_complex = tf.fft2d(x_complex)
print('using Fourier, input dim {0}, output dim {1}'.format(x.get_shape(), y_complex.get_shape()))
# x = tf.cast(x, tf.complex64)
# y = tf.fft3d(x)
# y = y[:,:,:,-1]
return y_complex
def _generator_encoder_decoder(sess, features, labels, channels, layer_output_skip=5):
print('use encoder decoder model')
# old variables
layers = []
old_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
# layers.append(features)
# definition
num_filter_generator = 8
layer_specs = [
num_filter_generator * 2, # encoder_2: [batch, 128, 128, ngf] => [batch, 64, 64, ngf * 2]
num_filter_generator * 4, # encoder_3: [batch, 64, 64, ngf * 2] => [batch, 32, 32, ngf * 4]
num_filter_generator * 8, # encoder_4: [batch, 32, 32, ngf * 4] => [batch, 16, 16, ngf * 8]
# num_filter_generator * 8, # encoder_5: [batch, 16, 16, ngf * 8] => [batch, 8, 8, ngf * 8]
# num_filter_generator * 8, # encoder_6: [batch, 8, 8, ngf * 8] => [batch, 4, 4, ngf * 8]
# num_filter_generator * 8, # encoder_7: [batch, 4, 4, ngf * 8] => [batch, 2, 2, ngf * 8]
num_filter_generator * 16, # encoder_8: [batch, 2, 2, ngf * 8] => [batch, 1, 1, ngf * 8]
]
# encoder_1: [batch, 256, 256, in_channels] => [batch, 128, 128, ngf]
with tf.variable_scope("encoder_1"):
output = conv(features, num_filter_generator, stride=2)
layers.append(output)
for out_channels in layer_specs:
with tf.variable_scope("encoder_%d" % (len(layers) + 1)):
rectified = lrelu(layers[-1], 0.2)
# [batch, in_height, in_width, in_channels] => [batch, in_height/2, in_width/2, out_channels]
convolved = conv(rectified, out_channels, stride=2)
output = batchnorm(convolved)
layers.append(output)
layer_specs = [
# (num_filter_generator * 16, 0.5), # decoder_8: [batch, 1, 1, ngf * 8] => [batch, 2, 2, ngf * 8 * 2]
# (num_filter_generator * 8, 0.5), # decoder_7: [batch, 2, 2, ngf * 8 * 2] => [batch, 4, 4, ngf * 8 * 2]
# (num_filter_generator * 8, 0.5), # decoder_6: [batch, 4, 4, ngf * 8 * 2] => [batch, 8, 8, ngf * 8 * 2]
(num_filter_generator * 8, 0.0), # decoder_5: [batch, 8, 8, ngf * 8 * 2] => [batch, 16, 16, ngf * 8 * 2]
(num_filter_generator * 4, 0.0), # decoder_4: [batch, 16, 16, ngf * 8 * 2] => [batch, 32, 32, ngf * 4 * 2]
(num_filter_generator * 2, 0.0), # decoder_3: [batch, 32, 32, ngf * 4 * 2] => [batch, 64, 64, ngf * 2 * 2]
(num_filter_generator, 0.0), # decoder_2: [batch, 64, 64, ngf * 2 * 2] => [batch, 128, 128, ngf * 2]
]
num_encoder_layers = len(layers)
for decoder_layer, (out_channels, dropout) in enumerate(layer_specs):
skip_layer = num_encoder_layers - decoder_layer - 1
with tf.variable_scope("decoder_%d" % (skip_layer + 1)):
if decoder_layer == 0:
# first decoder layer doesn't have skip connections
# since it is directly connected to the skip_layer
input = layers[-1]
else:
input = tf.concat(axis=3, values=[layer[-1], layers[skip_layer]]) # change the order of value and axisn, axis=3)
rectified = tf.nn.relu(input)
# [batch, in_height, in_width, in_channels] => [batch, in_height*2, in_width*2, out_channels]
output = deconv(rectified, out_channels)
output = batchnorm(output)
# if dropout > 0.0:
# output = tf.nn.dropout(output, keep_prob = 1 - dropout)
layers.append(output)
# decoder_1: [batch, 128, 128, ngf * 2] => [batch, 256, 256, generator_outputs_channels]
for x in layers:
print(x)
with tf.variable_scope("decoder_1"):
input = tf.concat(axis=3, values=[layer[-1], layers[0]]) #, axis=3)
rectified = tf.nn.relu(input)
output = deconv(rectified, channels)
# output = tf.tanh(output)
output = tf.nn.sigmoid(output)
layers.append(output)
# out variables
new_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
gene_vars = list(set(new_vars) - set(old_vars))
# select subset of layers
output_layers = [layers[0]] + layers[1:-1][::layer_output_skip] + [layers[-1]]
return layers[-1], gene_vars, output_layers
def _generator_model_with_pool(sess, features, labels, channels, layer_output_skip=5):
mapsize = 3
res_units = [64, 128, 128] #[64, 32, 16]#[256, 128, 96]
layer_pooling = [1, 1, 0]
print('use resnet conv-decov with pooling parameters:', res_units, layer_pooling)
old_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
# See Arxiv 1603.05027
model = Model('GEN', features)
list_layer_before_pool=[]
for index_layer in range(len(res_units)-1):
nunits = res_units[index_layer]
for j in range(2):
model.add_residual_block(nunits, mapsize=mapsize)
list_layer_before_pool.append(model.outputs[-1])
# conv
# model.add_batch_norm()
# model.add_relu()
# model.add_conv2d_transpose(nunits, mapsize=mapsize, stride=1, stddev_factor=1.)
model.add_batch_norm()
model.add_relu()
# pooling/striding
stride = layer_pooling[index_layer]+1
model.add_conv2d(nunits, mapsize=mapsize, stride=stride, stddev_factor=1.)
print('list_layer_before_pool', list_layer_before_pool)
print('model.outputs', model.outputs)
for index_layer_rev in range(len(res_units)-1):
index_layer = len(list_layer_before_pool)-1-index_layer_rev
nunits = res_units[index_layer]
for j in range(2):
model.add_residual_block(nunits, mapsize=mapsize)
# up-pool cov
if layer_pooling[index_layer]:
model.add_upscale()
model.add_batch_norm()
model.add_relu()
model.add_conv2d_transpose(nunits, mapsize=mapsize, stride=1, stddev_factor=1.)
# concat
model.add_concat(list_layer_before_pool[index_layer])
# conv
nunits = res_units[-1]
model.add_conv2d(nunits, mapsize=mapsize, stride=1, stddev_factor=2.)
model.add_relu()
# filter to channel number
model.add_conv2d(nunits, mapsize=1, stride=1, stddev_factor=2.)
model.add_relu()
# output
model.add_conv2d(channels, mapsize=1, stride=1, stddev_factor=1.)
model.add_sigmoid()
# get variables
new_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
gene_vars = list(set(new_vars) - set(old_vars))
# select subset of layers
output_layers = [model.outputs[0]] + model.outputs[1:-1][::layer_output_skip] + [model.outputs[-1]]
return model.get_output(), gene_vars, output_layers
def _generator_model_with_scale(sess, features, labels, masks, channels, layer_output_skip=5,
num_dc_layers=0):
# Upside-down all-convolutional resnet
channels = 2
#image_size = tf.shape(features)
mapsize = 3
res_units = [128, 128, 128, 128, 128] #[64, 32, 16]#[256, 128, 96]
scale_changes = [0,0,0,0,0,0]
print('use resnet without pooling:', res_units)
old_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
# See Arxiv 1603.05027
model = Model('GEN', features)
# loop different levels
for ru in range(len(res_units)-1):
nunits = res_units[ru]
for j in range(2):
model.add_residual_block(nunits, mapsize=mapsize)
# Spatial upscale (see http://distill.pub/2016/deconv-checkerboard/)
# and transposed convolution
if scale_changes[ru]>0:
model.add_upscale()
model.add_batch_norm()
model.add_relu()
model.add_conv2d_transpose(nunits, mapsize=mapsize, stride=1, stddev_factor=1.)
# Finalization a la "all convolutional net"
nunits = res_units[-1]
model.add_conv2d(nunits, mapsize=mapsize, stride=1, stddev_factor=2.)
# Worse: model.add_batch_norm()
model.add_relu()
model.add_conv2d(nunits, mapsize=1, stride=1, stddev_factor=2.)
# Worse: model.add_batch_norm()
model.add_relu()
# Last layer is sigmoid with no batch normalization
model.add_conv2d(channels, mapsize=1, stride=1, stddev_factor=1.)
#model.add_sigmoid()
# add dc connection for each block
if num_dc_layers >= 0:
# parameters
threshold_zero = 1./255.
mix_DC = 1 #0.95
# sampled kspace
first_layer = features
feature_kspace = Fourier(first_layer, separate_complex=True)
#mask_kspace = tf.cast(masks, dtype=tf.float32) #tf.greater(tf.abs(feature_kspace),threshold_zero)
#print('sampling_rate', sess.run(tf.reduce_sum(tf.abs(mask_kspace)) / tf.size(mask_kspace)))
mask_kspace = tf.cast(masks, tf.complex64) * mix_DC
#print('sampling_size', sess.run(tf.reduce_sum(tf.abs(mask_kspace))))
#print('mask_kspace', sess.run(mask_kspace))
projected_kspace = feature_kspace * mask_kspace
# add dc layers
num_dc_layerss = 1
for index_dc_layer in range(num_dc_layerss):
# get output and input
last_layer = model.outputs[-1]
# compute kspace
gene_kspace = Fourier(last_layer, separate_complex=True)
# affine projection
corrected_kspace = projected_kspace + gene_kspace * (1.0 - mask_kspace)
# inverse fft
corrected_complex = tf.ifft2d(corrected_kspace)
image_size = tf.shape(corrected_complex)
## get abs
#corrected_mag = tf.cast(tf.abs(corrected_complex), tf.float32)
#print('corrected_complex', corrected_complex.get_shape())
#get real and imaginary parts
corrected_real = tf.reshape(tf.real(corrected_complex), [FLAGS.batch_size, 256, 128, 1])
corrected_imag = tf.reshape(tf.imag(corrected_complex), [FLAGS.batch_size, 256, 128, 1])
#print('size_corrected_real', corrected_real.get_shape())
corrected_real_concat = tf.concat([corrected_real, corrected_imag], axis=3)
#print('corrected_concat', corrected_real_concat.get_shape())
#print('channels', channels)
# reshape
#labels_size = tf.shape(labels)
#corrected_mag = tf.reshape(corrected_mag, labels_size)
model.add_layer(corrected_real_concat)
# concat
# model.add_concat(corrected_mag)
# mixing and project to image domain
# model.add_residual_block(channels, mapsize=mapsize)
# model.add_conv2d(channels, mapsize=1, stride=1, stddev_factor=1.)
# final output
# model.add_sigmoid()
#print('variational network with DC correction', model.outputs)
new_vars = tf.global_variables()#tf.all_variables() , all_variables() are deprecated
gene_vars = list(set(new_vars) - set(old_vars))
# select subset of layers
output_layers = [model.outputs[0]] + model.outputs[1:-1][::layer_output_skip] + [model.outputs[-1]]
return model.get_output(), gene_vars, output_layers
def create_model(sess, features, labels, masks, architecture='resnet'):
# sess: TF sesson
# features: input, for SR/CS it is the input image
# labels: output, for SR/CS it is the groundtruth image
# architecture: aec for encode-decoder, resnet for upside down
# Generator
rows = int(features.get_shape()[1])
cols = int(features.get_shape()[2])
channels = int(features.get_shape()[3])
#print('channels', features.get_shape())
gene_minput = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, rows, cols, channels])
# TBD: Is there a better way to instance the generator?
if architecture == 'aec':
function_generator = lambda x,y,z,w: _generator_encoder_decoder(x,y,z,w)
elif architecture == 'pool':
function_generator = lambda x,y,z,w: _generator_model_with_pool(x,y,z,w)
elif architecture.startswith('var'):
num_dc_layers = 1
if architecture!='var':
try:
num_dc_layers = int(architecture.split('var')[-1])
except:
pass
function_generator = lambda x,y,z,m,w: _generator_model_with_scale(x,y,z,m,w,
num_dc_layers=num_dc_layers, layer_output_skip=7)
else:
function_generator = lambda x,y,z,m,w: _generator_model_with_scale(x,y,z,m,w,
num_dc_layers=0, layer_output_skip=7)
with tf.variable_scope('gene') as scope:
gene_output_1, gene_var_list, gene_layers_1 = function_generator(sess, features, labels, masks, 1)
scope.reuse_variables()
gene_output_2, _ , gene_layers_2 = function_generator(sess, gene_output_1, labels, masks, 1)
scope.reuse_variables()
gene_output_3, _ , gene_layers_3 = function_generator(sess, gene_output_2, labels, masks, 1)
scope.reuse_variables()
gene_output_4, _ , gene_layers_4 = function_generator(sess, gene_output_3, labels, masks, 1)
scope.reuse_variables()
gene_output_5, _ , gene_layers_5 = function_generator(sess, gene_output_4, labels, masks, 1)
scope.reuse_variables()
gene_output_6, _ , gene_layers_6 = function_generator(sess, gene_output_5, labels, masks, 1)
scope.reuse_variables()
gene_output_7, _ , gene_layers_7 = function_generator(sess, gene_output_6, labels, masks, 1)
scope.reuse_variables()
gene_output_8, _ , gene_layers_8 = function_generator(sess, gene_output_7, labels, masks, 1)
scope.reuse_variables()
gene_output_9, _ , gene_layers_9 = function_generator(sess, gene_output_8, labels, masks, 1)
scope.reuse_variables()
gene_output_10, _ , gene_layers_10 = function_generator(sess, gene_output_9, labels, masks, 1)
scope.reuse_variables()
gene_output_11, _ , gene_layers_11 = function_generator(sess, gene_output_10, labels, masks, 1)
scope.reuse_variables()
gene_output_12, _ , gene_layers_12 = function_generator(sess, gene_output_11, labels, masks, 1)
scope.reuse_variables()
gene_output_13, _ , gene_layers_13 = function_generator(sess, gene_output_12, labels, masks, 1)
scope.reuse_variables()
gene_output_14, _ , gene_layers_14 = function_generator(sess, gene_output_13, labels, masks, 1)
scope.reuse_variables()
gene_output_15, _ , gene_layers_15 = function_generator(sess, gene_output_14, labels, masks, 1)
scope.reuse_variables()
gene_output_16, _ , gene_layers_16 = function_generator(sess, gene_output_15, labels, masks, 1)
scope.reuse_variables()
gene_output_17, _ , gene_layers_17 = function_generator(sess, gene_output_16, labels, masks, 1)
scope.reuse_variables()
gene_output_18, _ , gene_layers_18 = function_generator(sess, gene_output_17, labels, masks, 1)
scope.reuse_variables()
gene_output_19, _ , gene_layers_19 = function_generator(sess, gene_output_18, labels, masks, 1)
scope.reuse_variables()
gene_output_20, _ , gene_layers_20 = function_generator(sess, gene_output_19, labels, masks, 1)
scope.reuse_variables()
gene_output_real = gene_output_1
gene_output_complex = tf.complex(gene_output_real[:,:,:,0], gene_output_real[:,:,:,1])
gene_output = tf.abs(gene_output_complex)
#print('gene_output_train', gene_output.get_shape())
gene_output = tf.reshape(gene_output, [FLAGS.batch_size, rows, cols, 1])
gene_layers = gene_layers_1
# for testing input
gene_moutput_1, _ , gene_mlayers_1 = function_generator(sess, gene_minput, labels, masks, 1)
scope.reuse_variables()
gene_moutput_2, _ , gene_mlayers_2= function_generator(sess, gene_moutput_1, labels, masks, 1)
scope.reuse_variables()
gene_moutput_3, _ , gene_mlayers_3= function_generator(sess, gene_moutput_2, labels, masks, 1)
scope.reuse_variables()
gene_moutput_4, _ , gene_mlayers_4= function_generator(sess, gene_moutput_3, labels, masks, 1)
scope.reuse_variables()
gene_moutput_5, _ , gene_mlayers_5= function_generator(sess, gene_moutput_4, labels, masks, 1)
scope.reuse_variables()
gene_moutput_6, _ , gene_mlayers_6= function_generator(sess, gene_moutput_5, labels, masks, 1)
scope.reuse_variables()
gene_moutput_7, _ , gene_mlayers_7= function_generator(sess, gene_moutput_6, labels, masks, 1)
scope.reuse_variables()
gene_moutput_8, _ , gene_mlayers_8= function_generator(sess, gene_moutput_7, labels, masks, 1)
scope.reuse_variables()
gene_moutput_9, _ , gene_mlayers_9= function_generator(sess, gene_moutput_8, labels, masks, 1)
scope.reuse_variables()
gene_moutput_10, _ , gene_mlayers_10= function_generator(sess, gene_moutput_9, labels, masks, 1)
scope.reuse_variables()
gene_moutput_11, _ , gene_mlayers_11= function_generator(sess, gene_moutput_10, labels, masks, 1)
scope.reuse_variables()
gene_moutput_12, _ , gene_mlayers_12= function_generator(sess, gene_moutput_11, labels, masks, 1)
scope.reuse_variables()
gene_moutput_13, _ , gene_mlayers_13= function_generator(sess, gene_moutput_12, labels, masks, 1)
scope.reuse_variables()
gene_moutput_14, _ , gene_mlayers_14= function_generator(sess, gene_moutput_13, labels, masks, 1)
scope.reuse_variables()
gene_moutput_15, _ , gene_mlayers_15= function_generator(sess, gene_moutput_14, labels, masks, 1)
scope.reuse_variables()
gene_moutput_16, _ , gene_mlayers_16= function_generator(sess, gene_moutput_15, labels, masks, 1)
scope.reuse_variables()
gene_moutput_17, _ , gene_mlayers_17= function_generator(sess, gene_moutput_16, labels, masks, 1)
scope.reuse_variables()
gene_moutput_18, _ , gene_mlayers_18= function_generator(sess, gene_moutput_17, labels, masks, 1)
scope.reuse_variables()
gene_moutput_19, _ , gene_mlayers_19= function_generator(sess, gene_moutput_18, labels, masks, 1)
scope.reuse_variables()
gene_moutput_20, _ , gene_mlayers_20= function_generator(sess, gene_moutput_19, labels, masks, 1)
scope.reuse_variables()
gene_moutput_real = gene_moutput_1
gene_moutput_complex = tf.complex(gene_moutput_real[:,:,:,0], gene_moutput_real[:,:,:,1])
gene_moutput = tf.abs(gene_moutput_complex)
#print('gene_moutput_test', gene_moutput.get_shape())
gene_moutput = tf.reshape(gene_moutput, [FLAGS.batch_size, rows, cols, 1])
gene_mlayers = gene_mlayers_1
# Discriminator with real data
disc_real_input = tf.identity(labels, name='disc_real_input')
# TBD: Is there a better way to instance the discriminator?
with tf.variable_scope('disc') as scope:
#print('hybrid_disc', FLAGS.hybrid_disc)
disc_real_output, disc_var_list, disc_layers = \
_discriminator_model(sess, features, disc_real_input, hybrid_disc=FLAGS.hybrid_disc)
scope.reuse_variables()
disc_fake_output, _, _ = _discriminator_model(sess, features, gene_output, hybrid_disc=FLAGS.hybrid_disc)
#test
scope.reuse_variables()
disc_moutput, _, disc_mlayers = \
_discriminator_model(sess, features, gene_moutput, hybrid_disc=FLAGS.hybrid_disc)
return [gene_minput, gene_moutput, gene_moutput_complex,
gene_output, gene_output_complex, gene_var_list, gene_layers, gene_mlayers,
disc_real_output, disc_fake_output, disc_moutput, disc_var_list, disc_layers, disc_mlayers]
# SSIM
def keras_var(x, axis=None, keepdims=False):
"""Variance of a tensor, alongside the specified axis.
# Arguments
x: A tensor or variable.
axis: An integer, the axis to compute the variance.
keepdims: A boolean, whether to keep the dimensions or not.
If `keepdims` is `False`, the rank of the tensor is reduced
by 1. If `keepdims` is `True`,
the reduced dimension is retained with length 1.
# Returns
A tensor with the variance of elements of `x`.
"""
# axis = _normalize_axis(axis, ndim(x))
if x.dtype.base_dtype == tf.bool:
x = tf.cast(x, floatx())
m = tf.reduce_mean(x, reduction_indices=axis, keep_dims=True)
devs_squared = tf.square(x - m)
return tf.reduce_mean(devs_squared,
reduction_indices=axis,
keep_dims=keepdims)
def keras_std(x, axis=None, keepdims=False):
"""Standard deviation of a tensor, alongside the specified axis.
# Arguments
x: A tensor or variable.
axis: An integer, the axis to compute the standard deviation.
keepdims: A boolean, whether to keep the dimensions or not.
If `keepdims` is `False`, the rank of the tensor is reduced
by 1. If `keepdims` is `True`,
the reduced dimension is retained with length 1.
# Returns
A tensor with the standard deviation of elements of `x`.
"""
return tf.sqrt(keras_var(x, axis=axis, keepdims=keepdims))
def keras_mean(x, axis=None, keepdims=False):
"""Mean of a tensor, alongside the specified axis.
# Arguments
x: A tensor or variable.
axis: A list of integer. Axes to compute the mean.
keepdims: A boolean, whether to keep the dimensions or not.
If `keepdims` is `False`, the rank of the tensor is reduced
by 1 for each entry in `axis`. If `keep_dims` is `True`,
the reduced dimensions are retained with length 1.
# Returns
A tensor with the mean of elements of `x`.
"""
# axis = _normalize_axis(axis, ndim(x))
if x.dtype.base_dtype == tf.bool:
x = tf.cast(x, floatx())
return tf.reduce_mean(x, reduction_indices=axis, keep_dims=keepdims)
def loss_DSSIS_tf11(y_true, y_pred, patch_size=5, batch_size=-1):
# get batch size
if batch_size<0:
batch_size = int(y_true.get_shape()[0])
else:
y_true = tf.reshape(y_true, [batch_size] + get_shape(y_pred)[1:])
y_pred = tf.reshape(y_pred, [batch_size] + get_shape(y_pred)[1:])
# batch, x, y, channel
# y_true = tf.transpose(y_true, [0, 2, 3, 1])
# y_pred = tf.transpose(y_pred, [0, 2, 3, 1])
patches_true = tf.extract_image_patches(y_true, [1, patch_size, patch_size, 1], [1, 2, 2, 1], [1, 1, 1, 1], "SAME")
patches_pred = tf.extract_image_patches(y_pred, [1, patch_size, patch_size, 1], [1, 2, 2, 1], [1, 1, 1, 1], "SAME")
#print(patches_true, patches_pred)
u_true = keras_mean(patches_true, axis=3)
u_pred = keras_mean(patches_pred, axis=3)
#print(u_true, u_pred)