从2014年Ian Goodfellow提出GANs(Generative adversarial networks)以来,GANs可以说是目前深度学习领域最为热门的研究内容之一,这种可以人工生成数据的方法给我们带来了丰富的想象。有研究者已经能够自动生成相当真实的卧室、专辑封面、人脸等图像,并且在此基础上做了一些有趣的事情。当然那些工作可能会相当困难,下面我们来实现一个简单的例子,建立一个能够生成手写数字的GAN。
GAN tensorflow 实作

GAN architecture

首先回顾一下GAN的结构GAN tensorflow 实作
Generative adversarial networks包含了两个部分,一个是生成器generator ,一个是判别器discriminator 。discriminator能够评估给定一个图像和真实图像的相似程度,或者说有多大可能性是人工生成的图像。discriminator 实质上相当于一个二分类器,在我们的例子中它是一个CNN。generator能根据随机输入的值来得到一个图像,在我们的例子中的generator是deconvolutional neural network。在整个训练迭代过程中,生成器和判别器网络的weights和biases的值依然会根据误差反向传播理论来训练得到。discriminator需要学习如何分辨real images和generator制造的fake images。同时generator会根据discriminator的反馈结果去学习如何生成更加真实的图像以至于discriminator不能分辨。
GAN tensorflow 实作

Loading MNIST data

首先导入tensorflow等需要用到的函数库,TensorFlow中提取了能够非常方便地导入MNIST数据的read_data_sets函数。

import tensorflow as tf
import numpy as np
import datetime
import matplotlib.pyplot as plt
%matplotlib inline

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/")

MNIST中每个图像的初始格式是一个784维的向量。可以使用reshape还原成28x28的图像。

sample_image = mnist.train.next_batch(1)[0]
print(sample_image.shape)

sample_image = sample_image.reshape([28, 28])
plt.imshow(sample_image, cmap='Greys')

GAN tensorflow 实作

Discriminator network

GAN tensorflow 实作

判别器网络实际上和CNN相似,包含两个卷积层和两个全连接层。

