Home

Awesome

<p align="center"> <h1 align="center">๐Ÿงถ Disent</h1> <p align="center"> <i>A modular disentangled representation learning framework built with PyTorch Lightning</i> </p> </p> <p align="center"> <a href="https://choosealicense.com/licenses/mit/" target="_blank"> <img alt="license" src="https://img.shields.io/github/license/nmichlo/disent?style=flat-square&color=lightgrey"/> </a> <a href="https://pypi.org/project/disent" target="_blank"> <img alt="python versions" src="https://img.shields.io/pypi/pyversions/disent?style=flat-square"/> </a> <a href="https://pypi.org/project/disent" target="_blank"> <img alt="pypi version" src="https://img.shields.io/pypi/v/disent?style=flat-square&color=blue"/> </a> <a href="https://github.com/nmichlo/disent/actions?query=workflow%3Atests"> <img alt="tests status" src="https://github.com/nmichlo/disent/actions/workflows/python-tests.yml/badge.svg"/> </a> <a href="https://github.com/psf/black" target="_blank"> <img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"/> </a> <a href="https://pycqa.github.io/isort" target="_blank"> <img alt="Imports: isort" src="https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336"/> </a> <!-- <a href="https://codecov.io/gh/nmichlo/disent/"> --> <!-- <img alt="code coverage" src="https://img.shields.io/codecov/c/gh/nmichlo/disent?token=86IZK3J038&style=flat-square"/> --> <!-- </a> --> <!-- <a href="https://github.com/nmichlo/disent"> --> <!-- <img alt="last commit" src="https://img.shields.io/github/last-commit/nmichlo/disent?style=flat-square&color=lightgrey"/> --> <!-- </a> --> </p> <p align="center"> <p align="center"> Visit the <a href="https://disent.michlo.dev/" target="_blank">docs</a> for more info, or browse the <a href="https://github.com/nmichlo/disent/releases">releases</a>. </p> <p align="center"> <a href="https://github.com/nmichlo/disent/issues/new/choose">Contributions</a> are welcome! </p> </p> <p align="center"> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ <br/> <i><b> NOTE:</b> My MSc. research has moved <a href="https://github.com/nmichlo/msc-research">here</a></i> <br/> <i>Some of the contributions have been incorporated directly into disent</i> <br/> โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ </p>

Table Of Contents


Overview

Disent is a modular disentangled representation learning framework for auto-encoders, built upon PyTorch-Lightning. This framework consists of various composable components that can be used to build and benchmark various disentanglement vision tasks.

The name of the framework is derived from both disentanglement and scientific dissent.

Get started with disent by installing it with $pip install disent or cloning this repository.

Goals

Disent aims to fill the following criteria:

  1. Provide high quality, readable, consistent and easily comparable implementations of frameworks
  2. Highlight difference between framework implementations by overriding hooks and minimising duplicate code
  3. Use best practice eg. torch.distributions
  4. Be extremely flexible & configurable
  5. Support low memory systems

Citing Disent

Please use the following citation if you use Disent in your own research:

