KDnuggets Home » News » 2018 » May » Tutorials, Overviews » GANs in TensorFlow from the Command Line: Creating Your First GitHub Project ( 18:n21 )

GANs in TensorFlow from the Command Line: Creating Your First GitHub Project


In this article I will present the steps to create your first GitHub Project. I will use as an example Generative Adversarial Networks.



By Rubens Zimbres, Data Scientist

I started contributing to GitHub in 2016. Since then my repos have more than 100 different files with Machine Learning, Deep Learning and Natural Language Processing codes I developed while studying Data Science. But they are simple repository of codes, I was not worried about developing a GitHub Project.

In this article I will present the steps to create your first GitHub Project. I will use as an example Generative Adversarial Networks. If you want to learn more about GANs, you can access my PDF presentation here:

https://github.com/RubensZimbres/Repo-2018/blob/master/Deep%20Learning%20Summer%20School/GANs_Summer_School_15_FINAL.pdf

As you will see in my project, there is a main.py file where the GAN code is placed and another file with the libraries required to properly run the code (requirements.txt).

The file main.py contains the GAN code itself and the arguments necessary to run the notebook. The arguments are inserted into main.py in this form:

import argparse

parser = argparse.ArgumentParser(description='')
parser.add_argument('--epoch', dest='nb_epoch', type=int, default=5000, help='# of epochs')
parser.add_argument('--learning_rate', dest='lr', type=float, default=0.0001, help='# learning rate')
parser.add_argument('--sample_size', dest='sample_size', type=int, default=60, help='# sample size')
parser.add_argument('--gen_hidden', dest='gen_hidden', type=int, default=80, help='# hidden nodes in generator')
parser.add_argument('--disc_hidden', dest='disc_hidden', type=int, default=80, help='# hidden nodes in discriminator')
parser.add_argument('--your_login', dest='your_login', type=str, default='rubens', help='# your login name')

args = vars(parser.parse_args())


The other file, requirements.txt contains the libraries necessary to run the file main.py in this simple form:

tensorflow
numpy
matplotlib
keras
pandas

Next, we can develop an interactive function to provide guidelines for the user, as well as to get his/her OS so that the code customizes the folder where the Tensorboard file is stored.

var0 = input("Are you using Linux? [y|n]")

if str(var0)=='n':
        logs_path = 'C:/Users/'+args['your_login']+'/Anaconda3/envs/Scripts/plot_1'
    else:
        logs_path = '/home/'+args['your_login']+'/anaconda3/envs/plot_1'


After that we define a function containing our GAN:

def GAN(sample_size):

    tf.reset_default_graph() 
    def generator(x, reuse=False):
        with tf.variable_scope('Generator', reuse=reuse):
            x = tf.layers.dense(x, units=6 * 6 * 64)
            x = tf.nn.relu(x)
            x = tf.reshape(x, shape=[-1, 6, 6, 64])
            x = tf.layers.conv2d_transpose(x, 32, 4, strides=2)
            x = tf.layers.conv2d_transpose(x, 1, 2, strides=2)
            x = tf.nn.relu(x)
            x = tf.reshape(x, [n,784])
            return x
    
    def discriminator(x, reuse=False):
        with tf.variable_scope('Discriminator', reuse=reuse):
            x = tf.reshape(x, [n,28,28,1])
            x = tf.layers.conv2d(x, 32, 5)
            x = tf.nn.relu(x)
            x = tf.layers.average_pooling2d(x, 2, 2,padding='same')
            x = tf.layers.conv2d(x, 64, 5,padding='same')
            x = tf.nn.relu(x)
            x = tf.layers.average_pooling2d(x, 8, 8)
            x = tf.contrib.layers.flatten(x)
            x = tf.layers.dense(x, 784)
            x = tf.nn.sigmoid(x)
        return x


... etc. At the end of this article I will provide the link to the full code in my repo.

We also add lines of code to generate our Tensorboard, using tf.summary:

Losses:

tf.summary.scalar("Generator_Loss", gen_loss)
tf.summary.scalar("Discriminator_Loss", disc_loss)


Images generated by Generator and classified by Discriminator:

tf.summary.image('GenSample', tf.reshape(gen_sample, [-1, 28, 28, 1]), 4)
tf.summary.image('stacked_gan', tf.reshape(stacked_gan, [-1, 28, 28, 1]), 4)


Also, histogram of weights:

for i in range(0,11):
    with tf.name_scope('layer'+str(i)):
        pesos=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
        tf.summary.histogram('pesos'+str(i), pesos[i])


Then we open the Tensorflow session to run backpropagation and generate images:

with tf.Session() as sess:
    sess.run(init)
    summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
    for i in range(1, num_steps+1):
        batch_x, batch_y=next_batch(batch_size, x_train
                                    , x_train_noisy)        
        feed_dict = {real_image_input: batch_x, noise_input: batch_y,
                     disc_target: batch_x, gen_target: batch_y}
        _, _, gl, dl,summary2 = sess.run([train_gen, train_disc, gen_loss, disc_loss,summary],
                                feed_dict=feed_dict)
        g = sess.run([stacked_gan], feed_dict={noise_input: batch_y})
        h = sess.run([gen_sample], feed_dict={noise_input: batch_y})
        summary_writer.add_summary(summary2, i)
        if i % show_steps == 0 or i == 1:
            print('Epoch %i: Generator Loss: %f, Discriminator Loss: %f' % (i, gl, dl))


Finally:

if __name__ == '__main__':
   GAN(args[‘sample_size’])


To run the code, you must run (in Anaconda folder):

$ git clone https://github.com/RubensZimbres/GAN-Project-2018

$ cd GAN-Project-2018

$ conda install --yes --file requirements.txt

$ python main.py --epoch=4000 --learning_rate=0.0001 –your_login=rubens


Where:

Arguments:
--epoch: default=5000
--learning_rate: default=0.0001
--sample_size: default=60
--gen_hidden: # hidden nodes in generator: default=80
--disc_hidden: # hidden nodes in discriminator: default=80
--your_login: your login in your OS: default:rubens


The arguments provided in this line of code will customize your GAN like this:

num_steps = args['nb_epoch']
learning_rate1=args['lr']
image_dim = 784 
gen_hidden_dim = args['gen_hidden']
disc_hidden_dim = args['disc_hidden']


This is the output you are expected to have:

Output image

Tensorboard will start AFTER you close the pop-up (GAN output with MNIST digits)

After closing the MNIST pop-up window, the code starts the Tensorboard server with this following line of code at main.py:

os.system('tensorboard –logdir='+logs_path)


You then access the URL provided by Tensorboard and access training details in your browser, like scalars (training losses), graph (GAN structure), images (generated and classified) and histograms (of weights). See the following pictures.

To access details of this project, access: https://github.com/RubensZimbres/GAN-Project-2018

Image
 

Image
 

Image

 
Bio: Rubens Zimbres, PhD, is a strategist and data scientist with over 23 years of experience in customer service, management and financial planning, having worked with crisis management, as CEO and Data Scientist in the areas of strategic planning and restructuring, physical and digital marketing, social networks analysis, personnel management, customer database analysis. He has 13 years of Market Research expertise and 10 years of data analysis experience.

Related: