Exemple #1
0
def main():

    mnist = input_data.read_data_sets('../data/MNIST_data', one_hot=True)

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

    with tf.Session(config=config) as s:

        # GAN Model
        model = cgan.CGAN(s, is_train=False)

        s.run(tf.global_variables_initializer())

        saver = tf.train.Saver()

        ckpt = tf.train.get_checkpoint_state('./model/')
        if ckpt and ckpt.model_checkpoint_path:
            ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
            saver.restore(s, os.path.join('./model/', ckpt_name))
        else:
            print("Cannot restore checkpoint!")
            return False

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

        # Create conditional one-hot vector, with index 5 = 1
        sample_y = np.zeros(shape=[model.sample_num, model.y_dim])
        sample_y[:, 5] = 1

        sample_x, _ = mnist.train.next_batch(model.sample_num)
        sample_x = np.reshape(
            sample_x,
            [-1, model.input_height, model.input_width, model.channel])

        samples = s.run(model.g,
                        feed_dict={
                            model.x: sample_x,
                            model.z: sample_z,
                            model.y: sample_y
                        })

        samples = np.reshape(
            samples,
            [-1, model.output_height, model.output_width, model.channel])

        # Export image generated by model G
        sample_image_height = model.sample_size
        sample_image_width = model.sample_size
        sample_dir = results['output'] + 'test.png'

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

    # Close tf.Session
    s.close()
Exemple #2
0
 def after_run(self, run_context, run_values):
     _ = run_context
     if self._should_trigger:
         self._timer.update_last_triggered_step(self._iter_count)
         # Save sample images, visualizing the current training results
         save_images(
             self.img_path + '_%02d_%04d.jpg' %
             (self.model.epoch_id, self.model.batch_id),
             run_values.results['sample_img'], self.grid_size)
         # save_images(self.img_path+'_%02d_%04d_out.jpg' % (self.model.epoch_id, self.model.batch_id),
         #             run_values.results['g_out'],
         #             self.grid_size)
         ## For checking. Save groundtruth (natuarl) training images.
         # save_images(self.img_path+'_%02d_%04d_gt.jpg' % (self.model.epoch_id, self.model.batch_id),
         #             run_values.results['gt_img'][:64],
         #             self.grid_size)
     self._iter_count += 1
Exemple #3
0
def main():
    start_time = time.time()  # Clocking start

    mnist = input_data.read_data_sets('../data/MNIST_data', one_hot=True)

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

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

        sample_z = np.random.uniform(-1., 1., [model.batch_size, model.z_dim]).astype(np.float32)
        
		sample_x, _ = mnist.train.next_batch(model.sample_num)

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

        samples = np.reshape(samples, [-1, model.output_height, model.output_width, model.channel])

        # 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'] + 'test_{: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)
        
	# Close tf.Session
	s.close()
Exemple #4
0
    def __call__(self, save_path):
        """
        ...
        """
        if not os.path.exists(save_path):
            os.makedirs(save_path)

        time_consumed_total = 0.
        for epoch in range(1, self.epochs + 1):
            start = time.time()
            total_gen_loss = 0
            total_disc_loss = 0
            for daily_parking_rate, condition in self.monthly_parking_rate:
                gen_loss, disc_loss = self.train_step(daily_parking_rate,
                                                      condition)
                total_gen_loss += gen_loss
                total_disc_loss += disc_loss

            time_consumed = time.time() - start
            time_consumed_total += time_consumed
            time_consumed_agv = time_consumed_total / epoch
            self.epochs_last = self.epochs - epoch
            estimate_time_last = self.epochs_last * time_consumed_agv
            print(
                'Time for epoch {}/{} is {} sec - gen_loss = {}, disc_loss = {}, time estimated to finish: {}'
                .format(epoch, self.epochs,
                        time.time() - start, total_gen_loss / batch_size,
                        total_disc_loss / batch_size, estimate_time_last))

            if epoch % save_interval == 0:
                save_images(save_path, epoch, self.generator, self.seed,
                            self.avg_weekend, self.avg_workday)
                if epoch > 150:
                    self.generate(save_path, epoch)
                    self.save_model(save_path, time_consumed_total)

        self.save_model(save_path, time_consumed_total)
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.
            if epoch >= train_step['epoch']:
                lr_decay = (train_step['epoch'] -
                            epoch) / (train_step['epoch'] / 2.)

            # 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()