@Misc{Michlo2021Disent,
  author =       {Nathan Juraj Michlo},
  title =        {Disent - A modular disentangled representation learning framework for pytorch},
  howpublished = {Github},
  year =         {2021},
  url =          {https://github.com/nmichlo/disent}
}

Features

Disent includes implementations of modules, metrics and datasets from various papers.

Note that "๐Ÿงต" means that the dataset, framework or metric was introduced by disent!

Datasets

Various common datasets used in disentanglement research are included with disent. The dataset loaders provide various features including:

Data input and target dataset augmentations and transforms are supported, as well as augmentations on the GPU or CPU at different points in the pipeline.

Frameworks

Disent provides the following Auto-Encoders and Variational Auto-Encoders!

Introduced in Disent

<details><summary><b>๐Ÿ— Todo</b>: <i>Many popular disentanglement frameworks still need to be added, please submit an issue if you have a request for an additional framework.</i></summary><p> </p></details>

Metrics

Various metrics are provided by disent that can be used to evaluate the learnt representations of models that have been trained on ground-truth data.

<details><summary><b>๐Ÿ— Todo</b>: <i>Some popular metrics still need to be added, please submit an issue if you wish to add your own, or you have a request.</i></summary><p> </p></details>

Schedules & Annealing

Hyper-parameter annealing is supported through the use of schedules. The currently implemented schedules include:


Architecture

The disent module structure:

โš ๏ธ The API Is Mostly Stable โš ๏ธ

Disent is still under development. Features and APIs are subject to change! However, I will try and minimise the impact of these.

A small suite of tests currently exist which will be expanded upon in time.

Hydra Experiment Directories

Easily run experiments with hydra config, these files are not available from pip install.

Extending The Default Configs

All configs in experiment/config can easily be extended or overridden without modifying any files. We can add a new config folder to the hydra search path by setting the environment variable DISENT_CONFIGS_PREPEND to point to a config folder that should take priority over those contained in the default folder.

The advantage of this is that new frameworks and datasets can be used with experiments without cloning or modifying disent itself. You can separate your research code from the library!


Examples

Python Example

The following is a basic working example of disent that trains a BetaVAE with a cyclic beta schedule and evaluates the trained model with various metrics.

<details><summary><b>๐Ÿ’พ Basic Example</b></summary> <p>
import lightning as L
import torch
from torch.utils.data import DataLoader

from disent.dataset import DisentDataset
from disent.dataset.data import XYObjectData
from disent.dataset.sampling import SingleSampler
from disent.dataset.transform import ToImgTensorF32
from disent.frameworks.vae import BetaVae
from disent.metrics import metric_dci
from disent.metrics import metric_mig
from disent.model import AutoEncoder
from disent.model.ae import DecoderConv64
from disent.model.ae import EncoderConv64
from disent.schedule import CyclicSchedule

# create the dataset & dataloaders
# - ToImgTensorF32 transforms images from numpy arrays to tensors and performs checks
# - if you use `num_workers != 0` in the DataLoader, the make sure to
#   wrap `trainer.fit` with `if __name__ == '__main__': ...`
data = XYObjectData()
dataset = DisentDataset(dataset=data, sampler=SingleSampler(), transform=ToImgTensorF32())
dataloader = DataLoader(dataset=dataset, batch_size=128, shuffle=True, num_workers=0)

# create the BetaVAE model
# - adjusting the beta, learning rate, and representation size.
module = BetaVae(
    model=AutoEncoder(
        # z_multiplier is needed to output mu & logvar when parameterising normal distribution
        encoder=EncoderConv64(x_shape=data.x_shape, z_size=10, z_multiplier=2),
        decoder=DecoderConv64(x_shape=data.x_shape, z_size=10),
    ),
    cfg=BetaVae.cfg(
        optimizer='adam',
        optimizer_kwargs=dict(lr=1e-3),
        loss_reduction='mean_sum',
        beta=4,
    )
)

# cyclic schedule for target 'beta' in the config/cfg. The initial value from the
# config is saved and multiplied by the ratio from the schedule on each step.
# - based on: https://arxiv.org/abs/1903.10145
module.register_schedule(
    'beta', CyclicSchedule(
        period=1024,  # repeat every: trainer.global_step % period
    )
)

# train model
# - for 2048 batches/steps
trainer = L.Trainer(
    max_steps=2048, gpus=1 if torch.cuda.is_available() else None, logger=False, enable_checkpointing=False
)
trainer.fit(module, dataloader)

# compute disentanglement metrics
# - we cannot guarantee which device the representation is on
# - this will take a while to run
get_repr = lambda x: module.encode(x.to(module.device))

metrics = {
    **metric_dci(dataset, get_repr, num_train=1000, num_test=500, show_progress=True),
    **metric_mig(dataset, get_repr, num_train=2000),
}

# evaluate
print('metrics:', metrics)
</p> </details>

Visit the docs for more examples!

Hydra Config Example

The entrypoint for basic experiments is experiment/run.py.

Some configuration will be required, but basic experiments can be adjusted by modifying the Hydra Config 1.1 files in experiment/config.

Modifying the main experiment/config/config.yaml is all you need for most basic experiments. The main config file contains a defaults list with entries corresponding to yaml configuration files (config options) in the subfolders (config groups) in experiment/config/<config_group>/<option>.yaml.

<details><summary><b>๐Ÿ’พ Config Defaults Example</b></summary> <p>
defaults:
  # data
  - sampling: default__bb
  - dataset: xyobject
  - augment: none
  # system
  - framework: adavae_os
  - model: vae_conv64
  # training
  - optimizer: adam
  - schedule: beta_cyclic
  - metrics: fast
  - run_length: short
  # logs
  - run_callbacks: vis
  - run_logging: wandb
  # runtime
  - run_location: local
  - run_launcher: local
  - run_action: train

# <rest of config.yaml left out>
...
</p> </details>

Easily modify any of these values to adjust how the basic experiment will be run. For example, change framework: adavae to framework: betavae, or change the dataset from xyobject to shapes3d. Add new options by adding new yaml files in the config group folders.

Weights and Biases is supported by changing run_logging: none to run_logging: wandb. However, you will need to login from the command line. W&B logging supports visualisations of latent traversals.


Install

pip install disent

Otherwise, to install from source we recommend using a conda virtual environment.

<details><summary><b>โคต๏ธ Install from Source</b></summary>
# clone the repo
git clone https://github.com/nmichlo/disent
cd disent

# create and activate the conda environment [py38,py39,py310]
conda create -n disent-py310 python=3.10
conda activate disent-py310

# check that the correct python version is used
which python
which pip

# make sure to upgrade pip
pip install --upgrade pip

# install minimal requirements
pip install -r requirements.txt

# (optional) install extra requirements
# - first do the above because torch is required to compile torchsort while installing
pip install -r requirements-extra.txt

# (optional) install test requirements
pip install -r requirements-test.txt
</details>

Development

Code style: black Imports: isort

Make sure to install pre-commit hooks to ensure code is automatically formatted correctly when committing or pushing changes to disent.

# install git hooks
pip install pre-commit
pre-commit install

# manually trigger all pre-commit hooks
pre-commit run --all-files

To run tests locally, make sure to install all the test and extra dependencies in your environment.

pip install -r requirements.txt
# torchsort first requires torch to be installed
pip install -r requirements-extra.txt -r requirements-test.txt

Why?