Example #1
0
def train(training_data, nb_epochs, batch_size, discriminative_model,
          generative_model, gan_model, noise_size, vocab_size, losses):
    for epoch in range(nb_epochs):
        d_training_samples, d_training_outputs = generate_mixed_data(
            training_data,
            generative_model,
            noise_size=noise_size,
            vocab_size=vocab_size,
            real_samples_size=batch_size,
            generated_samples_size=batch_size)
        make_trainable(discriminative_model, True)
        d_loss = discriminative_model.train_on_batch(x=d_training_samples,
                                                     y=d_training_outputs)

        losses['d'].append(float(d_loss))

        gan_training_samples = np.random.uniform(0,
                                                 1,
                                                 size=[batch_size, noise_size])
        gan_training_outputs = np.zeros([batch_size, 1])

        make_trainable(discriminative_model, False)
        gan_loss = gan_model.train_on_batch(x=gan_training_samples,
                                            y=gan_training_outputs)

        losses['g'].append(float(gan_loss))

        print('Epoch {}/{}\td_loss {:.3%}\tgan_loss {:.3%}'.format(
            epoch, nb_epochs, d_loss, gan_loss))
        export_losses(losses, fn='data/metrics/metrics.json', verbose=False)

    return losses
Example #2
0
def load_compile_reconstructions(generator_model1, generator_model2,noise_dim,img_source_dim1,img_source_dim2,opt_G, opt_rec, classificator_model2=None):
    rec1 = models.reconstructor(generator_model1, generator_model2, noise_dim, img_source_dim1)
    rec2 = models.reconstructor(generator_model2, generator_model1, noise_dim, img_source_dim2)
    rec1.compile(loss='mse',  optimizer=opt_rec)
    rec2.compile(loss='mse',  optimizer=opt_rec)

    if classificator_model2 is not None:
        models.make_trainable(generator_model1, False) #because generator_model1 is already trained by a classificator in a supervised setting
        models.make_trainable(generator_model2, True)
        models.make_trainable(classificator_model2, True)
        recClass = models.reconstructorClass(generator_model1, generator_model2, classificator_model2, noise_dim, img_source_dim1)
        recClass.compile(loss='categorical_crossentropy',  optimizer=opt_rec)
        return rec1,rec2,recClass
    else:
        return rec1,rec2
Example #3
0
def load_compile_models(noise_dim,
                        img_source_dim,
                        img_dest_dim,
                        deterministic,
                        pureGAN,
                        wd,
                        loss1,
                        loss2,
                        disc_type,
                        n_classes,
                        opt_D,
                        opt_G,
                        opt_C,
                        opt_Z,
                        suffix=None,
                        pretrained=False):
    # LOAD MODELS:
    generator_model = models.generator_google_mnistM(noise_dim, img_source_dim,
                                                     img_dest_dim,
                                                     deterministic, pureGAN,
                                                     wd, suffix)
    generator_ss = models.generator_mnistM(noise_dim, img_source_dim,
                                           img_dest_dim, deterministic,
                                           pureGAN, wd,
                                           suffix)  # sem-sup generator
    discriminator_model, discriminator2 = models.discriminator_dcgan_doubled(
        img_dest_dim, wd, n_classes, disc_type)
    discriminator_ss = models.discriminator_sem(img_dest_dim, wd, n_classes,
                                                disc_type)  #sem-sup disc
    #     classificator_model = models.classificator_svhn(img_dest_dim, n_classes, wd)
    if pretrained:
        classificator_model = models.resnet50classifier(
            img_dest_dim, n_classes, wd)
    else:
        classificator_model = models.classificator_google_mnistM(
            img_dest_dim, n_classes, wd)
        classificator_model2 = models.classificator_google_mnistM(
            img_dest_dim, n_classes, wd)

    DCGAN_model = models.DCGAN_naive(generator_model, discriminator_model,
                                     noise_dim, img_source_dim)
    GenClass_model = models.DCGAN_naive2(generator_model, classificator_model,
                                         noise_dim, img_source_dim)
    DCGAN_ss = models.DCGAN_naive3(
        generator_ss, discriminator_ss, noise_dim,
        img_source_dim)  #semi supervised gen-disc model

    if not deterministic:
        zclass_model = z_coerence(generator_model,
                                  img_source_dim,
                                  bn_mode=2,
                                  wd=wd,
                                  inject_noise=False,
                                  n_classes=n_classes,
                                  noise_dim=noise_dim,
                                  model_name="zClass")

    # COMPILE MODELS:
    generator_model.compile(loss=loss1, optimizer=opt_G)
    generator_ss.compile(loss=loss1, optimizer=opt_G)
    models.make_trainable(discriminator_model, False)
    models.make_trainable(discriminator2, False)
    models.make_trainable(classificator_model, False)
    models.make_trainable(classificator_model2, False)

    if disc_type == "simple_disc":
        DCGAN_model.compile(loss=[loss1], optimizer=opt_G)
        models.make_trainable(discriminator_model, True)
        discriminator_model.compile(loss=[loss1], optimizer=opt_D)
    elif disc_type == "nclass_disc":
        DCGAN_model.compile(loss=loss1, optimizer=opt_G)
        GenClass_model.compile(loss=['categorical_crossentropy'],
                               optimizer=opt_G)
        models.make_trainable(discriminator_model, True)
        models.make_trainable(discriminator2, True)
        discriminator_model.compile(loss=[loss1], optimizer=opt_D)
        discriminator2.compile(loss=loss2, optimizer=opt_D)
        discriminator_ss.compile(loss=[loss1, loss2],
                                 optimizer=opt_D)  #Sem-sup disc
        DCGAN_ss.compile(loss=['mse', 'categorical_crossentropy'],
                         optimizer=opt_G)
    models.make_trainable(classificator_model, True)
    classificator_model.compile(loss=loss2,
                                metrics=['accuracy'],
                                optimizer=opt_C)
    models.make_trainable(classificator_model2, True)
    classificator_model2.compile(loss=loss2,
                                 metrics=['accuracy'],
                                 optimizer=opt_C)

    if not deterministic:
        zclass_model.compile(loss=[loss1], optimizer=opt_Z)
        return generator_model, discriminator_model, classificator_model, classificator_model, classificator_model2, DCGAN_model, GenClass_model, generator_ss, discriminator_ss, DCGAN_ss, zclass_model
    else:
        return generator_model, discriminator_model, classificator_model, classificator_model, classificator_model2, DCGAN_model, GenClass_model, generator_ss, discriminator_ss, DCGAN_ss, None
