PyTorch implementations of algorithms for density estimation

Overview

pytorch-flows

A PyTorch implementations of Masked Autoregressive Flow and some other invertible transformations from Glow: Generative Flow with Invertible 1x1 Convolutions and Density estimation using Real NVP.

For MAF, I'm getting results similar to ones reported in the paper. GLOW requires some work.

Run

python main.py --dataset POWER

Available datasets are POWER, GAS, HEPMASS, MINIBONE and BSDS300. For the moment, I removed MNIST and CIFAR10 because I have plans to add pixel-based models later.

Datasets

The datasets are taken from the original MAF repository. Follow the instructions to get them.

Tests

Tests check invertibility, you can run them as

pytest flow_test.py
Comments
  • How to implement Conditional Masked Autoregressive Flow?

    How to implement Conditional Masked Autoregressive Flow?

    I'm trying to replicate the MNIST and CIFAR-10 experiments used in the paper. If I want to modify the code to add Conditional Masked Autoregressive Flow, which part of the neural network model should I modify?

    Thanks for your help!!

    opened by zhouhanc 4
  • Glow on GAS with NaN validation error

    Glow on GAS with NaN validation error

    To reproduce, use:

    python main.py --dataset GAS --flow glow
    

    Printed message for first epoch:

    Warning: Results for GLOW are not as good as for MAF yet.
    Warning: Results for GLOW are not as good as for MAF yet.
    Warning: Results for GLOW are not as good as for MAF yet.
    Warning: Results for GLOW are not as good as for MAF yet.
    Warning: Results for GLOW are not as good as for MAF yet.
    Train Epoch: 0 [0/852174 (0%)]	Loss: 6.648179
    Train Epoch: 0 [100000/852174 (12%)]	Loss: -2.193499
    Train Epoch: 0 [200000/852174 (23%)]	Loss: -2.497202
    Train Epoch: 0 [300000/852174 (35%)]	Loss: -1.122860
    Train Epoch: 0 [400000/852174 (47%)]	Loss: -2.892694
    Train Epoch: 0 [500000/852174 (59%)]	Loss: -2.268665
    Train Epoch: 0 [600000/852174 (70%)]	Loss: -3.088916
    Train Epoch: 0 [700000/852174 (82%)]	Loss: -3.798649
    Train Epoch: 0 [800000/852174 (94%)]	Loss: -2.554942
    
    Validation set: Average loss: nan
    
    Best validation at epoch 0: Average loss: inf
    
    opened by Zardinality 3
  • MADE generative pass, tanh layer for scaling params, moons example

    MADE generative pass, tanh layer for scaling params, moons example

    Feel free to incorporate any or none of these fixes/additions. Let me know if you'd like me to break up the PR. Thanks!

    1. Implement the generative pass (mode == 'inverse') for MADE module. This iteratively builds the output of the generative model given the input (e.g. the Gaussian latent data). The shift and scale parameters for the first element are independent of the data, and are determined simply by the bias weights.

    2. Add the HalfTanh module, which applies tanh to the second half of the dimensions of a given tensor. This is used in MADE, since tanh adds stability to the scale parameters.

    3. Add a 2D moon dataset example, including a visualization of generated samples. The command

    python main.py --dataset MOONS --flow maf --epochs 50
    

    results in the following plot:

    generated

    1. Changing sign of scale parameter in 'inverse' mode to correspond to the MAF paper.
    opened by ekrim 3
  • AttributeError: 'BatchNormFlow' object has no attribute 'batch_mean' in inverse mode

    AttributeError: 'BatchNormFlow' object has no attribute 'batch_mean' in inverse mode

    I'm running into a problem when I call model.forward(..., mode='inverse') before calling model.forward(..., mode='direct'), in which case batch_mean and batch_var are not defined. What is a proper why to fix this problem? I need to use inverse mode first during the training stage in my application.

    The code snippet looks like this. I've omitted some application specific codes.

    block = [
    fnn.MADE(num_inputs, num_hidden),
    fnn.BatchNormFlow(num_inputs),
    fnn.Reverse(num_inputs)
    ]
    model = fnn.FlowSequential(*blocks)
    model.train()
    model.forward(..., mode='inverse')
    

    AttributeError: 'BatchNormFlow' object has no attribute 'batch_mean'

    opened by zhouhanc 1
  • remove tanh before exp for MADE, add activation selection for both MADE and coupling layer

    remove tanh before exp for MADE, add activation selection for both MADE and coupling layer

    Previously we put a tanh on the end of the scale net for MADE, to prevent scale explosion. But in the original implementation the last layer does not have an activation function, instead they have different activation function in the intermediate layers for particular dataset. For coupling layer, the implementation of MAF repo have the scale and translate net apart, and use different activation function for each of those. This commit is trying to mimic these behaviors.

    opened by Zardinality 1
  • Undefined name: util --> datasets.util

    Undefined name: util --> datasets.util

    util is an undefined name in this context while datasets.util is imported on line 5.

    flake8 testing of https://github.com/ikostrikov/pytorch-flows on Python 3.7.0

    $ flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics

    ./datasets/power.py:33:9: F821 undefined name 'util'
            util.plot_hist_marginals(data_split.x)
            ^
    1     F821 undefined name 'util'
    1
    
    opened by cclauss 0
  • Need minus sign in reverse log density computation

    Need minus sign in reverse log density computation

    https://github.com/ikostrikov/pytorch-flows/blob/34a34a0f25a86479e221f5d9bc4a67857d6621d0/flows.py#L114

    This currently matches https://github.com/ikostrikov/pytorch-flows/blob/34a34a0f25a86479e221f5d9bc4a67857d6621d0/flows.py#L130 - but L114 should have no minus sign for the log determinant of the Jacobian.

    The forward computation is on https://github.com/ikostrikov/pytorch-flows/blob/34a34a0f25a86479e221f5d9bc4a67857d6621d0/flows.py#L113

    u = (inputs - m) * torch.exp(-a)

    The inverse of this is

    inputs = u * torch.exp(a) + m

    So the log determinant jacobian of the inverse is a.sum(-1, keepdim=True) not -a.sum(-1, keepdim=True).

    Might be missing something here!

    opened by altosaar 0
  • Add option to provide your own PDF for the latent variables in FlowSequential (not necessarily Gaussian)

    Add option to provide your own PDF for the latent variables in FlowSequential (not necessarily Gaussian)

    In our application, we've found the need to provide the PDF for the latent variables (in our case, it is useful that the PDF depends on the conditional inputs). We've found a small fix to your code that let's you have this option, by just adding an init method to FlowSequential and changing log_probs accordingly. I'll share the different code here. Here is the added init method

        def __init__(self, *args, log_probs = 'gaussian'):
            super(FlowSequential,self).__init__(*args)
            if log_probs = 'gaussian'
                def __log_probs(x, *_):
                    return torch.sum(-0.5 * x ** 2 - 0.5 * math.log(2 * math.pi),
                                                                -1, keepdim=True)
                self.log_probs = __log_probs
            else:
                self.log_probs = log_probs
    

    and here is the modified log_probs.

        def log_probs(self, inputs, cond_inputs = None):
            u, log_jacob = self(inputs, cond_inputs)
            log_probs = self.log_probs(u, cond_inputs)
            return (log_probs + log_jacob).sum(-1, keepdim=True)
    

    Then, to provide your own PDF, just do model = FlowSequential(*modules,logprobs=your_own_PDF)

    opened by joaompereira 0
  • Random connections in MADE perform better than deterministic masks

    Random connections in MADE perform better than deterministic masks

    I found that constructing masks using the original MADE paper https://arxiv.org/abs/1502.03509 (nodes are randomly assigned ids) works better than assigning an id deterministically as in https://github.com/ikostrikov/pytorch-flows/blob/5520ebe4e91f5829db695ffd5f1ba7376d92a446/flows.py#L19

    (The reason for this is likely simple, that if the output size is not an exact multiple of the number of hidden units, some input features will be assigned more hidden nodes than others)

    opened by altosaar 0
  • n-layer MADEs

    n-layer MADEs

    Does your code support MADEs with an arbitrary number of masked linear layers? My main question is really if the function get_mask works for hidden layers of arbitrary depth. Thanks!

    opened by Nate711 0
  • Pixel-based models?

    Pixel-based models?

    The readme states:

    For the moment, I removed MNIST and CIFAR10 because I have plans to add pixel-based models later.

    what do you mean by that? What's missing? I might be able to implement some stuff.

    opened by LeanderK 1
