Пример #1
0
def train_g64_d64_fine_tune():
    nb_units = 64
    generator_input_dim = 100
    nb_real = 64
    nb_fake = 128 + nb_real
    lr = 0.00002
    beta_1 = 0.5
    nb_batches_per_epoch = 100
    nb_epoch = 60
    output_dir = "models/dcgan_g64_d64_fine_tune"
    hdf5_fname = "/home/leon/data/tags_plain_t6.hdf5"

    g = dcgan_generator(nb_units, generator_input_dim)
    d = dcgan_discriminator(nb_units)

    g.load_weights("models/dcgan_g64_d64/generator.hdf5")
    d.load_weights("models/dcgan_g64_d64/fix_discriminator.hdf5")

    gan = sequential_to_gan(g,
                            d,
                            nb_real,
                            nb_fake,
                            nb_fake_for_gen=128,
                            nb_fake_for_dis=nb_real)

    save = SaveModels(
        {
            "generator_{epoch:04d}.hdf5": g,
            "discriminator_{epoch:04d}.hdf5": d
        },
        every_epoch=20,
        output_dir=output_dir)
    visual = VisualiseGAN(nb_samples=13**2,
                          output_dir=output_dir,
                          preprocess=lambda x: np.clip(x, -1, 1))

    real_z_gen = zip_real_z(real_generator(hdf5_fname, nb_real, range=(-1, 1)),
                            z_generator((nb_fake, generator_input_dim)))

    history = train_dcgan(gan,
                          Adam(lr, beta_1),
                          Adam(lr, beta_1),
                          real_z_gen,
                          nb_batches_per_epoch=nb_batches_per_epoch,
                          nb_epoch=nb_epoch,
                          callbacks=[save, visual])

    with open(os.path.join(output_dir, "history.json"), 'w+') as f:
        json.dump(history.history, f)
Пример #2
0
def train_g64_d64_dct():
    nb_units = 64
    generator_input_dim = 25
    nb_real = 64
    nb_fake = 96
    lr = 0.0002
    beta_1 = 0.5
    nb_batches_per_epoch = 100
    nb_epoch = 1000
    output_dir = "models/dcgan_g64_d64_dct"
    hdf5_fname = "/home/leon/data/tags_plain_t6.hdf5"

    g = dcgan_generator(nb_units, generator_input_dim)
    d = dcgan_discriminator(nb_units)

    gan = sequential_to_gan(g, d, nb_real, nb_fake)

    save = SaveModels({
        "generator.hdf5": g,
        "discriminator.hdf5": d
    },
                      output_dir=output_dir)
    visual = VisualiseGAN(nb_samples=13**2,
                          output_dir=output_dir,
                          preprocess=lambda x: np.clip(x, -1, 1))

    real_z_gen = zip_real_z(real_generator(hdf5_fname, nb_real),
                            z_generator((nb_fake, generator_input_dim)))

    history = train_dcgan(gan,
                          Adam(lr, beta_1),
                          Adam(lr, beta_1),
                          real_z_gen,
                          nb_batches_per_epoch=nb_batches_per_epoch,
                          nb_epoch=nb_epoch,
                          callbacks=[save, visual])
    with open(os.path.join(output_dir, "history.json"), 'w+') as f:
        json.dump(history.history, f)
Пример #3
0
 def generator():
     for z, (param, grid_idx) in zip(z_generator((batch_size, nb_z_param)),
                                     grids_lecture_generator(batch_size)):
         yield np.concatenate([param, z], axis=1), grid_idx