-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
552 lines (454 loc) · 21 KB
/
main.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
"""Code for training CycleGAN."""
from datetime import datetime
import json
import numpy as np
import os
import random
from scipy.misc import imsave
from matplotlib import pyplot
import click
import tensorflow as tf
import data_loader, losses, model
import accuracy
slim = tf.contrib.slim
class CycleGAN:
"""The CycleGAN module."""
def __init__(self, pool_size, lambda_a,
lambda_b, output_root_dir, to_restore,
base_lr, max_step, network_version,
csv_name, batch_size, checkpoint_dir, skip, save_freq):
current_time = datetime.now().strftime("%Y%m%d-%H%M%S")
self._pool_size = pool_size
self._size_before_crop = 286
self._lambda_a = lambda_a
self._lambda_b = lambda_b
self._output_dir = os.path.join(output_root_dir, current_time)
self._images_dir = os.path.join(self._output_dir, 'imgs')
self._num_imgs_to_save = 1
self._to_restore = to_restore
self._base_lr = base_lr
self._max_step = max_step
self._network_version = network_version
self._csv_name = csv_name
self._batch_size = batch_size
self._checkpoint_dir = checkpoint_dir
self._skip = skip
self._save_freq = save_freq
self.fake_images_A = np.zeros(
(self._pool_size, 64, model.IMG_HEIGHT, model.IMG_WIDTH,
model.IMG_CHANNELS)
)
self.fake_images_B = np.zeros(
(self._pool_size, 64, model.IMG_HEIGHT, model.IMG_WIDTH,
model.IMG_CHANNELS)
)
def model_setup(self):
"""
This function sets up the model to train.
self.input_A/self.input_B -> Set of training images.
self.fake_A/self.fake_B -> Generated images by corresponding generator
of input_A and input_B
self.lr -> Learning rate variable
self.cyc_A/ self.cyc_B -> Images generated after feeding
self.fake_A/self.fake_B to corresponding generator.
This is use to calculate cyclic loss
"""
self.input_a = tf.placeholder(
tf.float32, [
64,
model.IMG_WIDTH,
model.IMG_HEIGHT,
model.IMG_CHANNELS
], name="input_A")
self.input_b = tf.placeholder(
tf.float32, [
64,
model.IMG_WIDTH,
model.IMG_HEIGHT,
model.IMG_CHANNELS
], name="input_B")
self.fake_pool_A = tf.placeholder(
tf.float32, [
None,
model.IMG_WIDTH,
model.IMG_HEIGHT,
model.IMG_CHANNELS
], name="fake_pool_A")
self.fake_pool_B = tf.placeholder(
tf.float32, [
None,
model.IMG_WIDTH,
model.IMG_HEIGHT,
model.IMG_CHANNELS
], name="fake_pool_B")
self.global_step = slim.get_or_create_global_step()
self.num_fake_inputs = 0
self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr")
inputs = {
'images_a': self.input_a,
'images_b': self.input_b,
'fake_pool_a': self.fake_pool_A,
'fake_pool_b': self.fake_pool_B,
}
outputs = model.get_outputs(
inputs, network=self._network_version, skip=self._skip)
self.prob_real_a_is_real = outputs['prob_real_a_is_real']
self.prob_real_b_is_real = outputs['prob_real_b_is_real']
self.fake_images_a = outputs['fake_images_a']
self.fake_images_b = outputs['fake_images_b']
self.prob_fake_a_is_real = outputs['prob_fake_a_is_real']
self.prob_fake_b_is_real = outputs['prob_fake_b_is_real']
self.cycle_images_a = outputs['cycle_images_a']
self.cycle_images_b = outputs['cycle_images_b']
self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real']
self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real']
self.Acc_real_A = accuracy.Acc_real(self.prob_real_a_is_real)
self.Acc_fake_A = accuracy.Acc_fake(self.prob_fake_a_is_real)
self.Acc_real_B = accuracy.Acc_real(self.prob_real_b_is_real)
self.Acc_fake_B = accuracy.Acc_fake(self.prob_fake_b_is_real)
def compute_losses(self):
"""
In this function we are defining the variables for loss calculations
and training model.
d_loss_A/d_loss_B -> loss for discriminator A/B
g_loss_A/g_loss_B -> loss for generator A/B
*_trainer -> Various trainer for above loss functions
*_summ -> Summary variables for above loss functions
"""
cycle_consistency_loss_a = \
self._lambda_a * losses.cycle_consistency_loss(
real_images=self.input_a, generated_images=self.cycle_images_a,
)
cycle_consistency_loss_b = \
self._lambda_b * losses.cycle_consistency_loss(
real_images=self.input_b, generated_images=self.cycle_images_b,
)
lsgan_loss_a = losses.lsgan_loss_generator(self.prob_fake_a_is_real)
lsgan_loss_b = losses.lsgan_loss_generator(self.prob_fake_b_is_real)
g_loss_A = \
cycle_consistency_loss_a + cycle_consistency_loss_b + lsgan_loss_b
g_loss_B = \
cycle_consistency_loss_b + cycle_consistency_loss_a + lsgan_loss_a
d_loss_A = losses.lsgan_loss_discriminator(
prob_real_is_real=self.prob_real_a_is_real,
prob_fake_is_real=self.prob_fake_pool_a_is_real,
)
d_loss_B = losses.lsgan_loss_discriminator(
prob_real_is_real=self.prob_real_b_is_real,
prob_fake_is_real=self.prob_fake_pool_b_is_real,
)
optimizer = tf.train.AdamOptimizer(self.learning_rate, beta1=0.5)
self.model_vars = tf.trainable_variables()
d_A_vars = [var for var in self.model_vars if 'd_A' in var.name]
g_A_vars = [var for var in self.model_vars if 'g_A' in var.name]
d_B_vars = [var for var in self.model_vars if 'd_B' in var.name]
g_B_vars = [var for var in self.model_vars if 'g_B' in var.name]
self.d_A_trainer = optimizer.minimize(d_loss_A, var_list=d_A_vars)
self.d_B_trainer = optimizer.minimize(d_loss_B, var_list=d_B_vars)
self.g_A_trainer = optimizer.minimize(g_loss_A, var_list=g_A_vars)
self.g_B_trainer = optimizer.minimize(g_loss_B, var_list=g_B_vars)
self.g_A_loss = g_loss_A
self.g_B_loss = g_loss_B
self.d_A_loss = d_loss_A
self.d_B_loss = d_loss_B
for var in self.model_vars:
print(var.name)
# Summary variables for tensorboard
self.g_A_loss_summ = tf.summary.scalar("g_A_loss", g_loss_A)
self.g_B_loss_summ = tf.summary.scalar("g_B_loss", g_loss_B)
self.d_A_loss_summ = tf.summary.scalar("d_A_loss", d_loss_A)
self.d_B_loss_summ = tf.summary.scalar("d_B_loss", d_loss_B)
def save_images(self, sess, epoch):
"""
Saves input and output images.
:param sess: The session.
:param epoch: Currnt epoch.
"""
if not os.path.exists(self._images_dir):
os.makedirs(self._images_dir)
names = ['inputA_', 'inputB_', 'fakeA_',
'fakeB_', 'cycA_', 'cycB_']
with open(os.path.join(
self._output_dir, 'epoch_' + str(epoch) + '.html'
), 'w') as v_html:
for i in range(0, self._num_imgs_to_save):
print("Saving image {}/{}".format(i, self._num_imgs_to_save))
inputs = sess.run(self.inputs)
fake_A_temp, fake_B_temp, cyc_A_temp, cyc_B_temp = sess.run([
self.fake_images_a,
self.fake_images_b,
self.cycle_images_a,
self.cycle_images_b
], feed_dict={
self.input_a: inputs['images_i'],
self.input_b: inputs['images_j']
})
tensors = [inputs['images_i'], inputs['images_j'],
fake_B_temp, fake_A_temp, cyc_A_temp, cyc_B_temp]
for name, tensor in zip(names, tensors):
image_name = name + str(epoch) + "_" + str(i) + ".png"
imsave(os.path.join(self._images_dir, image_name),
((tensor[0] + 1) * 127.5).astype(np.uint8)
)
v_html.write(
"<img src=\"" +
os.path.join('imgs', image_name) + "\">"
)
v_html.write("<br>")
def fake_image_pool(self, num_fakes, fake, fake_pool):
"""
This function saves the generated image to corresponding
pool of images.
It keeps on feeling the pool till it is full and then randomly
selects an already stored image and replace it with new one.
"""
if num_fakes < self._pool_size:
fake_pool[num_fakes] = fake
return fake
else:
p = random.random()
if p > 0.5:
random_id = random.randint(0, self._pool_size - 1)
temp = fake_pool[random_id]
fake_pool[random_id] = fake
return temp
else:
return fake
# create a line plot of loss for the gan and save to file
def plot_loss_history(self, g_A_hist, g_B_hist, d_A_hist, d_B_hist):
# plot losses for mapping A to B
pyplot.subplot(2, 1, 1)
pyplot.plot(g_A_hist, label='g_A_loss')
pyplot.plot(d_B_hist, label='d_B_loss')
pyplot.legend()
# plot discriminator accuracy
pyplot.subplot(2, 1, 2)
pyplot.plot(g_B_hist, label='g_B_loss')
pyplot.plot(d_A_hist, label='d_A_loss')
pyplot.legend()
# save plot to file
pyplot.savefig('./output/cyclegan/plot_line_plot_loss.png')
pyplot.close()
# create a line plot of loss for the gan and save to file
def plot_acc_history(self, Acc_real_A_hist, Acc_real_B_hist, Acc_fake_A_hist, Acc_fake_B_hist):
# plot losses for mapping A to B
pyplot.subplot(2, 1, 1)
pyplot.plot(Acc_real_A_hist, label='acc_real_A')
pyplot.plot(Acc_fake_A_hist, label='acc_fake_A')
pyplot.legend()
# plot discriminator accuracy
pyplot.subplot(2, 1, 2)
pyplot.plot(Acc_real_B_hist, label='acc_real_B')
pyplot.plot(Acc_fake_B_hist, label='acc_fake_B')
pyplot.legend()
# save plot to file
pyplot.savefig('./output/cyclegan/plot_line_plot_accuracy.png')
pyplot.close()
def train(self):
"""Training Function."""
# Load Dataset from the dataset folder
self.inputs = data_loader.load_data(self._csv_name, self._batch_size)
# Build the network
self.model_setup()
# Loss function calculations
self.compute_losses()
# Initializing the global variables
init = (tf.global_variables_initializer(),
tf.local_variables_initializer())
saver = tf.train.Saver()
max_images = 10000
with tf.Session() as sess:
sess.run(init)
# Restore the model to run the model from last checkpoint
if self._to_restore:
chkpt_fname = tf.train.latest_checkpoint(self._checkpoint_dir)
saver.restore(sess, chkpt_fname)
writer = tf.summary.FileWriter(self._output_dir)
if not os.path.exists(self._output_dir):
os.makedirs(self._output_dir)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# prepare lists for storing loss stats each iteration
g_A_hist, g_B_hist, d_A_hist, d_B_hist = list(), list(), list(), list()
# prepare lists for storing accuracy stats each iteration
acc_real_A_hist, acc_real_B_hist, acc_fake_A_hist, acc_fake_B_hist = list(), list(), list(), list()
# Training Loop
for epoch in range(sess.run(self.global_step), self._max_step):
print("In the epoch ", epoch)
if (epoch+1) % self._save_freq == 0:
saver.save(sess, os.path.join(
self._output_dir, "cyclegan"), global_step=epoch)
# Dealing with the learning rate as per the epoch number
if epoch < 100:
curr_lr = self._base_lr
else:
curr_lr = self._base_lr - \
self._base_lr * (epoch - 100) / 100
if (epoch+1) % self._save_freq == 0:
self.save_images(sess, epoch)
for i in range(0, max_images // 64):
print("Processing batch {}/{}".format(i, max_images // 64))
inputs = sess.run(self.inputs)
# Optimizing the G_A network
_, fake_B_temp, g_A_loss, summary_str = sess.run(
[self.g_A_trainer,
self.fake_images_b,
self.g_A_loss,
self.g_A_loss_summ],
feed_dict={
self.input_a:
inputs['images_i'],
self.input_b:
inputs['images_j'],
self.learning_rate: curr_lr
}
)
writer.add_summary(summary_str, epoch * max_images // 64 + i)
fake_B_temp1 = self.fake_image_pool(
self.num_fake_inputs, fake_B_temp, self.fake_images_B)
# Optimizing the D_B network
_, d_B_loss, summary_str = sess.run(
[self.d_B_trainer, self.d_B_loss, self.d_B_loss_summ],
feed_dict={
self.input_a:
inputs['images_i'],
self.input_b:
inputs['images_j'],
self.learning_rate: curr_lr,
self.fake_pool_B: fake_B_temp1
}
)
writer.add_summary(summary_str, epoch * max_images // 64 + i)
# Optimizing the G_B network
_, fake_A_temp, g_B_loss, summary_str = sess.run(
[self.g_B_trainer,
self.fake_images_a,
self.g_B_loss,
self.g_B_loss_summ],
feed_dict={
self.input_a:
inputs['images_i'],
self.input_b:
inputs['images_j'],
self.learning_rate: curr_lr
}
)
writer.add_summary(summary_str, epoch * max_images // 64 + i)
fake_A_temp1 = self.fake_image_pool(
self.num_fake_inputs, fake_A_temp, self.fake_images_A)
# Optimizing the D_A network
_, d_A_loss, summary_str = sess.run(
[self.d_A_trainer, self.d_A_loss, self.d_A_loss_summ],
feed_dict={
self.input_a:
inputs['images_i'],
self.input_b:
inputs['images_j'],
self.learning_rate: curr_lr,
self.fake_pool_A: fake_A_temp1
}
)
writer.add_summary(summary_str, epoch * max_images // 64 + i)
# summarize the losses on this batch
print('>%d, g_A=%.3f, g_B=%.3f, d_A=%.3f, d_B=%.3f ' %
(i+1, g_A_loss, g_B_loss, d_A_loss, d_B_loss))
g_A_hist.append(g_A_loss)
g_B_hist.append(g_B_loss)
d_A_hist.append(d_A_loss)
d_B_hist.append(d_B_loss)
'''print('>%d, acc_real_A=%.3f, acc_real_B=%.3f, acc_fake_A=%.3f, acc_fake_B=%.3f ' %
(i+1, acc_real_A, acc_real_B, acc_fake_A, acc_fake_B))'''
writer.flush()
self.num_fake_inputs += 64
acc_real_A, acc_real_B, acc_fake_A, acc_fake_B = sess.run([
self.Acc_real_A,
self.Acc_real_B,
self.Acc_fake_A,
self.Acc_fake_B
], feed_dict={
self.input_a: inputs['images_i'],
self.input_b: inputs['images_j']
})
acc_real_A_hist.append(acc_real_A)
acc_real_B_hist.append(acc_real_B)
acc_fake_A_hist.append(acc_fake_A)
acc_fake_B_hist.append(acc_fake_B)
sess.run(tf.assign(self.global_step, epoch + 1))
self.plot_loss_history(g_A_hist, g_B_hist, d_A_hist, d_B_hist)
self.plot_acc_history(acc_real_A_hist, acc_real_B_hist, acc_fake_A_hist, acc_fake_B_hist)
coord.request_stop()
coord.join(threads)
writer.add_graph(sess.graph)
def test(self):
"""Test Function."""
print("Testing the results")
self.inputs = data_loader.load_data(self._csv_name, self._batch_size)
self.model_setup()
saver = tf.train.Saver()
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
chkpt_fname = tf.train.latest_checkpoint(self._checkpoint_dir)
saver.restore(sess, chkpt_fname)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
self._num_imgs_to_save = 10000
self.save_images(sess, 0)
coord.request_stop()
coord.join(threads)
@click.command()
@click.option('--to_train',
type=click.INT,
default=True,
help='Whether it is train or false.')
@click.option('--log_dir',
type=click.STRING,
default=None,
help='Where the data is logged to.')
@click.option('--config_filename',
type=click.STRING,
default='train',
help='The name of the configuration file.')
@click.option('--checkpoint_dir',
type=click.STRING,
default='',
help='The name of the train/test split.')
@click.option('--skip',
type=click.BOOL,
default=False,
help='Whether to add skip connection between input and output.')
def main(to_train, log_dir, config_filename, checkpoint_dir, skip):
"""
:param to_train: Specify whether it is training or testing. 1: training; 2:
resuming from latest checkpoint; 0: testing.
:param log_dir: The root dir to save checkpoints and imgs. The actual dir
is the root dir appended by the folder with the name timestamp.
:param config_filename: The configuration file.
:param checkpoint_dir: The directory that saves the latest checkpoint. It
only takes effect when to_train == 2.
:param skip: A boolean indicating whether to add skip connection between
input and output.
"""
if not os.path.isdir(log_dir):
os.makedirs(log_dir)
with open(config_filename) as config_file:
config = json.load(config_file)
lambda_a = float(config['_LAMBDA_A']) if '_LAMBDA_A' in config else 10.0
lambda_b = float(config['_LAMBDA_B']) if '_LAMBDA_B' in config else 10.0
pool_size = int(config['pool_size']) if 'pool_size' in config else 50
to_restore = (to_train == 2)
base_lr = float(config['base_lr']) if 'base_lr' in config else 0.0002
max_step = int(config['max_step']) if 'max_step' in config else 200
network_version = str(config['network_version'])
csv_name = str(config['csv_name'])
save_freq = float(config['_SAVE_FREQ'])
batch_size = 64
cyclegan_model = CycleGAN(pool_size, lambda_a, lambda_b, log_dir,
to_restore, base_lr, max_step, network_version,
csv_name, batch_size, checkpoint_dir, skip, save_freq)
if to_train > 0:
cyclegan_model.train()
else:
cyclegan_model.test()
if __name__ == '__main__':
main()