def discriminator(images, reuse_variables=None):
    with tf.variable_scope(tf.get_variable_scope(), reuse=reuse_variables) as scope:
        # 第一个卷积层
        # 使用32个5 x 5卷积模板
        d_w1 = tf.get_variable('d_w1', [5, 5, 1, 32], initializer=tf.truncated_normal_initializer(stddev=0.02))
        d_b1 = tf.get_variable('d_b1', [32], initializer=tf.constant_initializer(0))
        d1 = tf.nn.conv2d(input=images, filter=d_w1, strides=[1, 1, 1, 1], padding='SAME')
        d1 = d1 + d_b1
        d1 = tf.nn.relu(d1)
        d1 = tf.nn.avg_pool(d1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

        # 第二个卷积层
        # 使用64个5 x 5卷积模板,每个模板包含32个通道
        d_w2 = tf.get_variable('d_w2', [5, 5, 32, 64], initializer=tf.truncated_normal_initializer(stddev=0.02))
        d_b2 = tf.get_variable('d_b2', [64], initializer=tf.constant_initializer(0))
        d2 = tf.nn.conv2d(input=d1, filter=d_w2, strides=[1, 1, 1, 1], padding='SAME')
        d2 = d2 + d_b2
        d2 = tf.nn.relu(d2)
        d2 = tf.nn.avg_pool(d2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

        # 第一个全连接层
        d_w3 = tf.get_variable('d_w3', [7 * 7 * 64, 1024], initializer=tf.truncated_normal_initializer(stddev=0.02))
        d_b3 = tf.get_variable('d_b3', [1024], initializer=tf.constant_initializer(0))
        d3 = tf.reshape(d2, [-1, 7 * 7 * 64])
        d3 = tf.matmul(d3, d_w3)
        d3 = d3 + d_b3
        d3 = tf.nn.relu(d3)

        # 第二个全连接层
        d_w4 = tf.get_variable('d_w4', [1024, 1], initializer=tf.truncated_normal_initializer(stddev=0.02))
        d_b4 = tf.get_variable('d_b4', [1], initializer=tf.constant_initializer(0))
        d4 = tf.matmul(d3, d_w4) + d_b4

        # 最后输出一个非尺度化的值
        return d4

Generator network

GAN tensorflow 实作
生成器根据输入的随机的d维向量,最终输出一个28 x 28图像(实际用784维向量表示)。在生成器的每层将会使用到ReLU激活函数和batch normalization。
batch normalization 可能会有两个好处:更快的训练速度和更高的全局准确率。
GAN tensorflow 实作

def generator(z, batch_size, z_dim):
    g_w1 = tf.get_variable('g_w1', [z_dim, 3136], dtype=tf.float32, 
                           initializer=tf.truncated_normal_initializer(stddev=0.02))
    g_b1 = tf.get_variable('g_b1', [3136], initializer=tf.truncated_normal_initializer(stddev=0.02))
    g1 = tf.matmul(z, g_w1) + g_b1
    g1 = tf.reshape(g1, [-1, 56, 56, 1])
    g1 = tf.contrib.layers.batch_norm(g1, epsilon=1e-5, scope='bn1')
    g1 = tf.nn.relu(g1)

    g_w2 = tf.get_variable('g_w2', [3, 3, 1, z_dim/2], dtype=tf.float32, 
                       initializer=tf.truncated_normal_initializer(stddev=0.02))
    g_b2 = tf.get_variable('g_b2', [z_dim/2], initializer=tf.truncated_normal_initializer(stddev=0.02))
    g2 = tf.nn.conv2d(g1, g_w2, strides=[1, 2, 2, 1], padding='SAME')
    g2 = g2 + g_b2
    g2 = tf.contrib.layers.batch_norm(g2, epsilon=1e-5, scope='bn2')
    g2 = tf.nn.relu(g2)
    g2 = tf.image.resize_images(g2, [56, 56])

    g_w3 = tf.get_variable('g_w3', [3, 3, z_dim/2, z_dim/4], dtype=tf.float32, 
                       initializer=tf.truncated_normal_initializer(stddev=0.02))
    g_b3 = tf.get_variable('g_b3', [z_dim/4], initializer=tf.truncated_normal_initializer(stddev=0.02))
    g3 = tf.nn.conv2d(g2, g_w3, strides=[1, 2, 2, 1], padding='SAME')
    g3 = g3 + g_b3
    g3 = tf.contrib.layers.batch_norm(g3, epsilon=1e-5, scope='bn3')
    g3 = tf.nn.relu(g3)
    g3 = tf.image.resize_images(g3, [56, 56])

    g_w4 = tf.get_variable('g_w4', [1, 1, z_dim/4, 1], dtype=tf.float32, 
                       initializer=tf.truncated_normal_initializer(stddev=0.02))
    g_b4 = tf.get_variable('g_b4', [1], initializer=tf.truncated_normal_initializer(stddev=0.02))
    g4 = tf.nn.conv2d(g3, g_w4, strides=[1, 2, 2, 1], padding='SAME')
    g4 = g4 + g_b4
    g4 = tf.sigmoid(g4)
    
    # 输出g4的维度: batch_size x 28 x 28 x 1
    return g4

Training a GAN

# 清除默认图的堆栈,并设置全局图为默认图
tf.reset_default_graph()
batch_size = 50

z_placeholder = tf.placeholder(tf.float32, [None, z_dimensions], name='z_placeholder') 

x_placeholder = tf.placeholder(tf.float32, shape = [None,28,28,1], name='x_placeholder') 

Gz = generator(z_placeholder, batch_size, z_dimensions) 
Dx = discriminator(x_placeholder) 
Dg = discriminator(Gz, reuse_variables=True)

#discriminator 的loss 分为两部分
d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = Dx, labels = tf.ones_like(Dx)))
d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = Dg, labels = tf.zeros_like(Dg)))
d_loss=d_loss_real + d_loss_fake  
# Generator的目标是生成尽可能真实的图像,所以计算Dg和1的loss
g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = Dg, labels = tf.ones_like(Dg)))  

上面计算了loss 函数,接下来需要定义优化器optimizer。generator的optimizer只更新generator的网络权值,训练discriminator的时候需要固定generator的网络权值同时更新discriminator的权值。

tvars = tf.trainable_variables()

#分别保存discriminator和generator的权值
d_vars = [var for var in tvars if 'd_' in var.name]
g_vars = [var for var in tvars if 'g_' in var.name]

print([v.name for v in d_vars])
print([v.name for v in g_vars])

