16.03.2021 Views

Advanced Deep Learning with Keras

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Cross-Domain GANs

Finally, before we can use the CycleGAN to build and train functions, we have

to perform some data preparation. The modules cifar10_utils.py and other_

utils.py load the CIFAR10 train and test data. Please refer to the source code

for details of these two files. After loading, the train and test images are converted

to grayscale to generate the source data and test source data.

Following listing shows how the CycleGAN is used to build and train a generator

network (g_target) for colorization of grayscale images. Since CycleGAN is

symmetric, we also build and train a second generator network (g_source) that

converts from color to grayscale. Two CycleGAN colorization networks were

trained. The first use discriminators with a scalar output similar to vanilla GAN.

The second uses a 2 × 2 PatchGAN.

Listing 7.1.6, cyclegan-7.1.1.py shows us the CycleGAN for colorization problem:

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,

[ 224 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!