Code Monkey home page Code Monkey logo

deepscm's Introduction

Deep Structural Causal Models for Tractable Counterfactual Inference

arXiv Open In Colab

Counterfactual Example

This repository contains the code for the paper

N. Pawlowski+, D. C. Castro+, B. Glocker. Deep Structural Causal Models for Tractable Counterfactual Inference. Advances in Neural Information Processing Systems. 2020 [NeurIPS Proceedings] [arXiv] [NeurIPS][Poster]

(+: joint first authors)

If you use these tools or datasets in your publications, please consider citing the accompanying paper with a BibTeX entry similar to the following:

@inproceedings{pawlowski2020dscm,
    author = {Pawlowski, Nick and Castro, Daniel C. and Glocker, Ben},
    title = {Deep Structural Causal Models for Tractable Counterfactual Inference},
    year = {2020},
    booktitle={Advances in Neural Information Processing Systems},
}

Please refer to the tagged code for the code used for the NeurIPS publication.

Structure

This repository contains code and assets structured as follows:

  • deepscm/: contains the code used for running the experiments
    • arch/: model architectures used in experiments
    • datasets/: script for dataset generation and data loading used in experiments
    • distributions/: implementations of useful distributions or transformations
    • experiments/: implementation of experiments
    • morphomnist/: soft link to morphomnist tools in submodules
    • submodules/: git submodules
  • assets/
    • data/:
      • morphomnist/: used synthetic morphomnist dataset
      • ukbb/: subset of the ukbb testset that was used for the counterfactuals
    • models/: checkpoints of the trained models

Requirements

We use Python 3.7.2 for all experiments and you will need to install the following packages:

pip install numpy pandas pyro-ppl pytorch-lightning scikit-image scikit-learn scipy seaborn tensorboard torch torchvision

or simply run pip install -r requirements.txt. You will also need to sync the submodule: git submodule update --recursive --init.

Usage

We assume that the code is executed from the root directory of this repository.

Morpho-MNIST

You can recreate the data using the data creation script as:

python -m deepscm.datasets.morphomnist.create_synth_thickness_intensity_data --data-dir /path/to/morphomnist -o /path/to/dataset

where /path/to/morphomnist refers to the directory containing the files from the original MNIST dataset with the original morphometrics from Morpho-MNIST dataset. Alternatively we provide the generated data in data/morphomnist. You can then train the models as:

python -m deepscm.experiments.morphomnist.trainer -e SVIExperiment -m {IndependentVISEM, ConditionalDecoderVISEM, ConditionalVISEM} --data_dir /path/to/data --default_root_dir /path/to/checkpoints --decoder_type fixed_var {--gpus 0}

where IndependentVISEM is the independent model, ConditionalDecoderVISEM is the conditional model and ConditionalVISEM is the full model. The checkpoints are saved in /path/to/checkpoints or the provided checkpoints can be used for testing and plotting:

python -m deepscm.experiments.morphomnist.tester -c /path/to/checkpoint/version_?

where /path/to/checkpoint/version_? refers to the path containing the specific pytorch-lightning run. The notebooks for plotting are situated in deepscm/experiments/plotting/morphomnist.

UKBB

We are unable to share the UKBB dataset. However, if you have access to the UK Biobank or a similar dataset of brain scans, you can then train the models as:

python -m deepscm.experiments.medical.trainer -e SVIExperiment -m ConditionalVISEM --default_root_dir /path/to/checkpoints --downsample 3 --decoder_type fixed_var --train_batch_size 256 {--gpus 0}

The checkpoints are saved in /path/to/checkpoints or the provided checkpoints can be used for testing and plotting:

python -m deepscm.experiments.medical.tester -c /path/to/checkpoint/version_?

The notebooks for plotting are situated in deepscm/experiments/plotting/ukbb.

deepscm's People

Contributors

dccastro avatar miguelmonteiro avatar pawni 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  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  avatar  avatar  avatar  avatar

deepscm's Issues

Why not directly infer the value of x-based distribution?

Hello, thank you very much for your excellent work.
In deepscm-master\deepscm\experiments\morphomnist\nf\conditional_image_flow_sem.py line 132-140
Why not directly infer the value of x-based distribution like the function of infer_intensity_base or infer_e_t?
In your code, you still use "x_base_dist = Normal(self.x_base_loc, self.x_base_scale)" to generate the inference value of x_based.

KeyError: 'hparams' when running tester

I trained the full model on Morpho-MNIST using the command

python -m deepscm.experiments.morphomnist.trainer -e SVIExperiment -m ConditionalVISEM --data_dir /path/to/data --default_root_dir /path/to/checkpoints --decoder_type fixed_var.

This worked without issues. However, for running python -m deepscm.experiments.morphomnist.tester -c /path/to/checkpoint/version_0, I get KeyError: 'hparams' in torch.load(checkpoint_path, map_location=torch.device('cpu'))['hparams'].

