Exemple #1
0
    def __init__(self, hps, length=28 * 28):
        self.length = length
        self.shape = (self.length, )
        self.latent_dim = hps['latdim']

        #optimizer = Adam(0.0002, 0.5)
        opt = build_optimizer(hps)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator(units=hps['nn_units_d'],
                                                      alpha=hps['nn_alpha_d'])
        self.discriminator.compile(loss='binary_crossentropy',
                                   optimizer=opt,
                                   metrics=['accuracy'])

        # Build the generato
        self.generator = self.build_generator(units=hps['nn_units_g'],
                                              alpha=hps['nn_alpha_g'],
                                              momentum=hps['nn_momentum'])

        # The generator takes noise as input and generated imgs
        z = Input(shape=(self.latent_dim, ))
        img = self.generator(z)

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The valid takes generated images as input and determines validity
        valid = self.discriminator(img)

        # The combined model  (stacked generator and discriminator)
        # Trains the generator to fool the discriminator
        self.combined = Model(z, valid)
        self.combined.compile(loss=self.boundary_loss, optimizer=opt)
Exemple #2
0
    def build_VAE(self, hps):
        # build encoder model
        inputs = Input(shape=self.shape, name='encoder_input')
        x = Dense(self.intermediate_dim, activation='relu')(inputs)
        z_mean = Dense(self.latent_dim, name='z_mean')(x)
        z_log_var = Dense(self.latent_dim, name='z_log_var')(x)

        # use reparameterization trick to push the sampling out as input
        # note that "output_shape" isn't necessary with the TensorFlow backend
        z = Lambda(sampling, output_shape=(self.latent_dim, ),
                   name='z')([z_mean, z_log_var])

        # instantiate encoder model
        encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
        encoder.summary()
        self.encoder = encoder

        # build decoder model
        latent_inputs = Input(shape=(self.latent_dim, ), name='z_sampling')
        x = Dense(self.intermediate_dim, activation='relu')(latent_inputs)
        outputs = Dense(self.length, activation='tanh')(x)

        # instantiate decoder model
        decoder = Model(latent_inputs, outputs, name='decoder')
        decoder.summary()
        self.decoder = decoder

        # instantiate VAE model
        outputs = decoder(encoder(inputs)[2])
        vae = Model(inputs, outputs, name='vae_mlp')

        # VAE loss = mse_loss or xent_loss + kl_loss
        if self.mse_loss:
            reconstruction_loss = mse(inputs, outputs)
        else:
            reconstruction_loss = binary_crossentropy(inputs, outputs)
        reconstruction_loss *= self.length
        kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
        kl_loss = K.sum(kl_loss, axis=-1)
        if self.kl_annealing:
            kl_loss *= -0.5 * self.rate
            self.rate = K.tf.assign(self.rate, self.annealing_rate)
            self.annealing_rate *= self.annealing_factor
            self.rate = K.tf.assign(self.rate, K.clip(self.rate, 0.0, 1.0))
        else:
            kl_loss *= -0.5 * self.beta
        vae_loss = K.mean(reconstruction_loss + kl_loss)
        vae.add_loss(vae_loss)

        opt = build_optimizer(hps)
        vae.compile(optimizer=opt)
        vae.summary()
        self.vae = vae
Exemple #3
0
    def __init__(self, hps, length=28*28):
        self.length = length
        self.shape  = (self.length,)
        self.latent_dim = hps['latdim']

        # optimizer
        #opt = Adam(lr=0.0002, decay=8e-9)
        opt = build_optimizer(hps)

        # allocate generator and discriminant
        self.generator = self.build_generator(units=hps['nn_units_g'],
                                              alpha=hps['nn_alpha_g'],
                                              momentum=hps['nn_momentum'])
        self.generator.compile(loss='binary_crossentropy', optimizer=opt)
        self.discriminator = self.build_discriminator(units=hps['nn_units_d'],
                                                      alpha=hps['nn_alpha_d'])
        self.discriminator.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])

        self.adversarial_model = self.ad_model()
        self.adversarial_model.compile(loss='binary_crossentropy', optimizer=opt)
