DCGAN论文笔记+源码解析

DCGAN论文笔记+源码解析

论文地址:UNSUPERVISED REPRESENTATION LEARNING WITH DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS

源码地址:DCGAN in TensorFlow

DCGAN,Deep Convolutional Generative Adversarial Networks是生成对抗网络(Generative Adversarial Networks)的一种延伸,将卷积网络引入到生成式模型当中来做无监督的训练,利用卷积网络强大的特征提取能力来提高生成网络的学习效果。

DCGAN有以下特点:

1.在判别器模型中使用strided convolutions(跨步卷积)来替代空间池化(pooling),而在生成器模型中使用fractional strided convolutions,即deconv,反卷积层。

2.除了生成器模型的输出层和判别器模型的输入层,在网络其它层上都使用了Batch Normalization,使用BN可以稳定学习,有助于处理初始化不良导致的训练问题。

3.去除了全连接层,而直接使用卷积层连接生成器和判别器的输入层以及输出层。

4.在生成器的输出层使用Tanh激活函数,而在其它层使用ReLU;在判别器上使用leaky ReLU。

原论文中只给出了在LSUN实验上的生成器模型的结构图如下:


但是对于实验细节以及方法的介绍并不是很详细,于是便从源码入手来理解DCGAN的工作原理。

先看main.py:

  1. with tf.Session(config=run_config) as sess:
  2. if FLAGS.dataset == 'mnist':
  3. dcgan = DCGAN(
  4. sess,
  5. input_width=FLAGS.input_width,
  6. input_height=FLAGS.input_height,
  7. output_width=FLAGS.output_width,
  8. output_height=FLAGS.output_height,
  9. batch_size=FLAGS.batch_size,
  10. y_dim= 10,
  11. c_dim= 1,
  12. dataset_name=FLAGS.dataset,
  13. input_fname_pattern=FLAGS.input_fname_pattern,
  14. is_crop=FLAGS.is_crop,
  15. checkpoint_dir=FLAGS.checkpoint_dir,
  16. sample_dir=FLAGS.sample_dir)
因为我们使用DCGAN来生成MNIST数字手写体图像,注意这里的y_dim=10,表示0到9这10个类别,c_dim=1,表示灰度图像。

再看model.py:

  1. def discriminator(self, image, y=None, reuse=False):
  2. with tf.variable_scope( "discriminator") as scope:
  3. if reuse:
  4. scope.reuse_variables()
  5. yb = tf.reshape(y, [self.batch_size, 1, 1, self.y_dim])
  6. x = conv_cond_concat(image, yb)
  7. h0 = lrelu(conv2d(x, self.c_dim + self.y_dim, name= 'd_h0_conv'))
  8. h0 = conv_cond_concat(h0, yb)
  9. h1 = lrelu(self.d_bn1(conv2d(h0, self.df_dim + self.y_dim, name= 'd_h1_conv')))
  10. h1 = tf.reshape(h1, [self.batch_size, -1])
  11. h1 = tf.concat_v2([h1, y], 1)
  12. h2 = lrelu(self.d_bn2(linear(h1, self.dfc_dim, 'd_h2_lin')))
  13. h2 = tf.concat_v2([h2, y], 1)
  14. h3 = linear(h2, 1, 'd_h3_lin')
  15. return tf.nn.sigmoid(h3), h3
这里batch_size=64,image的维度为[64 28 28 1],y的维度是[64 10],yb的维度[64 1 1 10],x将image和yb连接起来,这相当于是使用了Conditional GAN,为图像提供标签作为条件信息,于是x的维度是[64 28 28 11],将x输入到卷积层conv2d,conv2d的代码如下:

  1. def conv2d(input_, output_dim,
  2. k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
  3. name="conv2d"):
  4. with tf.variable_scope(name):
  5. w = tf.get_variable( 'w', [k_h, k_w, input_.get_shape()[ -1], output_dim],
  6. initializer=tf.truncated_normal_initializer(stddev=stddev))
  7. conv = tf.nn.conv2d(input_, w, strides=[ 1, d_h, d_w, 1], padding= 'SAME')
  8. biases = tf.get_variable( 'biases', [output_dim], initializer=tf.constant_initializer( 0.0))
  9. conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
  10. return conv
卷积核的大小为5*5,stride为[1 2 2 1],通过2的卷积步长可以替代pooling进行降维,padding=‘SAME’,则卷积的输出维度为[64 14 14 11]。然后使用batch normalization及leaky ReLU的激活层,输出与yb再进行concat,得到h0,维度为[64 14 14 21]。同理,h1的维度为[64  7*7*74+10],h2的维度为[64 1024+10],然后连接一个线性输出,得到h3,维度为[64 1],由于我们希望判别器的输出代表概率,所以最终使用一个sigmoid的激活。

  1. def generator(self, z, y=None):
  2. with tf.variable_scope( "generator") as scope:
  3. s_h, s_w = self.output_height, self.output_width
  4. s_h2, s_h4 = int(s_h/ 2), int(s_h/ 4)
  5. s_w2, s_w4 = int(s_w/ 2), int(s_w/ 4)
  6. # yb = tf.expand_dims(tf.expand_dims(y, 1),2)
  7. yb = tf.reshape(y, [self.batch_size, 1, 1, self.y_dim])
  8. z = tf.concat_v2([z, y], 1)
  9. h0 = tf.nn.relu(
  10. self.g_bn0(linear(z, self.gfc_dim, 'g_h0_lin')))
  11. h0 = tf.concat_v2([h0, y], 1)
  12. h1 = tf.nn.relu(self.g_bn1(
  13. linear(h0, self.gf_dim* 2*s_h4*s_w4, 'g_h1_lin')))
  14. h1 = tf.reshape(h1, [self.batch_size, s_h4, s_w4, self.gf_dim * 2])
  15. h1 = conv_cond_concat(h1, yb)
  16. h2 = tf.nn.relu(self.g_bn2(deconv2d(h1,
  17. [self.batch_size, s_h2, s_w2, self.gf_dim * 2], name= 'g_h2')))
  18. h2 = conv_cond_concat(h2, yb)
  19. return tf.nn.sigmoid(
  20. deconv2d(h2, [self.batch_size, s_h, s_w, self.c_dim], name= 'g_h3'))
