In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
def show_images(images):
images = np.reshape(images, [images.shape[0], -1]) # images reshape to (batch_size, D)
sqrtn = int(np.ceil(np.sqrt(images.shape[0])))
sqrtimg = int(np.ceil(np.sqrt(images.shape[1])))
fig = plt.figure(figsize=(sqrtn, sqrtn))
gs = gridspec.GridSpec(sqrtn, sqrtn)
gs.update(wspace=0.05, hspace=0.05)
for i, img in enumerate(images):
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_aspect('equal')
plt.imshow(img.reshape([sqrtimg,sqrtimg]))
return
def preprocess_img(x):
return 2 * x - 1.0
def deprocess_img(x):
return (x + 1.0) / 2.0
def rel_error(x,y):
return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))
def count_params():
param_count = np.sum([np.prod(x.get_shape().as_list()) for x in tf.global_variables()])
return param_count
def get_session():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
return session
In [2]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/tmp/mnist', one_hot=False)
show_images(mnist.train.next_batch(16)[0])
In [3]:
def leaky_relu(x, alpha=0.01):
# implement LeakyReLU
In [4]:
def sample_noise(batch_size, dim):
# implement sampling of random noise vector of dim dimension
In [5]:
def discriminator(x):
with tf.variable_scope("discriminator"):
fc1 = tf.layers.dense(x, 256, activation=leaky_relu, use_bias=True)
fc2 = tf.layers.dense(fc1, 256, activation=leaky_relu, use_bias=True)
logits = tf.layers.dense(fc2, 1, activation=None, use_bias=True)
return logits
In [6]:
def generator(z):
with tf.variable_scope("generator"):
fc1 = tf.layers.dense(z, 1024, activation=tf.nn.relu, use_bias=True)
fc2 = tf.layers.dense(fc1, 1024, activation=tf.nn.relu, use_bias=True)
img = tf.layers.dense(fc2, 784, activation=tf.tanh, use_bias=True)
return img
Compute the generator and discriminator loss. The generator loss is: $$\ell_G = -\mathbb{E}_{z \sim p(z)}\left[\log D(G(z))\right]$$ and the discriminator loss is: $$ \ell_D = -\mathbb{E}_{x \sim p_\text{data}}\left[\log D(x)\right] - \mathbb{E}_{z \sim p(z)}\left[\log \left(1-D(G(z))\right)\right]$$
In [7]:
def gan_loss(logits_real, logits_fake):
#implement GAN Loss
return D_loss, G_loss
In [8]:
def get_solvers(learning_rate=1e-3, beta1=0.5):
D_solver = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=beta1)
G_solver = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=beta1)
return D_solver, G_solver
In [9]:
tf.reset_default_graph()
# number of images for each batch
batch_size = 128
noise_dim = 96
x = tf.placeholder(tf.float32, [None, 784])
# sample noize and generate fake examples
with tf.variable_scope("") as scope:
# get logits from discriminator for fake and real images
D_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'discriminator')
G_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'generator')
D_solver, G_solver = get_solvers()
D_loss, G_loss = gan_loss(logits_real, logits_fake)
D_train_step = D_solver.minimize(D_loss, var_list=D_vars)
G_train_step = G_solver.minimize(G_loss, var_list=G_vars)
In [10]:
# a giant helper function
def run_a_gan(sess, G_train_step, G_loss, D_train_step, D_loss,
show_every=250, print_every=50, batch_size=128, num_epoch=10):
# compute the number of iterations we need
max_iter = int(mnist.train.num_examples*num_epoch/batch_size)
for it in range(max_iter):
# every show often, show a sample result
if it % show_every == 0:
samples = sess.run(G_sample)
fig = show_images(samples[:16])
plt.show()
print()
# run a batch of data through the network
minibatch,minbatch_y = mnist.train.next_batch(batch_size)
_, D_loss_curr = sess.run([D_train_step, D_loss], feed_dict={x: minibatch})
_, G_loss_curr = sess.run([G_train_step, G_loss])
# print loss every so often.
# We want to make sure D_loss doesn't go to 0
if it % print_every == 0:
print('Iter: {}, D: {:.4}, G:{:.4}'.format(it,D_loss_curr,G_loss_curr))
print('Final images')
samples = sess.run(G_sample)
fig = show_images(samples[:16])
plt.show()
In [11]:
with get_session() as sess:
sess.run(tf.global_variables_initializer())
run_a_gan(sess,G_train_step,G_loss,D_train_step,D_loss)
Least Squares GAN, a newer, more stable alternative to the original GAN loss function. We'll implement equation (9) in the paper, with the generator loss: $$\ell_G = \frac{1}{2}\mathbb{E}_{z \sim p(z)}\left[\left(D(G(z))-1\right)^2\right]$$ and the discriminator loss: $$ \ell_D = \frac{1}{2}\mathbb{E}_{x \sim p_\text{data}}\left[\left(D(x)-1\right)^2\right] + \frac{1}{2}\mathbb{E}_{z \sim p(z)}\left[ \left(D(G(z))\right)^2\right]$$
In [12]:
def lsgan_loss(score_real, score_fake):
# implement LS GAN Loss
return D_loss, G_loss
In [13]:
D_loss, G_loss = lsgan_loss(logits_real, logits_fake)
D_train_step = D_solver.minimize(D_loss, var_list=D_vars)
G_train_step = G_solver.minimize(G_loss, var_list=G_vars)
In [14]:
with get_session() as sess:
sess.run(tf.global_variables_initializer())
run_a_gan(sess, G_train_step, G_loss, D_train_step, D_loss)
In [15]:
def discriminator(x):
with tf.variable_scope("discriminator"):
shape = tf.shape(x);
images = tf.reshape(x, [shape[0], 28, 28, 1])
conv1 = tf.layers.conv2d(images, filters=32, kernel_size=(5, 5),
activation=leaky_relu, use_bias=True);
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID");
conv2 = tf.layers.conv2d(pool1, filters=64, kernel_size=(5, 5),
activation=leaky_relu, use_bias=True);
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID");
flatten = tf.contrib.layers.flatten(pool2);
dense1 = tf.layers.dense(flatten, 16*64, activation=leaky_relu, use_bias=True);
logits = tf.layers.dense(dense1, 1, activation=None, use_bias=True);
return logits
In [16]:
def generator(z):
with tf.variable_scope("generator"):
dense1 = tf.layers.dense(inputs=z, units=1024, activation=tf.nn.relu)
batchnorm1 = tf.layers.batch_normalization(inputs=dense1)
dense2 = tf.layers.dense(inputs=batchnorm1, units=6272, activation=tf.nn.relu)
batchnorm2 = tf.layers.batch_normalization(inputs=dense2)
images = tf.reshape(batchnorm2, [-1, 7, 7, 128])
conv1 = tf.layers.conv2d_transpose(inputs=images, filters=64, kernel_size=[4, 4],
strides=2, padding='SAME', activation=tf.nn.relu)
batchnorm3 = tf.layers.batch_normalization(inputs=conv1)
conv2 = tf.layers.conv2d_transpose(inputs=batchnorm3, filters=1, kernel_size=[4, 4],
strides=2, padding='SAME', activation=tf.tanh)
img = tf.reshape(conv2, [-1, 28 * 28])
return img
In [17]:
tf.reset_default_graph()
batch_size = 128
noise_dim = 96
x = tf.placeholder(tf.float32, [None, 784])
# sample noize and generate fake examples
with tf.variable_scope("") as scope:
# get logits from discriminator for fake and real images
D_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,'discriminator')
G_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,'generator')
D_solver,G_solver = get_solvers()
D_loss, G_loss = gan_loss(logits_real, logits_fake)
D_train_step = D_solver.minimize(D_loss, var_list=D_vars)
G_train_step = G_solver.minimize(G_loss, var_list=G_vars)
In [18]:
with get_session() as sess:
sess.run(tf.global_variables_initializer())
run_a_gan(sess,G_train_step,G_loss,D_train_step,D_loss,num_epoch=5)
In [ ]: