Generative adversarial net with Mint


import numpy as np
import tensorflow as tf
import pickle
import matplotlib.pyplot as plt
from Gan import *
from model import *

#Tensorflow vesrion and read the dataset
print("TensorFlow Version: {}".format(tf.__version__))
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/')

# Define parameters
batch_size = 64
noise_size = 100
epochs = 5
n_samples = 25
learning_rate = 0.001
beta1 = 0.4
image_height=28
image_width=28
image_depth=1
noise_dim=100
data_shape=[64,28,28,1]
smooth=0.1
output_path= "/Users/changxingya/Documents/SublimePoject/GAN/output/sample{0}.jpg"

#input dataset(dataset image)
inputs_real = tf.placeholder(tf.float32, [None, image_height, image_width, image_depth], name='inputs_real')
inputs_noise = tf.placeholder(tf.float32, [None, noise_dim], name='inputs_noise')
g_outputs = get_generator(inputs_noise)
d_logits_real, d_outputs_real = get_discriminator(inputs_real)
d_logits_fake, d_outputs_fake = get_discriminator(g_outputs)

#calculate the loss
g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake,
                                                                                labels=tf.ones_like(d_outputs_fake)*(1-smooth)))

d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_real,
                                                                                     labels=tf.ones_like(d_outputs_real)*(1-smooth)))
d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake,
                                                                                     labels=tf.zeros_like(d_outputs_fake)))
d_loss = tf.add(d_loss_real, d_loss_fake)


#get the variables of the var.name
train_vars = tf.trainable_variables()  #get the all of the variable

g_vars = [var for var in train_vars if var.name.startswith("generator")]
d_vars = [var for var in train_vars if var.name.startswith("discriminator")]

with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
    g_opt = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(g_loss, var_list=g_vars)
    d_opt = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(d_loss, var_list=d_vars)


