forked from brade31919/SRGAN-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
699 lines (561 loc) · 32.7 KB
/
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from lib.ops import *
import collections
import os
import math
import scipy.misc as sic
import numpy as np
# Define the dataloader
def data_loader(FLAGS):
with tf.device('/cpu:0'):
# Define the returned data batches
Data = collections.namedtuple('Data', 'paths_LR, paths_HR, inputs, targets, image_count, steps_per_epoch')
#Check the input directory
if (FLAGS.input_dir_LR == 'None') or (FLAGS.input_dir_HR == 'None'):
raise ValueError('Input directory is not provided')
if (not os.path.exists(FLAGS.input_dir_LR)) or (not os.path.exists(FLAGS.input_dir_HR)):
raise ValueError('Input directory not found')
image_list_LR = os.listdir(FLAGS.input_dir_LR)
image_list_LR = [_ for _ in image_list_LR if _.endswith('.jpg')]
if len(image_list_LR)==0:
raise Exception('No png files in the input directory')
image_list_LR_temp = sorted(image_list_LR)
image_list_LR = [os.path.join(FLAGS.input_dir_LR, _) for _ in image_list_LR_temp]
image_list_HR = [os.path.join(FLAGS.input_dir_HR, _) for _ in image_list_LR_temp]
image_list_LR_tensor = tf.convert_to_tensor(image_list_LR, dtype=tf.string)
image_list_HR_tensor = tf.convert_to_tensor(image_list_HR, dtype=tf.string)
with tf.variable_scope('load_image'):
# define the image list queue
# image_list_LR_queue = tf.train.string_input_producer(image_list_LR, shuffle=False, capacity=FLAGS.name_queue_capacity)
# image_list_HR_queue = tf.train.string_input_producer(image_list_HR, shuffle=False, capacity=FLAGS.name_queue_capacity)
#print('[Queue] image list queue use shuffle: %s'%(FLAGS.mode == 'Train'))
output = tf.train.slice_input_producer([image_list_LR_tensor, image_list_HR_tensor],
shuffle=False, capacity=FLAGS.name_queue_capacity)
# Reading and decode the images
reader = tf.WholeFileReader(name='image_reader')
image_LR = tf.read_file(output[0])
image_HR = tf.read_file(output[1])
input_image_LR = tf.image.decode_jpeg(image_LR, channels=3)
input_image_HR = tf.image.decode_jpeg(image_HR, channels=3)
input_image_LR = tf.image.convert_image_dtype(input_image_LR, dtype=tf.float32)
input_image_HR = tf.image.convert_image_dtype(input_image_HR, dtype=tf.float32)
assertion = tf.assert_equal(tf.shape(input_image_LR)[2], 3, message="image does not have 3 channels")
with tf.control_dependencies([assertion]):
input_image_LR = tf.identity(input_image_LR)
input_image_HR = tf.identity(input_image_HR)
# Normalize the low resolution image to [0, 1], high resolution to [-1, 1]
a_image = preprocessLR(input_image_LR)
b_image = preprocess(input_image_HR)
inputs, targets = [a_image, b_image]
# The data augmentation part
with tf.name_scope('data_preprocessing'):
with tf.name_scope('random_crop'):
# Check whether perform crop
if (FLAGS.random_crop is True) and FLAGS.mode == 'train':
print('[Config] Use random crop')
# Set the shape of the input image. the target will have 4X size
input_size = tf.shape(inputs)
target_size = tf.shape(targets)
offset_w = tf.cast(tf.floor(tf.random_uniform([], 0, tf.cast(input_size[1], tf.float32) - FLAGS.crop_size)),
dtype=tf.int32)
offset_h = tf.cast(tf.floor(tf.random_uniform([], 0 , tf.cast(input_size[0], tf.float32) - FLAGS.crop_size)),
dtype=tf.int32)
if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet':
inputs = tf.image.crop_to_bounding_box(inputs, offset_h, offset_w, FLAGS.crop_size,
FLAGS.crop_size)
targets = tf.image.crop_to_bounding_box(targets, offset_h*4, offset_w*4, FLAGS.crop_size*4,
FLAGS.crop_size*4)
elif FLAGS.task == 'denoise':
inputs = tf.image.crop_to_bounding_box(inputs, offset_h, offset_w, FLAGS.crop_size,
FLAGS.crop_size)
targets = tf.image.crop_to_bounding_box(targets, offset_h, offset_w,
FLAGS.crop_size, FLAGS.crop_size)
# Do not perform crop
else:
inputs = tf.identity(inputs)
targets = tf.identity(targets)
with tf.variable_scope('random_flip'):
# Check for random flip:
if (FLAGS.flip is True) and (FLAGS.mode == 'train'):
print('[Config] Use random flip')
# Produce the decision of random flip
decision = tf.random_uniform([], 0, 1, dtype=tf.float32)
input_images = random_flip(inputs, decision)
target_images = random_flip(targets, decision)
else:
input_images = tf.identity(inputs)
target_images = tf.identity(targets)
if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet':
input_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3])
target_images.set_shape([FLAGS.crop_size*4, FLAGS.crop_size*4, 3])
elif FLAGS.task == 'denoise':
input_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3])
target_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3])
if FLAGS.mode == 'train':
paths_LR_batch, paths_HR_batch, inputs_batch, targets_batch = tf.train.shuffle_batch([output[0], output[1], input_images, target_images],
batch_size=FLAGS.batch_size, capacity=FLAGS.image_queue_capacity+4*FLAGS.batch_size,
min_after_dequeue=FLAGS.image_queue_capacity, num_threads=FLAGS.queue_thread)
else:
paths_LR_batch, paths_HR_batch, inputs_batch, targets_batch = tf.train.batch([output[0], output[1], input_images, target_images],
batch_size=FLAGS.batch_size, num_threads=FLAGS.queue_thread, allow_smaller_final_batch=True)
steps_per_epoch = int(math.ceil(len(image_list_LR) / FLAGS.batch_size))
if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet':
inputs_batch.set_shape([FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3])
targets_batch.set_shape([FLAGS.batch_size, FLAGS.crop_size*4, FLAGS.crop_size*4, 3])
elif FLAGS.task == 'denoise':
inputs_batch.set_shape([FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3])
targets_batch.set_shape([FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3])
return Data(
paths_LR=paths_LR_batch,
paths_HR=paths_HR_batch,
inputs=inputs_batch,
targets=targets_batch,
image_count=len(image_list_LR),
steps_per_epoch=steps_per_epoch
)
# The test data loader. Allow input image with different size
def test_data_loader(FLAGS):
# Get the image name list
if (FLAGS.input_dir_LR == 'None') or (FLAGS.input_dir_HR == 'None'):
raise ValueError('Input directory is not provided')
if (not os.path.exists(FLAGS.input_dir_LR)) or (not os.path.exists(FLAGS.input_dir_HR)):
raise ValueError('Input directory not found')
image_list_LR_temp = os.listdir(FLAGS.input_dir_LR)
image_list_LR = [os.path.join(FLAGS.input_dir_LR, _) for _ in image_list_LR_temp if _.split('.')[-1] == 'jpg']
image_list_HR = [os.path.join(FLAGS.input_dir_HR, _) for _ in image_list_LR_temp if _.split('.')[-1] == 'jpg']
# Read in and preprocess the images
def preprocess_test(name, mode):
im = sic.imread(name).astype(np.float32)
# check grayscale image
if im.shape[-1] != 3:
h, w = im.shape
temp = np.empty((h, w, 3), dtype=np.uint8)
temp[:, :, :] = im[:, :, np.newaxis]
im = temp.copy()
if mode == 'LR':
im = im / np.max(im)
elif mode == 'HR':
im = im / np.max(im)
im = im * 2 - 1
return im
image_LR = [preprocess_test(_, 'LR') for _ in image_list_LR]
image_HR = [preprocess_test(_, 'HR') for _ in image_list_HR]
# Push path and image into a list
Data = collections.namedtuple('Data', 'paths_LR, paths_HR, inputs, targets')
return Data(
paths_LR = image_list_LR,
paths_HR = image_list_HR,
inputs = image_LR,
targets = image_HR
)
# The inference data loader. Allow input image with different size
def inference_data_loader(FLAGS):
# Get the image name list
if (FLAGS.input_dir_LR == 'None'):
raise ValueError('Input directory is not provided')
if not os.path.exists(FLAGS.input_dir_LR):
raise ValueError('Input directory not found')
image_list_LR_temp = os.listdir(FLAGS.input_dir_LR)
image_list_LR = [os.path.join(FLAGS.input_dir_LR, _) for _ in image_list_LR_temp if _.split('.')[-1] == 'jpg']
# Read in and preprocess the images
def preprocess_test(name):
im = sic.imread(name).astype(np.float32)
# check grayscale image
if im.shape[-1] != 3:
h, w = im.shape
temp = np.empty((h, w, 3), dtype=np.uint8)
temp[:, :, :] = im[:, :, np.newaxis]
im = temp.copy()
im = im / np.max(im)
return im
image_LR = [preprocess_test(_) for _ in image_list_LR]
# Push path and image into a list
Data = collections.namedtuple('Data', 'paths_LR, inputs')
return Data(
paths_LR=image_list_LR,
inputs=image_LR
)
# Definition of the generator
def generator(gen_inputs, gen_output_channels, reuse=False, FLAGS=None):
# Check the flag
if FLAGS is None:
raise ValueError('No FLAGS is provided for generator')
# The Bx residual blocks
def residual_block(inputs, output_channel, stride, scope):
with tf.variable_scope(scope):
net = conv2(inputs, 3, output_channel, stride, use_bias=False, scope='conv_1',norm=FLAGS.w_norm)
net = batchnorm(net, FLAGS.is_training)
net = prelu_tf(net)
net = conv2(net, 3, output_channel, stride, use_bias=False, scope='conv_2',norm=FLAGS.w_norm)
net = batchnorm(net, FLAGS.is_training)
net = net + inputs
return net
with tf.variable_scope('generator_unit', reuse=reuse):
# The input layer
with tf.variable_scope('input_stage'):
net = conv2(gen_inputs, 9, 64, 1, scope='conv',norm=FLAGS.w_norm)
net = prelu_tf(net)
stage1_output = net
# The residual block parts
for i in range(1, FLAGS.num_resblock+1 , 1):
name_scope = 'resblock_%d'%(i)
net = residual_block(net, 64, 1, name_scope)
with tf.variable_scope('resblock_output'):
net = conv2(net, 3, 64, 1, use_bias=False, scope='conv',norm=FLAGS.w_norm)
net = batchnorm(net, FLAGS.is_training)
if FLAGS.attention:
net = attention(net,64,reuse=reuse,FLAGS)
net = net + stage1_output
with tf.variable_scope('subpixelconv_stage1'):
net = conv2(net, 3, 256, 1, scope='conv',norm=FLAGS.w_norm)
net = pixelShuffler(net, scale=2)
net = prelu_tf(net)
with tf.variable_scope('subpixelconv_stage2'):
net = conv2(net, 3, 256, 1, scope='conv',norm=FLAGS.w_norm)
net = pixelShuffler(net, scale=2)
net = prelu_tf(net)
with tf.variable_scope('output_stage'):
net = conv2(net, 9, gen_output_channels, 1, scope='conv',norm=FLAGS.w_norm)
return net
# Definition of the discriminator
def discriminator(dis_inputs, FLAGS=None, reuse=False):
if FLAGS is None:
raise ValueError('No FLAGS is provided for generator')
# Define the discriminator block
def discriminator_block(inputs, output_channel, kernel_size, stride, scope):
with tf.variable_scope(scope):
net = conv2(inputs, kernel_size, output_channel, stride, use_bias=False, scope='conv1',norm=FLAGS.w_norm)
net = batchnorm(net, FLAGS.is_training)
net = lrelu(net, 0.2)
return net
with tf.device('/gpu:0'):
with tf.variable_scope('discriminator_unit',reuse=reuse):
# The input layer
with tf.variable_scope('input_stage'):
net = conv2(dis_inputs, 3, 64, 1, scope='conv',norm=FLAGS.w_norm)
net = lrelu(net, 0.2)
# The discriminator block part
# block 1
net = discriminator_block(net, 64, 3, 2, 'disblock_1')
# block 2
net = discriminator_block(net, 128, 3, 1, 'disblock_2')
# block 3
net = discriminator_block(net, 128, 3, 2, 'disblock_3')
# block 4
net = discriminator_block(net, 256, 3, 1, 'disblock_4')
if FLAGS.attention:
net = attention(net,256,reuse=reuse,FLAGS)
# block 5
net = discriminator_block(net, 256, 3, 2, 'disblock_5')
# block 6
net = discriminator_block(net, 512, 3, 1, 'disblock_6')
# block_7
net = discriminator_block(net, 512, 3, 2, 'disblock_7')
# The dense layer 1
with tf.variable_scope('dense_layer_1'):
net = slim.flatten(net)
net = denselayer(net, 1024,norm=FLAGS.w_norm)
net = lrelu(net, 0.2)
# The dense layer 2
with tf.variable_scope('dense_layer_2'):
net = denselayer(net, 1,norm=FLAGS.w_norm)
net = tf.nn.sigmoid(net)
return net
def attention(x, ch, scope='attention', reuse=False,FLAGS=None):
with tf.variable_scope(scope, reuse=reuse):
f = conv2(x, ch // 8, kernel=1, stride=1, scope='f_conv',norm=FLAGS.w_norm) # [bs, h, w, c']
g = conv2(x, ch // 8, kernel=1, stride=1, scope='g_conv',norm=FLAGS.w_norm) # [bs, h, w, c']
h = conv2(x, ch, kernel=1, stride=1, scope='h_conv',norm=FLAGS.w_norm) # [bs, h, w, c]
# N = h * w
s = tf.matmul(hw_flatten(g), hw_flatten(f), transpose_b=True) # # [bs, N, N]
beta = tf.nn.softmax(s, axis=-1) # attention map
o = tf.matmul(beta, hw_flatten(h)) # [bs, N, C]
gamma = tf.get_variable("gamma", [1], initializer=tf.constant_initializer(0.0))
o = tf.reshape(o, shape=x.shape) # [bs, h, w, C]
x = gamma * o + x
return x
def VGG19_slim(input, type, reuse, scope):
# Define the feature to extract according to the type of perceptual
if type == 'VGG54':
target_layer = 'vgg_19/conv5/conv5_4'
elif type == 'VGG22':
target_layer = 'vgg_19/conv2/conv2_2'
else:
raise NotImplementedError('Unknown perceptual type')
_, output = vgg_19(input, is_training=False, reuse=reuse)
output = output[target_layer]
return output
# Define the whole network architecture
def SRGAN(inputs, targets, FLAGS, devices = ['/gpu:%d'%i for i in range(8)]):
# Define the container of the parameter
Network = collections.namedtuple('Network', 'discrim_real_output, discrim_fake_output, discrim_loss, \
discrim_grads_and_vars, adversarial_loss, content_loss, gen_grads_and_vars, gen_output, train, global_step, \
learning_rate')
#generator tower lists
tower_grads = []
tower_outputs = []
#discriminator tower lists
tower_grads_d = []
tower_outputs_real_d = []
tower_outputs_fake_d = []
tower_discriminator_global = []
with tf.device('/gpu:0'):
split_inputs = tf.split(inputs, len(devices), axis=0)
split_targets = tf.split(targets, len(devices), axis=0)
# Define the learning rate and global step
with tf.variable_scope('get_learning_rate_and_global_step'):
global_step = tf.contrib.framework.get_or_create_global_step()
learning_rate = tf.train.exponential_decay(FLAGS.learning_rate, global_step, FLAGS.decay_step, FLAGS.decay_rate,
staircase=FLAGS.stair)
incr_global_step = tf.assign(global_step, global_step + 1)
with tf.variable_scope(tf.get_variable_scope()) as scope:
for i, (inputs, targets, dev) in enumerate(zip(split_inputs, split_targets, devices)):
with tf.device(dev):
with tf.name_scope('tower%d'%i):
# Build the generator part
with tf.variable_scope('generator'):
output_channel = targets.get_shape().as_list()[-1]
gen_output = generator(inputs, output_channel, reuse=False, FLAGS=FLAGS)
gen_output.set_shape([FLAGS.batch_size/len(devices) , FLAGS.crop_size*4, FLAGS.crop_size*4, 3])
tower_outputs.append(gen_output)
# Build the fake discriminator
with tf.name_scope('fake_discriminator'):
with tf.variable_scope('discriminator', reuse=False):
discrim_fake_output = discriminator(gen_output, FLAGS=FLAGS)
tower_outputs_fake_d.append(discrim_fake_output)
# Build the real discriminator
with tf.name_scope('real_discriminator'):
with tf.variable_scope('discriminator', reuse=True):
discrim_real_output = discriminator(targets, FLAGS=FLAGS)
tower_outputs_real_d.append(discrim_real_output)
# Use the VGG54 feature
if FLAGS.perceptual_mode == 'VGG54':
with tf.name_scope('vgg19_1') as scope:
extracted_feature_gen = VGG19_slim(gen_output, FLAGS.perceptual_mode, reuse=False, scope=scope)
with tf.name_scope('vgg19_2') as scope:
extracted_feature_target = VGG19_slim(targets, FLAGS.perceptual_mode, reuse=True, scope=scope)
# Use the VGG22 feature
elif FLAGS.perceptual_mode == 'VGG22':
with tf.name_scope('vgg19_1') as scope:
extracted_feature_gen = VGG19_slim(gen_output, FLAGS.perceptual_mode, reuse=False, scope=scope)
with tf.name_scope('vgg19_2') as scope:
extracted_feature_target = VGG19_slim(targets, FLAGS.perceptual_mode, reuse=True, scope=scope)
# Use MSE loss directly
elif FLAGS.perceptual_mode == 'MSE':
extracted_feature_gen = gen_output
extracted_feature_target = targets
else:
raise NotImplementedError('Unknown perceptual type!!')
# Calculating the generator loss
with tf.variable_scope('generator_loss'):
# Content loss
with tf.variable_scope('content_loss'):
# Compute the euclidean distance between the two features
diff = extracted_feature_gen - extracted_feature_target
if FLAGS.perceptual_mode == 'MSE':
content_loss = tf.reduce_mean(tf.reduce_sum(tf.square(diff), axis=[3]))
else:
content_loss = FLAGS.vgg_scaling*tf.reduce_mean(tf.reduce_sum(tf.square(diff), axis=[3]))
with tf.variable_scope('adversarial_loss'):
adversarial_loss = tf.reduce_mean(-tf.log(discrim_fake_output + FLAGS.EPS))
gen_loss = content_loss + (FLAGS.ratio)*adversarial_loss
print(adversarial_loss.get_shape())
print(content_loss.get_shape())
# Calculating the discriminator loss
with tf.variable_scope('discriminator_loss'):
discrim_fake_loss = tf.log(1 - discrim_fake_output + FLAGS.EPS)
discrim_real_loss = tf.log(discrim_real_output + FLAGS.EPS)
discrim_loss = tf.reduce_mean(-(discrim_fake_loss + discrim_real_loss))
# Define the learning rate and global step
with tf.variable_scope('get_learning_rate_and_global_step'):
global_step = tf.contrib.framework.get_or_create_global_step()
learning_rate = tf.train.exponential_decay(FLAGS.learning_rate, global_step, FLAGS.decay_step, FLAGS.decay_rate, staircase=FLAGS.stair)
incr_global_step = tf.assign(global_step, global_step + 1)
#scope.reuse_variables()
with tf.variable_scope('dicriminator_train',reuse=tf.AUTO_REUSE):
discrim_tvars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='discriminator')
discrim_optimizer = tf.train.AdamOptimizer(learning_rate, beta1=FLAGS.beta)
discrim_grads_and_vars = discrim_optimizer.compute_gradients(discrim_loss, discrim_tvars)
#discrim_train = discrim_optimizer.apply_gradients(discrim_grads_and_vars)
#scope.reuse_variables()
tower_grads_d.append(discrim_grads_and_vars)
scope.reuse_variables()
with tf.variable_scope('generator_train'):
# Need to wait discriminator to perform train step
with tf.control_dependencies( tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
gen_tvars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='generator')
gen_optimizer = tf.train.AdamOptimizer(learning_rate, beta1=FLAGS.beta)
gen_grads_and_vars = gen_optimizer.compute_gradients(gen_loss, gen_tvars)
#gen_train = gen_optimizer.apply_gradients(gen_grads_and_vars)
tower_grads.append(gen_grads_and_vars)
#[ToDo] If we do not use moving average on loss??
exp_averager = tf.train.ExponentialMovingAverage(decay=0.99)
update_loss = exp_averager.apply([discrim_loss, adversarial_loss, content_loss])
#discriminator aggregation
avg_grads_d = average_gradients(tower_grads_d)
discrim_train = discrim_optimizer.apply_gradients(avg_grads_d)
all_outputs_real_d = tf.concat(tower_outputs_real_d, axis=0)
all_outputs_fake_d = tf.concat(tower_outputs_fake_d, axis=0)
with tf.control_dependencies([discrim_train]):
#generator aggregation
avg_grads = average_gradients(tower_grads)
gen_train = gen_optimizer.apply_gradients(avg_grads)
all_outputs_g = tf.concat(tower_outputs, axis=0)
return Network(
discrim_real_output = all_outputs_real_d,
discrim_fake_output = all_outputs_fake_d,
discrim_loss = exp_averager.average(discrim_loss),
discrim_grads_and_vars = discrim_grads_and_vars,
adversarial_loss = exp_averager.average(adversarial_loss),
content_loss = exp_averager.average(content_loss),
gen_grads_and_vars = gen_grads_and_vars,
gen_output = all_outputs_g,
train = tf.group(update_loss, incr_global_step, gen_train),
global_step = global_step,
learning_rate = learning_rate
)
def SRResnet(inputs, targets, FLAGS, devices = ['/gpu:%d'%i for i in range(8)]):
# Define the container of the parameter
Network = collections.namedtuple('Network', 'content_loss, gen_grads_and_vars, gen_output, train, global_step, \
learning_rate')
tower_grads = []
tower_outputs = []
with tf.device('/gpu:0'):
split_inputs = tf.split(inputs, len(devices), axis=0)
split_targets = tf.split(targets, len(devices), axis=0)
# Define the learning rate and global step
with tf.variable_scope('get_learning_rate_and_global_step'):
global_step = tf.contrib.framework.get_or_create_global_step()
learning_rate = tf.train.exponential_decay(FLAGS.learning_rate, global_step, FLAGS.decay_step, FLAGS.decay_rate,
staircase=FLAGS.stair)
incr_global_step = tf.assign(global_step, global_step + 1)
with tf.variable_scope(tf.get_variable_scope()) as scope:
for i, (inputs, targets, dev) in enumerate(zip(split_inputs, split_targets, devices)):
with tf.device(dev):
with tf.name_scope('tower%d'%i):
# Build the generator part
with tf.variable_scope('generator'):
output_channel = targets.get_shape().as_list()[-1]
gen_output = generator(inputs, output_channel, reuse=False, FLAGS=FLAGS)
gen_output.set_shape([FLAGS.batch_size/len(devices), FLAGS.crop_size * 4, FLAGS.crop_size * 4, 3])
tower_outputs.append(gen_output)
# Use the VGG54 feature
if FLAGS.perceptual_mode == 'VGG54':
with tf.name_scope('vgg19_1') as scope:
extracted_feature_gen = VGG19_slim(gen_output, FLAGS.perceptual_mode, reuse=False, scope=scope)
with tf.name_scope('vgg19_2') as scope:
extracted_feature_target = VGG19_slim(targets, FLAGS.perceptual_mode, reuse=True, scope=scope)
elif FLAGS.perceptual_mode == 'VGG22':
with tf.name_scope('vgg19_1') as scope:
extracted_feature_gen = VGG19_slim(gen_output, FLAGS.perceptual_mode, reuse=False, scope=scope)
with tf.name_scope('vgg19_2') as scope:
extracted_feature_target = VGG19_slim(targets, FLAGS.perceptual_mode, reuse=True, scope=scope)
elif FLAGS.perceptual_mode == 'MSE':
extracted_feature_gen = gen_output
extracted_feature_target = targets
else:
raise NotImplementedError('Unknown perceptual type')
# Calculating the generator loss
with tf.variable_scope('generator_loss'):
# Content loss
with tf.variable_scope('content_loss'):
# Compute the euclidean distance between the two features
# check=tf.equal(extracted_feature_gen, extracted_feature_target)
diff = extracted_feature_gen - extracted_feature_target
if FLAGS.perceptual_mode == 'MSE':
content_loss = tf.reduce_mean(tf.reduce_sum(tf.square(diff), axis=[3]))
else:
content_loss = FLAGS.vgg_scaling * tf.reduce_mean(tf.reduce_sum(tf.square(diff), axis=[3]))
gen_loss = content_loss
scope.reuse_variables()
with tf.variable_scope('generator_train'):
# Need to wait discriminator to perform train step
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
gen_tvars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='generator')
gen_optimizer = tf.train.AdamOptimizer(learning_rate, beta1=FLAGS.beta)
gen_grads_and_vars = gen_optimizer.compute_gradients(gen_loss, gen_tvars)
tower_grads.append(gen_grads_and_vars)
# [ToDo] If we do not use moving average on loss??
exp_averager = tf.train.ExponentialMovingAverage(decay=0.99)
update_loss = exp_averager.apply([content_loss])
avg_grads = average_gradients(tower_grads)
gen_train = gen_optimizer.apply_gradients(avg_grads)
all_outputs = tf.concat(tower_outputs, axis=0)
return Network(
content_loss=exp_averager.average(content_loss),
gen_grads_and_vars=gen_grads_and_vars,
gen_output=all_outputs,
train=tf.group(update_loss, incr_global_step, gen_train),
global_step=global_step,
learning_rate=learning_rate
)
def save_images(fetches, FLAGS, step=None):
image_dir = os.path.join(FLAGS.output_dir, "images")
if not os.path.exists(image_dir):
os.makedirs(image_dir)
filesets = []
in_path = fetches['path_LR']
name, _ = os.path.splitext(os.path.basename(str(in_path)))
fileset = {"name": name, "step": step}
if FLAGS.mode == 'inference':
kind = "outputs"
filename = name + ".jpg"
if step is not None:
filename = "%08d-%s" % (step, filename)
fileset[kind] = filename
out_path = os.path.join(image_dir, filename)
contents = fetches[kind][0]
with open(out_path, "wb") as f:
f.write(contents)
filesets.append(fileset)
else:
psnr = fetches['psnr']
ssim = fetches['SSIM']
for kind in ["inputs", "outputs", "targets"]:
if kind == "outputs":
filename = name + "-" + kind + "(PSNR: " +str(psnr)+" and SSIM: "+str(ssim)+ ").jpg"
else:
filename = name + "-" + kind + ".jpg"
if step is not None:
filename = "%08d-%s" % (step, filename)
fileset[kind] = filename
out_path = os.path.join(image_dir, filename)
contents = fetches[kind][0]
with open(out_path, "wb") as f:
f.write(contents)
filesets.append(fileset)
return filesets
def average_gradients(tower_grads):
"""Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over individual gradients. The inner list is over the gradient
calculation for each tower.
Returns:
List of pairs of (gradient, variable) where the gradient has been averaged
across all towers.
"""
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_mean(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads