<commit_before>from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import tensorflow as tf
import numpy as np

import sys
import time

import pggan_model as pggan

sys.path.append('../')
import image_utils as iu
from datasets import DataIterator
from datasets import CelebADataSet as DataSet


results = {
    'output': './gen_img/',
    'checkpoint': './model/checkpoint',
    'model': './model/PGGAN-model.ckpt'
}

train_step = {
    'epoch': 100,
    'batch_size': 16,
    'logging_step': 2000,
}

pg = [1, 2, 2, 3, 3, 4, 5, 5, 6, 6]
assert len(pg) == 11

r_pg = [1, 1, 2, 2, 3, 3, 4, 5, 5, 6]
assert len(r_pg) == 11


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

    # Celeb-A DataSet images
    ds = DataSet(input_height=128,
                 input_width=128,
                 input_channel=3).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:])

    # 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,
                   inv_type='127')

    # 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:
        # PGGAN Model
        model = pggan.PGGAN(s)  # PGGAN

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

        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,
                                  })

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

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


if __name__ == '__main__':
    main()<commit_msg>update: PGGAN in progress... v0.1.1 - fix scope - add random sample pick<commit_after>from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import tensorflow as tf
import numpy as np

import sys
import time
import random

import pggan_model as pggan

sys.path.append('../')
import image_utils as iu
from datasets import DataIterator
from datasets import CelebADataSet as DataSet


results = {
    'output': './gen_img/',
    'checkpoint': './model/checkpoint',
    'model': './model/PGGAN-model-'
}

train_step = {
    'epoch': 100,
    'batch_size': 16,
    'logging_step': 2000,
}

pg = [1, 2, 2, 3, 3, 4, 5, 5, 6, 6]
assert len(pg) == 11

r_pg = [1, 1, 2, 2, 3, 3, 4, 5, 5, 6]
assert len(r_pg) == 11


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

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

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

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

    # Generated image save
    iu.save_images(sample_x, size=[valid_image_height, valid_image_width], image_path=sample_dir,
                   inv_type='127')

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

    for idx, n_pg in enumerate(pg):

        with tf.Session(config=config) as s:
            # PGGAN Model
            model = pggan.PGGAN(s)  # PGGAN

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

            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,
                                      })

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

                        # 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 = 1
                        sample_image_width = 1
                        sample_dir = results['output'] + 'train_{0}.png'.format(global_step)

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

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

                    global_step += 1

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

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


if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.09698275862068965, 'fraction_numerical': 0.014988244514106583, 'mean_word_length': 1.262131619765123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4584314', 'n_tokens_mistral': 2935, 'n_tokens_neox': 2681, 'n_words': 610}