output_height和output_width为28,因此s_h和s_w为28,s_h2和s_w2为14,s_h4和s_w4为7。在这里z为平均分布的随机分布数,维度为[64 100],y的维度为[64 10],yb的维度是[64 1 1 10],z与y进行一个concat得到[64 110]的tensor,输入到一个线性层,输出维度是[64 1024],再经过batch normalization以及ReLU激活,并与y进行concat,输出h0的维度是[64 1034],同样的再经过一个线性层输出维度为[64 128*7*7],再进行reshape并与yb进行concat,得到h1,维度为[64 7 7 138],然后输入到一个deconv2d,做一个反卷积,也就是文中说的fractional strided convolutions,再经过batch normalization以及ReLU激活,并与yb进行concat,输出h2的维度是[64 14 14 138],最后再输入到deconv2d层以及sigmoid激活,得到生成器的输出,维度为[64 28 28 1]。

生成器以及判别器的输出:

  1. self.G = self.generator(self.z, self.y)
  2. self.D, self.D_logits = \
  3. self.discriminator(inputs, self.y, reuse= False)
  4. self.D_, self.D_logits_ = \
  5. self.discriminator(self.G, self.y, reuse= True)
其中D表示真实数据的判别器输出,D_表示生成数据的判别器输出。

再看损失函数:

  1. self.d_loss_real = tf.reduce_mean(
  2. tf.nn.sigmoid_cross_entropy_with_logits(
  3. logits=self.D_logits, targets=tf.ones_like(self.D)))
  4. self.d_loss_fake = tf.reduce_mean(
  5. tf.nn.sigmoid_cross_entropy_with_logits(
  6. logits=self.D_logits_, targets=tf.zeros_like(self.D_)))
  7. self.g_loss = tf.reduce_mean(
  8. tf.nn.sigmoid_cross_entropy_with_logits(
  9. logits=self.D_logits_, targets=tf.ones_like(self.D_)))
即对于真实数据,判别器的损失函数d_loss_real为判别器输出与1的交叉熵,而对于生成数据,判别器的损失函数d_loss_fake为输出与0的交叉熵,因此判别器的损失函数d_loss=d_loss_real+d_loss_fake;生成器的损失函数是g_loss判别器对于生成数据的输出与1的交叉熵。
优化器:
  1. d_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1) \
  2. .minimize(self.d_loss, var_list=self.d_vars)
  3. g_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1) \
  4. .minimize(self.g_loss, var_list=self.g_vars)
训练阶段:

  1. for epoch in xrange(config.epoch):
  2. batch_idxs = min(len(data_X), config.train_size) // config.batch_size
  3. for idx in xrange( 0, batch_idxs):
  4. batch_images = data_X[idx*config.batch_size:(idx+ 1)*config.batch_size]
  5. batch_labels = data_y[idx*config.batch_size:(idx+ 1)*config.batch_size]
  6. batch_images = np.array(batch).astype(np.float32)[:, :, :, None]
  7. batch_z = np.random.uniform( -1, 1, [config.batch_size, self.z_dim]) \
  8. .astype(np.float32)
  9. # Update D network
  10. _, summary_str = self.sess.run([d_optim, self.d_sum],
  11. feed_dict={
  12. self.inputs: batch_images,
  13. self.z: batch_z,
  14. self.y:batch_labels,
  15. })
  16. self.writer.add_summary(summary_str, counter)
  17. # Update G network
  18. _, summary_str = self.sess.run([g_optim, self.g_sum],
  19. feed_dict={
  20. self.z: batch_z,
  21. self.y:batch_labels,
  22. })
  23. self.writer.add_summary(summary_str, counter)
  24. # Run g_optim twice to make sure that d_loss does not go to zero (different from paper)
  25. _, summary_str = self.sess.run([g_optim, self.g_sum],
  26. feed_dict={ self.z: batch_z, self.y:batch_labels })
  27. self.writer.add_summary(summary_str, counter)
  28. errD_fake = self.d_loss_fake.eval({
  29. self.z: batch_z,
  30. self.y:batch_labels
  31. })
  32. errD_real = self.d_loss_real.eval({
  33. self.inputs: batch_images,
  34. self.y:batch_labels
  35. })
  36. errG = self.g_loss.eval({
  37. self.z: batch_z,
  38. self.y: batch_labels
  39. })
  40. counter += 1
与论文中不同的是,这里在一个batch中,更新两次生成器,更新一次判别器。

实验结果:

由于自己的笔记本配置有限,仅使用CPU来运行速度较慢,因此epoch仅设置为2,对于MNIST手写数字数据集的生成情况如下



原文:https://blog.csdn.net/wspba/article/details/54730871

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值