dlnd_face_generation


Face Generation

In this project, you'll use generative adversarial networks to generate new images of faces.

Get the Data

You'll be using two datasets in this project:

  • MNIST
  • CelebA

Since the celebA dataset is complex and you're doing GANs in a project for the first time, we want you to test your neural network on MNIST before CelebA. Running the GANs on MNIST will allow you to see how well your model trains sooner.

If you're using FloydHub, set data_dir to "/input" and use the FloydHub data ID "R5KrjnANiKVhLWAkpXhNBe".


In [1]:
data_dir = './data'

# FloydHub - Use with data ID "R5KrjnANiKVhLWAkpXhNBe"
data_dir = '/input'


"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import helper

helper.download_extract('mnist', data_dir)
helper.download_extract('celeba', data_dir)


Found mnist Data
Found celeba Data

Explore the Data

MNIST

As you're aware, the MNIST dataset contains images of handwritten digits. You can view the first number of examples by changing show_n_images.


In [2]:
show_n_images = 25

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
%matplotlib inline
import os
from glob import glob
from matplotlib import pyplot

mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L')
pyplot.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray')


Out[2]:
<matplotlib.image.AxesImage at 0x7fc994688f98>

CelebA

The CelebFaces Attributes Dataset (CelebA) dataset contains over 200,000 celebrity images with annotations. Since you're going to be generating faces, you won't need the annotations. You can view the first number of examples by changing show_n_images.


In [3]:
show_n_images = 25

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB')
pyplot.imshow(helper.images_square_grid(mnist_images, 'RGB'))


Out[3]:
<matplotlib.image.AxesImage at 0x7fc994587ac8>

Preprocess the Data

Since the project's main focus is on building the GANs, we'll preprocess the data for you. The values of the MNIST and CelebA dataset will be in the range of -0.5 to 0.5 of 28x28 dimensional images. The CelebA images will be cropped to remove parts of the image that don't include a face, then resized down to 28x28.

The MNIST images are black and white images with a single color channel while the CelebA images have 3 color channels (RGB color channel).

Build the Neural Network

You'll build the components necessary to build a GANs by implementing the following functions below:

  • model_inputs
  • discriminator
  • generator
  • model_loss
  • model_opt
  • train

Check the Version of TensorFlow and Access to GPU

This will check to make sure you have the correct version of TensorFlow and access to a GPU


In [4]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from distutils.version import LooseVersion
import warnings
import tensorflow as tf

# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer.  You are using {}'.format(tf.__version__)
print('TensorFlow Version: {}'.format(tf.__version__))

# Check for a GPU
if not tf.test.gpu_device_name():
    warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))


TensorFlow Version: 1.0.0
Default GPU Device: /gpu:0

Input

Implement the model_inputs function to create TF Placeholders for the Neural Network. It should create the following placeholders:

  • Real input images placeholder with rank 4 using image_width, image_height, and image_channels.
  • Z input placeholder with rank 2 using z_dim.
  • Learning rate placeholder with rank 0.

Return the placeholders in the following the tuple (tensor of real input images, tensor of z data)


In [5]:
import problem_unittests as tests

def model_inputs(image_width, image_height, image_channels, z_dim):
    """
    Create the model inputs
    :param image_width: The input image width
    :param image_height: The input image height
    :param image_channels: The number of image channels
    :param z_dim: The dimension of Z
    :return: Tuple of (tensor of real input images, tensor of z data, learning rate)
    """
    real_input = tf.placeholder(tf.float32, [None, image_width, image_height, image_channels], name="real_input")
    z_input = tf.placeholder(tf.float32, [None, z_dim], name="z_input")
    learning_rate = tf.placeholder(tf.float32, name="learning_rate")

    return real_input, z_input, learning_rate


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_inputs(model_inputs)


Tests Passed

Discriminator

Implement discriminator to create a discriminator neural network that discriminates on images. This function should be able to reuse the variabes in the neural network. Use tf.variable_scope with a scope name of "discriminator" to allow the variables to be reused. The function should return a tuple of (tensor output of the discriminator, tensor logits of the discriminator).


In [6]:
def leaky_relu(input, alpha=0.2, name="leaky_relu"):
    return tf.maximum(input, alpha * input, name=name)

