Skip to content

Commit

Permalink
add all files
Browse files Browse the repository at this point in the history
  • Loading branch information
melanibe committed Mar 4, 2024
0 parents commit 1c5b505
Show file tree
Hide file tree
Showing 64 changed files with 8,692 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI

on:
push:
branches: main
pull_request:
branches: main

jobs:
build-linux:
runs-on: ubuntu-latest
strategy:
max-parallel: 5
defaults:
run:
shell: bash -el {0}
steps:
- uses: actions/checkout@v3
- name: Set up Conda
uses: conda-incubator/setup-miniconda@v2
with:
environment-file: environmentCI.yml
python-version: 3.11.0
auto-activate-base: true
- name: Lint with flake8
run: |
python -V
conda info
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --extend-ignore=E203 --show-source --statistics --max-line-length=119
156 changes: 156 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
joined_simple.csv
*.sh
*.png
*.pdf
*.eps
embed_cf/

outputs
znew_scripts
padchest_cf_images_v0
cf_beta1balanced_scanner
cf_beta2balanced_scanner


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# custom gitignores
**/outputs/
**/test_outputs/
**/.vscode/
**/wandb/
cifar-10-batches-py
*.tar.gz
causal-contrastive.code-workspace
**/dscm_checkpoints/
/data/
MNIST/
playground.py
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Team MIRA - BioMedIA

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# CF-SimCLR: counterfactual contrastive learning

This repository contains the code for the paper "Counterfactual contrastive learning: robust representations via causal image synthesis".

![alt text](figure1.png)

## Overview
The repository is divided in three main parts:
* The [causal_model/](causal_models/) folder contains all code related to counterfactual inference model training. It contains its own README, giving you all necessary commands to train a DSCM on EMBED and PadChest.
* The [classification/](classification/) folder contains all the code related to self-supervised training as well as finetuning for evaluation (see below).
* The [data_handling/](data_handling/) folder contains everything you need to define your dataset classes. In particular, it contains all the boilerplate for CF-SimCLR specific data loading.
* The [evaluation/](evaluation/) folder contains all the code related to test inference and results plotting for reproducing the plots from the paper.


## Prerequisites

### Code dependencies
The code is written in PyTorch, with PyTorch Lightning.
You can install all our dependencies using our conda enviromnent requirements file `environment_gpu.yml`.

### Datasets
You will need to download the relevant datasets to run our code.
All datasets are publicly available and be downloaded at:
* PadChest [https://bimcv.cipf.es/bimcv-projects/padchest/](https://bimcv.cipf.es/bimcv-projects/padchest/), [https://www.sciencedirect.com/science/article/pii/S1361841520301614](https://www.sciencedirect.com/science/article/pii/S1361841520301614)
* RSNA Pneumonia Detection Dataset: [https://www.kaggle.com/c/rsna-pneumonia-detection-challenge](https://www.kaggle.com/c/rsna-pneumonia-detection-challenge)
* CheXpert [https://stanfordaimi.azurewebsites.net/datasets/8cbd9ed4-2eb9-4565-affc-111cf4f7ebe2](https://stanfordaimi.azurewebsites.net/datasets/8cbd9ed4-2eb9-4565-affc-111cf4f7ebe2)
* EMBED [https://pubs.rsna.org/doi/full/10.1148/ryai.220047](https://pubs.rsna.org/doi/full/10.1148/ryai.220047), [https://github.com/Emory-HITI/EMBED_Open_Data/tree/main](https://github.com/Emory-HITI/EMBED_Open_Data/tree/main)
* VinDR Mammo [https://vindr.ai/datasets/mammo](https://vindr.ai/datasets/mammo)

Once you have downloaded the datasets, please update the corresponding paths at the top of the `mammo.py` and `xray.py` files.
Additionally, for EMBED you will need to preprocess the original dataframes with our script `data_handling/csv_generation_code/generate_embed_csv.ipynb`. Similarly for RSNA please run first `data_handling/csv_generation_code/rsna_generate_full_csv.py`.


## Full workflow example for training and evaluating CF-SimCLR on mammography
Here we'll run through an example to train and evaluate CF-SimCLR on EMBED

1. Train a counterfactual image generation model with
```
python causal_models/main.py --hps embed
```

2. Generate and save all counterfactuals from every image in the training set with
```
python causal_models/save_embed_scanner_cf.py
```

3. Train the CF-SimCLR model
```
python classification/train.py experiment=simclr_embed data.use_counterfactuals=True data.counterfactual_contrastive=True
```
Alternatively to train a SimCLR baseline just run
```
python classification/train.py experiment=simclr_embed
```
Or to run the baseline with counterfactuals added to the training set without counterfactual contrastive objective
```
python classification/train.py experiment=simclr_embed data.use_counterfactuals=True counterfactual_contrastive=False
```

4. Train classifier with linear finetuning or finetuning
```
python classification/train.py experiment=base_density trainer.finetune_path=PATH_TO_ENCODER seed=33 trainer.freeze_encoder=True
```
You can choose the proportion of labelled data to use for finetuning with the flag `data.prop_train=1.0`

5. Evaluate on the test set by running the notebook `evaluation/embed_density.ipynb` to run and save inference results on the test set.
Empty file added __init__.py
Empty file.
14 changes: 14 additions & 0 deletions causal_models/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Code for counterfactual image generation

The code in this folder is adapted from the official code associated with the
'High Fidelity Image Counterfactuals with Probabilistic Causal Models' paper. Original code: [https://github.com/biomedia-mira/causal-gen](https://github.com/biomedia-mira/causal-gen).

## Train the counterfactual inference model

To train the counterfactual inference models in the paper you can simply run
`python causal_models/main.py --hps embed` replace by `padchest` if you want to train on chest x-rays. All associated hyperparameters are stored in `causal_models/hps.py`.

This assumes you have already set up your data folders as per the main repository.

## Generating and saving counterfactuals for contrastive training
To generate all possible domain counterfactuals given a trained model, you can use our predefined scripts: `save_embed_scanner_cf.py` and`save_padchest_scanner_cf.py`. Simply pass your checkpoint path and your target saving directory as command line arguments.
Empty file added causal_models/__init__.py
Empty file.
Loading

0 comments on commit 1c5b505

Please sign in to comment.