2019-3-10——生成对抗网络GAN---生成mnist手写数字图像

 """
生成对抗网络(GAN,Generative Adversarial Networks)的基本原理很简单:
假设有两个网络,生成网络G和判别网络D。生成网络G接受一个随机的噪声z并生成图片,
记为G(z);判别网络D的作用是判别一张图片x是否真实,对于输入x,D(x)是x为真实图片的概率。
在训练过程中, 生成器努力让生成的图片更加真实从而使得判别器无法辨别图像的真假,
而D的目标就是尽量把分辨出真实图片和生成网络*出的图片,这个过程就类似于二人博弈,
G和D构成了一个动态的“博弈过程”。随着时间的推移,生成器和判别器在不断地进行对抗,
最终两个网络达到一个动态平衡:生成器生成的图像G(z)接近于真实图像分布,而判别器识别不出真假图像,
即D(G(z))=0.5。最后,我们就可以得到一个生成网络G,用来生成图片。
"""
import tensorflow as tf
from matplotlib import pyplot as plt
import os
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('/MNIST_data/',one_hot=True)
batch_size=64
units_size=128
learning_rate=0.001
epoch=300
smooth=0.1
"""定义生成模型"""
def generatorModel(noise_img,units_size,out_size,alpha=0.01):
"""生成器的目的是:对于生成的图片,G希望D打上标签1"""
with tf.variable_scope('generator'):
FC=tf.layers.dense(noise_img,units_size)
relu=tf.nn.leaky_relu(FC,alpha)
drop=tf.layers.dropout(relu,rate=0.2)
logits=tf.layers.dense(drop,out_size)
outputs=tf.tanh(logits)
return logits,outputs """定义判别模型"""
def discriminatorModel(images,unite_size,alpha=0.01,reuse=False):
"""
判别器的目的是:
1. 对于真实图片,D要为其打上标签1
2. 对于生成图片,D要为其打上标签0
"""
with tf.variable_scope('discriminator',reuse=reuse):
FC=tf.layers.dense(images,units_size)
relu=tf.nn.leaky_relu(FC,alpha)
logits=tf.layers.dense(relu,1)
outputs=tf.sigmoid(logits)
return logits,outputs
"""定义损失函数"""
def loss_fenction(real_logits,fake_logits,smooth):
"""生成器希望判别器判别出来的标签为1; tf.ones_like()创建一个将所有元素都设置为1的张量"""
G_loss=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
logits=fake_logits,
labels=tf.ones_like(fake_logits)*(1-smooth))
)
"""判别器识别生成器产出的图片,希望识别出来的标签为0;tf.zeros_like()创建一个将所有元素都设置为0的张量"""
fake_loss=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
logits=fake_logits,
labels=tf.zeros_like(fake_logits))
)
"""判别器判别真实图片,希望判别出来的标签为1;tf.ones_like()创建一个将所有元素都设置为1的张量"""
real_loss=tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(
logits=real_logits,
labels=tf.ones_like(real_logits)*(1-smooth))
)
D_loss=tf.add(fake_loss,real_loss)
return G_loss,fake_loss,real_loss,D_loss
"""定义优化器"""
def optimizer(G_loss,D_loss,learning_rate):
"""因为GAN中一共训练了两个网络,所以分别对G和D进行优化"""
train_var=tf.trainable_variables() #需要训练的变量
G_var=[var for var in train_var if var.name.startswith('generator')]
D_var=[var for var in train_var if var.name.startswith('discriminator')]
G_optimizer=tf.train.AdadeltaOptimizer(learning_rate).minimize(G_loss,var_list=G_var)
D_optimizer=tf.train.AdadeltaOptimizer(learning_rate).minimize(D_loss,var_list=D_var)
return G_optimizer,D_optimizer
"""训练"""
def train(mnist):
image_size = mnist.train.images[0].shape[0]
real_images = tf.placeholder(tf.float32,[None,image_size])
fake_images = tf.placeholder(tf.float32,[None,image_size])
"""调用生成模型生成假图片G_output"""
G_logits,G_output = generatorModel(fake_images,units_size,image_size)
"""D对真实图像的判别"""
real_logits,real_output = discriminatorModel(real_images,units_size)
"""D对G生成图像的判别"""
fake_logits,fake_output=discriminatorModel(G_output,units_size,reuse=True)
G_loss,real_loss,fake_loss,D_loss=loss_fenction(real_logits,fake_logits,smooth)
G_optimizer,D_optimizer=optimizer(G_loss,D_loss,learning_rate) saver=tf.train.Saver()
step=0
with tf.Session() as session:
session.run(tf.global_variables_initializer())
for Epoch in range(epoch):
for batch_i in range(mnist.train.num_examples//batch_size):
batch_image,_=mnist.train.next_batch(batch_size)
"""对图像像素进行scale,tanh的输出结果为(-1,1)"""
batch_image=batch_image*2-1
"""模型的输入噪声"""
noise_image=np.random.uniform(-1,1,size=(batch_size,image_size))#从均匀分布[-1,1)中随机采样
session.run(G_optimizer,feed_dict={fake_images:noise_image})
session.run(D_optimizer,feed_dict={real_images:batch_image,fake_images:noise_image})
step=step+1
loss_D= session.run(D_loss, feed_dict={real_images: batch_image, fake_images: noise_image})
loss_real= session.run(real_loss, feed_dict={real_images: batch_image, fake_images: noise_image})
loss_fake= session.run(fake_loss, feed_dict={real_images: batch_image, fake_images: noise_image})
loss_G= session.run(G_loss, feed_dict={fake_images: noise_image})
print('epoch:', Epoch, 'loss_D:', loss_D,'loss_real', loss_real,'loss_fake', loss_fake, 'loss_G', loss_G)
model_path=os.getcwd()+os.sep+"mnist.model"
saver.save(session,model_path,global_step=step)
"""定义主函数"""
def main(argv=None):
train(mnist)
if __name__ =='__main__':
tf.app.run()
 import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
import pickle
import example88_0 UNITS_SIZE = example88_0.units_size def generatorImage(image_size):
sample_images = tf.placeholder(tf.float32, [None, image_size])
G_logits, G_output = example88_0.generatorModel(sample_images, UNITS_SIZE, image_size)
saver = tf.train.Saver()
with tf.Session() as session:
session.run(tf.global_variables_initializer())
saver.restore(session, tf.train.latest_checkpoint('.'))
sample_noise = np.random.uniform(-1, 1, size=(25, image_size))
samples = session.run(G_output, feed_dict={sample_images: sample_noise})
with open('samples.pkl', 'wb') as f:
pickle.dump(samples, f) def show():
with open('samples.pkl', 'rb') as f:
samples = pickle.load(f)
fig, axes = plt.subplots(figsize=(7, 7), nrows=5, ncols=5, sharey=True, sharex=True)
for ax, image in zip(axes.flatten(), samples):
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.imshow(image.reshape((28, 28)), cmap='Greys_r')
plt.show() def main(argv=None):
image_size = example88_0.mnist.train.images[0].shape[0]
generatorImage(image_size)
show() if __name__ == '__main__':
tf.app.run()

2019-3-10——生成对抗网络GAN---生成mnist手写数字图像

上一篇:ActiveMQ基本详解与总结


下一篇:php五大运行模式CGI,FAST-CGI,CLI,ISAPI,APACHE模式浅谈