In [7]:
def discriminator(images, reuse=False, keep_prob=0.9):
    """
    Create the discriminator network
    :param image: Tensor of input image(s)
    :param reuse: Boolean if the weights should be reused
    :return: Tuple of (tensor output of the discriminator, tensor logits of the discriminator)
    """
    with tf.variable_scope("discriminator", reuse=reuse):
        # convert 28x28x(input_dimension) into 14x14x64
        layer1 = tf.layers.conv2d(images, 64, 5, 2, 'same', kernel_initializer=tf.contrib.layers.xavier_initializer(True))
        layer1 = leaky_relu(layer1)
        #layer1 = tf.nn.dropout(layer1, keep_prob=keep_prob)
        #layer1 = tf.Print(layer1, [tf.shape(layer1)], "discriminator layer 1 shape", summarize=100)
        
        # convert 14x14x64 into 7x7x128
        layer2 = tf.layers.conv2d(layer1, 128, 5, 2, 'same', kernel_initializer=tf.contrib.layers.xavier_initializer(True))
        layer2 = tf.layers.batch_normalization(layer2, training=True)
        layer2 = leaky_relu(layer2)
        #layer2 = tf.nn.dropout(layer2, keep_prob=keep_prob)
        #layer2 = tf.Print(layer2, [tf.shape(layer2)], "discriminator layer 2 shape", summarize=100)
        
        # removed a layer as suggested by reviewer
        # convert 7x7x128 into 4x4x256
        layer3 = tf.layers.conv2d(layer2, 256, 5, 2, 'same', kernel_initializer=tf.contrib.layers.xavier_initializer(True))
        layer3 = tf.layers.batch_normalization(layer3, training=True)
        layer3 = leaky_relu(layer3)
        #layer3 = tf.maximum(alpha * layer3, layer3)
        #layer3 = tf.nn.dropout(layer3, keep_prob=keep_prob)
        #layer3 = tf.Print(layer3, [tf.shape(layer3)], "discriminator layer 3 shape", summarize=100)
        
        # flatten to linear array
        convolutions_output = tf.reshape(layer2, (-1, 4 * 4 * 256))
        
        logits = tf.layers.dense(convolutions_output, 1) # 1 single output
        output = tf.sigmoid(logits)
        
    return logits, output

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_discriminator(discriminator, tf)


Tests Passed

Generator

Implement generator to generate an image using z. This function should be able to reuse the variabes in the neural network. Use tf.variable_scope with a scope name of "generator" to allow the variables to be reused. The function should return the generated 28 x 28 x out_channel_dim images.


In [11]:
def generator(z, out_channel_dim, is_train=True, keep_prob=0.5):
    """
    Create the generator network
    :param z: Input z
    :param out_channel_dim: The number of channels in the output image
    :param is_train: Boolean if generator is being used for training
    :return: The tensor output of the generator
    """
    reuse = not is_train
    with tf.variable_scope('generator', reuse=reuse):       
        # transform input into a tensor of 4x4x256
        layer1 = tf.layers.dense(z, 4 * 4 * 256)
        layer1 = tf.reshape(layer1, (-1, 4, 4, 256))
        layer1 = tf.layers.batch_normalization(layer1, training=is_train)
        layer1 = leaky_relu(layer1)
        #layer1 = tf.nn.dropout(layer1, keep_prob=keep_prob)
        #layer1 = tf.Print(layer1, [tf.shape(layer1)], "generator layer 1 shape", summarize=100)
        
        # convert 4x4x256 into 8x8x128
        layer2 = tf.layers.conv2d_transpose(layer1, 128, 4, 1, 'valid', kernel_initializer=tf.contrib.layers.xavier_initializer(True))
        layer2 = tf.layers.batch_normalization(layer2, training=is_train)
        layer2 = leaky_relu(layer2)
        #layer2 = tf.nn.dropout(layer2, keep_prob=keep_prob)
        #layer2 = tf.Print(layer2, [tf.shape(layer2)], "generator layer 2 shape", summarize=100)

        # convert 8x8x128 into 16x16x64
        layer3 = tf.layers.conv2d_transpose(layer2, 64, 5, 2, 'same', kernel_initializer=tf.contrib.layers.xavier_initializer(True))
        layer3 = tf.layers.batch_normalization(layer3, training=is_train)
        layer3 = leaky_relu(layer3)
        #layer3 = tf.nn.dropout(layer3, keep_prob=keep_prob)
        #layer3 = tf.Print(layer3, [tf.shape(layer3)], "generator layer 3 shape", summarize=100)

        # convert 16x16x64 into 32x32x(out_channel_dim)
        logits = tf.layers.conv2d_transpose(layer3, out_channel_dim, 5, 2, 'same', kernel_initializer=tf.contrib.layers.xavier_initializer(True))
        #layer4 = tf.Print(logits, [tf.shape(logits)], "generator logits shape", summarize=100)
        output = tf.tanh(logits)    
    
    return output

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_generator(generator, tf)


Tests Passed

Loss

Implement model_loss to build the GANs for training and calculate the loss. The function should return a tuple of (discriminator loss, generator loss). Use the following functions you implemented:

  • discriminator(images, reuse=False)
  • generator(z, out_channel_dim, is_train=True)

In [12]:
def model_loss(input_real, input_z, out_channel_dim):
    """
    Get the loss for the discriminator and generator
    :param input_real: Images from the real dataset
    :param input_z: Z input
    :param out_channel_dim: The number of channels in the output image
    :return: A tuple of (discriminator loss, generator loss)
    """
    generator_network = generator(input_z, out_channel_dim, is_train=True)
    
    discriminator_real_network, discriminator_real_logits = discriminator(input_real, reuse=False)
    discriminator_fake_network, discriminator_fake_logits = discriminator(generator_network, reuse=True)
    
    discriminator_real_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
        logits=discriminator_real_logits,
        labels=tf.ones_like(discriminator_real_network) * 0.9 # smooth real labels
    ))
    discriminator_fake_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
        logits=discriminator_fake_logits,
        labels=tf.zeros_like(discriminator_fake_network)
    ))
    
    generator_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
        logits=discriminator_fake_logits,
        labels=tf.ones_like(discriminator_fake_network)
    ))
    
    discriminator_loss = discriminator_real_loss + discriminator_fake_loss

    return discriminator_loss, generator_loss


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_loss(model_loss)


