CS231n assignment3 Q5 Generative Adversarial Networks

LeakyReLU

def leaky_relu(x, alpha=0.01):
    """Compute the leaky ReLU activation function.

    Inputs:
    - x: TensorFlow Tensor with arbitrary shape
    - alpha: leak parameter for leaky ReLU

    Returns:
    TensorFlow Tensor with the same shape as x
    """
    # TODO: implement leaky ReLU
    condition = tf.less(x,0)
    res = tf.where(condition,alpha * x,x)
    return res

Random Noise

def sample_noise(batch_size, dim):
    """Generate random uniform noise from -1 to 1.

    Inputs:
    - batch_size: integer giving the batch size of noise to generate
    - dim: integer giving the dimension of the the noise to generate

    Returns:
    TensorFlow Tensor containing uniform noise in [-1, 1] with shape [batch_size, dim]
    """
    # TODO: sample and return noise
    return tf.random_uniform([batch_size,dim],minval = -1,maxval = 1)

Discriminator

Architecture:

  • Fully connected layer with input size 784 and output size 256
  • LeakyReLU with alpha 0.01
  • Fully connected layer with output size 256
  • LeakyReLU with alpha 0.01
  • Fully connected layer with output size 1
def discriminator(x):
    """Compute discriminator score for a batch of input images.

    Inputs:
    - x: TensorFlow Tensor of flattened input images, shape [batch_size, 784]

    Returns:
    TensorFlow Tensor with shape [batch_size, 1], containing the score
    for an image being real for each input image.
    """
    with tf.variable_scope("discriminator"):
        # TODO: implement architecture
        fc1 = tf.layers.dense(x,256,use_bias = True,name = ‘fc1‘)
        leaky_relu1 = leaky_relu(fc1,alpha = 0.01)
        fc2 = tf.layers.dense(leaky_relu1,256,use_bias = True,name = ‘fc2‘)
        leaky_relu2 = leaky_relu(fc2,alpha = 0.01)
        logits = tf.layers.dense(leaky_relu2,1,name = ‘fc3‘)
        return logits

Generator

Architecture:

  • Fully connected layer with inupt size tf.shape(z)[1] (the number of noise dimensions) and output size 1024
  • ReLU
  • Fully connected layer with output size 1024
  • ReLU
  • Fully connected layer with output size 784
  • TanH (To restrict every element of the output to be in the range [-1,1])
def generator(z):
    """Generate images from a random noise vector.

    Inputs:
    - z: TensorFlow Tensor of random noise with shape [batch_size, noise_dim]

    Returns:
    TensorFlow Tensor of generated images, with shape [batch_size, 784].
    """
    with tf.variable_scope("generator"):
        # TODO: implement architecture
        fc1 = tf.layers.dense(z,1024,use_bias = True,activation = tf.nn.relu)
        fc2 = tf.layers.dense(fc1,1024,use_bias = True,activation = tf.nn.relu)
        img = tf.layers.dense(fc2,784,use_bias = True,activation = tf.nn.tanh)
        return img

Gan loss

def gan_loss(logits_real, logits_fake):
    """Compute the GAN loss.

    Inputs:
    - logits_real: Tensor, shape [batch_size, 1], output of discriminator
        Unnormalized score that the image is real for each real image
    - logits_fake: Tensor, shape[batch_size, 1], output of discriminator
        Unnormalized score that the image is real for each fake image

    Returns:
    - D_loss: discriminator loss scalar
    - G_loss: generator loss scalar

    HINT: for the discriminator loss, you‘ll want to do the averaging separately for
    its two components, and then add them together (instead of averaging once at the very end).
    """
    # TODO: compute D_loss and G_loss
    loss1 = tf.nn.sigmoid_cross_entropy_with_logits(labels = tf.ones_like(logits_real),logits = logits_real,name = ‘discriminator_real_loss‘)
    loss2 = tf.nn.sigmoid_cross_entropy_with_logits(labels = tf.zeros_like(logits_fake),logits = logits_fake,name = ‘discriminator_fake_loss‘)
    loss3 = tf.nn.sigmoid_cross_entropy_with_logits(labels = tf.ones_like(logits_fake),logits = logits_fake,name = ‘generator_loss‘)
    D_loss = tf.reduce_mean(loss1 + loss2)
    G_loss = tf.reduce_mean(loss3)
    return D_loss, G_loss

