-
Notifications
You must be signed in to change notification settings - Fork 962
/
cyclegan-7.1.1.py
620 lines (531 loc) · 21.2 KB
/
cyclegan-7.1.1.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
"""Builds and trains a CycleGAN
CycleGAN is a cross-domain GAN. Like other GANs, it can be trained
in unsupervised manner.
CycleGAN is made of two generators (G & F) and two discriminators.
Each generator is a U-Network. The discriminator is a
typical decoder network with the option to use PatchGAN structure.
There are 2 datasets: x = source, y = target.
The forward-cycle solves x'= F(y') = F(G(x)) where y' is
the predicted output in y-domain and x' is the reconstructed input.
The target discriminator determines if y' is fake/real.
The objective of the forward-cycle generator G is to learn
how to trick the target discriminator into believing that y'
is real.
The backward-cycle improves the performance of CycleGAN by doing
the opposite of forward cycle. It learns how to solve
y' = G(x') = G(F(y)) where x' is the predicted output in the
x-domain. The source discriminator determines if x' is fake/real.
The objective of the backward-cycle generator F is to learn
how to trick the target discriminator into believing that x'
is real.
References:
[1]Zhu, Jun-Yan, et al. "Unpaired Image-to-Image Translation Using
Cycle-Consistent Adversarial Networks." 2017 IEEE International
Conference on Computer Vision (ICCV). IEEE, 2017.
[2]Ronneberger, Olaf, Philipp Fischer, and Thomas Brox. "U-net:
Convolutional networks for biomedical image segmentation."
International Conference on Medical image computing and
computer-assisted intervention. Springer, Cham, 2015.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.keras.layers import Activation, Dense, Input
from tensorflow.keras.layers import Conv2D, Flatten
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import concatenate
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.models import Model
from tensorflow.keras.models import load_model
# from keras_contrib.layers.normalization import InstanceNormalization
# from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization
# install: pip install tensorflow-addons
from tensorflow_addons.layers import InstanceNormalization
import numpy as np
import argparse
import cifar10_utils
import mnist_svhn_utils
import other_utils
import datetime
def encoder_layer(inputs,
filters=16,
kernel_size=3,
strides=2,
activation='relu',
instance_norm=True):
"""Builds a generic encoder layer made of Conv2D-IN-LeakyReLU
IN is optional, LeakyReLU may be replaced by ReLU
"""
conv = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same')
x = inputs
if instance_norm:
x = InstanceNormalization()(x)
if activation == 'relu':
x = Activation('relu')(x)
else:
x = LeakyReLU(alpha=0.2)(x)
x = conv(x)
return x
def decoder_layer(inputs,
paired_inputs,
filters=16,
kernel_size=3,
strides=2,
activation='relu',
instance_norm=True):
"""Builds a generic decoder layer made of Conv2D-IN-LeakyReLU
IN is optional, LeakyReLU may be replaced by ReLU
Arguments: (partial)
inputs (tensor): the decoder layer input
paired_inputs (tensor): the encoder layer output
provided by U-Net skip connection &
concatenated to inputs.
"""
conv = Conv2DTranspose(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same')
x = inputs
if instance_norm:
x = InstanceNormalization()(x)
if activation == 'relu':
x = Activation('relu')(x)
else:
x = LeakyReLU(alpha=0.2)(x)
x = conv(x)
x = concatenate([x, paired_inputs])
return x
def build_generator(input_shape,
output_shape=None,
kernel_size=3,
name=None):
"""The generator is a U-Network made of a 4-layer encoder
and a 4-layer decoder. Layer n-i is connected to layer i.
Arguments:
input_shape (tuple): input shape
output_shape (tuple): output shape
kernel_size (int): kernel size of encoder & decoder layers
name (string): name assigned to generator model
Returns:
generator (Model):
"""
inputs = Input(shape=input_shape)
channels = int(output_shape[-1])
e1 = encoder_layer(inputs,
32,
kernel_size=kernel_size,
activation='leaky_relu',
strides=1)
e2 = encoder_layer(e1,
64,
activation='leaky_relu',
kernel_size=kernel_size)
e3 = encoder_layer(e2,
128,
activation='leaky_relu',
kernel_size=kernel_size)
e4 = encoder_layer(e3,
256,
activation='leaky_relu',
kernel_size=kernel_size)
d1 = decoder_layer(e4,
e3,
128,
kernel_size=kernel_size)
d2 = decoder_layer(d1,
e2,
64,
kernel_size=kernel_size)
d3 = decoder_layer(d2,
e1,
32,
kernel_size=kernel_size)
outputs = Conv2DTranspose(channels,
kernel_size=kernel_size,
strides=1,
activation='sigmoid',
padding='same')(d3)
generator = Model(inputs, outputs, name=name)
return generator
def build_discriminator(input_shape,
kernel_size=3,
patchgan=True,
name=None):
"""The discriminator is a 4-layer encoder that outputs either
a 1-dim or a n x n-dim patch of probability that input is real
Arguments:
input_shape (tuple): input shape
kernel_size (int): kernel size of decoder layers
patchgan (bool): whether the output is a patch
or just a 1-dim
name (string): name assigned to discriminator model
Returns:
discriminator (Model):
"""
inputs = Input(shape=input_shape)
x = encoder_layer(inputs,
32,
kernel_size=kernel_size,
activation='leaky_relu',
instance_norm=False)
x = encoder_layer(x,
64,
kernel_size=kernel_size,
activation='leaky_relu',
instance_norm=False)
x = encoder_layer(x,
128,
kernel_size=kernel_size,
activation='leaky_relu',
instance_norm=False)
x = encoder_layer(x,
256,
kernel_size=kernel_size,
strides=1,
activation='leaky_relu',
instance_norm=False)
# if patchgan=True use nxn-dim output of probability
# else use 1-dim output of probability
if patchgan:
x = LeakyReLU(alpha=0.2)(x)
outputs = Conv2D(1,
kernel_size=kernel_size,
strides=2,
padding='same')(x)
else:
x = Flatten()(x)
x = Dense(1)(x)
outputs = Activation('linear')(x)
discriminator = Model(inputs, outputs, name=name)
return discriminator
def train_cyclegan(models,
data,
params,
test_params,
test_generator):
""" Trains the CycleGAN.
1) Train the target discriminator
2) Train the source discriminator
3) Train the forward and backward cyles of
adversarial networks
Arguments:
models (Models): Source/Target Discriminator/Generator,
Adversarial Model
data (tuple): source and target training data
params (tuple): network parameters
test_params (tuple): test parameters
test_generator (function): used for generating
predicted target and source images
"""
# the models
g_source, g_target, d_source, d_target, adv = models
# network parameters
batch_size, train_steps, patch, model_name = params
# train dataset
source_data, target_data, test_source_data, test_target_data\
= data
titles, dirs = test_params
# the generator image is saved every 2000 steps
save_interval = 2000
target_size = target_data.shape[0]
source_size = source_data.shape[0]
# whether to use patchgan or not
if patch > 1:
d_patch = (patch, patch, 1)
valid = np.ones((batch_size,) + d_patch)
fake = np.zeros((batch_size,) + d_patch)
else:
valid = np.ones([batch_size, 1])
fake = np.zeros([batch_size, 1])
valid_fake = np.concatenate((valid, fake))
start_time = datetime.datetime.now()
for step in range(train_steps):
# sample a batch of real target data
rand_indexes = np.random.randint(0,
target_size,
size=batch_size)
real_target = target_data[rand_indexes]
# sample a batch of real source data
rand_indexes = np.random.randint(0,
source_size,
size=batch_size)
real_source = source_data[rand_indexes]
# generate a batch of fake target data fr real source data
fake_target = g_target.predict(real_source)
# combine real and fake into one batch
x = np.concatenate((real_target, fake_target))
# train the target discriminator using fake/real data
metrics = d_target.train_on_batch(x, valid_fake)
log = "%d: [d_target loss: %f]" % (step, metrics[0])
# generate a batch of fake source data fr real target data
fake_source = g_source.predict(real_target)
x = np.concatenate((real_source, fake_source))
# train the source discriminator using fake/real data
metrics = d_source.train_on_batch(x, valid_fake)
log = "%s [d_source loss: %f]" % (log, metrics[0])
# train the adversarial network using forward and backward
# cycles. the generated fake source and target
# data attempts to trick the discriminators
x = [real_source, real_target]
y = [valid, valid, real_source, real_target]
metrics = adv.train_on_batch(x, y)
elapsed_time = datetime.datetime.now() - start_time
fmt = "%s [adv loss: %f] [time: %s]"
log = fmt % (log, metrics[0], elapsed_time)
print(log)
if (step + 1) % save_interval == 0:
test_generator((g_source, g_target),
(test_source_data, test_target_data),
step=step+1,
titles=titles,
dirs=dirs,
show=False)
# save the models after training the generators
g_source.save(model_name + "-g_source.h5")
g_target.save(model_name + "-g_target.h5")
def build_cyclegan(shapes,
source_name='source',
target_name='target',
kernel_size=3,
patchgan=False,
identity=False
):
"""Build the CycleGAN
1) Build target and source discriminators
2) Build target and source generators
3) Build the adversarial network
Arguments:
shapes (tuple): source and target shapes
source_name (string): string to be appended on dis/gen models
target_name (string): string to be appended on dis/gen models
kernel_size (int): kernel size for the encoder/decoder
or dis/gen models
patchgan (bool): whether to use patchgan on discriminator
identity (bool): whether to use identity loss
Returns:
(list): 2 generator, 2 discriminator,
and 1 adversarial models
"""
source_shape, target_shape = shapes
lr = 2e-4
decay = 6e-8
gt_name = "gen_" + target_name
gs_name = "gen_" + source_name
dt_name = "dis_" + target_name
ds_name = "dis_" + source_name
# build target and source generators
g_target = build_generator(source_shape,
target_shape,
kernel_size=kernel_size,
name=gt_name)
g_source = build_generator(target_shape,
source_shape,
kernel_size=kernel_size,
name=gs_name)
print('---- TARGET GENERATOR ----')
g_target.summary()
print('---- SOURCE GENERATOR ----')
g_source.summary()
# build target and source discriminators
d_target = build_discriminator(target_shape,
patchgan=patchgan,
kernel_size=kernel_size,
name=dt_name)
d_source = build_discriminator(source_shape,
patchgan=patchgan,
kernel_size=kernel_size,
name=ds_name)
print('---- TARGET DISCRIMINATOR ----')
d_target.summary()
print('---- SOURCE DISCRIMINATOR ----')
d_source.summary()
optimizer = RMSprop(lr=lr, decay=decay)
d_target.compile(loss='mse',
optimizer=optimizer,
metrics=['accuracy'])
d_source.compile(loss='mse',
optimizer=optimizer,
metrics=['accuracy'])
d_target.trainable = False
d_source.trainable = False
# build the computational graph for the adversarial model
# forward cycle network and target discriminator
source_input = Input(shape=source_shape)
fake_target = g_target(source_input)
preal_target = d_target(fake_target)
reco_source = g_source(fake_target)
# backward cycle network and source discriminator
target_input = Input(shape=target_shape)
fake_source = g_source(target_input)
preal_source = d_source(fake_source)
reco_target = g_target(fake_source)
# if we use identity loss, add 2 extra loss terms
# and outputs
if identity:
iden_source = g_source(source_input)
iden_target = g_target(target_input)
loss = ['mse', 'mse', 'mae', 'mae', 'mae', 'mae']
loss_weights = [1., 1., 10., 10., 0.5, 0.5]
inputs = [source_input, target_input]
outputs = [preal_source,
preal_target,
reco_source,
reco_target,
iden_source,
iden_target]
else:
loss = ['mse', 'mse', 'mae', 'mae']
loss_weights = [1., 1., 10., 10.]
inputs = [source_input, target_input]
outputs = [preal_source,
preal_target,
reco_source,
reco_target]
# build adversarial model
adv = Model(inputs, outputs, name='adversarial')
optimizer = RMSprop(lr=lr*0.5, decay=decay*0.5)
adv.compile(loss=loss,
loss_weights=loss_weights,
optimizer=optimizer,
metrics=['accuracy'])
print('---- ADVERSARIAL NETWORK ----')
adv.summary()
return g_source, g_target, d_source, d_target, adv
def graycifar10_cross_colorcifar10(g_models=None):
"""Build and train a CycleGAN that can do
grayscale <--> color cifar10 images
"""
model_name = 'cyclegan_cifar10'
batch_size = 32
train_steps = 100000
patchgan = True
kernel_size = 3
postfix = ('%dp' % kernel_size) \
if patchgan else ('%d' % kernel_size)
data, shapes = cifar10_utils.load_data()
source_data, _, test_source_data, test_target_data = data
titles = ('CIFAR10 predicted source images.',
'CIFAR10 predicted target images.',
'CIFAR10 reconstructed source images.',
'CIFAR10 reconstructed target images.')
dirs = ('cifar10_source-%s' % postfix, \
'cifar10_target-%s' % postfix)
# generate predicted target(color) and source(gray) images
if g_models is not None:
g_source, g_target = g_models
other_utils.test_generator((g_source, g_target),
(test_source_data, \
test_target_data),
step=0,
titles=titles,
dirs=dirs,
show=True)
return
# build the cyclegan for cifar10 colorization
models = build_cyclegan(shapes,
"gray-%s" % postfix,
"color-%s" % postfix,
kernel_size=kernel_size,
patchgan=patchgan)
# patch size is divided by 2^n since we downscaled the input
# in the discriminator by 2^n (ie. we use strides=2 n times)
patch = int(source_data.shape[1] / 2**4) if patchgan else 1
params = (batch_size, train_steps, patch, model_name)
test_params = (titles, dirs)
# train the cyclegan
train_cyclegan(models,
data,
params,
test_params,
other_utils.test_generator)
def mnist_cross_svhn(g_models=None):
"""Build and train a CycleGAN that can do mnist <--> svhn
"""
model_name = 'cyclegan_mnist_svhn'
batch_size = 32
train_steps = 100000
patchgan = True
kernel_size = 5
postfix = ('%dp' % kernel_size) \
if patchgan else ('%d' % kernel_size)
data, shapes = mnist_svhn_utils.load_data()
source_data, _, test_source_data, test_target_data = data
titles = ('MNIST predicted source images.',
'SVHN predicted target images.',
'MNIST reconstructed source images.',
'SVHN reconstructed target images.')
dirs = ('mnist_source-%s' \
% postfix, 'svhn_target-%s' % postfix)
# generate predicted target(svhn) and source(mnist) images
if g_models is not None:
g_source, g_target = g_models
other_utils.test_generator((g_source, g_target),
(test_source_data, \
test_target_data),
step=0,
titles=titles,
dirs=dirs,
show=True)
return
# build the cyclegan for mnist cross svhn
models = build_cyclegan(shapes,
"mnist-%s" % postfix,
"svhn-%s" % postfix,
kernel_size=kernel_size,
patchgan=patchgan)
# patch size is divided by 2^n since we downscaled the input
# in the discriminator by 2^n (ie. we use strides=2 n times)
patch = int(source_data.shape[1] / 2**4) if patchgan else 1
params = (batch_size, train_steps, patch, model_name)
test_params = (titles, dirs)
# train the cyclegan
train_cyclegan(models,
data,
params,
test_params,
other_utils.test_generator)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
help_ = "Load cifar10 source generator h5 model"
parser.add_argument("--cifar10_g_source", help=help_)
help_ = "Load cifar10 target generator h5 model"
parser.add_argument("--cifar10_g_target", help=help_)
help_ = "Load mnist_svhn source generator h5 model"
parser.add_argument("--mnist_svhn_g_source", help=help_)
help_ = "Load mnist_svhn target generator h5 model"
parser.add_argument("--mnist_svhn_g_target", help=help_)
help_ = "Train cifar10 colorization"
parser.add_argument("-c",
"--cifar10",
action='store_true',
help=help_)
help_ = "Train mnist-svhn cross domain cyclegan"
parser.add_argument("-m",
"--mnist-svhn",
action='store_true',
help=help_)
args = parser.parse_args()
# load pre-trained cifar10 source & target generators
if args.cifar10_g_source:
g_source = load_model(args.cifar10_g_source)
if args.cifar10_g_target:
g_target = load_model(args.cifar10_g_target)
g_models = (g_source, g_target)
graycifar10_cross_colorcifar10(g_models)
# load pre-trained mnist-svhn source & target generators
elif args.mnist_svhn_g_source:
g_source = load_model(args.mnist_svhn_g_source)
if args.mnist_svhn_g_target:
g_target = load_model(args.mnist_svhn_g_target)
g_models = (g_source, g_target)
mnist_cross_svhn(g_models)
# train a cifar10 CycleGAN
elif args.cifar10:
graycifar10_cross_colorcifar10()
# train a mnist-svhn CycleGAN
else:
mnist_cross_svhn()