#Define the session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for i in range(epochs):
        for j in range(mnist.train.num_examples//batch_size):
            #record the time of step
            steps += 1
            #read the dataset and reshape the tensor
            batch = mnist.train.next_batch(batch_size)

            batch_images = batch[0].reshape((batch_size, data_shape[1], data_shape[2], data_shape[3]))
            # scale to -1, 1
            batch_images = batch_images * 2 - 1
            # noise
            batch_noise = np.random.uniform(-1, 1, size=(batch_size, noise_size))

            # run optimizer
            sess.run(g_opt, feed_dict={inputs_real: batch_images,
                                                inputs_noise: batch_noise})
            sess.run(d_opt, feed_dict={inputs_real: batch_images,
                                                inputs_noise: batch_noise})
            #when step%2 remainder of zero,print the number of the loss
            if steps % 2 == 0:
                train_loss_d = d_loss.eval({inputs_real: batch_images,
                                                inputs_noise: batch_noise})
                train_loss_g = g_loss.eval({inputs_real: batch_images,
                                                inputs_noise: batch_noise})

                print("Epoch {}/{}....".format(i+1, epochs),
                "Discriminator Loss: {:.4f}....".format(train_loss_d),
                "Generator Loss: {:.4f}....". format(train_loss_g))
                samples = sess.run(g_outputs,
                               feed_dict={inputs_noise: batch_noise})

                samples = np.squeeze(samples, -1)
                show_result(samples, output_path.format(steps))

def get_generator(input_image,lab):
    with tf.variable_scope("generator",reuse=tf.AUTO_REUSE) as scope0:
        conv1 = tf.layers.conv2d(input_image, 32, 3, strides=2, padding='same')
        conv1 = tf.maximum(alpha * conv1, conv1)
        conv1 = tf.nn.dropout(conv1, keep_prob=0.8)

                # 14 x 14 x 32 to 7 x 7 x 64
        conv2 = tf.layers.conv2d(conv1, 64, 3, strides=2, padding='same')
        conv2 = tf.layers.batch_normalization(conv2, training=True)
        conv2 = tf.maximum(alpha * conv2, conv2)
        conv2 = tf.nn.dropout(conv2, keep_prob=0.8)

                # 7 x 7 x 64 to 4 x 4 x 128
        conv3 = tf.layers.conv2d(conv2, 128, 3, strides=2, padding='same')
        conv3 = tf.layers.batch_normalization(conv3, training=True)
        conv3 = tf.maximum(alpha * conv3, conv3)
        conv3 = tf.nn.dropout(conv3, keep_prob=0.8)

                # 4 x 4 x 128 to 2 x 2 x 256
        conv4 = tf.layers.conv2d(conv3, 256, 3, strides=2, padding='same')
        conv4 = tf.layers.batch_normalization(conv4, training=True)
        conv4 = tf.maximum(alpha * conv4, conv4)
        conv4 = tf.nn.dropout(conv4, keep_prob=0.8)

                # 2 x 2 x 256 to 2*2*256 x 1
        flatten = tf.reshape(conv4, (-1, 2*2*256))  # (?,8192)

                # none*100  to none*4 x 4 x 512
                # 全连接层
        deconv1 = tf.reshape(flatten, [-1, 2, 2, 256])
                # batch normalization
        deconv1 = tf.layers.batch_normalization(deconv1, training=is_train)
                # Leaky ReLU
        deconv1 = tf.maximum(alpha * deconv1, deconv1)
                # dropout
        deconv1 = tf.nn.dropout(deconv1, keep_prob=0.8)
                # 4 x 4 x 512 to 7 x 7 x 256
                #卷积的一个逆向过程
        deconv2 = tf.layers.conv2d_transpose(deconv1, 128, 3, strides=1, padding='valid')

        deconv2 = tf.layers.batch_normalization(deconv2, training=is_train)
        deconv2 = tf.maximum(alpha * deconv2, deconv2)
        deconv2 = tf.nn.dropout(deconv2, keep_prob=0.8)

                # 7 x 7 256 to 14 x 14 x 128
        deconv3 = tf.layers.conv2d_transpose(deconv2, 64, 3, strides=2, padding='same')
        deconv3 = tf.layers.batch_normalization(deconv3, training=is_train)
        deconv3 = tf.maximum(alpha * deconv3, deconv3)
        deconv3 = tf.nn.dropout(deconv3, keep_prob=0.8)

        deconv4 = tf.layers.conv2d_transpose(deconv3, 32, 3, strides=2, padding='same')
        deconv4 = tf.layers.batch_normalization(deconv4, training=is_train)
        deconv4 = tf.maximum(alpha * deconv4, deconv4)
        deconv4 = tf.nn.dropout(deconv4, keep_prob=0.8)

                # 14 x 14 x 128 to 28 x 28 x 1
        logits = tf.layers.conv2d_transpose(deconv4, output_dim, 3, strides=2, padding='same')
                # MNIST原始数据集的像素范围在0-1,这里的生成图片范围为(-1,1)
                # 因此在训练时,记住要把MNIST像素范围进行resize
        outputs = tf.tanh(logits)

        return outputs

def get_discriminator(input_):
    with tf.variable_scope("discriminator",reuse=tf.AUTO_REUSE) as scope1:
                # 28 x 28 x 1 to 14 x 14 x 128
                # 第一层不加入BN


        layer1 = tf.layers.conv2d(input_, 32, 3, strides=2, padding='same')
        layer1 = tf.maximum(alpha * layer1, layer1)
        layer1 = tf.nn.dropout(layer1, keep_prob=0.8)

                # 14 x 14 x 128 to 7 x 7 x 256
        layer2 = tf.layers.conv2d(layer1, 64, 3, strides=2, padding='same')
        layer2 = tf.layers.batch_normalization(layer2, training=True)
        layer2 = tf.maximum(alpha * layer2, layer2)
        layer2 = tf.nn.dropout(layer2, keep_prob=0.8)

                # 7 x 7 x 256 to 4 x 4 x 512
        layer3 = tf.layers.conv2d(layer2, 128, 3, strides=2, padding='same')
        layer3 = tf.layers.batch_normalization(layer3, training=True)
        layer3 = tf.maximum(alpha * layer3, layer3)
        layer3 = tf.nn.dropout(layer3, keep_prob=0.8)

        layer4 = tf.layers.conv2d(layer3, 256, 3, strides=2, padding='same')
        layer4 = tf.layers.batch_normalization(layer4, training=True)
        layer4 = tf.maximum(alpha * layer4, layer4)
        layer4 = tf.nn.dropout(layer4, keep_prob=0.8)

                # 4 x 4 x 512 to 4*4*512 x 1
        flatten = tf.reshape(layer4, (-1, 4*4*256))  # (?,8192)
        logits = tf.layers.dense(flatten, 1)  # (?, 1)
        outputs = tf.sigmoid(logits)  # (?, 1)

        return logits, outputs


猜你喜欢

转载自blog.csdn.net/SoyCoder/article/details/80865259