Optimizing

# TODO: create an AdamOptimizer for D_solver and G_solver
def get_solvers(learning_rate=1e-3, beta1=0.5):
    """Create solvers for GAN training.

    Inputs:
    - learning_rate: learning rate to use for both solvers
    - beta1: beta1 parameter for both solvers (first moment decay)

    Returns:
    - D_solver: instance of tf.train.AdamOptimizer with correct learning_rate and beta1
    - G_solver: instance of tf.train.AdamOptimizer with correct learning_rate and beta1
    """
    D_solver = tf.train.AdamOptimizer(learning_rate = learning_rate,beta1 = beta1)
    G_solver = tf.train.AdamOptimizer(learning_rate = learning_rate,beta1 = beta1)
    return D_solver, G_solver

Least Squares GAN

def lsgan_loss(scores_real, scores_fake):
    """Compute the Least Squares GAN loss.

    Inputs:
    - scores_real: Tensor, shape [batch_size, 1], output of discriminator
        The score for each real image
    - scores_fake: Tensor, shape[batch_size, 1], output of discriminator
        The score for each fake image    

    Returns:
    - D_loss: discriminator loss scalar
    - G_loss: generator loss scalar
    """
    # TODO: compute D_loss and G_loss
    D_loss = 0.5 * tf.reduce_mean(tf.square(scores_real - 1)) + 0.5 * tf.reduce_mean(tf.square(scores_fake))
    G_loss = 0.5 * tf.reduce_mean(tf.square(scores_fake - 1))
    return D_loss, G_loss

Deep Convolutional GANs

Discriminator

Architecture:

  • Conv2D: 32 Filters, 5x5, Stride 1, padding 0
  • Leaky ReLU(alpha=0.01)
  • Max Pool 2x2, Stride 2
  • Conv2D: 64 Filters, 5x5, Stride 1, padding 0
  • Leaky ReLU(alpha=0.01)
  • Max Pool 2x2, Stride 2
  • Flatten
  • Fully Connected with output size 4 x 4 x 64
  • Leaky ReLU(alpha=0.01)
  • Fully Connected with output size 1
def discriminator(x):
    """Compute discriminator score for a batch of input images.

    Inputs:
    - x: TensorFlow Tensor of flattened input images, shape [batch_size, 784]

    Returns:
    TensorFlow Tensor with shape [batch_size, 1], containing the score
    for an image being real for each input image.
    """
    x = tf.reshape(x,shape = (tf.shape(x)[0],28,28,1))
    with tf.variable_scope("discriminator"):
        # TODO: implement architecture
        conv1 = tf.layers.conv2d(x,filters = 32,kernel_size = (5,5),strides = (1,1),activation = leaky_relu)
        max_pool1 = tf.layers.max_pooling2d(conv1,pool_size = (2,2),strides = (2,2))
        conv2 = tf.layers.conv2d(max_pool1,filters = 64,kernel_size = (5,5),strides = (1,1),activation = leaky_relu)
        max_pool2 = tf.layers.max_pooling2d(conv2,pool_size = (2,2),strides = (2,2))
        flat = tf.contrib.layers.flatten(max_pool2)
        fc1 = tf.layers.dense(flat,4*4*64,activation = leaky_relu)
        logits = tf.layers.dense(fc1,1)
        return logits
test_discriminator(1102721)

Generator

