-
DCGAN
2020-03-03 03:17:33DCGANDCGAN
《unsupervised representation learning with deep convolutional generative adversarial networks》
—深度卷积生成对抗网络(DCGAN)
作者:Alec Radford & Luke Metz
发表会议及时间:ICLR 2016一 论文导读
如果判别大图像,比如1024*1024,则输入神经元会过多,对于判别器,参数量过于巨大。为了减少参数量,自然就会想到卷积神经网络。
2012: Alexnet
- 怎么做呢?
效果结果很差!!!!
所以DCGAN产生了
主要贡献
- 提出了一系列策略,成功的将GAN和CNN进行结合
- 能够训练卷积生成对抗网络,使得生成效果大大提升
- 通过充分的实验,对原理做了直观全面的解释
DCGAN作为2016年GAN的代表之作起到了明显的划分作用
MLP与CNN
- MLP
●网络有多个隐层组成
●每一个神经元都和上一层中的所有节点连接
●参数量大,训练难度大
●会丢失像素间的空间信息
- CNN
●稀疏连接,参数少
●可以处理更复杂的图像
●可以利用像素间的空间信息
●可以引入池化、空洞卷积等操作
Decoder中常用的3种上采样
●upsampling 插值上采样
●unpooling 反池化
●deconvolution 转置卷积
双立方插值和双线性插值一样,只不过是把周围4个值,变成周围16个值最常用的就是双线性插值
反池化,因为位置的关系,保留住了特征,特别是位置信息特征
转置卷积,要先进行扩充,再进行卷积转置卷积,相比于插值和uppooling的最大优点,就是有着可学习参数的卷积核,典型应用U-NET
GAN网络结构
●由判别模型和生成模型两部分组成
●生成模型负责生成假样本,欺骗判别器
●判别模性来分辨真样本和假样本
●两个模型交替训练,类似对抗博弈的过程
过程
一 训练判别器,固定生成器,使用反向传播
二 固定判别器,训练生成器,使用反向传播
GAN网络训练过程示意图
并不是说,一开始判别器就很厉害,即警察很厉害,判别器和生成器是一起进步的(因为一起训练),判别器每次都要比生成器强一点才行,这样一直一起变强。
二 论文精读
一 去掉判别器中所有的池化,用卷积代替
为什么用卷积代替池化
因为,卷积带有可学习参数,在GAN网络中,判别器D主要目的是辅助生成器G达到最优,如果用池化,则其梯度会稀疏,会导致判别器学习困难,如果判别器学习困难,他就不能很好的辅助指导生成器学习。
同时,卷积也要有下采样的作用
二 生成器使用反卷积
三 去掉判别网络的全连接层
因为FC层其巨大的参数量计算提高了网络的训练难度,改用了卷积
四 改用batch normalization
五 激活函数改用tanh 和 Leaky RelLu
其目的和去池化等同,因为避免梯度稀疏,特别是Leaky RelLu
训练参数设置
训练GAN要了解
突变就是没有学到分布,只是进行了记忆
下图是没有突变,是学到了分布
三 代码实现
四 问题思索
恍然大悟!是解码,解码,解码(decode),以前一直把上采样,看作解码了,解码就是解码,是总称。
补充小知识:深度学习三巨头之一的Bengio Y的数学功底非常强,GAN网络的作者就是Bengio的学生,作为Bengio组出来的文章,数学论证一般都是非常充分的
-
dcgan
2019-04-25 16:04:56来源:...""" Deep Convolutional Generative Adversarial Network (DCGAN). Using deep convolutional generative adversarial networks (DCGAN) to ge...来源:https://github.com/aymericdamien/TensorFlow-Examples#tutorials
""" Deep Convolutional Generative Adversarial Network (DCGAN). Using deep convolutional generative adversarial networks (DCGAN) to generate digit images from a noise distribution. References: - Unsupervised representation learning with deep convolutional generative adversarial networks. A Radford, L Metz, S Chintala. arXiv:1511.06434. Links: - [DCGAN Paper](https://arxiv.org/abs/1511.06434). - [MNIST Dataset](http://yann.lecun.com/exdb/mnist/). Author: Aymeric Damien Project: https://github.com/aymericdamien/TensorFlow-Examples/ """ from __future__ import division, print_function, absolute_import import matplotlib.pyplot as plt import numpy as np import tensorflow as tf # Import MNIST data from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) # Training Params num_steps = 20000 batch_size = 32 # Network Params image_dim = 784 # 28*28 pixels * 1 channel gen_hidden_dim = 256 disc_hidden_dim = 256 noise_dim = 200 # Noise data points # Generator Network # Input: Noise, Output: Image def generator(x, reuse=False): with tf.variable_scope('Generator', reuse=reuse): # TensorFlow Layers automatically create variables and calculate their # shape, based on the input. x = tf.layers.dense(x, units=6 * 6 * 128) x = tf.nn.tanh(x) # Reshape to a 4-D array of images: (batch, height, width, channels) # New shape: (batch, 6, 6, 128) x = tf.reshape(x, shape=[-1, 6, 6, 128]) # Deconvolution, image shape: (batch, 14, 14, 64) x = tf.layers.conv2d_transpose(x, 64, 4, strides=2) # Deconvolution, image shape: (batch, 28, 28, 1) x = tf.layers.conv2d_transpose(x, 1, 2, strides=2) # Apply sigmoid to clip values between 0 and 1 x = tf.nn.sigmoid(x) return x # Discriminator Network # Input: Image, Output: Prediction Real/Fake Image def discriminator(x, reuse=False): with tf.variable_scope('Discriminator', reuse=reuse): # Typical convolutional neural network to classify images. x = tf.layers.conv2d(x, 64, 5) x = tf.nn.tanh(x) x = tf.layers.average_pooling2d(x, 2, 2) x = tf.layers.conv2d(x, 128, 5) x = tf.nn.tanh(x) x = tf.layers.average_pooling2d(x, 2, 2) x = tf.contrib.layers.flatten(x) x = tf.layers.dense(x, 1024) x = tf.nn.tanh(x) # Output 2 classes: Real and Fake images x = tf.layers.dense(x, 2) return x # Build Networks # Network Inputs noise_input = tf.placeholder(tf.float32, shape=[None, noise_dim]) real_image_input = tf.placeholder(tf.float32, shape=[None, 28, 28, 1]) # Build Generator Network gen_sample = generator(noise_input) # Build 2 Discriminator Networks (one from noise input, one from generated samples) disc_real = discriminator(real_image_input) disc_fake = discriminator(gen_sample, reuse=True) disc_concat = tf.concat([disc_real, disc_fake], axis=0) # Build the stacked generator/discriminator stacked_gan = discriminator(gen_sample, reuse=True) # Build Targets (real or fake images) disc_target = tf.placeholder(tf.int32, shape=[None]) gen_target = tf.placeholder(tf.int32, shape=[None]) # Build Loss disc_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( logits=disc_concat, labels=disc_target)) gen_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( logits=stacked_gan, labels=gen_target)) # Build Optimizers optimizer_gen = tf.train.AdamOptimizer(learning_rate=0.001) optimizer_disc = tf.train.AdamOptimizer(learning_rate=0.001) # Training Variables for each optimizer # By default in TensorFlow, all variables are updated by each optimizer, so we # need to precise for each one of them the specific variables to update. # Generator Network Variables gen_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Generator') # Discriminator Network Variables disc_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Discriminator') # Create training operations train_gen = optimizer_gen.minimize(gen_loss, var_list=gen_vars) train_disc = optimizer_disc.minimize(disc_loss, var_list=disc_vars) # Initialize the variables (i.e. assign their default value) init = tf.global_variables_initializer() # Start training with tf.Session() as sess: # Run the initializer sess.run(init) for i in range(1, num_steps+1): # Prepare Input Data # Get the next batch of MNIST data (only images are needed, not labels) batch_x, _ = mnist.train.next_batch(batch_size) batch_x = np.reshape(batch_x, newshape=[-1, 28, 28, 1]) # Generate noise to feed to the generator z = np.random.uniform(-1., 1., size=[batch_size, noise_dim]) # Prepare Targets (Real image: 1, Fake image: 0) # The first half of data fed to the generator are real images, # the other half are fake images (coming from the generator). batch_disc_y = np.concatenate( [np.ones([batch_size]), np.zeros([batch_size])], axis=0) # Generator tries to fool the discriminator, thus targets are 1. batch_gen_y = np.ones([batch_size]) # Training feed_dict = {real_image_input: batch_x, noise_input: z, disc_target: batch_disc_y, gen_target: batch_gen_y} _, _, gl, dl = sess.run([train_gen, train_disc, gen_loss, disc_loss], feed_dict=feed_dict) if i % 100 == 0 or i == 1: print('Step %i: Generator Loss: %f, Discriminator Loss: %f' % (i, gl, dl)) # Generate images from noise, using the generator network. f, a = plt.subplots(4, 10, figsize=(10, 4)) for i in range(10): # Noise input. z = np.random.uniform(-1., 1., size=[4, noise_dim]) g = sess.run(gen_sample, feed_dict={noise_input: z}) for j in range(4): # Generate image from noise. Extend to 3 channels for matplot figure. img = np.reshape(np.repeat(g[j][:, :, np.newaxis], 3, axis=2), newshape=(28, 28, 3)) a[j][i].imshow(img) f.show() plt.draw() plt.waitforbuttonpress()
-
Add DCGAN
2020-12-26 02:00:41<p>How about adding a DCGAN implementation to Bolts? In a private repo, I refactored the PyTorch example implementation (https://github.com/pytorch/examples/blob/master/dcgan/main.py) to PyTorch ...
-
从代理HTTP角度看网红直播
-
Liunx 优化思路与实操步骤
-
鸿蒙系统Harmonyos源码架构分析-第1期第2课
-
Samba 服务配置与管理
-
Python教程:__iter__和__next__
-
URL编程——获取指向的资源
-
PPT大神之路高清教程
-
小红书直播数据!小红书图文视频笔记数据榜单
-
别就知道吃汤圆!你知道元宵节是用来脱单的吗?
-
exports is not defined
-
react实现复制input输入框内容到粘贴板
-
说说电商行业的助手高匿代理
-
react-native-document-picker一个适合react native打开本地文件,上传文件的组件
-
Thinkpad-E440-Hackintosh-master.zip
-
AnyTXT Searcher(本地文本搜索工具)官方中文版V1.2.394 | 可批量搜索txt文件内容.zip
-
第一型错误与第二型错误( I 型错误 II 型错误)
-
servlet.docx
-
测试wifi芯片的base.apk测试软件
-
钡原子三重态的碰撞诱导受激辐射
-
2月第3周小红书视频、图文、直播排行榜