Code Monkey home page Code Monkey logo

biva-pytorch's Introduction

BIVA (PyTorch)

Official PyTorch BIVA implementation (BIVA: A Very Deep Hierarchy of Latent Variables forGenerative Modeling) for binarized MNIST and CIFAR. The original Tensorflow implementation can be found here.

Run the Experiments

conda create --name biva python=3.7
conda activate biva
pip install -r requirements.txt
CUDA_VISIBLE_DEVICES=0 python run_deepvae.py --dataset binmnist --q_dropout 0.5 --p_dropout 0.5 --device cuda
CUDA_VISIBLE_DEVICES=0 python run_deepvae.py --dataset cifar10 --q_dropout 0.2 --p_dropout 0 --device cuda

Citation

@article{maale2019biva,
    title={BIVA: A Very Deep Hierarchy of Latent Variables for Generative Modeling},
    author={Lars Maaløe and Marco Fraccaro and Valentin Liévin and Ole Winther},
    year={2019},
    eprint={1902.02102},
    archivePrefix={arXiv},
    primaryClass={stat.ML}
}

Pip package

Install the Requirements

  • pytorch 1.3.0
  • torchvision
  • matplotlib
  • tensorboard
  • booster-pytorch==0.0.2

Install as a Package

pip install git+https://github.com/vlievin/biva-pytorch.git

Build Deep VAEs

import torch
from torch.distributions import Bernoulli

from biva import DenseNormal, ConvNormal
from biva import VAE, LVAE, BIVA

# build a 2 layers VAE for binary images

# define the stochastic layers
z = [
    {'N': 8, 'kernel': 5, 'block': ConvNormal},  # z1
    {'N': 16, 'block': DenseNormal}  # z2
]

# define the intermediate layers
# each stage defines the configuration of the blocks for q_(z_{l} | z_{l-1}) and p_(z_{l-1} | z_{l})
# each stage is defined by a sequence of 3 resnet blocks
# each block is degined by a tuple [filters, kernel, stride]
stages = [
    [[64, 3, 1], [64, 3, 1], [64, 3, 2]],
    [[64, 3, 1], [64, 3, 1], [64, 3, 2]]
]

# build the model
model = VAE(tensor_shp=(-1, 1, 28, 28), stages=stages, latents=z, dropout=0.5)

# forward pass and data-dependent initialization
x = torch.empty((8, 1, 28, 28)).uniform_().bernoulli()
data = model(x)  # data = {'x_' : p(x|z), z \sim q(z|x), 'kl': [kl_z1, kl_z2]}

# sample from prior
data = model.sample_from_prior(N=16)  # data = {'x_' : p(x|z), z \sim p(z)}
samples = Bernoulli(logits=data['x_']).sample()

biva-pytorch's People

Contributors

dependabot[bot] avatar martinengelcke avatar vlievin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar

biva-pytorch's Issues

Problem when sampling from the posterior

Hello,

I would like to compare the quality of the reconstructed images with the quality of the generated images, in other terms, to compare the images generated by sampling via the prior and those generated by using the estimated posteriors.

Once BIVA is trained on Cifar10 dataset, I used the following code (starting after https://github.com/vlievin/biva-pytorch/blob/master/run_deepvae.py#L192)

def build_and_save_grid(data, filename, N=100):
     # make grid
    nrow = math.floor(math.sqrt(N))
    grid = make_grid(data, nrow=nrow)

    # normalize
    grid -= grid.min()
    grid /= grid.max()

    # save the raw image
    img = grid.data.permute(1, 2, 0).cpu().numpy()
    matplotlib.image.imsave(f"./output/{filename}.png", img)

load_model(ema.model, logdir)
           
with torch.no_grad():
    x = next(iter(train_loader)).to(opt.device)
    build_and_save_grid(x, "original")

    x_ = ema.model(x).get('x_')
    x_ = likelihood(logits=x_).sample()
    build_and_save_grid(x_, "reconstruction")

    x_ = ema.model.sample_from_prior(100).get('x_')
    x_ = likelihood(logits=x_).sample()
    build_and_save_grid(x_, "generation")

I was quite surprised to observe that the reconstructed images have a very poor quality compared to the generated images:
Reconstruction:
reconstruction
Generation:
generation

I assume that there is an error somewhere, but I couldn't find it. Do you have any idea?

Thanks,
Reuben

Network configuration for 64x64 natural image dataset

Hi,
Nice work! I am curious about how BIVA performs on the CUB bird dataset. I want to make it work on 64x64 resolution, thus wanting to reference the configuration of CelebA but found none. Particularly, I am not very sure about the network configuration. I have followed the instruction in the original paper and written one, presented below. Could you please help me to see whether it is correct? Many thanks!

def get_deep_vae_cub():
    filters = 64
    no_layers = 2
    enc = []
    z = []

    enc_z1 = [[filters, 7, 1]] * no_layers
    enc_z1 += [[filters, 7, 2]]
    z_1 = {'N': 20, 'kernel': 32, 'block': ConvNormal}
    enc += [enc_z1]
    z += [z_1]

    enc_z2 = [[filters, 5, 1]] * no_layers
    enc_z2 += [[filters, 5, 2]]
    z_2 = {'N': 19, 'kernel': 16, 'block': ConvNormal}
    enc += [enc_z2]
    z += [z_2]

    for i in range(3, 11):
        # layer 3~10
        enc_zi = [[filters, 3, 1]] * no_layers
        enc_zi += [[filters, 3, 1]]
        z_i = {'N': 21-i, 'kernel': 16, 'block': ConvNormal}
        enc += [enc_zi]
        z += [z_i]

    enc_z11 = [[filters, 3, 1]] * no_layers
    enc_z11 += [[filters, 3, 2]]
    z_11 = {'N': 10, 'kernel': 8, 'block': ConvNormal}
    enc += [enc_z11]
    z += [z_11]

    for i in range(12, 20):
        # layer 12 ~ 19
        enc_zi = [[2 * filters, 3, 1]] * no_layers
        enc_zi += [[2 * filters, 3, 1]]
        z_i = {'N': 21 - i, 'kernel': 8, 'block': ConvNormal}
        enc += [enc_zi]
        z += [z_i]

    enc_z20 = [[2 * filters, 3, 1]] * no_layers
    enc_z20 += [[2 * filters, 3, 2]]
    z_20 = {'N': 1, 'kernel': 4, 'block': ConvNormal}
    enc += [enc_z20]
    z += [z_20]

    return enc, z

Role of the "projection" layer

Hello,

First congratulations for the quality of your work and the clarity of the released code! It is a pleasure to work on it.

I was wondering, what is the underlying justification of the use of a fully-connected "projection layers" after sampling $z^{bu}{i}$ and $z^{td}{i}$? Is it only to model covariance structure?

Thank you,
Reuben

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.