コード例 #1
0
def test_celeb(n_epochs=100,
               crop_shape=[100, 100, 3],
               n_filters=[100, 100, 100, 100],
               filter_sizes=[3, 3, 3, 3]):
    """Summary

    Returns
    -------
    name : TYPE
        Description
    """
    files = CELEB()
    train_vaegan(files=files,
                 batch_size=100,
                 n_epochs=n_epochs,
                 crop_shape=crop_shape,
                 crop_factor=0.8,
                 input_shape=[218, 178, 3],
                 convolutional=True,
                 variational=True,
                 n_filters=n_filters,
                 n_hidden=None,
                 n_code=64,
                 filter_sizes=filter_sizes,
                 activation=tf.nn.elu,
                 ckpt_name='./celeb.ckpt')
コード例 #2
0
def test_celeb():
    """Train an autoencoder on Celeb Net.
    """
    files = CELEB()
    train_vae(files=files,
              input_shape=[218, 178, 3],
              batch_size=100,
              n_epochs=50,
              crop_shape=[64, 64, 3],
              crop_factor=0.8,
              convolutional=True,
              variational=True,
              n_filters=[100, 100, 100],
              n_hidden=250,
              n_code=100,
              dropout=True,
              filter_sizes=[3, 3, 3],
              activation=tf.nn.sigmoid,
              ckpt_name='./celeb.ckpt')
コード例 #3
0
def train_ds():

    init_lr_g = 1e-4  # learning rates
    init_lr_d = 1e-4

    n_latent = 100  # still need to dig into this idea of a latent variable

    n_epochs = 1000000
    batch_size = 200
    n_samples = 15

    # Image sizes, crop etc
    input_shape = [218, 178, 3]
    crop_shape = [64, 64, 3]
    crop_factor = 0.8

    from libs.dataset_utils import create_input_pipeline
    from libs.datasets import CELEB

    files = CELEB()

    # Feed the network batch by batch, tailor images
    batch = create_input_pipeline(files=files,
                                  batch_size=batch_size,
                                  n_epochs=n_epochs,
                                  crop_shape=crop_shape,
                                  crop_factor=crop_factor,
                                  shape=input_shape)

    # [None] + crop_shape: batch (number of samples) + shape of tailored images
    gan = GAN(input_shape=[None] + crop_shape,
              n_features=10,
              n_latent=n_latent,
              rgb=True,
              debug=False)

    # List all the variables
    # Discriminator
    vars_d = [
        v for v in tf.trainable_variables()
        if v.name.startswith('discriminator')
    ]
    print('Training discriminator variables:')
    [
        print(v.name) for v in tf.trainable_variables()
        if v.name.startswith('discriminator')
    ]

    # Generator
    vars_g = [
        v for v in tf.trainable_variables() if v.name.startswith('generator')
    ]
    print('Training generator variables:')
    [
        print(v.name) for v in tf.trainable_variables()
        if v.name.startswith('generator')
    ]

    #########

    zs = np.random.uniform(-1.0, 1.0, [4, n_latent]).astype(np.float32)
    zs = make_latent_manifold(zs, n_samples)

    # Even the learning rates will be learnt! Those will be passed
    # to the opt_g & d below, which use the Adam algorithm
    lr_g = tf.placeholder(tf.float32, shape=[], name='learning_rate_g')
    lr_d = tf.placeholder(tf.float32, shape=[], name='learning_rate_d')

    # Check regularization intros above (before the code).
    # Process applied to discriminator and generator variables
    try:
        from tf.contrib.layers import apply_regularization
        d_reg = apply_regularization(tf.contrib.layers.l2_regularizer(1e-6),
                                     vars_d)
        g_reg = apply_regularization(tf.contrib.layers.l2_regularizer(1e-6),
                                     vars_g)
    except:
        d_reg, g_reg = 0, 0

    # Those two are passed to the Generator & Discriminator
    # respectively through sess.run below
    # (Both networks are trained alternatively)
    opt_g = tf.train.AdamOptimizer(lr_g, name='Adam_g').minimize(
        gan['loss_G'] + g_reg, var_list=vars_g)
    opt_d = tf.train.AdamOptimizer(lr_d, name='Adam_d').minimize(
        gan['loss_D'] + d_reg, var_list=vars_d)

    #########

    sess = tf.Session()
    init_op = tf.global_variables_initializer()

    #########

    # More Tensorboard summaries
    saver = tf.train.Saver()
    sums = gan['sums']

    G_sum_op = tf.summary.merge([
        sums['G'], sums['loss_G'], sums['z'], sums['loss_D_fake'],
        sums['D_fake']
    ])
    D_sum_op = tf.summary.merge([
        sums['loss_D'], sums['loss_D_real'], sums['loss_D_fake'], sums['z'],
        sums['x'], sums['D_real'], sums['D_fake']
    ])

    writer = tf.summary.FileWriter("./logs", sess.graph_def)

    #########

    # Multithreading / parallel calculations (if with GPU)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    sess.run(init_op)

    # g = tf.get_default_graph()
    # [print(op.name) for op in g.get_operations()]

    #########

    # Checkpoint savery
    if os.path.exists("gan.ckpt"):
        saver.restore(sess, "gan.ckpt")
        print("GAN model restored.")

    fig, ax = plt.subplots(1, 1, figsize=(10, 10))

    step_i, t_i = 0, 0
    loss_d = 1
    loss_g = 1
    n_loss_d, total_loss_d = 1, 1
    n_loss_g, total_loss_g = 1, 1

    try:
        while not coord.should_stop():

            batch_xs = sess.run(batch)
            step_i += 1
            batch_zs = np.random.uniform(
                -1.0, 1.0, [batch_size, n_latent]).astype(np.float32)

            this_lr_g = min(1e-2, max(1e-6, init_lr_g * (loss_g / loss_d)**2))
            this_lr_d = min(1e-2, max(1e-6, init_lr_d * (loss_d / loss_g)**2))

            # this_lr_d *= ((1.0 - (step_i / 100000)) ** 2)
            # this_lr_g *= ((1.0 - (step_i / 100000)) ** 2)

            # 2 out of 3 steps (or according to another random-based criterium,
            # cf. the commented if & equation), we train the Discriminator,
            # and the last one we train the Generator instead

            # if np.random.random() > (loss_g / (loss_d + loss_g)):
            if step_i % 3 == 1:
                loss_d, _, sum_d = sess.run(
                    [gan['loss_D'], opt_d, D_sum_op],
                    feed_dict={
                        gan['x']: batch_xs,
                        gan['z']: batch_zs,
                        gan['train']: True,
                        lr_d: this_lr_d
                    })
                total_loss_d += loss_d
                n_loss_d += 1

                writer.add_summary(sum_d, step_i)  # Tensorboard

                print('%04d d* = lr: %0.08f, loss: %08.06f, \t' %
                      (step_i, this_lr_d, loss_d) +
                      'g  = lr: %0.08f, loss: %08.06f' % (this_lr_g, loss_g))

            else:

                loss_g, _, sum_g = sess.run([gan['loss_G'], opt_g, G_sum_op],
                                            feed_dict={
                                                gan['z']: batch_zs,
                                                gan['train']: True,
                                                lr_g: this_lr_g
                                            })
                total_loss_g += loss_g
                n_loss_g += 1

                writer.add_summary(sum_g, step_i)  # Tensorboard

                print('%04d d  = lr: %0.08f, loss: %08.06f, \t' %
                      (step_i, this_lr_d, loss_d) +
                      'g* = lr: %0.08f, loss: %08.06f' % (this_lr_g, loss_g))

            if step_i % 100 == 0:

                samples = sess.run(gan['G'],
                                   feed_dict={
                                       gan['z']: zs,
                                       gan['train']: False
                                   })

                # Create a wall of images of the latent space (what the
                # network learns)
                montage(
                    np.clip((samples + 1) * 127.5, 0, 255).astype(np.uint8),
                    'imgs/gan_%08d.png' % t_i)
                t_i += 1

                print('generator loss:', total_loss_g / n_loss_g)
                print('discriminator loss:', total_loss_d / n_loss_d)

                # Save variable to disk
                save_path = saver.save(sess,
                                       "./gan.ckpt",
                                       global_step=step_i,
                                       write_meta_graph=False)
                print("Model saved in file: %s" % save_path)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        # One thread issued an exception, let them all shut down
        coord.request_stop()

    # Wait for them to finish
    coord.join(threads)

    # Clean up
    sess.close()