Exemple #4
0
    def __init__(self, hps):
        if (hps['npx'] % 4):
            raise ValueError(
                'WGAN: Width and height need to be divisible by 4.')
        # Input shape
        self.img_rows = hps['npx']
        self.img_cols = hps['npx']
        self.img_shape = (self.img_rows, self.img_cols, 1)
        self.latent_dim = hps['latdim']

        #opt = Adam(0.0002, 0.5)
        opt = build_optimizer(hps)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator(
            units=hps['nn_units_d'],
            alpha=hps['nn_alpha'],
            momentum=hps['nn_momentum_d'],
            dropout=hps['nn_dropout'])
        self.discriminator.compile(loss='binary_crossentropy',
                                   optimizer=opt,
                                   metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator(units=hps['nn_units_g'],
                                              momentum=hps['nn_momentum_g'])

        # The generator takes noise as input and generates imgs
        z = Input(shape=(self.latent_dim, ))
        img = self.generator(z)

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The discriminator takes generated images as input and determines validity
        valid = self.discriminator(img)

        # The combined model  (stacked generator and discriminator)
        # Trains the generator to fool the discriminator
        self.combined = Model(z, valid)
        self.combined.compile(loss='binary_crossentropy', optimizer=opt)
Exemple #5
0
    def __init__(self, hps, length=28*28):
        self.length = length
        self.shape  = (self.length,)
        self.latent_dim = hps['latdim']

        # opt =  Adam(0.0002, 0.5)
        opt = build_optimizer(hps)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator(units=hps['nn_units_d'],
                                                      alpha=hps['nn_alpha_d'],
                                                      minibatch_discriminator=\
                                                      hps['nn_minibatch_discriminator'],
                                                      nb_kernels=hps['nn_nb_kernels'],
                                                      kernel_dim=hps['nn_kernel_dim'],
                                                      aux_ratio=hps['nn_aux_ratio'])

        self.discriminator.compile(loss='mse', optimizer=opt, metrics=['accuracy'])

        # Build the generator
        self.generator = self.build_generator(units=hps['nn_units_g'],
                                              alpha=hps['nn_alpha_g'],
                                              momentum=hps['nn_momentum'])

        # The generator takes noise as input and generated imgs
        z = Input(shape=(self.latent_dim,))
        img = self.generator(z)

        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # The valid takes generated images as input and determines validity
        valid = self.discriminator(img)

        # The combined model  (stacked generator and discriminator)
        # Trains generator to fool discriminator
        self.combined = Model(z, valid)
        # (!!!) Optimize w.r.t. MSE loss instead of crossentropy
        self.combined.compile(loss='mse', optimizer=opt)
Exemple #6
0
    def __init__(self, hps, length=28*28):
        self.length = length
        self.shape = (self.length,)
        self.latent_dim = hps['latdim']

        #opt = Adam(0.0002, 0.5)
        opt = build_optimizer(hps)
        
        # Build and compile the discriminator
        self.discriminator = self.build_discriminator(units=hps['nn_units_discr'],
                                                      alpha=hps['nn_alpha_discr'])
        self.discriminator.compile(loss='binary_crossentropy',
                                   optimizer=opt,
                                   metrics=['accuracy'])

        # Build the encoder / decoder
        self.encoder = self.build_encoder(units=hps['nn_units_enc'],
                                          alpha=hps['nn_alpha_enc'])
        self.decoder = self.build_decoder(units=hps['nn_units_dec'],
                                          alpha=hps['nn_alpha_dec'])

        img = Input(shape=self.shape)
        # The generator takes the image, encodes it and reconstructs it
        # from the encoding
        encoded_repr = self.encoder(img)
        reconstructed_img = self.decoder(encoded_repr)

        # For the adversarial_autoencoder model we will only train the generator
        self.discriminator.trainable = False

        # The discriminator determines validity of the encoding
        validity = self.discriminator(encoded_repr)

        # The adversarial_autoencoder model  (stacked generator and discriminator)
        self.adversarial_autoencoder = Model(img, [reconstructed_img, validity])
        self.adversarial_autoencoder.compile(loss=['mse', 'binary_crossentropy'],
                                             loss_weights=[0.999, 0.001],
                                             optimizer=opt)
Exemple #7
0
    def __init__(self, hps):
        if (hps['npx'] % 4):
            raise ValueError(
                'WGAN: Width and height need to be divisible by 4.')
        self.img_rows = hps['npx']
        self.img_cols = hps['npx']
        self.img_shape = (self.img_rows, self.img_cols, 1)
        self.latent_dim = hps['latdim']

        # Following parameter and optimizer set as recommended in paper
        self.n_critic = hps['n_critic']
        opt = build_optimizer(hps)

        # Build the generator and critic
        self.generator = self.build_generator(units=hps['nn_units_g'],
                                              momentum=hps['nn_momentum_g'])
        self.critic = self.build_critic(units=hps['nn_units_d'],
                                        alpha=hps['nn_alpha'],
                                        momentum=hps['nn_momentum_d'],
                                        dropout=hps['nn_dropout'])

        #-------------------------------
        # Construct Computational Graph
        #       for the Critic
        #-------------------------------

        # Freeze generator's layers while training critic
        self.generator.trainable = False

        # Image input (real sample)
        real_img = Input(shape=self.img_shape)

        # Noise input
        z_disc = Input(shape=(self.latent_dim, ))
        # Generate image based of noise (fake sample)
        fake_img = self.generator(z_disc)

        # Discriminator determines validity of the real and fake images
        fake = self.critic(fake_img)
        valid = self.critic(real_img)

        # Construct weighted average between real and fake images
        interpolated_img = RandomWeightedAverage()([real_img, fake_img])
        # Determine validity of weighted sample
        validity_interpolated = self.critic(interpolated_img)

        # Use Python partial to provide loss function with additional
        # 'averaged_samples' argument
        partial_gp_loss = partial(self.gradient_penalty_loss,
                                  averaged_samples=interpolated_img)
        partial_gp_loss.__name__ = 'gradient_penalty'  # Keras requires function names

        self.critic_model = Model(inputs=[real_img, z_disc],
                                  outputs=[valid, fake, validity_interpolated])
        self.critic_model.compile(loss=[
            self.wasserstein_loss, self.wasserstein_loss, partial_gp_loss
        ],
                                  optimizer=opt,
                                  loss_weights=[1, 1, 10])
        #-------------------------------
        # Construct Computational Graph
        #         for Generator
        #-------------------------------

        # For the generator we freeze the critic's layers
        self.critic.trainable = False
        self.generator.trainable = True

        # Sampled noise for input to generator
        z_gen = Input(shape=(self.latent_dim, ))
        # Generate images based of noise
        img = self.generator(z_gen)
        # Discriminator determines validity
        valid = self.critic(img)
        # Defines generator model
        self.generator_model = Model(z_gen, valid)
        self.generator_model.compile(loss=self.wasserstein_loss, optimizer=opt)
Exemple #8
0
    def __init__(self, hps):
        # Input shape
        self.img_rows = hps['npixels']
        self.img_cols = hps['npixels']
        self.img_shape = (self.img_rows, self.img_cols, 1)
        
        # Load the training data
        self.load_data(hps)
        
        # Calculate output shape of D (PatchGAN)
        if (self.img_rows%16==0):
            patch = int(self.img_rows / 2**4)
        elif (self.img_rows%8==0):
            patch = int(self.img_rows / 2**3)
        else:
            raise ValueError('CycleGAN: npixels has to be a multiple of 8')
        self.disc_patch = (patch, patch, 1)

        # Number of filters in the first layer of G and D
        self.gf = hps['g_filters']
        self.df = hps['d_filters']

        # Loss weights
        # Cycle-consistency loss
        self.lambda_cycle = hps['lambda_cycle']
        # Identity loss
        self.lambda_id = hps['lambda_id_factor'] * self.lambda_cycle

        #optimizer = Adam(0.0002, 0.5)
        optimizer = build_optimizer(hps)

        # Build and compile the discriminators
        self.d_A = self.build_discriminator()
        self.d_B = self.build_discriminator()
        self.d_A.compile(loss='mse', optimizer=optimizer,
                         metrics=['accuracy'])
        self.d_B.compile(loss='mse', optimizer=optimizer,
                         metrics=['accuracy'])

        # Build the generators
        self.g_AB = self.build_generator()
        self.g_BA = self.build_generator()

        # Input images from both domains
        img_A = Input(shape=self.img_shape)
        img_B = Input(shape=self.img_shape)

        # Translate images to the other domain
        fake_B = self.g_AB(img_A)
        fake_A = self.g_BA(img_B)
        # Translate images back to original domain
        reconstr_A = self.g_BA(fake_B)
        reconstr_B = self.g_AB(fake_A)
        # Identity mapping of images
        img_A_id = self.g_BA(img_A)
        img_B_id = self.g_AB(img_B)

        # For the combined model we will only train the generators
        self.d_A.trainable = False
        self.d_B.trainable = False

        # Discriminators determines validity of translated images
        valid_A = self.d_A(fake_A)
        valid_B = self.d_B(fake_B)

        # Combined model trains generators to fool discriminators
        self.combined = Model(inputs=[img_A, img_B],
                              outputs=[valid_A, valid_B,
                                       reconstr_A, reconstr_B,
                                       img_A_id, img_B_id ])
        self.combined.compile(loss=['mse', 'mse',
                                    'mae', 'mae',
                                    'mae', 'mae'],
                              loss_weights=[1, 1,
                                            self.lambda_cycle, self.lambda_cycle,
                                            self.lambda_id, self.lambda_id ],
                              optimizer=optimizer)