Exemple #6
0
def main():
    start_time = time.time()  # Clocking start

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

    # Loading Cifar-10 DataSet
    ds = DataSet2(
        height=32,
        width=32,
        channel=3,
        ds_path=
        "/media/shar/240A27640A2731EA/shared2/Awesome-GANs-master/BGAN/cifar/",
        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=True
    )  # 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:
        # BGAN Model
        model = bgan.BGAN(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:
            # 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')

        d_loss = 0.
        d_overpowered = False
        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']):

            #batch_x, _ = mnist.train.next_batch(model.batch_size)
            batch_x, _ = ds_iter.next_batch()
            batch_x = batch_x.reshape(-1, model.n_input)
            batch_z = np.random.uniform(
                -1., 1., [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,
                              })
            # Generated image save
            iu.save_images(samples,
                           size=[sample_image_height, sample_image_width],
                           image_path=sample_dir,
                           inv_type='127')

            d_overpowered = d_loss < g_loss / 2.
            # Logging
            if global_step % train_step['logging_interval'] == 0:
                batch_x, _ = ds_iter.next_batch()
                batch_z = np.random.uniform(
                    -1., 1.,
                    [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,
                    })

                # 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., 1.,
                    [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[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_1{: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)
                print(sample_dir)
            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 #7
0
def main():
    start_time = time.time()  # clocking start

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
    config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options)

    with tf.Session(config=config) as s:
        end_time = time.time() - start_time

        # BEGAN Model
        model = began.BEGAN(s)

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

        # load model & graph & weight
        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:
            global_step = 0
            print('[-] No checkpoint file found')
            # return

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

        # loading Celeb-A dataset
        ds = DataSet(input_height=64,
                     input_width=64,
                     input_channel=3,
                     dataset_name="celeb-a")
        images = ds.images

        sample_z = np.random.uniform(-1.,
                                     1.,
                                     size=(model.sample_num,
                                           model.z_dim)).astype(np.float32)

        d_overpowered = False
        kt = tf.Variable(0., dtype=tf.float32)  # init K_0 value, 0

        batch_per_epoch = int(len(images) / paras['batch_size'])
        for epoch in range(paras['epoch']):
            for step in range(batch_per_epoch):
                iter_ = datasets.DataIterator([images], paras['batch_size'])

                # k_t update
                # k_t+1 = K_t + lambda_k * (gamma * d_real - d_fake)
                kt = kt + model.lambda_k * (model.gamma * model.D_real -
                                            model.D_fake)

                # z update
                batch_z = np.random.uniform(
                    -1., 1.,
                    [paras['batch_size'], model.z_dim]).astype(np.float32)

                # update D network
                if not d_overpowered:
                    s.run(model.d_op,
                          feed_dict={
                              model.x: 0,
                              model.z: batch_z,
                              model.kt: kt
                          })

                # update G network
                s.run(model.g_op, feed_dict={model.z: batch_z, model.kt: kt})

                if global_step % paras['logging_interval'] == 0:
                    batch_z = np.random.uniform(
                        -1., 1.,
                        [paras['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: 0,
                            model.z: batch_z
                        })

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

                    # update overpowered
                    d_overpowered = d_loss < g_loss / 3

                    # training G model with sample image and noise
                    samples = s.run(model.G,
                                    feed_dict={
                                        model.x: 0,
                                        model.z: sample_z
                                    })

                    # 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 = dirs[
                        'sample_output'] + 'train_{0}_{1}.png'.format(
                            epoch, 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, dirs['model'], global_step=step)

                global_step += 1

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

    # close tf.Session
    s.close()
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., 1., [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., 1., [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 #9
0
def main():
    start_time = time.time()  # Clocking start

    # MNIST Dataset load
    mnist = input_data.read_data_sets('../data/MNIST_data', one_hot=True)

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

    with tf.Session(config=config) as s:
        # InfoGAN Model
        model = infogan.InfoGAN(s)

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

        sample_x, _ = mnist.test.next_batch(model.sample_num)
        sample_x = np.reshape(sample_x, [-1] + model.image_shape[1:])
        #sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim]).astype(np.float32)
        z = np.random.uniform(-1., 1., model.z_dim).astype(np.float32)
        sample_z = [z] * model.sample_num

        sample_c = np.zeros(
            shape=[model.sample_num, model.n_cat + model.n_cont])

        samples_cont = np.random.normal(loc=0.0,
                                        scale=1.0,
                                        size=model.sample_num)
        samples_cont = np.sort(samples_cont)

        k = 0
        sc = 2

        for j in range(model.sample_num):
            sample_c[j][k] = 1
            sample_c[j][11] = samples_cont[j] * sc
            k += 1
            if k == 10:
                k = 0

        d_overpowered = False
        for step in range(train_step['global_step']):
            batch_x, _ = mnist.train.next_batch(model.batch_size)
            batch_x = np.reshape(batch_x, [-1] + model.image_shape[1:])
            batch_x = batch_x * 2 - 1
            batch_z = np.random.uniform(
                -1., 1., [model.batch_size, model.z_dim]).astype(np.float32)
            batch_cat = np.random.multinomial(1,
                                              model.n_cat *
                                              [1.0 / model.n_cat],
                                              size=model.batch_size)
            mean = np.zeros(shape=[model.batch_size, model.n_cont])
            stddev = np.ones(shape=[model.batch_size, model.n_cont])
            epsilon = np.random.normal(loc=0.0,
                                       scale=1.0,
                                       size=[model.batch_size, model.n_cont])
            batch_cont = mean + epsilon * stddev
            batch_c = np.concatenate((batch_cat, batch_cont), axis=1)

            # 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,
                                      model.c: batch_c,
                                  })

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

            _, q_loss = s.run([model.q_op, model.q_loss],
                              feed_dict={
                                  model.x: batch_x,
                                  model.z: batch_z,
                                  model.c: batch_c
                              })

            d_overpowered = d_loss < g_loss / 2

            # Logging
            if step % train_step['logging_interval'] == 0:
                batch_x, _ = mnist.test.next_batch(model.batch_size)
                batch_x = np.reshape(batch_x, [-1] + model.image_shape[1:])
                batch_x = batch_x * 2 - 1
                batch_z = np.random.uniform(
                    -1., 1.,
                    [model.batch_size, model.z_dim]).astype(np.float32)
                batch_cat = np.random.multinomial(1,
                                                  model.n_cat *
                                                  [1.0 / model.n_cat],
                                                  size=model.batch_size)
                mean = np.zeros(shape=[model.batch_size, model.n_cont])
                stddev = np.ones(shape=[model.batch_size, model.n_cont])
                epsilon = np.random.normal(
                    loc=0.0, scale=1.0, size=[model.batch_size, model.n_cont])
                batch_cont = mean + epsilon * stddev
                batch_c = np.concatenate((batch_cat, batch_cont), axis=1)

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

                d_overpowered = d_loss < g_loss / 2

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

                for k in range(0, 10):

                    sample_feed = [sample_c[k]]

                    # Training G model with sample image and noise
                    samples = s.run(model.g,
                                    feed_dict={
                                        model.x: sample_x,
                                        model.z: [sample_z[0]],
                                        model.c: sample_feed
                                    })
                    # Summary saver
                    model.writer.add_summary(summary, step)

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

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

                # Training G model with sample image and noise
                samples = s.run(model.g,
                                feed_dict={
                                    model.x: sample_x,
                                    model.z: sample_z,
                                    model.c: sample_c
                                })
                # 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 #10
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:
        # DCGAN model
        model = dcgan.DCGAN(s, batch_size=train_step['batch_size'])

        # 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 = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
            print("[+] global step : %s" % global_step, " successfully loaded")
        else:
            global_step = 0
            print('[-] No checkpoint file found')

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

        # Training, test data set
        dataset = DataSet(input_height=32,
                          input_width=32,
                          input_channel=3,
                          name='cifar-100')
        dataset_iter = DataIterator(dataset.train_images, dataset.train_labels,
                                    train_step['batch_size'])

        sample_x = dataset.valid_images[:model.sample_num].astype(
            np.float32) / 225.
        sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim])

        d_overpowered = False  # G loss > D loss * 2

        step = int(global_step)
        cont = int(step / 750)
        for epoch in range(cont, cont + train_step['epoch']):
            for batch_images, _ in dataset_iter.iterate():
                batch_x = batch_images.astype(np.float32) / 225.
                batch_z = np.random.uniform(
                    -1., 1.,
                    [train_step['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.z: batch_z})

                d_overpowered = d_loss < g_loss / 2.

                if step % train_step['logging_interval'] == 0:
                    batch_z = np.random.uniform(
                        -1., 1.,
                        [train_step['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.

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

                    # Training G model with sample image and noise
                    samples = s.run(model.g,
                                    feed_dict={
                                        model.x: sample_x,
                                        model.z: sample_z,
                                    })

                    # 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_{0}_{1}.png'.format(epoch, 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)

                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 #11
0
def main():
    with tf.Session() as sess:
        num_epoch = 5
        checkpoint_interval = 10

        batch_size = 64
        image_size = 32

        model = LAPGAN(sess, batch_size=batch_size)

        dataset = Dataset("cifar10/")
        dataset_iter = DataIterator(dataset.train_images, dataset.train_labels, batch_size)

        summary_writer = tf.train.SummaryWriter('logs_{0}/'.format(int(time.time())), sess.graph_def)

        sess.run(tf.initialize_all_variables())

        sample_images = dataset.valid_images[:model.sample_size].astype(np.float32) / 255.0
        sample_z = np.random.uniform(-1.0, 1.0, size=(model.sample_size , model.z_dim))

        d_overpowered = False

        step = 0
        for epoch in range(num_epoch):
            for batch_images, _ in dataset_iter.iterate():
                # I0 = batch_images / 255.0
                # I1 = downsample(tf.constant(I0, tf.float32))
                # l0 = sess.run(upsample(I1))
                # h0 = I0 - l0
                # z0 = np.random.uniform(-1.0, 1.0, (batch_size,) + image_size + (1,)).astype(np.float32)
                # l0 = np.concatenate([l0, z0], axis=-1)

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

                # update d network
                if not d_overpowered:
                    sess.run(model.d_optim, feed_dict={ model.x: batch_images, model.z: batch_z })

                # update g network
                sess.run(model.g_optim, feed_dict={ model.z: batch_z })

                if step % checkpoint_interval == 0:
                    # I0 = dataset.valid_images / 255.0
                    # I1 = downsample(tf.constant(I0, tf.float32))
                    # l0 = sess.run(upsample(I1))
                    # h0 = I0 - l0
                    # z0 = np.random.uniform(-1.0, 1.0, I0.shape[:-1] + (1,)).astype(np.float32)
                    # l0 = np.concatenate([l0, z0], axis=-1)

                    batch_images = dataset.valid_images[:batch_size].astype(np.float32) / 255.0
                    batch_z = np.random.uniform(-1.0, 1.0, [batch_size, model.z_dim]).astype(np.float32)

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

                    d_overpowered = d_loss < g_loss / 2

                    samples = sess.run(model.G, feed_dict={
                        model.x: sample_images,
                        model.z: sample_z
                    })

                    summary_writer.add_summary(summary, step)
                    save_images(samples, [8, 8], './samples/train_{0}_{1}.png'.format(epoch, step))
                    print('[{0}, {1}] loss: {2} (D) {3} (G) (d overpowered?: {4})'.format(epoch, step, d_loss, g_loss, d_overpowered))

                step += 1
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.
        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.,
                                        1.,
                                        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.)

            if global_step % train_step['logging_interval'] == 0:
                batch_x, _ = mnist.test.next_batch(model.batch_size)
                batch_z = np.random.uniform(
                    -1., 1.,
                    [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.)

                # 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., 1.,
                    [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()
def main():
    start_time = time.time()  # Clocking start

    height, width, channel = 128, 128, 3

    # loading CelebA DataSet # from 'raw images' or 'h5'
    use_h5 = True
    if not use_h5:
        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,
                                       "Anno/list_attr_celeba.txt"),
            ds_image_path=os.path.join(cfg.celeba, "Img/img_align_celeba/"),
            ds_type="CelebA",
            use_save=True,
            save_file_name=os.path.join(cfg.celeba, "CelebA-%d.h5" % height),
            save_type="to_h5",
            use_img_scale=False,
        )
    else:
        ds = DataSet(
            height=height,
            width=height,
            channel=channel,
            ds_image_path=os.path.join(cfg.celeba, "CelebA-%d.h5" % height),
            ds_label_path=os.path.join(cfg.celeba,
                                       "Anno/list_attr_celeba.txt"),
            # ds_image_path=os.path.join(cfg.celeba, "Img/img_align_celeba/"),
            ds_type="CelebA",
            use_save=False,
            # save_file_name=os.path.join(cfg.celeba, "CelebA-%d.h5" % height),
            # save_type="to_h5",
            use_img_scale=False,
        )

    num_images = ds.num_images

    # 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, "sample.png"),
                   inv_type='127')

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

    del ds

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

    with tf.Session(config=config) as s:
        # SAGAN Model
        model = sagan.SAGAN(s,
                            height=height,
                            width=width,
                            channel=channel,
                            batch_size=train_step['batch_size'],
                            use_gp=False,
                            use_hinge_loss=True)

        # 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 // (num_images // model.batch_size
                                      )  # recover n_epoch
        ds_iter.pointer = saved_global_step % (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., 1.,
                    [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,
                                    })

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

                    # is_mean, is_std = t.inception_score(iu.inverse_transform(samples, inv_type='127'))
                    # fid_score = t.fid_score(real_img=batch_x, fake_img=samples[:model.batch_size])

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

                    # 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, "SAGAN.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()
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., 1.,
                    [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., 1.,
                        [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., 1.,
                        [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()
                                    model.z: sample_z,
                                })

                    samples = np.reshape(samples, [-1] + model.image_shape[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_1{: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)
                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()


if __name__ == '__main__':
Exemple #16
0
def main():
    start_time = time.time()  # Clocking start

    # MNIST Dataset load
    mnist = input_data.read_data_sets('../data/MNIST_data', one_hot=True)

    # 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())

        sample_x, _ = mnist.train.next_batch(model.sample_num)
        sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim]).astype(np.float32)

        d_overpowered = False
        for step in range(train_step['global_step']):
            batch_x, _ = mnist.train.next_batch(model.batch_size)
            batch_x = batch_x.reshape(-1, model.n_input)
            batch_x = batch_x*2 - 1

            batch_z = np.random.uniform(-1., 1., 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)

            if step % train_step['logging_interval'] == 0:
                batch_x, _ = mnist.test.next_batch(model.batch_size)
                batch_z = np.random.uniform(-1., 1., [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)

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

                # Training G model with sample image and noise
                samples = s.run(model.g,feed_dict={model.x: sample_x,model.z: sample_z,})

                samples = np.reshape(samples, [-1, model.output_height, model.output_width, model.channel])

                # 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))  # took about 370s on my machine

    # Close tf.Session
    s.close()
Exemple #17
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:
        # BEGAN Model
        model = began.BEGAN(s)  # BEGAN

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

        # Celeb-A DataSet images
        ds = DataSet(input_height=32,
                     input_width=32,
                     input_channel=3,
                     mode='r').images
        dataset_iter = DataIterator(ds, None, train_step['batch_size'],
                                    label_off=True)

        sample_x = ds[:model.sample_num]
        sample_x = np.reshape(sample_x, [-1] + model.image_shape[1:])
        sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim]).astype(np.float32)

        # Export real image
        valid_image_height = model.sample_size
        valid_image_width = model.sample_size
        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)

        global_step = 0
        for epoch in range(train_step['epoch']):
            for batch_images in dataset_iter.iterate():
                batch_x = np.reshape(batch_images, [-1] + model.image_shape[1:])
                batch_z = np.random.uniform(-1., 1., [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., 1., [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
                    samples = s.run(model.g,
                                    feed_dict={
                                        model.x: sample_x,
                                        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()
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., 1.,
                    [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., 1.,
                        [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

    # MNIST Dataset load
    mnist = DataSet().data

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

    with tf.Session(config=config) as s:
        # CoGAN Model
        model = cogan.CoGAN(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., 1., [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., 1.,
                    [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., 1.,
                    [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()
Exemple #20
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., 1., [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., 1.,
                    [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., 1.,
                    [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

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

    with tf.Session(config=config) as s:
        image_size = crop_size = 128

        # CycleGAN Model
        model = cyclegan.CycleGAN(s,
                                  input_height=image_size,
                                  input_width=image_size,
                                  input_channel=3,
                                  batch_size=train_step['batch_size'])

        # Celeb-A DataSet images
        data_set_name = 'vangogh2photo'
        ds = DataSet(input_height=image_size,
                     input_width=image_size,
                     input_channel=3,
                     crop_size=crop_size,
                     batch_size=train_step['batch_size'],
                     name=data_set_name)

        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.
            if epoch >= 100 and epoch % 10 == 0:
                lr_decay = (train_step['epochs'] -
                            epoch) / (train_step['epochs'] / 2.)

            # re-implement DataIterator for multi-input
            pointer = 0
            num_images = min(ds.num_images_a, ds.num_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.num_images_a), np.arange(
                        ds.num_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
    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)  # BEGAN

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

        # Celeb-A DataSet images
        ds = DataSet(height=64,
                     width=64,
                     channel=3,
                     ds_path="/home/zero/hdd/DataSet/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., 1., [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:
                    _, 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 %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., 1., [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()
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., 1., [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., 1., [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., 1., [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 #24
0
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):
                offsetA = (step * paras['batch_size']) % (dataset.img_A.shape[0] - paras['batch_size'])
                offsetB = (step * paras['batch_size']) % (dataset.img_B.shape[0] - paras['batch_size'])

                # batch data set
                batch_A = dataset.img_A[offsetA:(offsetA + paras['batch_size']), :]
                batch_B = dataset.img_B[offsetB:(offsetB + 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

                    # 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()
Exemple #25
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:
        # LAPGAN model # D/G Models are same as DCGAN
        model = lapgan.LAPGAN(s, batch_size=train_step['batch_size'])

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

        # Training, test data set
        dataset = DataSet(input_height=32,
                          input_width=32,
                          input_channel=3,
                          name='cifar-10')
        dataset_iter = DataIterator(dataset.train_images, dataset.train_labels,
                                    train_step['batch_size'])

        step = 0
        cont = int(step / 750)
        for epoch in range(cont, cont + train_step['epoch']):
            for batch_images, batch_labels in dataset_iter.iterate():
                batch_images = batch_images.astype(np.float32) / 225.

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

                # 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.d_reals_prob[0], model.d_fakes_prob[0], model.x1_coarse,
                    model.d_loss[0], model.g_loss[0],

                    model.x2_fine, model.g[1], model.d_reals_prob[1], model.d_fakes_prob[1], model.x2_coarse,
                    model.d_loss[1], model.g_loss[1],

                    model.x3_fine, model.g[2], model.d_reals_prob[2], model.d_fakes_prob[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],  # D/G ops
                ],
                    feed_dict={
                        model.x1_fine: batch_images,  # images
                        model.y: batch_labels,        # classes
                        model.z[0]: z[0], model.z[1]: z[1], model.z[2]: z[2]  # z-noises
                    })

                # Logging
                if step % train_step['logging_interval'] == 0:
                    batch_x = batch_images[:model.sample_num]
                    batch_y = batch_labels[:model.sample_num]

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

                    # 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.d_reals_prob[0], model.d_fakes_prob[0], model.x1_coarse,
                        model.d_loss[0], model.g_loss[0],

                        model.x2_fine, model.g[1], model.d_reals_prob[1], model.d_fakes_prob[1], model.x2_coarse,
                        model.d_loss[1], model.g_loss[1],

                        model.x3_fine, model.g[2], model.d_reals_prob[2], model.d_fakes_prob[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: batch_y,        # classes
                            model.z[0]: z[0], model.z[1]: z[1], model.z[2]: z[2]  # z-noises
                        })

                    # Print loss
                    print("[+] Epoch %03d Step %05d => " % (epoch, step),
                          " D loss : {:.8f}".format(d_loss_1.mean()),
                          " G loss : {:.8f}".format(g_loss_1.mean()))

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

                    # Summary saver
                    model.writer.add_summary(summary, 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}_{1}.png'.format(epoch, 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)  # time saving

                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 #26
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.
                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()
Exemple #27
0
def main():
    start_time = time.time()  # Clocking start

    # MNIST Dataset load
    mnist = DataSet().data

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

    with tf.Session(config=config) as s:
        # InfoGAN Model
        model = infogan.InfoGAN(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, [-1] + model.image_shape[1:])
        sample_z = np.random.uniform(
            -1., 1., [model.sample_num, model.z_dim]).astype(np.float32)
        sample_c = np.concatenate(
            (sample_y, np.zeros([model.sample_num, model.n_cont])), axis=1)

        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, [-1] + model.image_shape[1:])
            batch_z = np.random.uniform(
                -1., 1., [model.batch_size, model.z_dim]).astype(np.float32)
            batch_c = np.concatenate(
                (batch_y, np.random.uniform(-1., 1., [model.batch_size, 2])),
                axis=1)

            # 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,
                                      model.c: batch_c,
                                  })

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

            d_overpowered = d_loss < g_loss / 2

            # Logging
            if step % train_step['logging_interval'] == 0:
                batch_x, batch_y = mnist.test.next_batch(model.batch_size)
                batch_x = np.reshape(batch_x, [-1] + model.image_shape[1:])
                batch_z = np.random.uniform(
                    -1., 1.,
                    [model.batch_size, model.z_dim]).astype(np.float32)
                batch_c = np.concatenate(
                    (batch_y, np.random.uniform(-1., 1.,
                                                [model.batch_size, 2])),
                    axis=1)

                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,
                        model.c: batch_c,
                    })

                d_overpowered = d_loss < g_loss / 2

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

                # Training G model with sample image and noise
                samples = s.run(model.g,
                                feed_dict={
                                    model.x: sample_x,
                                    model.z: sample_z,
                                    model.c: sample_c,
                                })

                # 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()
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., 1., [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., 1., [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 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., 1.,
                    [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., 1.,
                        [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()
Exemple #30
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:
        # AnoGAN Model
        # anomalies detect off (just training model)  -> False
        # anomalies detect on  (based on trained model-> True
        if not os.path.exists('./model'):
            detection = False
        else:
            detection = True

        model = anogan.AnoGAN(s, detect=detection)  # AnoGAN

        global_step = 0
        if detection:
            # 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 = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                print("[+] global step : %s" % global_step,
                      " successfully loaded")
            else:
                print('[-] No checkpoint file found')

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

        # Celeb-A DataSet images
        ds = DataSet(
            input_height=64,  # in the paper, 108
            input_width=64,  # in the paper, 108
            input_channel=3).images
        # To-Do
        # Getting anomaly data

        dataset_iter = DataIterator(ds,
                                    None,
                                    train_step['batch_size'],
                                    label_off=True)

        sample_x = ds[:model.sample_num]
        sample_x = np.reshape(sample_x, [-1] + model.image_shape[1:])
        sample_z = np.random.uniform(
            -1., 1., [model.sample_num, model.z_dim]).astype(np.float32)

        # Export real image
        valid_image_height = model.sample_size
        valid_image_width = model.sample_size
        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)

        for epoch in range(train_step['epoch']):
            for batch_images in dataset_iter.iterate():
                batch_x = np.reshape(batch_images,
                                     [-1] + model.image_shape[1:])
                batch_z = np.random.uniform(
                    -1., 1.,
                    [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., 1.,
                        [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
                    samples = s.run(model.g,
                                    feed_dict={
                                        model.x: sample_x,
                                        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()