Example #4
0
def main():
    np.random.seed(0)

    # Quick run
    # vocab_size = 1000
    # max_len = 600
    # n_samples = 128

    vocab_size = 2000
    max_len = 1000
    n_samples = 128

    noise_size = 100
    data, labels = load_data(max_len=max_len,
                             vocab_size=vocab_size,
                             n_samples=n_samples)

    generative_optimizer = Adam(lr=1e-4)
    discriminative_optimizer = Adam(lr=1e-3)

    # In: (None, 100) <-- rnd noise
    # Out: (None, 1000, 2000) <-- sentence of (max_len) words encoded in (vocab_size)
    generative_model = build_generative(noise_size, max_len, vocab_size)
    generative_model.compile(loss='binary_crossentropy',
                             optimizer=generative_optimizer)
    # print(generative_model.summary())

    # In: (None, 1000, 2000) <-- sentence of (max_len) words encoded in (vocab_size)
    # Out: (None, 1) <-- probability of the sentence being real
    discriminative_model = build_discriminative(max_len, vocab_size)
    discriminative_model.compile(loss='binary_crossentropy',
                                 optimizer=discriminative_optimizer)
    # print(discriminative_model.summary())

    # Stacked GAN
    # In: (None, 100) <-- rnd noise
    # Out: (None, 1) <-- probability of the sentence being real
    gan = build_gan(noise_size, discriminative_model, generative_model)
    gan.compile(loss='binary_crossentropy', optimizer=generative_optimizer)
    # print(gan.summary())

    # -- Training the discriminator alone
    print('=' * 10 + 'Training discriminative model' + '=' * 10)

    print('-' * 10 + 'Building training data' + '-' * 10)
    training_samples, training_outputs = generate_mixed_data(
        data.train,
        generative_model,
        noise_size=noise_size,
        vocab_size=vocab_size,
        real_samples_size=100,
        generated_samples_size=100)
    print('Training samples shape: ', training_samples.shape)
    print('Training outputs shape: ', training_outputs.shape)

    print('-' * 10 + 'Building testing data' + '-' * 10)
    testing_samples, testing_outputs = generate_mixed_data(
        data.test,
        generative_model,
        noise_size=noise_size,
        vocab_size=vocab_size,
        real_samples_size=100,
        generated_samples_size=100)
    print('Testing samples shape: ', testing_samples.shape)
    print('Testing outputs shape: ', testing_outputs.shape)

    print('-' * 10 + 'Running the training process' + '-' * 10)
    make_trainable(discriminative_model, True)
    discriminative_model.fit(training_samples,
                             training_outputs,
                             batch_size=128,
                             epochs=2)

    print('-' * 10 + 'Evaluating the discriminative model' + '-' * 10)
    scores = discriminative_model.evaluate(testing_samples, testing_outputs)
    print('Loss on testing samples: {:.2%}'.format(scores))

    losses = {"d": [], "g": []}

    try:
        change_lr(gan, 1e-4)
        change_lr(discriminative_model, 1e-3)

        losses = train(nb_epochs=6000,
                       batch_size=32,
                       training_data=data.train,
                       discriminative_model=discriminative_model,
                       generative_model=generative_model,
                       gan_model=gan,
                       noise_size=noise_size,
                       vocab_size=vocab_size,
                       losses=losses)
        export('1', losses, discriminative_model, generative_model, gan)

        change_lr(gan, 1e-5)
        change_lr(discriminative_model, 1e-4)

        losses = train(nb_epochs=2000,
                       batch_size=32,
                       training_data=data.train,
                       discriminative_model=discriminative_model,
                       generative_model=generative_model,
                       gan_model=gan,
                       noise_size=noise_size,
                       vocab_size=vocab_size,
                       losses=losses)
        export('2', losses, discriminative_model, generative_model, gan)

        change_lr(gan, 1e-6)
        change_lr(discriminative_model, 1e-5)

        losses = train(nb_epochs=2000,
                       batch_size=32,
                       training_data=data.train,
                       discriminative_model=discriminative_model,
                       generative_model=generative_model,
                       gan_model=gan,
                       noise_size=noise_size,
                       vocab_size=vocab_size,
                       losses=losses)
        export('3', losses, discriminative_model, generative_model, gan)

    except KeyboardInterrupt as _:
        export('quitedInBetween', losses, discriminative_model,
               generative_model, gan)