Architecture:

  • Fully connected with output size 1024
  • ReLU
  • BatchNorm
  • Fully connected with output size 7 x 7 x 128
  • ReLU
  • BatchNorm
  • Resize into Image Tensor of size 7, 7, 128
  • Conv2D^T (transpose): 64 filters of 4x4, stride 2
  • ReLU
  • BatchNorm
  • Conv2d^T (transpose): 1 filter of 4x4, stride 2
  • TanH
def generator(z):
    """Generate images from a random noise vector.

    Inputs:
    - z: TensorFlow Tensor of random noise with shape [batch_size, noise_dim]

    Returns:
    TensorFlow Tensor of generated images, with shape [batch_size, 784].
    """
    batch_size = tf.shape(z)[0]
    with tf.variable_scope("generator"):
        # TODO: implement architecture
        fc1 = tf.layers.dense(z,1024,activation = tf.nn.relu,use_bias = True)
        bn1 = tf.layers.batch_normalization(fc1,training = True)
        fc2 = tf.layers.dense(bn1,7*7*128,activation = tf.nn.relu,use_bias = True)
        bn2 = tf.layers.batch_normalization(fc2,training = True)
        resize = tf.reshape(bn2,shape = (-1,7,7,128))
        filter_conv1 = tf.get_variable(‘deconv1‘,[4,4,64,128]) # [height, width, output_channels, in_channels]
        conv_tr1 = tf.nn.conv2d_transpose(resize,filter = filter_conv1,output_shape = [batch_size,14,14,64],strides = [1,2,2,1])
        bias1 = tf.get_variable(‘deconv1_bias‘,[64])
        conv_tr1 += bias1
        relu_conv_tr1 = tf.nn.relu(conv_tr1)
        bn3 = tf.layers.batch_normalization(relu_conv_tr1,training = True)
        filter_conv2 = tf.get_variable(‘deconv2‘,[4,4,1,64])
        conv_tr2 = tf.nn.conv2d_transpose(bn3,filter = filter_conv2,output_shape = [batch_size,28,28,1],strides = [1,2,2,1])
        bias2 = tf.get_variable(‘deconv2_bias‘,[1])
        conv_tr2 += bias2
        img = tf.nn.tanh(conv_tr2)
        img = tf.contrib.layers.flatten(img)
        return img
test_generator(6595521)

Epoch: 0, D: 0.2112, G:0.3559

Epoch: 1, D: 0.196, G:0.2681

Epoch: 2, D: 0.1689, G:0.2728

Epoch: 3, D: 0.1618, G:0.2215

Epoch: 4, D: 0.1968, G:0.2461

Epoch: 5, D: 0.1968, G:0.2429

Epoch: 6, D: 0.2316, G:0.1997

Epoch: 7, D: 0.2206, G:0.1858

Epoch: 8, D: 0.2131, G:0.1815

Epoch: 9, D: 0.2345, G:0.1732

Final images

原文地址:https://www.cnblogs.com/bernieloveslife/p/10224687.html

时间: 2024-11-08 13:14:45

CS231n assignment3 Q5 Generative Adversarial Networks的相关文章

Paper Reading: Perceptual Generative Adversarial Networks for Small Object Detection

Perceptual Generative Adversarial Networks for Small Object Detection 2017-07-11  19:47:46   CVPR 2017 This paper use GAN to handle the issue of small object detection which is a very hard problem in general object detection. As shown in the followin

论文笔记之:UNSUPERVISED REPRESENTATION LEARNING WITH DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS

UNSUPERVISED REPRESENTATION LEARNING WITH DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS  ICLR 2016  摘要:近年来 CNN 在监督学习领域的巨大成功 和 无监督学习领域的无人问津形成了鲜明的对比,本文旨在链接上这两者之间的缺口.提出了一种 deep convolutional generative adversarial networks (DCGANs),that have certai

Speech Bandwidth Extension Using Generative Adversarial Networks