Indeed, I saw in the debugger that 'hparams' does not exist as dictionary key. However, there exists a key called 'hyper_parameters'. Upon changing that (which already seems wrong), I get an error further down in the line exp_class.load_from_checkpoint(checkpoint_path, pyro_model=model), saying TypeError: __init__() missing 1 required positional argument: 'hparams'.

I checked the code and to me the errors do seem plausible. I would be more surprised if this code would work for anyone. I did use the correct python & package versions.

question about impelemention error : self.hparams = hparams

Thanks for your great work.
when I run the code :
python -m deepscm.experiments.morphomnist.trainer -e SVIExperiment -m ConditionalVISEM --data_dir /path/to/data --default_root_dir /path/to/checkpoints --decoder_type fixed_var {--gpus 0}

some wrong happened:
In deepscm\experiments\morphomnist\base_experiment.py

Traceback (most recent call last):
  File "E:\anaconda3\envs\dscm\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "E:\anaconda3\envs\dscm\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "deepscm\deepscm\experiments\morphomnist\trainer.py", line 97, in <module>
    experiment = exp_class(hparams, model)
  File "\deepscm\deepscm\experiments\morphomnist\sem_vi\base_sem_experiment.py", 
line 270, in __init__
    super().__init__(hparams, pyro_model)
  File "deepscm\deepscm\experiments\morphomnist\base_experiment.py", line 175, in __init__
    self.hparams = hparams
  File "E:\anaconda3\envs\dscm\lib\site-packages\torch\nn\modules\module.py", line 1225, in __setattr__
    object.__setattr__(self, name, value)
AttributeError: can't set attribute

I'm no idea how to fix this issue. Looking forward to your reply

Encountered NaN under the ConditionalDecoderVISEM setting

Hello,

Thanks for open-sourcing this beautiful project. I am currently trying to replicate the results in Table 1 of the paper, however, while I was trying to train the Conditional model, I have experienced this error message stating that a NaN loss value has been encountered, after training the model for 222 epochs. Just to mention, I performed a clean installation of the necessary libraries and I have used the Morpho-MNIST data creation script which you provided. Would there be anything wrong with the calculation of the ELBO of the p(intensity), since that's the only metric that has gone to NaN?

Steps to reproduce the behavior:

python -m deepscm.experiments.morphomnist.trainer -e SVIExperiment -m ConditionalDecoderVISEM --data_dir data/morphomnist/ --default_root_dir checkpoints/ --decoder_type fixed_var --gpus 0

Whole Error Message:

Epoch 222: 6%|โ–‹ | 15/251 [00:01<00:28, 8.31it/s, loss=952243.375, v_num=2]/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pyro/infer/tracegraph_elbo.py:261: UserWarning: Encountered NaN: loss
warn_if_nan(loss, "loss")
Traceback (most recent call last):
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/ilkay/Documents/caner/deepscm/deepscm/experiments/morphomnist/trainer.py", line 62, in
trainer.fit(experiment)
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 859, in fit
self.single_gpu_train(model)
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/distrib_parts.py", line 503, in single_gpu_train
self.run_pretrain_routine(model)
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/trainer.py", line 1015, in run_pretrain_routine
self.train()
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 347, in train
self.run_training_epoch()
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 419, in run_training_epoch
_outputs = self.run_training_batch(batch, batch_idx)
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 597, in run_training_batch
loss, batch_output = optimizer_closure()
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 561, in optimizer_closure
output_dict = self.training_forward(split_batch, batch_idx, opt_idx, self.hiddens)
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/pytorch_lightning/trainer/training_loop.py", line 727, in training_forward
output = self.model.training_step(*args)
File "/home/ilkay/Documents/caner/deepscm/deepscm/experiments/morphomnist/sem_vi/base_sem_experiment.py", line 385, in training_step
raise ValueError('loss went to nan with metrics:\n{}'.format(metrics))
ValueError: loss went to nan with metrics:
{'log p(x)': tensor(-3502.9570, device='cuda:0', grad_fn=), 'log p(intensity)': tensor(nan, device='cuda:0', grad_fn=), 'log p(thickness)': tensor(-0.9457, device='cuda:0', grad_fn=), 'p(z)': tensor(-22.2051, device='cuda:0', grad_fn=), 'q(z)': tensor(54.3670, device='cuda:0', grad_fn=), 'log p(z) - log q(z)': tensor(-76.5721, device='cuda:0', grad_fn=)}
Exception ignored in: <function tqdm.del at 0x7ffb7e9d2320>
Traceback (most recent call last):
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/tqdm/std.py", line 1135, in del
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/tqdm/std.py", line 1282, in close
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/tqdm/std.py", line 1467, in display
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/tqdm/std.py", line 1138, in repr
File "/home/ilkay/miniconda3/envs/torch/lib/python3.7/site-packages/tqdm/std.py", line 1425, in format_dict
TypeError: cannot unpack non-iterable NoneType object

Environment

PyTorch version: 1.7.1
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: Could not collect

