Advanced Deep Learning with Keras

fourpersent2020
from fourpersent2020 More from this publisher
16.03.2021 Views

Listing 7.1.3, cyclegan-7.1.1.py shows discriminator implementation in Keras:def build_discriminator(input_shape,kernel_size=3,patchgan=True,name=None):"""The discriminator is a 4-layer encoder that outputs eithera 1-dim or a n x n-dim patch of probability that input is realArguments:input_shape (tuple): input shapekernel_size (int): kernel size of decoder layerspatchgan (bool): whether the output is a patch or just a 1-dimname (string): name assigned to discriminator modelReturns: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 probabilityChapter 7[ 217 ]

Cross-Domain GANs# else use 1-dim output of probabilityif patchgan:x = LeakyReLU(alpha=0.2)(x)outputs = Conv2D(1,kernel_size=kernel_size,strides=1,padding='same')(x)else:x = Flatten()(x)x = Dense(1)(x)outputs = Activation('linear')(x)discriminator = Model(inputs, outputs, name=name)return discriminatorUsing the generator and discriminator builders, we are now able to build theCycleGAN. Listing 7.1.4 shows the builder function. In line with our discussion inthe previous section, two generators, g_source = F and g_target = G, and twodiscriminators, d_source = D xand d_target = D yare instantiated. The forwardcycle is x ' = F(G(x)) = reco_source = g_source(g_target(source_input)).The backward cycle is y ' = G(F(y)) = reco_target = g_target(g_source(target_input)).The inputs to the adversarial model are the source and target data while the outputsare the outputs of D xand D yand the reconstructed inputs, x' and y.' The identitynetwork is not used in this example due to the difference between the numberof channels of the grayscale image and color image. We use the recommendedloss weights of λ1= 1.0 and λ2= 10.0 for the GAN and cyclic consistency lossesrespectively. Similar to GANs in the previous chapters, we use RMSprop witha learning rate of 2e-4 and decay rate of 6e-8 for the optimizer of the discriminators.The learning and decay rate for the adversarial is half of the discriminator's.Listing 7.1.4, cyclegan-7.1.1.py shows us the CycleGAN builder in Keras:def build_cyclegan(shapes,source_name='source',target_name='target',kernel_size=3,patchgan=False,identity=False):"""Build the CycleGAN[ 218 ]

Cross-Domain GANs

# else use 1-dim output of probability

if patchgan:

x = LeakyReLU(alpha=0.2)(x)

outputs = Conv2D(1,

kernel_size=kernel_size,

strides=1,

padding='same')(x)

else:

x = Flatten()(x)

x = Dense(1)(x)

outputs = Activation('linear')(x)

discriminator = Model(inputs, outputs, name=name)

return discriminator

Using the generator and discriminator builders, we are now able to build the

CycleGAN. Listing 7.1.4 shows the builder function. In line with our discussion in

the previous section, two generators, g_source = F and g_target = G, and two

discriminators, d_source = D x

and d_target = D y

are instantiated. The forward

cycle is x ' = F(G(x)) = reco_source = g_source(g_target(source_input)).

The backward cycle is y ' = G(F(y)) = reco_target = g_target(g_source

(target_input)).

The inputs to the adversarial model are the source and target data while the outputs

are the outputs of D x

and D y

and the reconstructed inputs, x' and y.' The identity

network is not used in this example due to the difference between the number

of channels of the grayscale image and color image. We use the recommended

loss weights of λ

1

= 1.0 and λ

2

= 10.0 for the GAN and cyclic consistency losses

respectively. Similar to GANs in the previous chapters, we use RMSprop with

a learning rate of 2e-4 and decay rate of 6e-8 for the optimizer of the discriminators.

The learning and decay rate for the adversarial is half of the discriminator's.

Listing 7.1.4, cyclegan-7.1.1.py shows us the CycleGAN builder in Keras:

def build_cyclegan(shapes,

source_name='source',

target_name='target',

kernel_size=3,

patchgan=False,

identity=False

):

"""Build the CycleGAN

[ 218 ]

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

Saved successfully!

Ooh no, something went wrong!