Tests Passed

Optimization

Implement model_opt to create the optimization operations for the GANs. Use tf.trainable_variables to get all the trainable variables. Filter the variables with names that are in the discriminator and generator scope names. The function should return a tuple of (discriminator training operation, generator training operation).


In [13]:
def model_opt(d_loss, g_loss, learning_rate, beta1):
    """
    Get optimization operations
    :param d_loss: Discriminator loss Tensor
    :param g_loss: Generator loss Tensor
    :param learning_rate: Learning Rate Placeholder
    :param beta1: The exponential decay rate for the 1st moment in the optimizer
    :return: A tuple of (discriminator training operation, generator training operation)
    """
    trainable_variables = tf.trainable_variables()
    discriminator_variables = [variable for variable in trainable_variables if variable.name.startswith('discriminator')]
    generator_variables = [variable for variable in trainable_variables if variable.name.startswith('generator')]
    
    # choo chooo
    discriminator_train = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(d_loss, var_list=discriminator_variables)
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope='generator')):
        generator_train = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(g_loss, var_list=generator_variables)
   
    return discriminator_train, generator_train


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_opt(model_opt, tf)


Tests Passed

Neural Network Training

Show Output

Use this function to show the current output of the generator during training. It will help you determine how well the GANs is training.


In [14]:
"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np

def show_generator_output(sess, n_images, input_z, out_channel_dim, image_mode):
    """
    Show example output for the generator
    :param sess: TensorFlow session
    :param n_images: Number of Images to display
    :param input_z: Input Z Tensor
    :param out_channel_dim: The number of channels in the output image
    :param image_mode: The mode to use for images ("RGB" or "L")
    """
    cmap = None if image_mode == 'RGB' else 'gray'
    z_dim = input_z.get_shape().as_list()[-1]
    example_z = np.random.uniform(-1, 1, size=[n_images, z_dim])

    samples = sess.run(
        generator(input_z, out_channel_dim, False),
        feed_dict={input_z: example_z})

    images_grid = helper.images_square_grid(samples, image_mode)
    pyplot.imshow(images_grid, cmap=cmap)
    pyplot.show()

Train

Implement train to build and train the GANs. Use the following functions you implemented:

  • model_inputs(image_width, image_height, image_channels, z_dim)
  • model_loss(input_real, input_z, out_channel_dim)
  • model_opt(d_loss, g_loss, learning_rate, beta1)

Use the show_generator_output to show generator output while you train. Running show_generator_output for every batch will drastically increase training time and increase the size of the notebook. It's recommended to print the generator output every 100 batches.


In [16]:
def train(epoch_count, batch_size, z_dim, learning_rate, beta1, get_batches, data_shape, data_image_mode):
    """
    Train the GAN
    :param epoch_count: Number of epochs
    :param batch_size: Batch Size
    :param z_dim: Z dimension
    :param learning_rate: Learning Rate
    :param beta1: The exponential decay rate for the 1st moment in the optimizer
    :param get_batches: Function to get batches
    :param data_shape: Shape of the data
    :param data_image_mode: The image mode to use for images ("RGB" or "L")
    """
    image_width = data_shape[1]
    image_height = data_shape[2]
    image_channels = data_shape[3]
    input_real, input_z, learning_rate_tensor = model_inputs(image_width, image_height, image_channels, z_dim)
    
    discriminator_loss, generator_loss = model_loss(input_real, input_z, image_channels)
    
    discriminator_training, generator_training = model_opt(discriminator_loss, generator_loss, learning_rate, beta1)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch_i in range(epoch_count):
            iterations = 0
            for batch_images in get_batches(batch_size):
                batch_images = batch_images * 2.0 # normalize to -1, 1
                iterations += 1
                                
                #batch_z_input = np.random.normal(0, scale=1.0, size=(batch_size, z_dim))
                batch_z_input = np.random.uniform(-1, 1, size=(batch_size, z_dim))
                network_feed_dict = {
                    input_real: batch_images,
                    input_z: batch_z_input,
                    learning_rate_tensor: learning_rate
                }
                
                _ = sess.run(discriminator_training, feed_dict=network_feed_dict)
                _ = sess.run(generator_training, feed_dict=network_feed_dict)
                
                if iterations % 10 == 0:
                    training_loss_discriminator = discriminator_loss.eval(network_feed_dict)
                    training_loss_generator = generator_loss.eval(network_feed_dict)
                    
                    print("Iteration {:3} -- Epoch {}/{} -- Discriminator loss: {:5.5f} -- Generator loss: {:5.5f}".format(
                        iterations,
                        epoch_i + 1,
                        epoch_count,
                        training_loss_discriminator,
                        training_loss_generator
                    ))
                
                if iterations % 100 == 0:
                    show_generator_output(sess, show_n_images, input_z, image_channels, data_image_mode)
        
        show_generator_output(sess, show_n_images, input_z, image_channels, data_image_mode)
                    
    print("Done!")

