This Github repository hosts our code and demos for LaDDer, a method that allows the accurate modelling of the data distribution in a low-dimensional latent space, as proposed in our paper:
LaDDer: Latent Data Distribution Modelling with a Generative Prior.
Shuyu Lin1 and Ronald Clark2
1University of Oxford, 2Imperial College London
Usually a fixed prior distribution is adopted to be the data distribution and the encoder is hoped to be powerful enough to transform the high-dimensional data samples (such as images) into a latent space where the data distribute complies with the fixed prior distribution. However, we find this assumption is often overly strict.
Instead we propose to learn the data distribution (i.e. the prior) directly from data and model the prior as a second generative model. Such a flexible prior model is able to fit with the complex data distribution even in the latent space. An overview of our model is shown below (pls pay attention to the sophisticated density structure in the true data distribution visualised from MNIST dataset):
A comparison of the derived data distribution across 5 methods for the MNIST dataset in a 2D latent space is shown below. Please notice the difference between the true data distribution and the commonly used simple Gaussian. This indicates naively using a simple Gaussian as the prior can really limit the generative model's performance. Among the 5 methods, LaDDer gives the most faithful modelling of the true data distribution. For details, please refer to our paper at link.
Using Google Colab is the easiest way to run the code. Simply open a new notebook, then execute the following commands (exactly as shown) to set the Tensorflow version and clone the repository:
%tensorflow_version 1.x
!git clone https://github.com/lin-shuyu/ladder-latent-data-distribution-modelling.git
%cd ladder-latent-data-distribution-modelling
Then run the training script train.py for MNIST datasets by executing:
!python3 train.py --config codes/mnist_digit_config.json
Or, run the interpolation demo script as follows:
%run latent-space-interpolation-mnist.ipynb
We use 3 datasets in this project.
- MNIST digit (loaded directly from
tf.keras.datasets.mnist
) - MNIST fashion (loaded directly from
tf.keras.datasets.fashion_mnist
) - CelebA face dataset
- This dataset contains 202599 RGB images of celebrity faces. Dataset can be downloaded from link
- We first resize all the images to 128 × 128 and rescale the pixel values to be within [0, 1]. We then randomly select 20k images (about 10%) to be the test set.
- [To do] Add the script to pre-process the downloaded celebA dataset.
- [Not tested] A loader seems available on tensorflow at this link.
Our code is written in Python3 with Tensorflow 1.15 library. The python libraries we used in the project are listed in the requirements.txt
. We suggest to build a virtual environment using either virtualenv package or miniconda. we named our virtual environment for this project as ladder-env
. Details of setting up the virtual environment is given below.
To install and set up virtualenv, please follow the procedures here. After virtualenv or venv is installed, create the ladder-env
environment and activate. Note: tensorflow 1 can only be installed with python 3.5-3.7.
- Update pip:
pip3 install --upgrade pip
- Install the required libraries:
pip3 install -r requirements.txt
To install and set up (mini)conda, please follow the procedures here. After (mini)conda is installed,
- Create the
ladder-env
environment:conda create --name ladder-env python=3.7
- Activate the environment:
conda activate ladder-env
- Install the required libraries:
conda install matplotlib scikit-learn scipy tqdm jupyter numpy
conda install -c conda-forge opencv
conda install -c conda-forge tensorflow=1.15
conda install -c conda-forge tensorflow-probability=0.8.0
pip install -U dm-sonnet==1.23
Our demo code in demo/
uses a jupyter notebook. To add the virtualenv ladder-env
to the notebook kernel, please:
- Open a terminal and direct to the LaDDer folder.
- Activate the virtual environment on the command line:
source ladder-env/bin/activate
with venv / virtualenv orconda activate ladder-env
with conda. - Add the virtualenv as a jupyter kernel:
ipython kernel install --name "ladder-env" --user
Our code is organised in the following manner. The main codes for running the training and for using a demo for latent space interpolation are at top level and in the files train.py
and latent-space-interpolation-mnist.ipynb
respectively.
The codes needed to train LaDDer are all kept in codes/
folder. The functional codes, such as dataloader, models and trainers, are in python scripts. We use .json
file to specify model parameters for the training for each dataset. The provided .json
files contain parameters that work well for our models, but pls feel free to modify different parameters for your interest.
We also prepare a demo code in jupyter notebook for using the derived data distribution for latent space interpolation. As we proposed in our paper, the knowledge of the data distribution can better facilitate downstream tasks. We propose a method to integrate such knowledge to better interpolate between a pair of images. All the supporting files for the demo is given in demo/
folder.
The majorIt only contains a demo for MNIST dataset for now. We will add another demo for CelebA dataset. The generated figures in the demo code will be saved to figures/
folder for your reference.
We also offer pre-trained models for all three datasets and they are saved in pretrained_models
. The latent space interpolation demo will need these pre-trained models.
To start a training, please follow the following procedures:
- Open a terminal and direct to the LaDDer folder.
- Activate the virtualenv on the command line:
source ladder-env/bin/activate
- Run
python3 train.py --config codes/mnist_digit_config.json
, for example, to start a training for MNIST digit dataset. Please change the.json
file to others for other datasets.
The training for MNIST digit and fashion should be able to run without a GPU. Feel free to try.
The training results (reconstruction plots and generated samples) and trained models will be saved to experiments/
folder, which will be created the first you started training.
To use our demo code, please:
- First install all the required libraries, add the virtualenv as a jupyter kernel.
- Launch a jupyter notebook by calling
jupyter-notebook
on the command line in the project folder. - Open
latent-space-interpolation-mnist.ipynb
. - Run the code prepared one cell at a time. The default setting will initiate a latent space interpolation for MNIST digit dataset.
- Please feel free to change parameters, such as
n_step
,n_iter
and start and end image index (idx_start
andidx_end
), to see results for different setting.
[To add] .json
file for MNIST fashion dataset for the interpolation demo and the demo code for CelebA dataset.
Hope you enjoy playing with our code and find it helpful for your projects! Happy data distribution modelling!
If you find our codes/project relevant to your work, please cite us in your work:
@inproceedings{ladder,
title={LaDDer: Latent Data Distribution Modelling with a Generative Prior},
author={Lin, Shuyu and Clark, Ronald},
booktitle={British Machine Vision Conference (BMVC)},
year={2020}
}