Home

Awesome

Conditional Variational Auto-Encoder for MNIST

An implementation of conditional variational auto-encoder (CVAE) for MNIST descripbed in the paper:

Implemented Models

The paper suggests three models.

If you are interested in missing parts, please see original implementation or replications (Tensorflow) (Chainer) of it.

Results

Reproduce

Well trained auto-encoder must be able to reproduce input image.
Since CVAE uses additional information (labels) which is not used in VAE, CVAE shows better performance than CVAE.
The following results can be reproduced with command:

python run_main.py --dim_z 2
<table align='center'> <tr align='center'> <td> </td> <td> Input image </td> <td> CVAE </td> <td> VAE </td> </tr> <tr> <td> Epoch 1 </td> <td><img src = 'samples/CVAE/input.jpg' height = '230px'> <td><img src = 'samples/CVAE/PRR_epoch_00.jpg' height = '230px'> <td><img src = 'samples/VAE/PRR_epoch_00.jpg' height = '230px'> </tr> <tr> <td> Epoch 20 </td> <td><img src = 'samples/CVAE/input.jpg' height = '230px'> <td><img src = 'samples/CVAE/PRR_epoch_19.jpg' height = '230px'> <td><img src = 'samples/VAE/PRR_epoch_19.jpg' height = '230px'> </tr> </table>

Denoising

When training, salt & pepper noise is added to input image, so that CVAE can reduce noise and restore original input image.
The following results can be reproduced with command:

python run_main.py --dim_z 2 --add_noise True
<table align='center'> <tr align='center'> <td> Original input image </td> <td> Input image with noise </td> <td> Restored image via CVAE </td> <td> Restored image via VAE </td> </tr> <tr> <td><img src = 'samples/CVAE/input.jpg' height = '200px'> <td><img src = 'samples/CVAE/denoised/input_noise.jpg' height = '200px'> <td><img src = 'samples/CVAE/denoised/PRR_epoch_19.jpg' height = '200px'> <td><img src = 'samples/VAE/denoised/PRR_epoch_19.jpg' height = '200px'> </tr> </table>

Conditional Generation : Learned MNIST manifold

Visualisation of handwriting styles learned by the model with 2D z-space in Figure. 1(a) in the paper.
The following results can be reproduced with command:

python run_main.py --dim_z 2 --PMLR True
<table align='center'> <tr align='center'> <td> Learned MNIST manifold with a condition of label 2</td> <td> Learned MNIST manifold with a condition of label 3</td> <td> Learned MNIST manifold with a condition of label 4</td> </tr> <tr> <td><img src = 'samples/CVAE/PMLR_epoch_19_02.jpg' height = '300px'> <td><img src = 'samples/CVAE/PMLR_epoch_19_03.jpg' height = '300px'> <td><img src = 'samples/CVAE/PMLR_epoch_19_04.jpg' height = '300px'> </tr> </table>

Conditional Generation : Analogical reasoning with random z-vectors

Analogical reasoning with generative semi-supervised models using a 2D z-space like in Figure. 1(b) in the paper.
The following results can be reproduced with command:

python run_main.py --dim_z 2 --PARR True
<table align='center'> <tr align='center'> <td><img src = 'samples/CVAE/PARR_index.jpg' height = '200px'> </tr> </table>

The leftmost columns show images generated from 4 z-vectors. The other columns show analogical fantasies of x by the generative model, where the latent variable z of each row is set to the value inferred from the test-set image on the left by the inference network.

Conditional Generation : Analogical reasoning with a given image

CVAE encodes a given handwritten digit image to a z-vector.
With an obtained z-vector, various images with similar style to the given image can be generated by changing label-condition.

<table align='center'> <tr align='center'> <td> input </td> <td> generated images </td> </tr> <tr> <td><img src = 'samples/Analogical_Reasoning/input.jpg' height = '40px'> <td><img src = 'samples/Analogical_Reasoning/merged.jpg' height = '40px'> </tr> </table>

Usage

Prerequisites

  1. Tensorflow
  2. Python packages : numpy, scipy, PIL(or Pillow), matplotlib

Command

python run_main.py --dim_z <latent vector dimension>

Example: python run_main.py --dim_z 20

Arguments

Required :

Optional :

References

The implementation is based on the projects:
[1] https://github.com/wiseodd/generative-models/tree/master/VAE/conditional_vae
[2] http://wiseodd.github.io/techblog/2016/12/17/conditional-vae/

Acknowledgements

This implementation has been tested with Tensorflow 1.0.1 on Windows 10.