Exemple #1
0
def main():
    start_time = time.time()  # Clocking start

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # DRAGAN model
        model = dragan.DRAGAN(s, batch_size=train_step['batch_size'])

        # Initializing variables
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %s" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        # MNIST DataSet images
        mnist = DataSet(ds_path="D:\\DataSet/mnist/").data

        for global_step in range(saved_global_step, train_step['global_step']):
            batch_x, _ = mnist.train.next_batch(model.batch_size)
            batch_x_p = get_perturbed_images(batch_x)
            batch_x = np.reshape(batch_x, [-1] + model.image_shape)
            batch_x_p = np.reshape(batch_x_p, [-1] + model.image_shape)

            batch_z = np.random.uniform(
                -1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

            # Update D network
            _, d_loss = s.run(
                [model.d_op, model.d_loss],
                feed_dict={
                    model.x: batch_x,
                    model.x_p: batch_x_p,
                    model.z: batch_z,
                },
            )

            # Update G network
            _, g_loss = s.run(
                [model.g_op, model.g_loss],
                feed_dict={
                    model.z: batch_z,
                },
            )

            if global_step % train_step['logging_interval'] == 0:
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                d_loss, g_loss, summary = s.run(
                    [model.d_loss, model.g_loss, model.merged],
                    feed_dict={
                        model.x: batch_x,
                        model.x_p: batch_x_p,
                        model.z: batch_z,
                    },
                )

                # Print loss
                print(
                    "[+] Global Step %05d => " % global_step,
                    " D loss : {:.8f}".format(d_loss),
                    " G loss : {:.8f}".format(g_loss),
                )

                # Training G model with sample image and noise
                sample_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.sample_num, model.z_dim]).astype(np.float32)
                samples = s.run(
                    model.g,
                    feed_dict={
                        model.z: sample_z,
                    },
                )

                samples = np.reshape(samples, [-1] + model.image_shape)

                # Summary saver
                model.writer.add_summary(summary, global_step)

                # Export image generated by model G
                sample_image_height = model.sample_size
                sample_image_width = model.sample_size
                sample_dir = results['output'] + 'train_{0}.png'.format(
                    global_step)

                # Generated image save
                iu.save_images(samples,
                               size=[sample_image_height, sample_image_width],
                               image_path=sample_dir)

                # Model save
                model.saver.save(s, results['model'], global_step)

            global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #2
0
def main():
    start_time = time.time()  # Clocking start

    # MNIST Dataset Load
    mnist = DataSet(ds_path="D:\\DataSet/mnist/").data

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # CGAN Model
        model = cgan.CGAN(s, batch_size=train_step['batch_size'])

        # initializing
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        sample_y = np.zeros(shape=[model.sample_num, model.n_classes])
        for i in range(10):
            sample_y[10 * i:10 * (i + 1), i] = 1

        for global_step in range(saved_global_step, train_step['global_step']):
            batch_x, batch_y = mnist.train.next_batch(model.batch_size)
            batch_z = np.random.uniform(
                -1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

            # Update D network
            _, d_loss = s.run(
                [model.d_op, model.d_loss],
                feed_dict={
                    model.x: batch_x,
                    model.c: batch_y,
                    model.z: batch_z,
                    model.do_rate: 0.5,
                },
            )

            # Update G network
            _, g_loss = s.run([model.g_op, model.g_loss],
                              feed_dict={
                                  model.c: batch_y,
                                  model.z: batch_z,
                                  model.do_rate: 0.5,
                              })

            # Logging
            if global_step % train_step['logging_interval'] == 0:
                batch_x, batch_y = mnist.test.next_batch(model.batch_size)
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                d_loss, g_loss, summary = s.run(
                    [model.d_loss, model.g_loss, model.merged],
                    feed_dict={
                        model.x: batch_x,
                        model.c: batch_y,
                        model.z: batch_z,
                        model.do_rate: 0.5,
                    },
                )

                # Print Loss
                print(
                    "[+] Step %08d => " % global_step,
                    " D loss : {:.8f}".format(d_loss),
                    " G loss : {:.8f}".format(g_loss),
                )

                # Training G model with sample image and noise
                sample_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.sample_num, model.z_dim]).astype(np.float32)
                samples = s.run(model.g,
                                feed_dict={
                                    model.c: sample_y,
                                    model.z: sample_z,
                                    model.do_rate: 0.0,
                                })

                samples = np.reshape(samples, [-1, 28, 28, 1])

                # Summary saver
                model.writer.add_summary(summary, global_step)

                # Export image generated by model G
                sample_image_height = model.sample_size
                sample_image_width = model.sample_size
                sample_dir = results['output'] + 'train_{:08d}.png'.format(
                    global_step)

                # Generated image save
                iu.save_images(samples,
                               size=[sample_image_height, sample_image_width],
                               image_path=sample_dir)

                # Model save
                model.saver.save(s, results['model'], global_step)

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
def main():
    start_time = time.time()  # clocking start

    # Dataset
    dataset = DataSets(height=64,
                       width=64,
                       channel=3,
                       ds_path='D:/DataSets/pix2pix/',
                       ds_name="vangogh2photo")

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as s:
        # DiscoGAN model
        model = discogan.DiscoGAN(s)

        # load model & graph & weight
        global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
            print("[+] global step : %s" % global_step, " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        # initializing variables
        tf.global_variables_initializer().run()

        d_overpowered = False  # G loss > D loss * 2
        for epoch in range(paras['epoch']):
            for step in range(1000):
                offset_a = (step * paras['batch_size']) % (
                    dataset.images_a.shape[0] - paras['batch_size'])
                offset_b = (step * paras['batch_size']) % (
                    dataset.images_b.shape[0] - paras['batch_size'])

                # batch data set
                batch_a = dataset.images_a[offset_a:(offset_a +
                                                     paras['batch_size']), :]
                batch_b = dataset.images_b[offset_b:(offset_b +
                                                     paras['batch_size']), :]

                # update D network
                if not d_overpowered:
                    s.run(model.d_op, feed_dict={model.A: batch_a})

                # update G network
                s.run(model.g_op, feed_dict={model.B: batch_b})

                if epoch % paras['logging_interval'] == 0:
                    d_loss, g_loss, summary = s.run(
                        [model.d_loss, model.g_loss, model.merged],
                        feed_dict={
                            model.A: batch_a,
                            model.B: batch_b
                        })

                    # print loss
                    print(
                        "[+] Epoch %03d Step %04d => " % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                    )

                    # update overpowered
                    d_overpowered = d_loss < g_loss / 2.0

                    # training G model with sample image and noise
                    ab_samples = s.run(model.G_s2b,
                                       feed_dict={model.A: batch_a})
                    ba_samples = s.run(model.G_b2s,
                                       feed_dict={model.B: batch_b})

                    # summary saver
                    model.writer.add_summary(summary, global_step=global_step)

                    # export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_ab_dir = results[
                        'sample_output'] + 'train_A_{0}_{1}.png'.format(
                            epoch, global_step)
                    sample_ba_dir = results[
                        'sample_output'] + 'train_B_{0}_{1}.png'.format(
                            epoch, global_step)

                    # Generated image save
                    iu.save_images(
                        ab_samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_ab_dir)
                    iu.save_images(
                        ba_samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_ba_dir)

                    # model save
                    model.saver.save(s,
                                     results['model'],
                                     global_step=global_step)

        end_time = time.time() - start_time

        # elapsed time
        print("[+] Elapsed time {:.8f}s".format(end_time))

        # close tf.Session
        s.close()