MNIST

Test your GANs architecture on MNIST. After 2 epochs, the GANs should be able to generate images that look like handwritten digits. Make sure the loss of the generator is lower than the loss of the discriminator or close to 0.


In [17]:
batch_size = 64
z_dim = 100
learning_rate = 0.0002
beta1 = 0.5


"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
epochs = 2

mnist_dataset = helper.Dataset('mnist', glob(os.path.join(data_dir, 'mnist/*.jpg')))
with tf.Graph().as_default():
    train(epochs, batch_size, z_dim, learning_rate, beta1, mnist_dataset.get_batches,
          mnist_dataset.shape, mnist_dataset.image_mode)


Iteration  10 -- Epoch 1/2 -- Discriminator loss: 1.49406 -- Generator loss: 0.53327
Iteration  20 -- Epoch 1/2 -- Discriminator loss: 1.41183 -- Generator loss: 0.60039
Iteration  30 -- Epoch 1/2 -- Discriminator loss: 1.40485 -- Generator loss: 0.62734
Iteration  40 -- Epoch 1/2 -- Discriminator loss: 1.39558 -- Generator loss: 0.63465
Iteration  50 -- Epoch 1/2 -- Discriminator loss: 1.39671 -- Generator loss: 0.65352
Iteration  60 -- Epoch 1/2 -- Discriminator loss: 1.38990 -- Generator loss: 0.66076
Iteration  70 -- Epoch 1/2 -- Discriminator loss: 1.38148 -- Generator loss: 0.66226
Iteration  80 -- Epoch 1/2 -- Discriminator loss: 1.38689 -- Generator loss: 0.66204
Iteration  90 -- Epoch 1/2 -- Discriminator loss: 1.38035 -- Generator loss: 0.65731
Iteration 100 -- Epoch 1/2 -- Discriminator loss: 1.37422 -- Generator loss: 0.66275
Iteration 110 -- Epoch 1/2 -- Discriminator loss: 1.37601 -- Generator loss: 0.67547
Iteration 120 -- Epoch 1/2 -- Discriminator loss: 1.39204 -- Generator loss: 0.66191
Iteration 130 -- Epoch 1/2 -- Discriminator loss: 1.36946 -- Generator loss: 0.66353
Iteration 140 -- Epoch 1/2 -- Discriminator loss: 1.38360 -- Generator loss: 0.66287
Iteration 150 -- Epoch 1/2 -- Discriminator loss: 1.38108 -- Generator loss: 0.65801
Iteration 160 -- Epoch 1/2 -- Discriminator loss: 1.36951 -- Generator loss: 0.66609
Iteration 170 -- Epoch 1/2 -- Discriminator loss: 1.37159 -- Generator loss: 0.66823
Iteration 180 -- Epoch 1/2 -- Discriminator loss: 1.37134 -- Generator loss: 0.66600
Iteration 190 -- Epoch 1/2 -- Discriminator loss: 1.36338 -- Generator loss: 0.66246
Iteration 200 -- Epoch 1/2 -- Discriminator loss: 1.36214 -- Generator loss: 0.66142
Iteration 210 -- Epoch 1/2 -- Discriminator loss: 1.35984 -- Generator loss: 0.65709
Iteration 220 -- Epoch 1/2 -- Discriminator loss: 1.36069 -- Generator loss: 0.66477
Iteration 230 -- Epoch 1/2 -- Discriminator loss: 1.36340 -- Generator loss: 0.66588
Iteration 240 -- Epoch 1/2 -- Discriminator loss: 1.36488 -- Generator loss: 0.66578
Iteration 250 -- Epoch 1/2 -- Discriminator loss: 1.36611 -- Generator loss: 0.66439
Iteration 260 -- Epoch 1/2 -- Discriminator loss: 1.36881 -- Generator loss: 0.66695
Iteration 270 -- Epoch 1/2 -- Discriminator loss: 1.36132 -- Generator loss: 0.66520
Iteration 280 -- Epoch 1/2 -- Discriminator loss: 1.34311 -- Generator loss: 0.66620
Iteration 290 -- Epoch 1/2 -- Discriminator loss: 1.35379 -- Generator loss: 0.66063
Iteration 300 -- Epoch 1/2 -- Discriminator loss: 1.35807 -- Generator loss: 0.66679
Iteration 310 -- Epoch 1/2 -- Discriminator loss: 1.34662 -- Generator loss: 0.66124
Iteration 320 -- Epoch 1/2 -- Discriminator loss: 1.35682 -- Generator loss: 0.65842
Iteration 330 -- Epoch 1/2 -- Discriminator loss: 1.39221 -- Generator loss: 0.64739
Iteration 340 -- Epoch 1/2 -- Discriminator loss: 1.37790 -- Generator loss: 0.65084
Iteration 350 -- Epoch 1/2 -- Discriminator loss: 1.38078 -- Generator loss: 0.66123
Iteration 360 -- Epoch 1/2 -- Discriminator loss: 1.36679 -- Generator loss: 0.65294
Iteration 370 -- Epoch 1/2 -- Discriminator loss: 1.37028 -- Generator loss: 0.66977
Iteration 380 -- Epoch 1/2 -- Discriminator loss: 1.36882 -- Generator loss: 0.66367
Iteration 390 -- Epoch 1/2 -- Discriminator loss: 1.36543 -- Generator loss: 0.66789
Iteration 400 -- Epoch 1/2 -- Discriminator loss: 1.37252 -- Generator loss: 0.66629
Iteration 410 -- Epoch 1/2 -- Discriminator loss: 1.36325 -- Generator loss: 0.66693
Iteration 420 -- Epoch 1/2 -- Discriminator loss: 1.38840 -- Generator loss: 0.64371
Iteration 430 -- Epoch 1/2 -- Discriminator loss: 1.37635 -- Generator loss: 0.64441
Iteration 440 -- Epoch 1/2 -- Discriminator loss: 1.36826 -- Generator loss: 0.65783
Iteration 450 -- Epoch 1/2 -- Discriminator loss: 1.36981 -- Generator loss: 0.65855
Iteration 460 -- Epoch 1/2 -- Discriminator loss: 1.36504 -- Generator loss: 0.66282
Iteration 470 -- Epoch 1/2 -- Discriminator loss: 1.37107 -- Generator loss: 0.67654
Iteration 480 -- Epoch 1/2 -- Discriminator loss: 1.36560 -- Generator loss: 0.66191
Iteration 490 -- Epoch 1/2 -- Discriminator loss: 1.37337 -- Generator loss: 0.65951
Iteration 500 -- Epoch 1/2 -- Discriminator loss: 1.37641 -- Generator loss: 0.65625
Iteration 510 -- Epoch 1/2 -- Discriminator loss: 1.37446 -- Generator loss: 0.65268
Iteration 520 -- Epoch 1/2 -- Discriminator loss: 1.36380 -- Generator loss: 0.66138
Iteration 530 -- Epoch 1/2 -- Discriminator loss: 1.37567 -- Generator loss: 0.65184
Iteration 540 -- Epoch 1/2 -- Discriminator loss: 1.37776 -- Generator loss: 0.66630
Iteration 550 -- Epoch 1/2 -- Discriminator loss: 1.37137 -- Generator loss: 0.64537
Iteration 560 -- Epoch 1/2 -- Discriminator loss: 1.36949 -- Generator loss: 0.67662
Iteration 570 -- Epoch 1/2 -- Discriminator loss: 1.36481 -- Generator loss: 0.66396
Iteration 580 -- Epoch 1/2 -- Discriminator loss: 1.37091 -- Generator loss: 0.66140
Iteration 590 -- Epoch 1/2 -- Discriminator loss: 1.36566 -- Generator loss: 0.66276
Iteration 600 -- Epoch 1/2 -- Discriminator loss: 1.37510 -- Generator loss: 0.65578
Iteration 610 -- Epoch 1/2 -- Discriminator loss: 1.37096 -- Generator loss: 0.65401
Iteration 620 -- Epoch 1/2 -- Discriminator loss: 1.36943 -- Generator loss: 0.65220
Iteration 630 -- Epoch 1/2 -- Discriminator loss: 1.37897 -- Generator loss: 0.65882
Iteration 640 -- Epoch 1/2 -- Discriminator loss: 1.36590 -- Generator loss: 0.66213
Iteration 650 -- Epoch 1/2 -- Discriminator loss: 1.37912 -- Generator loss: 0.65585
Iteration 660 -- Epoch 1/2 -- Discriminator loss: 1.36815 -- Generator loss: 0.65370
Iteration 670 -- Epoch 1/2 -- Discriminator loss: 1.36493 -- Generator loss: 0.66238
Iteration 680 -- Epoch 1/2 -- Discriminator loss: 1.37017 -- Generator loss: 0.65789
Iteration 690 -- Epoch 1/2 -- Discriminator loss: 1.37836 -- Generator loss: 0.66019
Iteration 700 -- Epoch 1/2 -- Discriminator loss: 1.37697 -- Generator loss: 0.65255
Iteration 710 -- Epoch 1/2 -- Discriminator loss: 1.37949 -- Generator loss: 0.65715
Iteration 720 -- Epoch 1/2 -- Discriminator loss: 1.37272 -- Generator loss: 0.66632
Iteration 730 -- Epoch 1/2 -- Discriminator loss: 1.38274 -- Generator loss: 0.64531
Iteration 740 -- Epoch 1/2 -- Discriminator loss: 1.37086 -- Generator loss: 0.64989
Iteration 750 -- Epoch 1/2 -- Discriminator loss: 1.37694 -- Generator loss: 0.64999
Iteration 760 -- Epoch 1/2 -- Discriminator loss: 1.37796 -- Generator loss: 0.64293
Iteration 770 -- Epoch 1/2 -- Discriminator loss: 1.38321 -- Generator loss: 0.65507
Iteration 780 -- Epoch 1/2 -- Discriminator loss: 1.37870 -- Generator loss: 0.65869
Iteration 790 -- Epoch 1/2 -- Discriminator loss: 1.37942 -- Generator loss: 0.66168
Iteration 800 -- Epoch 1/2 -- Discriminator loss: 1.37840 -- Generator loss: 0.66533
Iteration 810 -- Epoch 1/2 -- Discriminator loss: 1.37507 -- Generator loss: 0.65634
Iteration 820 -- Epoch 1/2 -- Discriminator loss: 1.38242 -- Generator loss: 0.65277
Iteration 830 -- Epoch 1/2 -- Discriminator loss: 1.37558 -- Generator loss: 0.64894
Iteration 840 -- Epoch 1/2 -- Discriminator loss: 1.38469 -- Generator loss: 0.66445
Iteration 850 -- Epoch 1/2 -- Discriminator loss: 1.37776 -- Generator loss: 0.66411
Iteration 860 -- Epoch 1/2 -- Discriminator loss: 1.38281 -- Generator loss: 0.65188
Iteration 870 -- Epoch 1/2 -- Discriminator loss: 1.37862 -- Generator loss: 0.65542
Iteration 880 -- Epoch 1/2 -- Discriminator loss: 1.37978 -- Generator loss: 0.65727
Iteration 890 -- Epoch 1/2 -- Discriminator loss: 1.36783 -- Generator loss: 0.65369
Iteration 900 -- Epoch 1/2 -- Discriminator loss: 1.37503 -- Generator loss: 0.64617
Iteration 910 -- Epoch 1/2 -- Discriminator loss: 1.38052 -- Generator loss: 0.65787
Iteration 920 -- Epoch 1/2 -- Discriminator loss: 1.37869 -- Generator loss: 0.64722
Iteration 930 -- Epoch 1/2 -- Discriminator loss: 1.36779 -- Generator loss: 0.65876
Iteration  10 -- Epoch 2/2 -- Discriminator loss: 1.38041 -- Generator loss: 0.64901
Iteration  20 -- Epoch 2/2 -- Discriminator loss: 1.36632 -- Generator loss: 0.66462
Iteration  30 -- Epoch 2/2 -- Discriminator loss: 1.37622 -- Generator loss: 0.66340
Iteration  40 -- Epoch 2/2 -- Discriminator loss: 1.37230 -- Generator loss: 0.65485
Iteration  50 -- Epoch 2/2 -- Discriminator loss: 1.37740 -- Generator loss: 0.67327
Iteration  60 -- Epoch 2/2 -- Discriminator loss: 1.36974 -- Generator loss: 0.67467
Iteration  70 -- Epoch 2/2 -- Discriminator loss: 1.36927 -- Generator loss: 0.66660
Iteration  80 -- Epoch 2/2 -- Discriminator loss: 1.36256 -- Generator loss: 0.65913
Iteration  90 -- Epoch 2/2 -- Discriminator loss: 1.37550 -- Generator loss: 0.65346
Iteration 100 -- Epoch 2/2 -- Discriminator loss: 1.37662 -- Generator loss: 0.65400
Iteration 110 -- Epoch 2/2 -- Discriminator loss: 1.39438 -- Generator loss: 0.65282
Iteration 120 -- Epoch 2/2 -- Discriminator loss: 1.37653 -- Generator loss: 0.64456
Iteration 130 -- Epoch 2/2 -- Discriminator loss: 1.38077 -- Generator loss: 0.66933
Iteration 140 -- Epoch 2/2 -- Discriminator loss: 1.38291 -- Generator loss: 0.65916
Iteration 150 -- Epoch 2/2 -- Discriminator loss: 1.36375 -- Generator loss: 0.66532
Iteration 160 -- Epoch 2/2 -- Discriminator loss: 1.37523 -- Generator loss: 0.66558
Iteration 170 -- Epoch 2/2 -- Discriminator loss: 1.37821 -- Generator loss: 0.65200
Iteration 180 -- Epoch 2/2 -- Discriminator loss: 1.37769 -- Generator loss: 0.66739
Iteration 190 -- Epoch 2/2 -- Discriminator loss: 1.38169 -- Generator loss: 0.65740
Iteration 200 -- Epoch 2/2 -- Discriminator loss: 1.38232 -- Generator loss: 0.65991
Iteration 210 -- Epoch 2/2 -- Discriminator loss: 1.35996 -- Generator loss: 0.66547
Iteration 220 -- Epoch 2/2 -- Discriminator loss: 1.37690 -- Generator loss: 0.66720
Iteration 230 -- Epoch 2/2 -- Discriminator loss: 1.37850 -- Generator loss: 0.64915
Iteration 240 -- Epoch 2/2 -- Discriminator loss: 1.38891 -- Generator loss: 0.65640
Iteration 250 -- Epoch 2/2 -- Discriminator loss: 1.39062 -- Generator loss: 0.66178
Iteration 260 -- Epoch 2/2 -- Discriminator loss: 1.37938 -- Generator loss: 0.67267
Iteration 270 -- Epoch 2/2 -- Discriminator loss: 1.38111 -- Generator loss: 0.66096
Iteration 280 -- Epoch 2/2 -- Discriminator loss: 1.38243 -- Generator loss: 0.65928
Iteration 290 -- Epoch 2/2 -- Discriminator loss: 1.37663 -- Generator loss: 0.65826
Iteration 300 -- Epoch 2/2 -- Discriminator loss: 1.38177 -- Generator loss: 0.66911
Iteration 310 -- Epoch 2/2 -- Discriminator loss: 1.36855 -- Generator loss: 0.67581
Iteration 320 -- Epoch 2/2 -- Discriminator loss: 1.36771 -- Generator loss: 0.66124
Iteration 330 -- Epoch 2/2 -- Discriminator loss: 1.37353 -- Generator loss: 0.66605
Iteration 340 -- Epoch 2/2 -- Discriminator loss: 1.38160 -- Generator loss: 0.66426
Iteration 350 -- Epoch 2/2 -- Discriminator loss: 1.37789 -- Generator loss: 0.66006
Iteration 360 -- Epoch 2/2 -- Discriminator loss: 1.38129 -- Generator loss: 0.65441
Iteration 370 -- Epoch 2/2 -- Discriminator loss: 1.39422 -- Generator loss: 0.66610
Iteration 380 -- Epoch 2/2 -- Discriminator loss: 1.38747 -- Generator loss: 0.67008
Iteration 390 -- Epoch 2/2 -- Discriminator loss: 1.38172 -- Generator loss: 0.66062
Iteration 400 -- Epoch 2/2 -- Discriminator loss: 1.37947 -- Generator loss: 0.66055
Iteration 410 -- Epoch 2/2 -- Discriminator loss: 1.38437 -- Generator loss: 0.65776
Iteration 420 -- Epoch 2/2 -- Discriminator loss: 1.37906 -- Generator loss: 0.66820
Iteration 430 -- Epoch 2/2 -- Discriminator loss: 1.37124 -- Generator loss: 0.66141
Iteration 440 -- Epoch 2/2 -- Discriminator loss: 1.37486 -- Generator loss: 0.65754
Iteration 450 -- Epoch 2/2 -- Discriminator loss: 1.37750 -- Generator loss: 0.66035
Iteration 460 -- Epoch 2/2 -- Discriminator loss: 1.38407 -- Generator loss: 0.65515
Iteration 470 -- Epoch 2/2 -- Discriminator loss: 1.37095 -- Generator loss: 0.67082
Iteration 480 -- Epoch 2/2 -- Discriminator loss: 1.38100 -- Generator loss: 0.65101
Iteration 490 -- Epoch 2/2 -- Discriminator loss: 1.37115 -- Generator loss: 0.67341
Iteration 500 -- Epoch 2/2 -- Discriminator loss: 1.38195 -- Generator loss: 0.65977
Iteration 510 -- Epoch 2/2 -- Discriminator loss: 1.37001 -- Generator loss: 0.66375
Iteration 520 -- Epoch 2/2 -- Discriminator loss: 1.38497 -- Generator loss: 0.65633
Iteration 530 -- Epoch 2/2 -- Discriminator loss: 1.37383 -- Generator loss: 0.65780
Iteration 540 -- Epoch 2/2 -- Discriminator loss: 1.38214 -- Generator loss: 0.66265
Iteration 550 -- Epoch 2/2 -- Discriminator loss: 1.38205 -- Generator loss: 0.65582
Iteration 560 -- Epoch 2/2 -- Discriminator loss: 1.37785 -- Generator loss: 0.66576
Iteration 570 -- Epoch 2/2 -- Discriminator loss: 1.39917 -- Generator loss: 0.64841
Iteration 580 -- Epoch 2/2 -- Discriminator loss: 1.37042 -- Generator loss: 0.67963
Iteration 590 -- Epoch 2/2 -- Discriminator loss: 1.37307 -- Generator loss: 0.67502
Iteration 600 -- Epoch 2/2 -- Discriminator loss: 1.37923 -- Generator loss: 0.67245
Iteration 610 -- Epoch 2/2 -- Discriminator loss: 1.38567 -- Generator loss: 0.65250
Iteration 620 -- Epoch 2/2 -- Discriminator loss: 1.38400 -- Generator loss: 0.67406
Iteration 630 -- Epoch 2/2 -- Discriminator loss: 1.39008 -- Generator loss: 0.67617
Iteration 640 -- Epoch 2/2 -- Discriminator loss: 1.38559 -- Generator loss: 0.66520
Iteration 650 -- Epoch 2/2 -- Discriminator loss: 1.38001 -- Generator loss: 0.67564
Iteration 660 -- Epoch 2/2 -- Discriminator loss: 1.37820 -- Generator loss: 0.67251
Iteration 670 -- Epoch 2/2 -- Discriminator loss: 1.37296 -- Generator loss: 0.67660
Iteration 680 -- Epoch 2/2 -- Discriminator loss: 1.36913 -- Generator loss: 0.68000
Iteration 690 -- Epoch 2/2 -- Discriminator loss: 1.38109 -- Generator loss: 0.67211
Iteration 700 -- Epoch 2/2 -- Discriminator loss: 1.38822 -- Generator loss: 0.65463
Iteration 710 -- Epoch 2/2 -- Discriminator loss: 1.37365 -- Generator loss: 0.66203
Iteration 720 -- Epoch 2/2 -- Discriminator loss: 1.39271 -- Generator loss: 0.66679
Iteration 730 -- Epoch 2/2 -- Discriminator loss: 1.38900 -- Generator loss: 0.66664
Iteration 740 -- Epoch 2/2 -- Discriminator loss: 1.37697 -- Generator loss: 0.66816
Iteration 750 -- Epoch 2/2 -- Discriminator loss: 1.38236 -- Generator loss: 0.65646
Iteration 760 -- Epoch 2/2 -- Discriminator loss: 1.38646 -- Generator loss: 0.66936
Iteration 770 -- Epoch 2/2 -- Discriminator loss: 1.38673 -- Generator loss: 0.66431
Iteration 780 -- Epoch 2/2 -- Discriminator loss: 1.39124 -- Generator loss: 0.66247
Iteration 790 -- Epoch 2/2 -- Discriminator loss: 1.38600 -- Generator loss: 0.66886
Iteration 800 -- Epoch 2/2 -- Discriminator loss: 1.37275 -- Generator loss: 0.66353
Iteration 810 -- Epoch 2/2 -- Discriminator loss: 1.38367 -- Generator loss: 0.68082
Iteration 820 -- Epoch 2/2 -- Discriminator loss: 1.38756 -- Generator loss: 0.66790
Iteration 830 -- Epoch 2/2 -- Discriminator loss: 1.38212 -- Generator loss: 0.67807
Iteration 840 -- Epoch 2/2 -- Discriminator loss: 1.38296 -- Generator loss: 0.67275
Iteration 850 -- Epoch 2/2 -- Discriminator loss: 1.38484 -- Generator loss: 0.67097
Iteration 860 -- Epoch 2/2 -- Discriminator loss: 1.38815 -- Generator loss: 0.67199
Iteration 870 -- Epoch 2/2 -- Discriminator loss: 1.38561 -- Generator loss: 0.67457
Iteration 880 -- Epoch 2/2 -- Discriminator loss: 1.37928 -- Generator loss: 0.67809
Iteration 890 -- Epoch 2/2 -- Discriminator loss: 1.37904 -- Generator loss: 0.67248
Iteration 900 -- Epoch 2/2 -- Discriminator loss: 1.37570 -- Generator loss: 0.67661
Iteration 910 -- Epoch 2/2 -- Discriminator loss: 1.38636 -- Generator loss: 0.66240
Iteration 920 -- Epoch 2/2 -- Discriminator loss: 1.38243 -- Generator loss: 0.65363
Iteration 930 -- Epoch 2/2 -- Discriminator loss: 1.38378 -- Generator loss: 0.65989
Done!

