Git Product home page Git Product logo

cvae's Introduction

Conditional Variaional AutoEncoder(CVAE)-Tensorflow

I Write the Tensorflow code for CVAE(M1) , M1 is the Latent Discriminative Model

This code has following features

  1. when we train our model, I use 0.6 dropout rate.
  2. All activation functions are leaky relu.
  3. I use He_initializer for weights initialization.

Enviroment

  • OS: Ubuntu 16.04

  • Graphic Card /RAM : 1080TI /16G

  • Python 3.5

  • Tensorflow-gpu version: 1.4.0rc2

  • OpenCV 3.4.1

Schematic of CVAE

์‚ฌ์ง„1

Code

1. Conditional Gaussian Encoder

def conditional_gaussian_encoder(self, X, Y, keep_prob):

    with tf.variable_scope("gaussian_encoder", reuse = tf.AUTO_REUSE):
        X_input = tf.concat((X,Y), axis =1)
        net = drop_out(leaky(dense(X_input, self.n_hidden[0], name = "Dense_1")), keep_prob)
        net = drop_out(leaky(dense(net, self.n_hidden[1], name="Dense_2")), keep_prob)
        net = dense(net, self.n_z*2, name ="Dense_3")
        mean = net[:,:self.n_z]
        std = tf.nn.softplus(net[:,self.n_z:]) + 1e-6

    return mean, std

2. Conditional Bernoulli Decoder

def conditional_bernoulli_decoder(self,Z, Y, keep_prob):

    with tf.variable_scope("bernoulli_decoder", reuse = tf.AUTO_REUSE):
        z_input = tf.concat((Z,Y), axis = 1)
        net = drop_out(leaky(dense(z_input, self.n_hidden[2], name = "Dense_1")), keep_prob)
        net = drop_out(leaky(dense(net, self.n_hidden[3], name="Dense_2")), keep_prob)
        net = tf.nn.sigmoid(dense(net, self.n_out, name = "Dense_3"))
    
    return net

3. Conditional Variational AutoEncoder

def Conditional_Variational_AutoEncoder(self, X, X_noised, Y, keep_prob):

    X_flatten = tf.reshape(X, [-1, self.n_out])
    X_flatten_noised = tf.reshape(X_noised, [-1, self.n_out])

    mean, std = self.conditional_gaussian_encoder(X_flatten_noised, Y, keep_prob)
    z = mean + std*tf.random_normal(tf.shape(mean, out_type = tf.int32), 0, 1, dtype = tf.float32)

    X_out = self.conditional_bernoulli_decoder(z, Y, keep_prob)
    X_out = tf.clip_by_value(X_out, 1e-8, 1 - 1e-8)

    likelihood = tf.reduce_mean(tf.reduce_sum(X_flatten*tf.log(X_out) + (1 - X_flatten)*tf.log(1 - X_out), 1))
    KL_Div = tf.reduce_mean(0.5 * tf.reduce_sum(1 - tf.log(tf.square(std) + 1e-8)
                                                + tf.square(mean)
                                                + tf.square(std), 1))

    Recon_error = -1*likelihood
    Regul_error = KL_Div

    self.ELBO = Recon_error + Regul_error

    return z, X_out, self.ELBO

Result

1. Denoising and Restoring

python main.py --add_noise True
Original Images Images denoised Images Restored via CVAE

2. Manifold Learning Result

python main.py --n_z 2 --PMLR True
Manifold with a condition of 0 Manifold with a condition of 1 2d MNIST Manifold

3. Conditional Generation

python main.py --PARR True

Reference Papers

1. https://arxiv.org/abs/1406.5298

2. https://arxiv.org/abs/1312.6114

3. https://arxiv.org/abs/1606.05908

Reference

1.https://github.com/hwalsuklee/tensorflow-mnist-VAE

2.https://github.com/hwalsuklee/tensorflow-mnist-CVAE

3.https://github.com/MINGUKKANG/VAE-tensorflow

cvae's People

Contributors

mingukkang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.