Adam是GAN的最好的优化方法,它利用了自适应学习率和学习惯性。调用Adam's minimize function来寻找最小loss,并且通过var_list来指定需要更新的参数。


d_trainer = tf.train.AdamOptimizer(0.0003).minimize(d_loss, var_list=d_vars)
g_trainer = tf.train.AdamOptimizer(0.0001).minimize(g_loss, var_list=g_vars)

使用TensorBoard来观察训练情况,打开terminal输入
tensorboard --logdir=tensorboard/

打开TensorBoard的地址是http://localhost:6006

tf.get_variable_scope().reuse_variables()

tf.summary.scalar('Generator_loss', g_loss)
tf.summary.scalar('Discriminator_loss_real', d_loss_real)
tf.summary.scalar('Discriminator_loss_fake', d_loss_fake)

images_for_tensorboard = generator(z_placeholder, batch_size, z_dimensions)
tf.summary.image('Generated_images', images_for_tensorboard, 5)
merged = tf.summary.merge_all()
logdir = "tensorboard/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + "/"
writer = tf.summary.FileWriter(logdir, sess.graph)

下面进行迭代更新参数。对discriminator先进行预训练,这样对generator的训练有好处。

sess = tf.Session()
sess.run(tf.global_variables_initializer())

# 对discriminator的预训练
for i in range(300):
    z_batch = np.random.normal(0, 1, size=[batch_size, z_dimensions])
    real_image_batch = mnist.train.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1])
    _, __, dLossReal, dLossFake = sess.run([d_trainer_real, d_trainer_fake, d_loss_real, d_loss_fake],
                                           {x_placeholder: real_image_batch, z_placeholder: z_batch})

    if(i % 100 == 0):
        print("dLossReal:", dLossReal, "dLossFake:", dLossFake)

# 交替训练 generator和discriminator
for i in range(100000):
    real_image_batch = mnist.train.next_batch(batch_size)[0].reshape([batch_size, 28, 28, 1])
    z_batch = np.random.normal(0, 1, size=[batch_size, z_dimensions])

    # 用 real and fake images对discriminator训练
    _,dLossReal, dLossFake = sess.run([d_trainer,d_loss_real, d_loss_fake],
                                           {x_placeholder: real_image_batch, z_placeholder: z_batch})

    # 训练 generator
    z_batch = np.random.normal(0, 1, size=[batch_size, z_dimensions])
    _ = sess.run(g_trainer, feed_dict={z_placeholder: z_batch})

    if i % 10 == 0:
        # 更新 TensorBoard 统计
        z_batch = np.random.normal(0, 1, size=[batch_size, z_dimensions])
        summary = sess.run(merged, {z_placeholder: z_batch, x_placeholder: real_image_batch})
        writer.add_summary(summary, i)

    if i % 100 == 0:
        # 每 100 iterations, 输出一个生成的图像
        print("Iteration:", i, "at", datetime.datetime.now())
        z_batch = np.random.normal(0, 1, size=[1, z_dimensions])
        generated_images = generator(z_placeholder, 1, z_dimensions)
        images = sess.run(generated_images, {z_placeholder: z_batch})
        plt.imshow(images[0].reshape([28, 28]), cmap='Greys')
        plt.show()
        # 输出discriminator的值
        im = images[0].reshape([1, 28, 28, 1])
        result = discriminator(x_placeholder)
        estimate = sess.run(result, {x_placeholder: im})
        print("Estimate:", estimate)

More

众所周知,由于GAN的表达能力非常强,几乎能够刻画任意概率分布,GAN的训练过程是非常困难的(容易跑偏)。如果没有找到合适的超参和网络结构,并且进行合理的训练过程,容易在discriminator和generator中间出现一方压倒另一方的情况。
一种常见失败情况是discriminator压倒generator的时候,对generator生成的每个image,discriminator几乎都能认为是fake image,这时generator几乎找不到下降的梯度。因此对discriminator的输出并没有经过sigmoid 函数(sigmoid function 会将输出推向0或1)。
另一种失败情况是“mode collapse”,指的是generator发现并利用了discriminator某些漏洞。例如generator发现某个图像a能让discriminator判定为真,那么generator可能会学习到:对任意输入的noise vector z,只需要输出和a几乎相同的图像。
研究人员已经指出了一部分对建立更加稳定的GAN有帮助的GAN hacks

Resources

Ian Goodfellow 最近的GAN教程
...