Owner
Ilya Kostrikov
Post doc
Ilya Kostrikov
Scripts of Machine Learning Algorithms from Scratch. Implementations of machine learning models and algorithms using nothing but NumPy with a focus on accessibility. Aims to cover everything from basic to advance.

Algo-ScriptML Python implementations of some of the fundamental Machine Learning models and algorithms from scratch. The goal of this project is not t

Algo Phantoms 81 Nov 26, 2022
Drone-based Joint Density Map Estimation, Localization and Tracking with Space-Time Multi-Scale Attention Network

DroneCrowd Paper Detection, Tracking, and Counting Meets Drones in Crowds: A Benchmark. Introduction This paper proposes a space-time multi-scale atte

VisDrone 98 Nov 16, 2022
Estimation of human density in a closed space using deep learning.

Siemens HOLLZOF challenge - Human Density Estimation Add project description here. Installing Dependencies: Install Python3 either system-wide, user-w

null 3 Aug 8, 2021
This program presents convolutional kernel density estimation, a method used to detect intercritical epilpetic spikes (IEDs)

Description This program presents convolutional kernel density estimation, a method used to detect intercritical epilpetic spikes (IEDs) in [Gardy et

Ludovic Gardy 0 Feb 9, 2022
Re-implementation of the Noise Contrastive Estimation algorithm for pyTorch, following "Noise-contrastive estimation: A new estimation principle for unnormalized statistical models." (Gutmann and Hyvarinen, AISTATS 2010)

Noise Contrastive Estimation for pyTorch Overview This repository contains a re-implementation of the Noise Contrastive Estimation algorithm, implemen

Denis Emelin 42 Nov 24, 2022
PyTorch version of Stable Baselines, reliable implementations of reinforcement learning algorithms.

PyTorch version of Stable Baselines, reliable implementations of reinforcement learning algorithms.

DLR-RM 4.7k Jan 1, 2023
PyTorch implementations of deep reinforcement learning algorithms and environments

Deep Reinforcement Learning Algorithms with PyTorch This repository contains PyTorch implementations of deep reinforcement learning algorithms and env

Petros Christodoulou 4.7k Jan 4, 2023
Pytorch implementations of popular off-policy multi-agent reinforcement learning algorithms, including QMix, VDN, MADDPG, and MATD3.

Off-Policy Multi-Agent Reinforcement Learning (MARL) Algorithms This repository contains implementations of various off-policy multi-agent reinforceme

null 183 Dec 28, 2022
Independent and minimal implementations of some reinforcement learning algorithms using PyTorch (including PPO, A3C, A2C, ...).

PyTorch RL Minimal Implementations There are implementations of some reinforcement learning algorithms, whose characteristics are as follow: Less pack

Gemini Light 4 Dec 31, 2022
Pytorch Implementations of large number classical backbone CNNs, data enhancement, torch loss, attention, visualization and some common algorithms.

Torch-template-for-deep-learning Pytorch implementations of some **classical backbone CNNs, data enhancement, torch loss, attention, visualization and

Li Shengyan 270 Dec 31, 2022
Machine Learning From Scratch. Bare bones NumPy implementations of machine learning models and algorithms with a focus on accessibility. Aims to cover everything from linear regression to deep learning.

Machine Learning From Scratch About Python implementations of some of the fundamental Machine Learning models and algorithms from scratch. The purpose

Erik Linder-Norén 21.8k Jan 9, 2023
Details about the wide minima density hypothesis and metrics to compute width of a minima

wide-minima-density-hypothesis Details about the wide minima density hypothesis and metrics to compute width of a minima This repo presents the wide m

Nikhil Iyer 9 Dec 27, 2022
SMD-Nets: Stereo Mixture Density Networks

SMD-Nets: Stereo Mixture Density Networks This repository contains a Pytorch implementation of "SMD-Nets: Stereo Mixture Density Networks" (CVPR 2021)

Fabio Tosi 115 Dec 26, 2022
This YoloV5 based model is fit to detect people and different types of land vehicles, and displaying their density on a fitted map, according to their coordinates and detected labels.

This YoloV5 based model is fit to detect people and different types of land vehicles, and displaying their density on a fitted map, according to their

Liron Bdolah 8 May 22, 2022
Official code of the paper "Expanding Low-Density Latent Regions for Open-Set Object Detection" (CVPR 2022)

OpenDet Expanding Low-Density Latent Regions for Open-Set Object Detection (CVPR2022) Jiaming Han, Yuqiang Ren, Jian Ding, Xingjia Pan, Ke Yan, Gui-So

csuhan 64 Jan 7, 2023
Deep learning algorithms for muon momentum estimation in the CMS Trigger System

Deep learning algorithms for muon momentum estimation in the CMS Trigger System The Compact Muon Solenoid (CMS) is a general-purpose detector at the L

anuragB 2 Oct 6, 2021
Monocular Depth Estimation - Weighted-average prediction from multiple pre-trained depth estimation models

merged_depth runs (1) AdaBins, (2) DiverseDepth, (3) MiDaS, (4) SGDepth, and (5) Monodepth2, and calculates a weighted-average per-pixel absolute dept

Pranav 39 Nov 21, 2022
Web service for facial landmark detection, head pose estimation, facial action unit recognition, and eye-gaze estimation based on OpenFace 2.0

OpenGaze: Web Service for OpenFace Facial Behaviour Analysis Toolkit Overview OpenFace is a fantastic tool intended for computer vision and machine le

Sayom Shakib 4 Nov 3, 2022
OpenFace – a state-of-the art tool intended for facial landmark detection, head pose estimation, facial action unit recognition, and eye-gaze estimation.

OpenFace 2.2.0: a facial behavior analysis toolkit Over the past few years, there has been an increased interest in automatic facial behavior analysis

Tadas Baltrusaitis 5.8k Dec 31, 2022