コード例 #4
0
ファイル: gan.py プロジェクト: meftaul/kadenze-cadl
def train_ds():
    """Summary
    
    Returns
    -------
    name : TYPE
        Description
    """
    init_lr_g = 1e-4
    init_lr_d = 1e-4
    n_latent = 100
    n_epochs = 1000000
    batch_size = 200
    n_samples = 15
    input_shape = [218, 178, 3]
    crop_shape = [64, 64, 3]
    crop_factor = 0.8

    from libs.dataset_utils import create_input_pipeline
    from libs.datasets import CELEB

    files = CELEB()
    batch = create_input_pipeline(files=files,
                                  batch_size=batch_size,
                                  n_epochs=n_epochs,
                                  crop_shape=crop_shape,
                                  crop_factor=crop_factor,
                                  shape=input_shape)

    gan = GAN(input_shape=[None] + crop_shape,
              n_features=10,
              n_latent=n_latent,
              rgb=True,
              debug=False)

    vars_d = [
        v for v in tf.trainable_variables()
        if v.name.startswith('discriminator')
    ]
    print('Training discriminator variables:')
    [
        print(v.name) for v in tf.trainable_variables()
        if v.name.startswith('discriminator')
    ]

    vars_g = [
        v for v in tf.trainable_variables() if v.name.startswith('generator')
    ]
    print('Training generator variables:')
    [
        print(v.name) for v in tf.trainable_variables()
        if v.name.startswith('generator')
    ]
    zs = np.random.uniform(-1.0, 1.0, [4, n_latent]).astype(np.float32)
    zs = make_latent_manifold(zs, n_samples)

    lr_g = tf.placeholder(tf.float32, shape=[], name='learning_rate_g')
    lr_d = tf.placeholder(tf.float32, shape=[], name='learning_rate_d')

    try:
        from tf.contrib.layers import apply_regularization
        d_reg = apply_regularization(tf.contrib.layers.l2_regularizer(1e-6),
                                     vars_d)
        g_reg = apply_regularization(tf.contrib.layers.l2_regularizer(1e-6),
                                     vars_g)
    except:
        d_reg, g_reg = 0, 0

    opt_g = tf.train.AdamOptimizer(lr_g, name='Adam_g').minimize(
        gan['loss_G'] + g_reg, var_list=vars_g)
    opt_d = tf.train.AdamOptimizer(lr_d, name='Adam_d').minimize(
        gan['loss_D'] + d_reg, var_list=vars_d)

    # %%
    # We create a session to use the graph
    config = tf.ConfigProto(device_count={'GPU': 0})
    sess = tf.Session(config=config)
    init_op = tf.initialize_all_variables()

    saver = tf.train.Saver()
    sums = gan['sums']
    G_sum_op = tf.merge_summary([
        sums['G'], sums['loss_G'], sums['z'], sums['loss_D_fake'],
        sums['D_fake']
    ])
    D_sum_op = tf.merge_summary([
        sums['loss_D'], sums['loss_D_real'], sums['loss_D_fake'], sums['z'],
        sums['x'], sums['D_real'], sums['D_fake']
    ])
    writer = tf.train.SummaryWriter("./logs", sess.graph)

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    sess.run(init_op)
    # g = tf.get_default_graph()
    # [print(op.name) for op in g.get_operations()]

    fig, ax = plt.subplots(1, 1, figsize=(10, 10))
    step_i, t_i = 0, 0
    loss_d = 1
    loss_g = 1
    n_loss_d, total_loss_d = 1, 1
    n_loss_g, total_loss_g = 1, 1

    ckpt = glob('gan.ckpt*')
    if ckpt:
        ckpt = sorted(ckpt)[-1]
        saver.restore(sess, ckpt)
        step_i = int(ckpt.split('-')[-1])
        t_i = int(sorted(glob('imgs/*.png'))[-1].split('_')[-1][:-4]) + 1
        print("GAN model restored.")

    try:
        while not coord.should_stop():
            batch_xs = sess.run(batch)
            step_i += 1
            batch_zs = np.random.uniform(
                -1.0, 1.0, [batch_size, n_latent]).astype(np.float32)

            this_lr_g = min(1e-2, max(1e-6, init_lr_g * (loss_g / loss_d)**2))
            this_lr_d = min(1e-2, max(1e-6, init_lr_d * (loss_d / loss_g)**2))
            # this_lr_d *= ((1.0 - (step_i / 100000)) ** 2)
            # this_lr_g *= ((1.0 - (step_i / 100000)) ** 2)

            # if np.random.random() > (loss_g / (loss_d + loss_g)):
            if step_i % 3 == 1:
                loss_d, _, sum_d = sess.run(
                    [gan['loss_D'], opt_d, D_sum_op],
                    feed_dict={
                        gan['x']: batch_xs,
                        gan['z']: batch_zs,
                        gan['train']: True,
                        lr_d: this_lr_d
                    })
                total_loss_d += loss_d
                n_loss_d += 1
                writer.add_summary(sum_d, step_i)
                print('%04d d* = lr: %0.08f, loss: %08.06f, \t' %
                      (step_i, this_lr_d, loss_d) +
                      'g  = lr: %0.08f, loss: %08.06f' % (this_lr_g, loss_g))
            else:
                loss_g, _, sum_g = sess.run([gan['loss_G'], opt_g, G_sum_op],
                                            feed_dict={
                                                gan['z']: batch_zs,
                                                gan['train']: True,
                                                lr_g: this_lr_g
                                            })
                total_loss_g += loss_g
                n_loss_g += 1
                writer.add_summary(sum_g, step_i)
                print('%04d d  = lr: %0.08f, loss: %08.06f, \t' %
                      (step_i, this_lr_d, loss_d) +
                      'g* = lr: %0.08f, loss: %08.06f' % (this_lr_g, loss_g))

            if step_i % 100 == 0:
                samples = sess.run(gan['G'],
                                   feed_dict={
                                       gan['z']: zs,
                                       gan['train']: False
                                   })
                montage(
                    np.clip((samples + 1) * 127.5, 0, 255).astype(np.uint8),
                    'imgs/gan_%08d.png' % t_i)
                t_i += 1

                print('generator loss:', total_loss_g / n_loss_g)
                print('discriminator loss:', total_loss_d / n_loss_d)

                # Save the variables to disk.
                save_path = saver.save(sess,
                                       "./gan.ckpt",
                                       global_step=step_i,
                                       write_meta_graph=False)
                print("Model saved in file: %s" % save_path)
    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        # One of the threads has issued an exception.  So let's tell all the
        # threads to shutdown.
        coord.request_stop()

    # Wait until all threads have finished.
    coord.join(threads)

    # Clean up the session.
    sess.close()