def main():
    start_time = time.time()  # Clocking start

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        if os.path.exists("./orig-model/"):
            detect = True  # There has to be pre-trained file
        else:
            detect = False

        # AnoGAN Model
        model = anogan.AnoGAN(detect=detect, use_label=False)  # AnoGAN

        # Initializing
        s.run(tf.global_variables_initializer())

        # loading CelebA DataSet
        ds = DataSet(
            height=64,
            width=64,
            channel=3,
            ds_image_path="D:\\DataSet/CelebA/CelebA-64.h5",
            ds_label_path="D:\\DataSet/CelebA/Anno/list_attr_celeba.txt",
            # ds_image_path="D:\\DataSet/CelebA/Img/img_align_celeba/",
            ds_type="CelebA",
            use_save=False,
            save_file_name="D:\\DataSet/CelebA/CelebA-128.h5",
            save_type="to_h5",
            use_img_scale=False,
            # img_scale="-1,1"
        )

        # saving sample images
        test_images = np.reshape(iu.transform(ds.images[:16], inv_type='127'),
                                 (16, 64, 64, 3))
        iu.save_images(test_images,
                       size=[4, 4],
                       image_path=results['output'] + 'sample.png',
                       inv_type='127')

        ds_iter = DataIterator(x=ds.images,
                               y=None,
                               batch_size=train_step['batch_size'],
                               label_off=True)

        # To-Do
        # Getting anomaly data

        # Load model & Graph & Weights
        if not detect or not os.path.exists("./ano-model/"):
            ckpt = tf.train.get_checkpoint_state('./orig-model/')
        else:
            ckpt = tf.train.get_checkpoint_state('./ano-model/')

        saved_global_step = 0
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        global_step = saved_global_step
        start_epoch = global_step // (ds.num_images // model.batch_size
                                      )  # recover n_epoch
        ds_iter.pointer = saved_global_step % (
            ds.num_images // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epoch']):
            for batch_images in ds_iter.iterate():
                batch_x = np.reshape(batch_images,
                                     [-1] + model.image_shape[1:])
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                # Update D network
                _, d_loss = s.run([model.d_op, model.d_loss],
                                  feed_dict={
                                      model.x: batch_x,
                                      model.z: batch_z,
                                  })

                # Update G network
                _, g_loss = s.run([model.g_op, model.g_loss],
                                  feed_dict={
                                      model.z: batch_z,
                                  })

                if global_step % train_step['logging_step'] == 0:
                    batch_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.batch_size, model.z_dim]).astype(np.float32)

                    # Summary
                    d_loss, g_loss, summary = s.run(
                        [model.d_loss, model.g_loss, model.merged],
                        feed_dict={
                            model.x: batch_x,
                            model.z: batch_z,
                        })

                    # Print loss
                    print(
                        "[+] Epoch %04d Step %07d =>" % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                    )

                    # Summary saver
                    model.writer.add_summary(summary, epoch)

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(model.g_test,
                                    feed_dict={
                                        model.z: sample_z,
                                    })

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{0}_{1}.png'.format(
                        epoch, global_step)

                    # Generated image save
                    iu.save_images(
                        samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_dir)

                    # Model save
                    if not detect:
                        model.saver.save(s,
                                         results['orig-model'],
                                         global_step=global_step)
                    else:
                        model.saver.save(s,
                                         results['ano-model'],
                                         global_step=global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #5
0
def main():
    start_time = time.time()  # Clocking start

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # CycleGAN Model
        model = cyclegan.CycleGAN(s,
                                  height=128,
                                  width=128,
                                  channel=3,
                                  batch_size=train_step['batch_size'])

        # Celeb-A DataSet images
        ds = DataSet(height=128,
                     width=128,
                     channel=3,
                     ds_path="D:/DataSet/pix2pix/",
                     ds_name='vangogh2photo')

        img_a = ds.images_a
        img_b = ds.images_b

        print("[*] image A shape : ", img_a.shape)
        print("[*] image B shape : ", img_b.shape)

        n_sample = model.sample_num

        sample_image_height = model.sample_size
        sample_image_width = model.sample_size
        sample_dir_a = results['output'] + 'valid_a.png'
        sample_dir_b = results['output'] + 'valid_b.png'

        sample_a, sample_b = img_a[:n_sample], img_b[:n_sample]
        sample_a = np.reshape(sample_a, [-1] + model.image_shape[1:])
        sample_b = np.reshape(sample_b, [-1] + model.image_shape[1:])

        # Generated image save
        iu.save_images(sample_a, [sample_image_height, sample_image_width],
                       sample_dir_a)
        iu.save_images(sample_b, [sample_image_height, sample_image_width],
                       sample_dir_b)

        print("[+] pre-processing elapsed time : {:.8f}s".format(time.time() -
                                                                 start_time))

        # Initializing
        s.run(tf.global_variables_initializer())

        global_step = 0
        for epoch in range(train_step['epochs']):
            # learning rate decay
            lr_decay = 1.0
            if epoch >= 100 and epoch % 10 == 0:
                lr_decay = (train_step['epochs'] -
                            epoch) / (train_step['epochs'] / 2.0)

            # re-implement DataIterator for multi-input
            pointer = 0
            num_images = min(ds.n_images_a, ds.n_images_b)
            for i in range(num_images // train_step['batch_size']):
                start = pointer
                pointer += train_step['batch_size']

                if pointer > num_images:  # if ended 1 epoch
                    # Shuffle training DataSet
                    perm_a, perm_b = np.arange(ds.n_images_a), np.arange(
                        ds.n_images_b)

                    np.random.shuffle(perm_a)
                    np.random.shuffle(perm_b)

                    img_a, img_b = img_a[perm_a], img_a[perm_b]

                    start = 0
                    pointer = train_step['batch_size']

                end = pointer

                batch_a = np.reshape(img_a[start:end], model.image_shape)
                batch_b = np.reshape(img_a[start:end], model.image_shape)

                for _ in range(model.n_train_critic):
                    s.run(
                        model.d_op,
                        feed_dict={
                            model.a: batch_a,
                            model.b: batch_b,
                            model.lr_decay: lr_decay,
                        },
                    )

                w, gp, g_loss, cycle_loss, _ = s.run(
                    [
                        model.w, model.gp, model.g_loss, model.cycle_loss,
                        model.g_op
                    ],
                    feed_dict={
                        model.a: batch_a,
                        model.b: batch_b,
                        model.lr_decay: lr_decay,
                    },
                )

                if global_step % train_step['logging_step'] == 0:
                    # Summary
                    summary = s.run(
                        model.merged,
                        feed_dict={
                            model.a: batch_a,
                            model.b: batch_b,
                            model.lr_decay: lr_decay,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Global Step %08d =>" % global_step,
                        " G loss     : {:.8f}".format(g_loss),
                        " Cycle loss : {:.8f}".format(cycle_loss),
                        " w          : {:.8f}".format(w),
                        " gp         : {:.8f}".format(gp),
                    )

                    # Summary saver
                    model.writer.add_summary(summary, global_step=global_step)

                    # Training G model with sample image and noise
                    samples_a2b = s.run(
                        model.g_a2b,
                        feed_dict={
                            model.a: sample_a,
                            model.b: sample_b,
                            model.lr_decay: lr_decay,
                        },
                    )
                    samples_b2a = s.run(
                        model.g_b2a,
                        feed_dict={
                            model.a: sample_a,
                            model.b: sample_b,
                            model.lr_decay: lr_decay,
                        },
                    )

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir_a2b = results[
                        'output'] + 'train_a2b_{0}.png'.format(global_step)
                    sample_dir_b2a = results[
                        'output'] + 'train_b2a_{0}.png'.format(global_step)

                    # Generated image save
                    iu.save_images(samples_a2b,
                                   [sample_image_height, sample_image_width],
                                   sample_dir_a2b)
                    iu.save_images(samples_b2a,
                                   [sample_image_height, sample_image_width],
                                   sample_dir_b2a)

                    # Model save
                    model.saver.save(s,
                                     results['model'],
                                     global_step=global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
def main():
    start_time = time.time()  # Clocking start

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # pre-chosen
        attr_labels = [
            'Big_Nose',
            'Black_Hair',
            'Blond_Hair',
            'Blurry',
            'Brown_Hair',
            'Bushy_Eyebrows',
            'Chubby',
            'Double_Chin',
            'Eyeglasses',
            'Gray_Hair',
        ]

        # StarGAN Model
        model = stargan.StarGAN(s, attr_labels=attr_labels)  # StarGAN

        # Initializing
        s.run(tf.global_variables_initializer())

        # loading CelebA DataSet
        ds = DataSet(
            height=64,
            width=64,
            channel=3,
            ds_image_path="D:/DataSet/CelebA/CelebA-64.h5",
            ds_label_path="D:/DataSet/CelebA/Anno/list_attr_celeba.txt",
            # ds_image_path="D:/DataSet/CelebA/Img/img_align_celeba/",
            ds_type="CelebA",
            use_save=False,
            save_file_name="D:/DataSet/CelebA/CelebA-64.h5",
            save_type="to_h5",
            use_img_scale=False,
            img_scale="-1,1",
        )

        # x_A # Celeb-A
        img_a = np.reshape(ds.images, [-1, 64, 64, 3])
        attr_a = ds.labels

        # x_B # Celeb-A # copied from x_A
        # later it'll be replaced to another DataSet like RaFD, used in the paper
        # but i can't find proper(good) DataSets, so i just do with single-domain (Celeb-A)
        # img_b = img_a[:]
        # attr_b = attr_a[:]

        # ds_a_iter = DataIterator(img_a, attr_a, train_step['batch_size'])
        # ds_b_iter = DataIterator(img_b, attr_b, train_step['batch_size'])

        print("[+] pre-processing elapsed time : {:.8f}s".format(time.time() - start_time))
        print("[*] image_A     :", img_a.shape, " attribute A :", attr_a.shape)

        global_step = 0
        for epoch in range(train_step['epoch']):
            # learning rate decay
            lr_decay = 1.0
            if epoch >= train_step['epoch']:
                lr_decay = (train_step['epoch'] - epoch) / (train_step['epoch'] / 2.0)

            # re-implement DataIterator for multi-input
            pointer = 0
            for i in range(ds.num_images // train_step['batch_size']):
                start = pointer
                pointer += train_step['batch_size']

                if pointer > ds.num_images:  # if ended 1 epoch
                    # Shuffle training DataSet
                    perm = np.arange(ds.num_images)
                    np.random.shuffle(perm)

                    # To-Do
                    # Getting Proper DataSet
                    img_a, img_b = img_a[perm], img_a[perm]
                    attr_a, attr_b = attr_a[perm], attr_a[perm]

                    start = 0
                    pointer = train_step['batch_size']

                end = pointer

                x_a, y_a = img_a[start:end], attr_a[start:end][:]
                x_b, y_b = img_a[start:end], attr_a[start:end][:]

                x_a = iu.transform(x_a, inv_type='127')
                x_b = iu.transform(x_b, inv_type='127')

                batch_a = ds.concat_data(x_a, y_a)
                batch_b = ds.concat_data(x_b, y_b)
                eps = np.random.rand(train_step['batch_size'], 1, 1, 1)

                # Generate fake_B
                fake_b = s.run(model.fake_B, feed_dict={model.x_A: batch_a})

                # Update D network - 5 times
                for _ in range(5):
                    _, d_loss = s.run(
                        [model.d_op, model.d_loss],
                        feed_dict={
                            model.x_B: batch_b,
                            model.y_B: y_b,
                            model.fake_x_B: fake_b,
                            model.lr_decay: lr_decay,
                            model.epsilon: eps,
                        },
                    )

                # Update G network - 1 time
                _, g_loss = s.run(
                    [model.g_op, model.g_loss],
                    feed_dict={
                        model.x_A: batch_a,
                        model.x_B: batch_b,
                        model.y_B: y_b,
                        model.lr_decay: lr_decay,
                        model.epsilon: eps,
                    },
                )

                if global_step % train_step['logging_step'] == 0:
                    eps = np.random.rand(train_step['batch_size'], 1, 1, 1)

                    # Summary
                    samples, d_loss, g_loss, summary = s.run(
                        [model.fake_A, model.d_loss, model.g_loss, model.merged],
                        feed_dict={
                            model.x_A: batch_a,
                            model.x_B: batch_b,
                            model.y_B: y_b,
                            model.fake_x_B: fake_b,
                            model.lr_decay: lr_decay,
                            model.epsilon: eps,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Epoch %04d Step %07d =>" % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                    )

                    # Summary saver
                    model.writer.add_summary(summary, epoch)

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{0}_{1}.png'.format(epoch, global_step)

                    # Generated image save
                    iu.save_images(
                        samples, size=[sample_image_height, sample_image_width], image_path=sample_dir, inv_type='127'
                    )

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
def main():
    start_time = time.time()  # Clocking start

    # UrbanSound8K Dataset load
    mnist = MNISTDataSet().data

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # CoGAN Model
        model = segan.SEGAN(s)

        # Initializing
        s.run(tf.global_variables_initializer())

        sample_x, _ = mnist.test.next_batch(model.sample_num)
        sample_y = np.zeros(shape=[model.sample_num, model.n_classes])
        for i in range(10):
            sample_y[10 * i:10 * (i + 1), i] = 1

        for step in range(train_step['global_step']):
            batch_x, batch_y = mnist.train.next_batch(model.batch_size)
            batch_x = np.reshape(batch_x, model.image_shape)
            batch_z = np.random.uniform(
                -1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

            # Update D network
            _, d_loss = s.run(
                [model.d_op, model.d_loss],
                feed_dict={
                    model.x_1: batch_x,
                    model.x_2: batch_x,
                    # model.y: batch_y,
                    model.z: batch_z,
                },
            )

            # Update G network
            _, g_loss = s.run(
                [model.g_op, model.g_loss],
                feed_dict={
                    model.x_1: batch_x,
                    model.x_2: batch_x,
                    # model.y: batch_y,
                    model.z: batch_z,
                },
            )

            if step % train_step['logging_interval'] == 0:
                batch_x, batch_y = mnist.train.next_batch(model.batch_size)
                batch_x = np.reshape(batch_x, model.image_shape)
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                d_loss, g_loss, summary = s.run(
                    [model.d_loss, model.g_loss, model.merged],
                    feed_dict={
                        model.x_1: batch_x,
                        model.x_2: batch_x,
                        # model.y: batch_y,
                        model.z: batch_z,
                    },
                )

                # Print loss
                print("[+] Step %08d => " % step,
                      " D loss : {:.8f}".format(d_loss),
                      " G loss : {:.8f}".format(g_loss))

                sample_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.sample_num, model.z_dim]).astype(np.float32)

                # Training G model with sample image and noise
                samples_1 = s.run(
                    model.g_sample_1,
                    feed_dict={
                        # model.y: sample_y,
                        model.z: sample_z,
                    },
                )

                samples_2 = s.run(
                    model.g_sample_2,
                    feed_dict={
                        # model.y: sample_y,
                        model.z: sample_z,
                    },
                )

                samples_1 = np.reshape(samples_1, [-1] + model.image_shape[1:])
                samples_2 = np.reshape(samples_2, [-1] + model.image_shape[1:])

                # Summary saver
                model.writer.add_summary(summary, global_step=step)

                # Export image generated by model G
                sample_image_height = model.sample_size
                sample_image_width = model.sample_size

                sample_dir_1 = results['output'] + 'train_1_{:08d}.png'.format(
                    step)
                sample_dir_2 = results['output'] + 'train_2_{:08d}.png'.format(
                    step)

                # Generated image save
                iu.save_images(samples_1,
                               size=[sample_image_height, sample_image_width],
                               image_path=sample_dir_1)
                iu.save_images(samples_2,
                               size=[sample_image_height, sample_image_width],
                               image_path=sample_dir_2)

                # Model save
                model.saver.save(s, results['model'], global_step=step)

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
def main():
    start_time = time.time()  # Clocking start

    height, width, channel = 128, 128, 3

    # loading CelebA DataSet
    ds = DataSet(
        height=height,
        width=height,
        channel=channel,
        # ds_image_path="D:\\DataSet/CelebA/CelebA-%d.h5" % height,
        ds_label_path=os.path.join(cfg.celeba_path, "Anno/list_attr_celeba.txt"),
        ds_image_path=os.path.join(cfg.celeba_path, "Img/img_align_celeba/"),
        ds_type="CelebA",
        use_save=True,
        save_file_name=os.path.join(cfg.celeba_path, "CelebA-%d.h5" % height),
        save_type="to_h5",
        use_img_scale=False,
    )

    # saving sample images
    test_images = np.reshape(iu.transform(ds.images[:16], inv_type='127'), (16, height, width, channel))
    iu.save_images(test_images, size=[4, 4], image_path=os.path.join(cfg.output_path, "sample.png"), inv_type='127')

    ds_iter = DataIterator(x=ds.images, y=None, batch_size=train_step['batch_size'], label_off=True)

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # BigGAN Model
        model = biggan.BigGAN(s, height=height, width=width, channel=channel, batch_size=train_step['batch_size'])

        # Initializing
        s.run(tf.global_variables_initializer())

        print("[*] Reading checkpoints...")

        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state(cfg.model_path)
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % saved_global_step, " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        global_step = saved_global_step
        start_epoch = global_step // (ds.num_images // model.batch_size)  # recover n_epoch
        ds_iter.pointer = saved_global_step % (ds.num_images // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epochs']):
            for batch_x in ds_iter.iterate():
                batch_x = iu.transform(batch_x, inv_type='127')
                batch_x = np.reshape(batch_x, (model.batch_size, model.height, model.width, model.channel))
                batch_z = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

                # Update D network
                _, d_loss = s.run(
                    [model.d_op, model.d_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                # Update G network
                _, g_loss = s.run(
                    [model.g_op, model.g_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                if global_step % train_step['logging_interval'] == 0:
                    summary = s.run(
                        model.merged,
                        feed_dict={
                            model.x: batch_x,
                            model.z: batch_z,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Epoch %04d Step %08d => " % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                    )

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(-1.0, 1.0, [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(
                        model.g_test,
                        feed_dict={
                            model.z: sample_z,
                        },
                    )

                    # Summary saver
                    model.writer.add_summary(summary, global_step)

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = os.path.join(cfg.output, 'train_{:08d}.png'.format(global_step))

                    # Generated image save
                    iu.save_images(
                        samples, size=[sample_image_height, sample_image_width], image_path=sample_dir, inv_type='127'
                    )

                    # Model save
                    model.saver.save(s, os.path.join(cfg.model_path, "BigGAN.ckpt"), global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #9
0
def main():
    start_time = time.time()  # Clocking start

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # DualGAN Model
        model = dualgan.DualGAN(s)  # DualGAN

        # Initializing
        s.run(tf.global_variables_initializer())

        # Celeb-A DataSet images
        ds = DataSet(height=32, width=32, channel=3, ds_path="D:/DataSets/CelebA/", ds_type="CelebA").images
        ds_iter = DataIterator(ds, None, train_step['batch_size'], label_off=True)

        global_step = 0
        for epoch in range(train_step['epoch']):
            for batch_images in ds_iter.iterate():
                batch_x = np.reshape(batch_images, [-1] + model.image_shape[1:])
                batch_z = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

                # Update D network
                _, d_loss = s.run(
                    [model.d_op, model.d_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                # Update G network
                _, g_loss = s.run(
                    [model.g_op, model.g_loss],
                    feed_dict={
                        model.z: batch_z,
                    },
                )

                # Update k_t
                _, k, m_global = s.run(
                    [model.k_update, model.k, model.m_global],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                if global_step % train_step['logging_step'] == 0:
                    batch_z = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

                    # Summary
                    _, k, m_global, d_loss, g_loss, summary = s.run(
                        [model.k_update, model.k, model.m_global, model.d_loss, model.g_loss, model.merged],
                        feed_dict={
                            model.x: batch_x,
                            model.z: batch_z,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Epoch %04d Step %07d =>" % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                        " k : {:.8f}".format(k),
                        " M : {:.8f}".format(m_global),
                    )

                    # Summary saver
                    model.writer.add_summary(summary, epoch)

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(-1.0, 1.0, [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(
                        model.g,
                        feed_dict={
                            model.z: sample_z,
                        },
                    )

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{0}_{1}.png'.format(epoch, global_step)

                    # Generated image save
                    iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir)

                    # Model save
                    model.saver.save(s, results['model'], global_step=global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #10
0
def main():
    start_time = time.time()  # Clocking start

    # Celeb-A DataSet images
    ds = DataSet(
        input_height=1024,
        input_width=1024,
        input_channel=3,
        ds_type="CelebA-HQ",
        ds_path="/home/zero/hdd/DataSet/CelebA-HQ",
    ).images
    n_ds = 30000
    dataset_iter = DataIterator(ds,
                                None,
                                train_step['batch_size'],
                                label_off=True)

    rnd = random.randint(0, n_ds)
    sample_x = ds[rnd]
    sample_x = np.reshape(sample_x, [-1, 1024, 1024, 3])

    # Export real image
    valid_image_height = 1
    valid_image_width = 1
    sample_dir = results['output'] + 'valid.png'

    # Generated image save
    iu.save_images(sample_x,
                   size=[valid_image_height, valid_image_width],
                   image_path=sample_dir,
                   inv_type='127')
    print("[+] sample image saved!")

    print("[+] pre-processing took {:.8f}s".format(time.time() - start_time))

    # GPU configure
    gpu_config = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_config)

    for idx, n_pg in enumerate(pg):

        with tf.Session(config=config) as s:
            pg_t = False if idx % 2 == 0 else True

            # PGGAN Model
            model = pggan.PGGAN(s, pg=n_pg, pg_t=pg_t)  # PGGAN

            # Initializing
            s.run(tf.global_variables_initializer())

            if not n_pg == 1 and not n_pg == 7:
                if pg_t:
                    model.r_saver.restore(
                        s, results['model'] + '%d-%d.ckpt' % (idx, r_pg[idx]))
                    model.out_saver.restore(
                        s, results['model'] + '%d-%d.ckpt' % (idx, r_pg[idx]))
                else:
                    model.saver.restore(
                        s, results['model'] + '%d-%d.ckpt' % (idx, r_pg[idx]))

            global_step = 0
            for epoch in range(train_step['epoch']):
                # Later, adding n_critic for optimizing D net
                for batch_images in dataset_iter.iterate():
                    batch_x = np.reshape(batch_images, (-1, 128, 128, 3))
                    batch_x = (batch_x + 1.0) * 127.5  # re-scaling to (0, 255)
                    batch_x = image_resize(batch_x, s=model.output_size)
                    batch_x = (batch_x / 127.5) - 1.0  # re-scaling to (-1, 1)
                    batch_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.batch_size, model.z_dim]).astype(np.float32)

                    if pg_t and not pg == 0:
                        alpha = global_step / 32000.0
                        low_batch_x = zoom(batch_x, zoom=[1.0, 0.5, 0.5, 1.0])
                        low_batch_x = zoom(low_batch_x,
                                           zoom=[1.0, 2.0, 2.0, 1.0])
                        batch_x = alpha * batch_x + (1.0 - alpha) * low_batch_x

                    # Update D network
                    _, d_loss = s.run([model.d_op, model.d_loss],
                                      feed_dict={
                                          model.x: batch_x,
                                          model.z: batch_z,
                                      })

                    # Update G network
                    _, g_loss = s.run([model.g_op, model.g_loss],
                                      feed_dict={
                                          model.z: batch_z,
                                      })

                    # Update alpha_trans
                    s.run(model.alpha_trans_update,
                          feed_dict={model.step_pl: global_step})

                    if global_step % train_step['logging_step'] == 0:
                        gp, d_loss, g_loss, summary = s.run(
                            [
                                model.gp, model.d_loss, model.g_loss,
                                model.merged
                            ],
                            feed_dict={
                                model.x: batch_x,
                                model.z: batch_z,
                            },
                        )

                        # Print loss
                        print(
                            "[+] PG %d Epoch %03d Step %07d =>" %
                            (n_pg, epoch, global_step),
                            " D loss : {:.6f}".format(d_loss),
                            " G loss : {:.6f}".format(g_loss),
                            " GP     : {:.6f}".format(gp),
                        )

                        # Summary saver
                        model.writer.add_summary(summary, global_step)

                        # Training G model with sample image and noise
                        sample_z = np.random.uniform(
                            -1.0, 1.0,
                            [model.sample_num, model.z_dim]).astype(np.float32)

                        samples = s.run(model.g,
                                        feed_dict={
                                            model.z: sample_z,
                                        })
                        samples = np.clip(samples, -1, 1)

                        # Export image generated by model G
                        sample_image_height = 1
                        sample_image_width = 1
                        sample_dir = results[
                            'output'] + 'train_{0}.png'.format(global_step)

                        # Generated image save
                        iu.save_images(
                            samples,
                            size=[sample_image_height, sample_image_width],
                            image_path=sample_dir,
                            inv_type='127',
                        )

                        # Model save
                        model.saver.save(s,
                                         results['model'] + '%d-%d.ckpt' %
                                         (idx, n_pg),
                                         global_step=global_step)

                    global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))
def main():
    start_time = time.time()  # Clocking start

    # loading CelebA DataSet
    ds = DataSet(
        height=64,
        width=64,
        channel=3,
        ds_image_path="D:/DataSet/CelebA/CelebA-64.h5",
        ds_label_path="D:/DataSet/CelebA/Anno/list_attr_celeba.txt",
        # ds_image_path="D:/DataSet/CelebA/Img/img_align_celeba/",
        ds_type="CelebA",
        use_save=False,
        save_file_name="D:/DataSet/CelebA/CelebA-64.h5",
        save_type="to_h5",
        use_img_scale=False,
        img_scale="-1,1",
    )

    # saving sample images
    test_images = np.reshape(iu.transform(ds.images[:100], inv_type='127'),
                             (100, 64, 64, 3))
    iu.save_images(test_images,
                   size=[10, 10],
                   image_path=results['output'] + 'sample.png',
                   inv_type='127')

    ds_iter = DataIterator(x=ds.images,
                           y=None,
                           batch_size=train_step['batch_size'],
                           label_off=True)

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # MAGAN Model
        model = magan.MAGAN(s)

        # Initializing
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %s" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        n_steps = ds.num_images // model.batch_size  # training set size

        # Pre-Train
        print("[*] pre-training - getting proper Margin")

        margin = 0  # 3.0585415484215974
        if margin == 0:
            sum_d_loss = 0.0
            for i in range(2):
                for batch_x in ds_iter.iterate():
                    batch_x = np.reshape(
                        iu.transform(batch_x, inv_type='127'),
                        (model.batch_size, model.height, model.width,
                         model.channel),
                    )
                    batch_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.batch_size, model.z_dim]).astype(np.float32)

                    _, d_real_loss = s.run(
                        [model.d_op, model.d_real_loss],
                        feed_dict={
                            model.x: batch_x,
                            model.z: batch_z,
                            model.m: 0.0,
                        },
                    )
                    sum_d_loss += d_real_loss

                print("[*] Epoch {:1d} Sum of d_real_loss : {:.8f}".format(
                    i + 1, sum_d_loss))

            # Initial margin value
            margin = sum_d_loss / n_steps

        print("[+] Margin : {0}".format(margin))

        old_margin = margin
        s_g_0 = np.inf  # Sg_0 = infinite

        global_step = saved_global_step
        start_epoch = global_step // (ds.num_images // model.batch_size
                                      )  # recover n_epoch
        ds_iter.pointer = saved_global_step % (
            ds.num_images // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epochs']):
            s_d, s_g = 0.0, 0.0
            for batch_x in ds_iter.iterate():
                batch_x = iu.transform(batch_x, inv_type='127')
                batch_x = np.reshape(batch_x, (model.batch_size, model.height,
                                               model.width, model.channel))
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                # Update D network
                _, d_loss, d_real_loss = s.run(
                    [model.d_op, model.d_loss, model.d_real_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                        model.m: margin,
                    },
                )

                # Update D real sample
                s_d += np.sum(d_real_loss)

                # Update G network
                _, g_loss, d_fake_loss = s.run(
                    [model.g_op, model.g_loss, model.d_fake_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                        model.m: margin,
                    },
                )

                # Update G fake sample
                s_g += np.sum(d_fake_loss)

                # Logging
                if global_step % train_step['logging_interval'] == 0:
                    summary = s.run(
                        model.merged,
                        feed_dict={
                            model.x: batch_x,
                            model.z: batch_z,
                            model.m: margin,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Epoch %03d Global Step %05d => " %
                        (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                    )

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(
                        model.g,
                        feed_dict={
                            model.z: sample_z,
                            model.m: margin,
                        },
                    )

                    # Summary saver
                    model.writer.add_summary(summary, global_step)

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{:08d}.png'.format(
                        global_step)

                    # Generated image save
                    iu.save_images(
                        samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_dir,
                        inv_type='127')

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                global_step += 1

            # Update margin
            if s_d / n_steps < margin and s_d < s_g and s_g_0 <= s_g:
                margin = s_d / n_steps
                print("[*] Margin updated from {:8f} to {:8f}".format(
                    old_margin, margin))
                old_margin = margin

            s_g_0 = s_g

            # Convergence Measure
            e_d = s_d / n_steps
            e_g = s_g / n_steps
            l_ = e_d + np.abs(e_d - e_g)

            print("[+] Epoch %03d " % epoch, " L : {:.8f}".format(l_))

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
def main():
    start_time = time.time()  # Clocking start

    # MNIST Dataset load
    mnist = DataSet(ds_path="D:/DataSet/mnist/").data

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # GAN Model
        model = gan.GAN(s)

        # Initializing
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %s" % saved_global_step, " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        d_loss = 0.0
        d_overpowered = False
        for global_step in range(saved_global_step, train_step['global_step']):
            batch_x, _ = mnist.train.next_batch(model.batch_size)
            batch_x = batch_x.reshape(-1, model.n_input)
            batch_z = np.random.uniform(-1.0, 1.0, size=[model.batch_size, model.z_dim]).astype(np.float32)

            # Update D network
            if not d_overpowered:
                _, d_loss = s.run(
                    [model.d_op, model.d_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

            # Update G network
            _, g_loss = s.run(
                [model.g_op, model.g_loss],
                feed_dict={
                    model.x: batch_x,
                    model.z: batch_z,
                },
            )

            d_overpowered = d_loss < (g_loss / 2.0)

            if global_step % train_step['logging_interval'] == 0:
                batch_x, _ = mnist.test.next_batch(model.batch_size)
                batch_z = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

                d_loss, g_loss, summary = s.run(
                    [model.d_loss, model.g_loss, model.merged],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                d_overpowered = d_loss < (g_loss / 2.0)

                # Print loss
                print(
                    "[+] Step %08d => " % global_step,
                    " D loss : {:.8f}".format(d_loss),
                    " G loss : {:.8f}".format(g_loss),
                )

                # Training G model with sample image and noise
                sample_z = np.random.uniform(-1.0, 1.0, [model.sample_num, model.z_dim]).astype(np.float32)
                samples = s.run(
                    model.g,
                    feed_dict={
                        model.z: sample_z,
                    },
                )

                samples = np.reshape(samples, [-1, model.height, model.width, model.channel])

                # Summary saver
                model.writer.add_summary(summary, global_step)

                # Export image generated by model G
                sample_image_height = model.sample_size
                sample_image_width = model.sample_size
                sample_dir = results['output'] + 'train_{:08d}.png'.format(global_step)

                # Generated image save
                iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir)

                # Model save
                model.saver.save(s, results['model'], global_step)

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))  # took about 370s on my machine

    # Close tf.Session
    s.close()
Exemple #13
0
def main():
    start_time = time.time()  # Clocking start

    # Training, test data set
    ds = DataSet(height=32, width=32, channel=3, ds_path='D:\\DataSet/cifar/cifar-10-batches-py/', ds_name='cifar-10')

    ds_iter = DataIterator(ds.train_images, ds.train_labels, train_step['batch_size'])

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # LAPGAN model
        model = lapgan.LAPGAN(s, batch_size=train_step['batch_size'])

        # Initializing variables
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %s" % saved_global_step, " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        sample_y = np.zeros(shape=[model.sample_num, model.n_classes])
        for i in range(10):
            sample_y[10 * i : 10 * (i + 1), i] = 1

        global_step = saved_global_step
        start_epoch = global_step // (len(ds.train_images) // model.batch_size)  # recover n_epoch
        ds_iter.pointer = saved_global_step % (len(ds.train_images) // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epoch']):
            for batch_images, batch_labels in ds_iter.iterate():
                batch_x = iu.transform(batch_images, inv_type='127')

                z = []
                for i in range(3):
                    z.append(np.random.uniform(-1.0, 1.0, [train_step['batch_size'], model.z_noises[i]]))

                # Update D/G networks
                (
                    img_fake,
                    img_coarse,
                    d_loss_1,
                    g_loss_1,
                    _,
                    _,
                    _,
                    d_loss_2,
                    g_loss_2,
                    _,
                    _,
                    d_loss_3,
                    g_loss_3,
                    _,
                    _,
                    _,
                    _,
                    _,
                    _,
                ) = s.run(
                    [
                        model.g[0],
                        model.x1_coarse,
                        model.d_loss[0],
                        model.g_loss[0],
                        model.x2_fine,
                        model.g[1],
                        model.x2_coarse,
                        model.d_loss[1],
                        model.g_loss[1],
                        model.x3_fine,
                        model.g[2],
                        model.d_loss[2],
                        model.g_loss[2],
                        model.d_op[0],
                        model.g_op[0],
                        model.d_op[1],
                        model.g_op[1],
                        model.d_op[2],
                        model.g_op[2],
                    ],
                    feed_dict={
                        model.x1_fine: batch_x,  # images
                        model.y: batch_labels,  # classes
                        model.z[0]: z[0],
                        model.z[1]: z[1],
                        model.z[2]: z[2],  # z-noises
                        model.do_rate: 0.5,
                    },
                )

                # Logging
                if global_step % train_step['logging_interval'] == 0:
                    batch_x = ds.test_images[np.random.randint(0, len(ds.test_images), model.sample_num)]
                    batch_x = iu.transform(batch_x, inv_type='127')

                    z = []
                    for i in range(3):
                        z.append(np.random.uniform(-1.0, 1.0, [model.sample_num, model.z_noises[i]]))

                    # Update D/G networks
                    (
                        img_fake,
                        img_coarse,
                        d_loss_1,
                        g_loss_1,
                        _,
                        _,
                        _,
                        d_loss_2,
                        g_loss_2,
                        _,
                        _,
                        d_loss_3,
                        g_loss_3,
                        _,
                        _,
                        _,
                        _,
                        _,
                        _,
                        summary,
                    ) = s.run(
                        [
                            model.g[0],
                            model.x1_coarse,
                            model.d_loss[0],
                            model.g_loss[0],
                            model.x2_fine,
                            model.g[1],
                            model.x2_coarse,
                            model.d_loss[1],
                            model.g_loss[1],
                            model.x3_fine,
                            model.g[2],
                            model.d_loss[2],
                            model.g_loss[2],
                            model.d_op[0],
                            model.g_op[0],
                            model.d_op[1],
                            model.g_op[1],
                            model.d_op[2],
                            model.g_op[2],
                            model.merged,
                        ],
                        feed_dict={
                            model.x1_fine: batch_x,  # images
                            model.y: sample_y,  # classes
                            model.z[0]: z[0],
                            model.z[1]: z[1],
                            model.z[2]: z[2],  # z-noises
                            model.do_rate: 0.0,
                        },
                    )

                    # Print loss
                    d_loss = (d_loss_1 + d_loss_2 + d_loss_3) / 3.0
                    g_loss = (g_loss_1 + g_loss_2 + g_loss_3) / 3.0
                    print(
                        "[+] Epoch %03d Step %05d => " % (epoch, global_step),
                        " Avg D loss : {:.8f}".format(d_loss),
                        " Avg G loss : {:.8f}".format(g_loss),
                    )

                    # Training G model with sample image and noise
                    samples = img_fake + img_coarse

                    # Summary saver
                    model.writer.add_summary(summary, global_step)  # time saving

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{0}.png'.format(global_step)

                    # Generated image save
                    iu.save_images(
                        samples, size=[sample_image_height, sample_image_width], image_path=sample_dir, inv_type='127'
                    )

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                global_step += 1

        end_time = time.time() - start_time  # Clocking end

        # Elapsed time
        print("[+] Elapsed time {:.8f}s".format(end_time))

        # Close tf.Session
        s.close()
Exemple #14
0
def main():
    start_time = time.time()  # Clocking start

    # loading CelebA DataSet
    ds = DataSet(
        height=64,
        width=64,
        channel=3,
        ds_image_path="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5",
        ds_label_path="/home/zero/hdd/DataSet/CelebA/Anno/list_attr_celeba.txt",
        # ds_image_path="/home/zero/hdd/DataSet/CelebA/Img/img_align_celeba/",
        ds_type="CelebA",
        use_save=False,
        save_file_name="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5",
        save_type="to_h5",
        use_img_scale=False,
        # img_scale="-1,1"
    )

    # saving sample images
    test_images = np.reshape(iu.transform(ds.images[:16], inv_type='127'),
                             (16, 64, 64, 3))
    iu.save_images(test_images,
                   size=[4, 4],
                   image_path=results['output'] + 'sample.png',
                   inv_type='127')

    ds_iter = DataIterator(x=ds.images,
                           y=None,
                           batch_size=train_step['batch_size'],
                           label_off=True)

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # EBGAN Model
        model = ebgan.EBGAN(
            s, enable_pull_away=True)  # using pull away loss # EBGAN-PT

        # Initializing
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %s" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        global_step = saved_global_step
        start_epoch = global_step // (ds.num_images // model.batch_size
                                      )  # recover n_epoch
        ds_iter.pointer = saved_global_step % (
            ds.num_images // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epochs']):
            for batch_x in ds_iter.iterate():
                batch_x = iu.transform(batch_x, inv_type='127')
                batch_x = np.reshape(batch_x, (model.batch_size, model.height,
                                               model.width, model.channel))
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                _, d_loss = s.run(
                    [model.d_op, model.d_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                # Update G network
                _, g_loss = s.run(
                    [model.g_op, model.g_loss],
                    feed_dict={
                        model.z: batch_z,
                    },
                )

                # Logging
                if global_step % train_step['logging_interval'] == 0:
                    summary = s.run(
                        model.merged,
                        feed_dict={
                            model.x: batch_x,
                            model.z: batch_z,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Epoch %02d Step %08d => " % (epoch, global_step),
                        " D  loss : {:.8f}".format(d_loss),
                        " G  loss : {:.8f}".format(g_loss),
                    )

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(
                        model.g,
                        feed_dict={
                            model.z: sample_z,
                        },
                    )

                    # Summary saver
                    model.writer.add_summary(summary, global_step)

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{:08d}.png'.format(
                        global_step)

                    # Generated image save
                    iu.save_images(
                        samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_dir,
                        inv_type='127')

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
def main():
    start_time = time.time()  # Clocking start

    # Loading Cifar-10 DataSet
    ds = DataSet(height=32,
                 width=32,
                 channel=3,
                 ds_path="D:/DataSet/cifar/cifar-10-batches-py/",
                 ds_name='cifar-10')

    ds_iter = DataIterator(
        x=iu.transform(ds.train_images, '127'),
        y=ds.train_labels,
        batch_size=train_step['batch_size'],
        label_off=False
    )  # using label # maybe someday, i'll change this param's name

    # Generated image save
    test_images = iu.transform(ds.test_images[:100], inv_type='127')
    iu.save_images(test_images,
                   size=[10, 10],
                   image_path=results['output'] + 'sample.png',
                   inv_type='127')

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # ACGAN Model
        model = acgan.ACGAN(s,
                            batch_size=train_step['batch_size'],
                            n_classes=ds.n_classes)

        # Initializing
        s.run(tf.global_variables_initializer())

        sample_y = np.zeros(shape=[model.sample_num, model.n_classes])
        for i in range(10):
            sample_y[10 * i:10 * (i + 1), i] = 1

        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        global_step = saved_global_step
        start_epoch = global_step // (len(ds.train_images) // model.batch_size
                                      )  # recover n_epoch
        ds_iter.pointer = saved_global_step % (
            len(ds.train_images) // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epochs']):
            for batch_x, batch_y in ds_iter.iterate():
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                # Update D network
                _, d_loss = s.run(
                    [model.d_op, model.d_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.y: batch_y,
                        model.z: batch_z,
                    },
                )

                # Update G/C networks
                _, g_loss, _, c_loss = s.run(
                    [model.g_op, model.g_loss, model.c_op, model.c_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.y: batch_y,
                        model.z: batch_z,
                    },
                )

                if global_step % train_step['logging_interval'] == 0:
                    batch_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.batch_size, model.z_dim]).astype(np.float32)

                    d_loss, g_loss, c_loss, summary = s.run(
                        [
                            model.d_loss, model.g_loss, model.c_loss,
                            model.merged
                        ],
                        feed_dict={
                            model.x: batch_x,
                            model.y: batch_y,
                            model.z: batch_z,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Epoch %04d Step %08d => " % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                        " C loss : {:.8f}".format(c_loss),
                    )

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(
                        model.g,
                        feed_dict={
                            model.y: sample_y,
                            model.z: sample_z,
                        },
                    )

                    # Summary saver
                    model.writer.add_summary(summary, global_step)

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{:08d}.png'.format(
                        global_step)

                    # Generated image save
                    iu.save_images(
                        samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_dir,
                        inv_type='127')

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #16
0
def main():
    start_time = time.time()  # Clocking start

    # loading CelebA DataSet
    labels = [
        'Black_Hair',
        'Blond_Hair',
        'Blurry',
        'Eyeglasses',
        'Gray_Hair',
        'Male',
        'Smiling',
        'Wavy_Hair',
        'Wearing_Hat',
        'Young',
    ]

    ds = DataSet(
        height=64,
        width=64,
        channel=3,
        ds_image_path="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5",
        ds_label_path="/home/zero/hdd/DataSet/CelebA/Anno/list_attr_celeba.txt",
        attr_labels=labels,
        # ds_image_path="D:\\DataSet/CelebA/Img/img_align_celeba/",
        ds_type="CelebA",
        use_save=False,
        save_file_name="D:\\DataSet/CelebA/CelebA-64.h5",
        save_type="to_h5",
        use_img_scale=False,
        # img_scale="-1,1"
    )

    # saving sample images
    test_images = np.reshape(iu.transform(ds.images[:16], inv_type='127'),
                             (16, 64, 64, 3))
    iu.save_images(test_images,
                   size=[4, 4],
                   image_path=results['output'] + 'sample.png',
                   inv_type='127')

    ds_iter = DataIterator(x=ds.images,
                           y=ds.labels,
                           batch_size=train_step['batch_size'],
                           label_off=False)

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # InfoGAN Model
        model = infogan.InfoGAN(s,
                                height=64,
                                width=64,
                                channel=3,
                                batch_size=train_step['batch_size'],
                                n_categories=len(ds.labels))
        # fixed z-noise
        sample_z = np.random.uniform(
            -1.0, 1.0, [model.sample_num, model.z_dim]).astype(np.float32)

        # Initializing
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %s" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        global_step = saved_global_step
        start_epoch = global_step // (ds.num_images // model.batch_size
                                      )  # recover n_epoch
        ds_iter.pointer = saved_global_step % (
            ds.num_images // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epochs']):
            for batch_x, batch_y in ds_iter.iterate():
                batch_x = iu.transform(batch_x, inv_type='127')
                batch_x = np.reshape(batch_x, (model.batch_size, model.height,
                                               model.width, model.channel))

                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                batch_z_con = gen_continuous(model.batch_size,
                                             model.n_continous_factor)
                batch_z_cat = gen_category(model.batch_size,
                                           model.n_categories)
                batch_c = np.concatenate((batch_z_con, batch_z_cat), axis=1)

                # Update D network
                _, d_loss = s.run([model.d_op, model.d_loss],
                                  feed_dict={
                                      model.c: batch_c,
                                      model.x: batch_x,
                                      model.z: batch_z,
                                  })

                # Update G network
                _, g_loss = s.run([model.g_op, model.g_loss],
                                  feed_dict={
                                      model.c: batch_c,
                                      model.x: batch_x,
                                      model.z: batch_z,
                                  })

                # Logging
                if global_step % train_step['logging_interval'] == 0:
                    summary = s.run(model.merged,
                                    feed_dict={
                                        model.c: batch_c,
                                        model.x: batch_x,
                                        model.z: batch_z,
                                    })

                    # Print loss
                    print(
                        "[+] Epoch %02d Step %08d => " % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                    )

                    # Training G model with sample image and noise
                    sample_z_con = np.zeros(
                        (model.sample_num, model.n_continous_factor))
                    for i in range(10):
                        sample_z_con[10 * i:10 * (i + 1),
                                     0] = np.linspace(-2, 2, 10)

                    sample_z_cat = np.zeros(
                        (model.sample_num, model.n_categories))
                    for i in range(10):
                        sample_z_cat[10 * i:10 * (i + 1), i] = 1

                    sample_c = np.concatenate((sample_z_con, sample_z_cat),
                                              axis=1)

                    samples = s.run(model.g,
                                    feed_dict={
                                        model.c: sample_c,
                                        model.z: sample_z,
                                    })

                    # Summary saver
                    model.writer.add_summary(summary, global_step)

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{:08d}.png'.format(
                        global_step)

                    # Generated image save
                    iu.save_images(
                        samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_dir,
                        inv_type='127')

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #17
0
def main():
    start_time = time.time()  # Clocking start

    # Div2K - Track 1: Bicubic downscaling - x4 DataSet load
    """
    ds = DataSet(ds_path="/home/zero/hdd/DataSet/DIV2K/",
                 ds_name="X4",
                 use_save=True,
                 save_type="to_h5",
                 save_file_name="/home/zero/hdd/DataSet/DIV2K/DIV2K",
                 use_img_scale=True)
    """
    ds = DataSet(
        ds_hr_path="/home/zero/hdd/DataSet/DIV2K/DIV2K-hr.h5",
        ds_lr_path="/home/zero/hdd/DataSet/DIV2K/DIV2K-lr.h5",
        use_img_scale=True,
    )

    hr, lr = ds.hr_images, ds.lr_images

    print("[+] Loaded HR image ", hr.shape)
    print("[+] Loaded LR image ", lr.shape)

    # GPU configure
    gpu_config = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(allow_soft_placement=True,
                            log_device_placement=False,
                            gpu_options=gpu_config)

    with tf.Session(config=config) as s:
        with tf.device("/gpu:1"):  # Change
            # SRGAN Model
            model = srgan.SRGAN(s,
                                batch_size=train_step['batch_size'],
                                use_vgg19=False)

        # Initializing
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % global_step, " successfully loaded")
        else:
            global_step = 0
            print('[-] No checkpoint file found')

        start_epoch = global_step // (ds.n_images // train_step['batch_size'])

        rnd = np.random.randint(0, ds.n_images)
        sample_x_hr, sample_x_lr = hr[rnd], lr[rnd]

        sample_x_hr, sample_x_lr = (
            np.reshape(sample_x_hr, [1] + model.hr_image_shape[1:]),
            np.reshape(sample_x_lr, [1] + model.lr_image_shape[1:]),
        )

        # Export real image
        # valid_image_height = model.sample_size
        # valid_image_width = model.sample_size
        sample_hr_dir, sample_lr_dir = results[
            'output'] + 'valid_hr.png', results['output'] + 'valid_lr.png'

        # Generated image save
        iu.save_images(sample_x_hr,
                       size=[1, 1],
                       image_path=sample_hr_dir,
                       inv_type='127')

        iu.save_images(sample_x_lr,
                       size=[1, 1],
                       image_path=sample_lr_dir,
                       inv_type='127')

        learning_rate = 1e-4
        for epoch in range(start_epoch, train_step['train_epochs']):
            pointer = 0
            for i in range(ds.n_images // train_step['batch_size']):
                start = pointer
                pointer += train_step['batch_size']

                if pointer > ds.n_images:  # if 1 epoch is ended
                    # Shuffle training DataSet
                    perm = np.arange(ds.n_images)
                    np.random.shuffle(perm)

                    hr, lr = hr[perm], lr[perm]

                    start = 0
                    pointer = train_step['batch_size']

                end = pointer

                batch_x_hr, batch_x_lr = hr[start:end], lr[start:end]

                # reshape
                batch_x_hr = np.reshape(batch_x_hr,
                                        [train_step['batch_size']] +
                                        model.hr_image_shape[1:])
                batch_x_lr = np.reshape(batch_x_lr,
                                        [train_step['batch_size']] +
                                        model.lr_image_shape[1:])

                # Update Only G network
                d_loss, g_loss, g_init_loss = 0.0, 0.0, 0.0
                if epoch <= train_step['init_epochs']:
                    _, g_init_loss = s.run(
                        [model.g_init_op, model.g_cnt_loss],
                        feed_dict={
                            model.x_hr: batch_x_hr,
                            model.x_lr: batch_x_lr,
                            model.lr: learning_rate,
                        },
                    )
                # Update G/D network
                else:
                    _, d_loss = s.run(
                        [model.d_op, model.d_loss],
                        feed_dict={
                            model.x_hr: batch_x_hr,
                            model.x_lr: batch_x_lr,
                            model.lr: learning_rate,
                        },
                    )

                    _, g_loss = s.run(
                        [model.g_op, model.g_loss],
                        feed_dict={
                            model.x_hr: batch_x_hr,
                            model.x_lr: batch_x_lr,
                            model.lr: learning_rate,
                        },
                    )

                if i % train_step['logging_interval'] == 0:
                    # Print loss
                    if epoch <= train_step['init_epochs']:
                        print(
                            "[+] Epoch %04d Step %08d => " %
                            (epoch, global_step),
                            " MSE loss : {:.8f}".format(g_init_loss),
                        )
                    else:
                        print(
                            "[+] Epoch %04d Step %08d => " %
                            (epoch, global_step),
                            " D loss : {:.8f}".format(d_loss),
                            " G loss : {:.8f}".format(g_loss),
                        )

                        summary = s.run(
                            model.merged,
                            feed_dict={
                                model.x_hr: batch_x_hr,
                                model.x_lr: batch_x_lr,
                                model.lr: learning_rate,
                            },
                        )

                        # Summary saver
                        model.writer.add_summary(summary, global_step)

                    # Training G model with sample image and noise
                    sample_x_lr = np.reshape(sample_x_lr, [model.sample_num] +
                                             model.lr_image_shape[1:])
                    samples = s.run(
                        model.g,
                        feed_dict={
                            model.x_lr: sample_x_lr,
                            model.lr: learning_rate,
                        },
                    )

                    # Export image generated by model G
                    # sample_image_height = model.output_height
                    # sample_image_width = model.output_width
                    sample_dir = results['output'] + 'train_{:08d}.png'.format(
                        global_step)

                    # Generated image save
                    iu.save_images(samples,
                                   size=[1, 1],
                                   image_path=sample_dir,
                                   inv_type='127')

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                # Learning Rate update
                if epoch and epoch % model.lr_update_epoch == 0:
                    learning_rate *= model.lr_decay_rate
                    learning_rate = max(learning_rate, model.lr_low_boundary)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
def main():
    start_time = time.time()  # Clocking start

    # Training, Test data set
    # loading Cifar DataSet
    ds = DataSet(height=32, width=32, channel=3, ds_path='D:\\DataSet/cifar/cifar-10-batches-py/', ds_name='cifar-10')

    # saving sample images
    test_images = np.reshape(iu.transform(ds.test_images[:16], inv_type='127'), (16, 32, 32, 3))
    iu.save_images(test_images, size=[4, 4], image_path=results['output'] + 'sample.png', inv_type='127')

    ds_iter = DataIterator(x=ds.train_images, y=None, batch_size=train_step['batch_size'], label_off=True)

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # GAN Model
        model = lsgan.LSGAN(s, train_step['batch_size'])

        # Initializing variables
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0

        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % saved_global_step, " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        global_step = saved_global_step
        start_epoch = global_step // (len(ds.train_images) // model.batch_size)
        ds_iter.pointer = saved_global_step % (len(ds.train_images) // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epoch']):
            for batch_x in ds_iter.iterate():
                batch_x = iu.transform(batch_x, inv_type='127')
                batch_x = np.reshape(batch_x, [-1] + model.image_shape[1:])
                batch_z = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

                # Update D network
                _, d_loss = s.run([model.d_op, model.d_loss], feed_dict={model.x: batch_x, model.z: batch_z})

                # Update G network
                _, g_loss = s.run([model.g_op, model.g_loss], feed_dict={model.x: batch_x, model.z: batch_z})

                # Logging
                if global_step % train_step['logging_interval'] == 0:
                    d_loss, g_loss, summary = s.run(
                        [model.d_loss, model.g_loss, model.merged], feed_dict={model.x: batch_x, model.z: batch_z}
                    )

                    # Print loss
                    print(
                        "[+] Epoch %02d Step %08d => " % (epoch, global_step),
                        " D loss : {:.8f}".format(d_loss),
                        " G loss : {:.8f}".format(g_loss),
                    )

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(-1.0, 1.0, [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(model.g, feed_dict={model.z: sample_z,})

                    # Summary saver
                    model.writer.add_summary(summary, global_step)

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{:08d}.png'.format(global_step)

                    # Generated image save
                    iu.save_images(
                        samples, size=[sample_image_height, sample_image_width], image_path=sample_dir, inv_type='127'
                    )

                    # Model save
                    model.saver.save(s, results['model'], global_step)

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #19
0
def main():
    start_time = time.time()  # Clocking start

    # Loading MNIST DataSet
    mnist = DataSet(ds_path="D:\\DataSet/mnist/").data

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    idx = 1
    divergences = [
        'GAN',
        'KL',
        'Reverse-KL',
        'JS',
        'JS-Weighted',
        'Squared-Hellinger',
        'Pearson',
        'Neyman',
        'Jeffrey',
        'Total-Variation',
    ]
    assert 0 <= idx < len(divergences)

    results['output'] += '%s/' % divergences[idx]
    results['model'] += '%s/fGAN-model.ckpt' % divergences[idx]

    with tf.Session(config=config) as s:
        # f-GAN model
        model = fgan.FGAN(s,
                          batch_size=train_step['batch_size'],
                          divergence_method=divergences[idx],
                          use_tricky_g_loss=True)

        # Initializing variables
        s.run(tf.global_variables_initializer())

        # Load model & Graph & Weights
        saved_global_step = 0

        ckpt = tf.train.get_checkpoint_state('./model/%s/' % divergences[idx])
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        for global_step in range(saved_global_step,
                                 train_step['global_steps']):
            batch_x, _ = mnist.train.next_batch(model.batch_size)
            batch_z = np.random.uniform(
                -1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

            # Update D network
            _, d_loss = s.run([model.d_op, model.d_loss],
                              feed_dict={
                                  model.x: batch_x,
                                  model.z: batch_z,
                              })

            # Update G network
            _, g_loss = s.run([model.g_op, model.g_loss],
                              feed_dict={
                                  model.x: batch_x,
                                  model.z: batch_z,
                              })

            if global_step % train_step['logging_interval'] == 0:
                summary = s.run(model.merged,
                                feed_dict={
                                    model.x: batch_x,
                                    model.z: batch_z,
                                })

                # Print loss
                print(
                    "[+] Global step %06d => " % global_step,
                    " D loss : {:.8f}".format(d_loss),
                    " G loss : {:.8f}".format(g_loss),
                )

                # Training G model with sample image and noise
                sample_z = np.random.uniform(-1.0, 1.0,
                                             [model.sample_num, model.z_dim])
                samples = s.run(model.g, feed_dict={
                    model.z: sample_z,
                })
                samples = np.reshape(samples, (-1, 28, 28, 1))

                # Summary saver
                model.writer.add_summary(summary, global_step)

                # Export image generated by model G
                sample_image_height = model.sample_size
                sample_image_width = model.sample_size
                sample_dir = results['output'] + 'train_{0}.png'.format(
                    global_step)

                # Generated image save
                iu.save_images(samples,
                               size=[sample_image_height, sample_image_width],
                               image_path=sample_dir,
                               inv_type='255')

                # Model save
                model.saver.save(s, results['model'], global_step)

        end_time = time.time() - start_time  # Clocking end

        # Elapsed time
        print("[+] Elapsed time {:.8f}s".format(end_time))

        # Close tf.Session
        s.close()
Exemple #20
0
def main():
    start_time = time.time()  # Clocking start

    # MNIST Dataset load
    mnist = DataSet(ds_path="./").data

    # GPU configure
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as s:
        # SGAN Model
        model = sgan.SGAN(s)

        # Initializing
        s.run(tf.global_variables_initializer())

        sample_x, sample_y = mnist.test.next_batch(model.sample_num)
        # sample_x = np.reshape(sample_x, [model.sample_num, model.n_input])

        d_overpowered = False
        for step in range(train_step['global_step']):
            batch_x, batch_y = mnist.train.next_batch(model.batch_size)
            # batch_x = np.reshape(batch_x, [model.batch_size, model.n_input])
            batch_z_0 = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)
            batch_z_1 = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

            # Update D network
            if not d_overpowered:
                _, d_0_loss, _, _ = s.run(
                    [model.d_0_op, model.d_0_loss, model.d_1_op, model.d_1_loss],
                    feed_dict={model.x: batch_x, model.y: batch_y, model.z_1: batch_z_1, model.z_0: batch_z_0,},
                )

            # Update G network
            _, g_0_loss, _, _ = s.run(
                [model.g_0_op, model.g_0_loss, model.g_1_op, model.g_1_loss],
                feed_dict={model.x: batch_x, model.y: batch_y, model.z_1: batch_z_1, model.z_0: batch_z_0,},
            )

            d_overpowered = d_0_loss < g_0_loss / 2

            if step % train_step['logging_interval'] == 0:
                batch_x, batch_y = mnist.train.next_batch(model.batch_size)
                # batch_x = np.reshape(batch_x, [model.batch_size, model.n_input])
                batch_z_0 = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)
                batch_z_1 = np.random.uniform(-1.0, 1.0, [model.batch_size, model.z_dim]).astype(np.float32)

                d_0_loss, _, g_0_loss, _, summary = s.run(
                    [model.d_0_loss, model.d_1_loss, model.g_0_loss, model.g_1_loss, model.merged],
                    feed_dict={model.x: batch_x, model.y: batch_y, model.z_1: batch_z_1, model.z_0: batch_z_0,},
                )

                d_overpowered = d_0_loss < g_0_loss / 2

                # Print loss
                print(
                    "[+] Step %08d => " % step, " D loss : {:.8f}".format(d_0_loss), " G loss : {:.8f}".format(g_0_loss)
                )

                # Training G model with sample image and noise
                sample_z_0 = np.random.uniform(-1.0, 1.0, [model.sample_num, model.z_dim]).astype(np.float32)
                sample_z_1 = np.random.uniform(-1.0, 1.0, [model.sample_num, model.z_dim]).astype(np.float32)
                _, samples = s.run(
                    [model.g_1, model.g_0], feed_dict={model.y: sample_y, model.z_1: sample_z_1, model.z_0: sample_z_0,}
                )

                samples = np.reshape(samples, [model.batch_size] + model.image_shape)

                # Summary saver
                model.writer.add_summary(summary, step)

                # Export image generated by model G
                sample_image_height = model.sample_size
                sample_image_width = model.sample_size
                sample_dir = results['output'] + 'train_{:08d}.png'.format(step)

                # Generated image save
                iu.save_images(samples, size=[sample_image_height, sample_image_width], image_path=sample_dir)

                # Model save
                model.saver.save(s, results['model'], global_step=step)

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()
Exemple #21
0
def main():
    start_time = time.time()  # Clocking start

    # loading CelebA DataSet
    ds = DataSet(
        height=64,
        width=64,
        channel=3,
        ds_image_path="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5",
        ds_label_path="/home/zero/hdd/DataSet/CelebA/Anno/list_attr_celeba.txt",
        # ds_image_path="/home/zero/hdd/DataSet/CelebA/Img/img_align_celeba/",
        ds_type="CelebA",
        use_save=False,
        save_file_name="/home/zero/hdd/DataSet/CelebA/CelebA-64.h5",
        save_type="to_h5",
        use_img_scale=False,
        # img_scale="-1,1"
    )

    # saving sample images
    test_images = np.reshape(iu.transform(ds.images[:100], inv_type='127'),
                             (100, 64, 64, 3))
    iu.save_images(test_images,
                   size=[10, 10],
                   image_path=results['output'] + 'sample.png',
                   inv_type='127')

    ds_iter = DataIterator(x=ds.images,
                           y=None,
                           batch_size=train_step['batch_size'],
                           label_off=True)

    # GPU configure
    gpu_config = tf.GPUOptions(allow_growth=True)
    config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_config)

    with tf.Session(config=config) as s:
        # BEGAN Model
        model = began.BEGAN(s, batch_size=train_step['batch_size'],
                            gamma=0.5)  # BEGAN

        # Initializing
        s.run(tf.global_variables_initializer())

        print("[*] Reading checkpoints...")

        saved_global_step = 0
        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            model.saver.restore(s, ckpt.model_checkpoint_path)

            saved_global_step = int(
                ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            print("[+] global step : %d" % saved_global_step,
                  " successfully loaded")
        else:
            print('[-] No checkpoint file found')

        global_step = saved_global_step
        start_epoch = global_step // (ds.num_images // model.batch_size
                                      )  # recover n_epoch
        ds_iter.pointer = saved_global_step % (
            ds.num_images // model.batch_size)  # recover n_iter
        for epoch in range(start_epoch, train_step['epoch']):
            for batch_x in ds_iter.iterate():
                batch_x = iu.transform(batch_x, inv_type='127')
                batch_x = np.reshape(batch_x, (model.batch_size, model.height,
                                               model.width, model.channel))
                batch_z = np.random.uniform(
                    -1.0, 1.0,
                    [model.batch_size, model.z_dim]).astype(np.float32)

                # Update D network
                _, d_loss = s.run(
                    [model.d_op, model.d_loss],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                # Update G network
                _, g_loss = s.run(
                    [model.g_op, model.g_loss],
                    feed_dict={
                        model.z: batch_z,
                    },
                )

                # Update k_t
                _, k, m_global = s.run(
                    [model.k_update, model.k, model.m_global],
                    feed_dict={
                        model.x: batch_x,
                        model.z: batch_z,
                    },
                )

                if global_step % train_step['logging_step'] == 0:
                    summary = s.run(
                        model.merged,
                        feed_dict={
                            model.x: batch_x,
                            model.z: batch_z,
                        },
                    )

                    # Print loss
                    print(
                        "[+] Epoch %03d Step %07d =>" % (epoch, global_step),
                        " D loss : {:.6f}".format(d_loss),
                        " G loss : {:.6f}".format(g_loss),
                        " k : {:.6f}".format(k),
                        " M : {:.6f}".format(m_global),
                    )

                    # Summary saver
                    model.writer.add_summary(summary, global_step)

                    # Training G model with sample image and noise
                    sample_z = np.random.uniform(
                        -1.0, 1.0,
                        [model.sample_num, model.z_dim]).astype(np.float32)
                    samples = s.run(
                        model.g,
                        feed_dict={
                            model.z: sample_z,
                        },
                    )

                    # Export image generated by model G
                    sample_image_height = model.sample_size
                    sample_image_width = model.sample_size
                    sample_dir = results['output'] + 'train_{0}.png'.format(
                        global_step)

                    # Generated image save
                    iu.save_images(
                        samples,
                        size=[sample_image_height, sample_image_width],
                        image_path=sample_dir,
                        inv_type='127')

                    # Model save
                    model.saver.save(s,
                                     results['model'],
                                     global_step=global_step)

                # Learning Rate update
                if global_step and global_step % model.lr_update_step == 0:
                    s.run([model.g_lr_update, model.d_lr_update])

                global_step += 1

    end_time = time.time() - start_time  # Clocking end

    # Elapsed time
    print("[+] Elapsed time {:.8f}s".format(end_time))

    # Close tf.Session
    s.close()