Python version: 3.7 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: Quadro RTX 6000
Nvidia driver version: 440.100
cuDNN version: /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.4
[pip3] pytorch-lightning==0.7.6
[pip3] torch==1.7.1
[pip3] torchvision==0.6.0a0+35d732a
[conda] blas 1.0 mkl
[conda] cudatoolkit 10.2.89 hfd86e86_1
[conda] mkl 2020.1 217
[conda] mkl-service 2.3.0 py37he904b0f_0
[conda] mkl_fft 1.1.0 py37h23d657b_0
[conda] mkl_random 1.1.1 py37h0573a6f_0
[conda] numpy 1.19.4 pypi_0 pypi
[conda] pytorch-lightning 0.7.6 pypi_0 pypi
[conda] torch 1.7.1 pypi_0 pypi
[conda] torchvision 0.6.1 py37_cu102 pytorch

Questions about implementation

Thank you very much for your interesting work.
I am very interested in this work, but I have the following problem when I run the code.
I look forward to receiving your answers.

Traceback (most recent call last):
File "D:\anaconda\envs\torch\lib\runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, Error)
File "D:\anaconda\envs\torch\lib\runpy.py", line 109, in get_module_details
import(pkg_name)
File "C:\Users\admin\Downloads\deepscm-master (1)\deepscm-master\deepscm\experiments\morphomnist_init
.py", line 3, in
from . import sem_vi # noqa: F401
File "C:\Users\admin\Downloads\deepscm-master (1)\deepscm-master\deepscm\experiments\morphomnist\sem_vi_init
.py", line 1, in
from .base_sem_experiment import SVIExperiment
File "C:\Users\admin\Downloads\deepscm-master (1)\deepscm-master\deepscm\experiments\morphomnist\sem_vi\base_sem_experiment.py", line 16, in
from deepscm.distributions.deep import DeepMultivariateNormal, DeepIndepNormal, Conv2dIndepNormal, DeepLowRankMultivariateNormal
File "C:\Users\admin\Downloads\deepscm-master (1)\deepscm-master\deepscm\distributions\deep.py", line 8, in
from .params import MixtureParams
File "C:\Users\admin\Downloads\deepscm-master (1)\deepscm-master\deepscm\distributions\params.py", line 8, in
from .mixture import Mixture
File "C:\Users\admin\Downloads\deepscm-master (1)\deepscm-master\deepscm\distributions\mixture.py", line 14, in
class Mixture(TorchDistribution, Generic[T]):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Empty lightning_args

Hi, I've failed to run your code and I am getting this message

AttributeError: 'Namespace' object has no attribute 'default_root_dir

It seems that the lighning_args is the issue here. Also here is the command that I have run:

python -m deepscm.experiments.morphomnist.trainer -e SVIExperiment -m IndependentVISEM --data_dir ~/Desktop/Projects/Benchmarking/deepscm/dataset --default_root_dir ~/Desktop/Projects/Benchmarking/checkpoints1 --decoder_type fixed_var --gpus=1

Guidance for custom datasets

Thanks for releasing deepscm. I really enjoyed reading the paper, and appreciate the code. I see under deepscam/datasets there are a few scripts for the mnist and medical datasets. Do you have any guidance on the best way to prepare a custom dataset to train?

version conflict?

deepscm/experiments/medical/tester.py breaks at experiment = exp_class.load_from_checkpoint(checkpoint_path, pyro_model=model) due to an update to pytorch_lightning

In order to open the pre-trained model, I suggest this (somewhat hacky) alternative:

    # first initialise the experiment
    experiment = exp_class(argparse.Namespace(**hparams), model)
    # then load the state_dict
    state_dict = torch.load(checkpoint_path, map_location=torch.device('cpu'))['state_dict']
    # fix a key error TODO: understand why there is a key error?
    Old = ['pyro_model.age_flow_components.0.layer.unnormalized_derivatives',
           'pyro_model.age_flow_components.0.layer.unnormalized_heights',
           'pyro_model.age_flow_components.0.layer.unnormalized_lambdas',
           'pyro_model.age_flow_components.0.layer.unnormalized_widths']
    New = ['pyro_model.age_flow_components.0.unnormalized_derivatives',
           'pyro_model.age_flow_components.0.unnormalized_heights',
           'pyro_model.age_flow_components.0.unnormalized_lambdas',
           'pyro_model.age_flow_components.0.unnormalized_widths']
    for old, new in zip(Old, New):
        state_dict[new] = state_dict[old]
        del state_dict[old]
    # check it worked
    model_state_dict_keys = set(experiment.state_dict().keys())
    state_dict_keys = set(list(state_dict))
    if model_state_dict_keys != state_dict_keys:
        raise KeyError("model keys and checkpoint keys do not match for these keys:\nmodel: {}\ncheckpoint:{}".format(
model_state_dict_keys - state_dict_keys, state_dict_keys - model_state_dict_keys))
    # load state_dict
    experiment.load_state_dict(state_dict)

Hi

Thanks for your open sourcing. I try to replicate the code and have the following problem:
"from deepscm.morphomnist import io
ModuleNotFoundError: No module named 'deepscm.morphomnist"
How can I fix this issue? Looking forward your reply ๐Ÿ˜Š

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.