CelebA

Run your GANs on CelebA. It will take around 20 minutes on the average GPU to run one epoch. You can run the whole epoch or stop when it starts to generate realistic faces.


In [18]:
batch_size = 32
z_dim = 128
learning_rate = 0.0002
beta1 = 0.5

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
epochs = 1

celeba_dataset = helper.Dataset('celeba', glob(os.path.join(data_dir, 'img_align_celeba/*.jpg')))
with tf.Graph().as_default():
    train(epochs, batch_size, z_dim, learning_rate, beta1, celeba_dataset.get_batches,
          celeba_dataset.shape, celeba_dataset.image_mode)


Iteration  10 -- Epoch 1/1 -- Discriminator loss: 1.52326 -- Generator loss: 0.52265
Iteration  20 -- Epoch 1/1 -- Discriminator loss: 1.39052 -- Generator loss: 0.61177
Iteration  30 -- Epoch 1/1 -- Discriminator loss: 1.35005 -- Generator loss: 0.63169
Iteration  40 -- Epoch 1/1 -- Discriminator loss: 1.34553 -- Generator loss: 0.64501
Iteration  50 -- Epoch 1/1 -- Discriminator loss: 1.31878 -- Generator loss: 0.65214
Iteration  60 -- Epoch 1/1 -- Discriminator loss: 1.35357 -- Generator loss: 0.64057
Iteration  70 -- Epoch 1/1 -- Discriminator loss: 1.37064 -- Generator loss: 0.64581
Iteration  80 -- Epoch 1/1 -- Discriminator loss: 1.37834 -- Generator loss: 0.64680
Iteration  90 -- Epoch 1/1 -- Discriminator loss: 1.34896 -- Generator loss: 0.65379
Iteration 100 -- Epoch 1/1 -- Discriminator loss: 1.34780 -- Generator loss: 0.65107