论文下载地址.博客园文章地址. 摘要 语音盲带宽扩展技术已经出现了一段时间,但到目前为止还没有出现广泛的部署,部分原因是增加的带宽伴随着附加的工件.本文提出了三代盲带宽扩展技术,从矢量量化映射到高斯混合模型,再到基于生成对抗性网络的深层神经网络的最新体系结构.这种最新的方法在质量上有了很大的提高,并证明了基于机器学习的盲带宽扩展算法在客观上和主观上都能达到与宽带编解码器相当的质量.我们相信,盲带宽扩展现在可以达到足够高的质量,以保证在现有的电信网络中部署. 关键词:盲源带宽扩展,人工带宽扩展,生

StarGAN: Unified Generative Adversarial Networks for Multi-Domain Image-to-Image Translation - 1 - 多个域间的图像翻译论文学习

Abstract 最近在两个领域上的图像翻译研究取得了显著的成果.但是在处理多于两个领域的问题上,现存的方法在尺度和鲁棒性上还是有所欠缺,因为需要为每个图像域对单独训练不同的模型.为了解决该问题,我们提出了StarGAN方法,这是一个新型的可扩展的方法,能够仅使用一个单一模型就实现多领域的图像翻译.StarGAN这样的统一模型的结构允许在单个网络上同时训练带有不同领域的多个数据集.这使得StarGAN的翻译图像质量优于现有的模型,并具有将输入图像灵活地翻译到任意目标域的新能力.通过实验,验证了该

[论文理解] On the "steerability" of generative adversarial networks

On the "steerability" of generative adversarial networks Intro 本文提出对GAN的latent space进行操作的一种方法,通过对latent space的编辑实现生成域外样本,操控生成样本的基本属性,如控制生成样本的位置.光照.二维旋转.三维旋转等等. 文章的主要贡献为: 证明并实现了通过在latent space中的"walk"能够实现类似人类世界中相机的运动.颜色变换等操作,这些操作是通过自监督的

GAN01: Introductory guide to Generative Adversarial Networks (GANs) and their promise!

引用:Introductory guide to Generative Adversarial Networks (GANs) and their promise! What is a GAN? Let us take an analogy to explain the concept: 如果你想在某件事上做到更好,例如下棋,你会怎么做? 你或许会找一个比自己厉害的对手. 然后你会在你们对决中分析你错的地方和他对的地方, 并思考在下一场对决中你该如何击败对手. 你会不断重复这个过程,知道你击败对

A Review on Generative Adversarial Networks: Algorithms, Theory, and Applications

1 Inttroduction GANs由两个模型组成:生成器和鉴别器.生成器试图捕获真实示例的分布,以便生成新的数据样本.鉴别器通常是一个二值分类器,尽可能准确地将生成样本与真实样本区分开来.GANs的优化问题是一个极大极小优化问题.优化终止于相对于生成器的最小值和相对于鉴别器的最大值的鞍点. 2.1 Generative algorithms 生成算法可分为两类:显式密度模型和隐式密度模型. 2.1,1 Explicit density model 显式密度模型假设分布,利用真实数据训练包含

生成对抗网络 Generative Adversarial Networks

转自:https://zhuanlan.zhihu.com/p/26499443 生成对抗网络GAN是由蒙特利尔大学Ian Goodfellow教授和他的学生在2014年提出的机器学习架构. 要全面理解生成对抗网络,首先要理解的概念是监督式学习和非监督式学习.监督式学习是指基于大量带有标签的训练集与测试集的机器学习过程,比如监督式图片分类器需要一系列图片和对应的标签("猫","狗"-),而非监督式学习则不需要这么多额外的工作,它们可以自己从错误中进行学习,并降低未来

对抗生成网络 Generative Adversarial Networks

1. Basic idea 基本任务:要得到一个generator,能够模拟想要的数据分布.(一个低维向量到一个高维向量的映射) discriminator就像是一个score function. 如果想让generator生成想要的目标数据,就把这些真实数据作为discriminator的输入,discriminator的另一部分输入就是generator生成的数据. 1. 初始化generator和discriminator. 2. 迭代: 固定generator的参数,更新discrimi