{"id":21341695,"url":"https://github.com/marksgraham/ddpm-ood","last_synced_at":"2025-07-12T15:30:35.690Z","repository":{"id":173169882,"uuid":"566531671","full_name":"marksgraham/ddpm-ood","owner":"marksgraham","description":"Official PyTorch code for \"Out-of-distribution detection with denoising diffusion models\"","archived":false,"fork":false,"pushed_at":"2024-06-09T17:20:09.000Z","size":177,"stargazers_count":33,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-06-09T18:43:42.054Z","etag":null,"topics":["diffusion-models","out-of-distribution-detection","pytorch"],"latest_commit_sha":null,"homepage":"https://arxiv.org/abs/2211.07740","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marksgraham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-15T21:44:54.000Z","updated_at":"2024-06-09T17:20:12.000Z","dependencies_parsed_at":"2024-01-18T10:44:30.346Z","dependency_job_id":null,"html_url":"https://github.com/marksgraham/ddpm-ood","commit_stats":null,"previous_names":["marksgraham/ddpm-ood"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksgraham%2Fddpm-ood","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksgraham%2Fddpm-ood/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksgraham%2Fddpm-ood/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marksgraham%2Fddpm-ood/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marksgraham","download_url":"https://codeload.github.com/marksgraham/ddpm-ood/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225824739,"owners_count":17529906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["diffusion-models","out-of-distribution-detection","pytorch"],"created_at":"2024-11-22T00:58:01.425Z","updated_at":"2024-11-22T00:58:02.088Z","avatar_url":"https://github.com/marksgraham.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eDenoising diffusion models for out-of-distribution detection\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\nPerform reconstruction-based out-of-distribution detection with DDPMs.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg width=\"800\" height=\"300\" src=\"https://user-images.githubusercontent.com/7947315/233470531-df6437d7-e277-4147-96a0-6aa354cf2ef4.svg\"\u003e\n\u003c/p\u003e\n\n\n## Intro\n\nThis codebase contains the code to perform unsupervised out-of-distribution detection with diffusion models.\nIt supports the use of DDPMs as well as Latent Diffusion Models (LDM) for dealing with higher dimensional 2D or 3D data.\nIt is based on work published in [1] and [2].\n\n[1] [Denoising diffusion models for out-of-distribution detection, CVPR VAND Workshop 2023](https://arxiv.org/abs/2211.07740)\n\n[2] [Unsupervised 3D out-of-distribution detection with latent diffusion models, MICCAI 2023](https://arxiv.org/abs/2307.03777)\n\n## Setup\n\n### Install\nCreate a fresh virtualenv (this codebase was developed and tested with Python 3.8) and then install the required packages:\n\n```pip install -r requirements.txt```\n\nYou can also build the docker image\n```bash\ncd docker/\nbash create_docker_image.sh\n```\n\n### Setup paths\nSelect where you want your data and model outputs stored.\n```\ndata_root=/root/for/downloaded/dataset\noutput_root=/root/for/saved/models\n```\n\n## Run with DDPM\nWe'll use the example of FashionMNIST as an in-distribution dataset and [SVHN,CIFAR10, CelebA] as out-of-distribution datasets.\n### Download and process datasets\n```bash\npython src/data/get_computer_vision_datasets.py --data_root=${data_root}\n```\nN.B. If the error \"The daily quota of the file img_align_celeba.zip is exceeded and it can't be downloaded\" is thrown,\nyou need to download these files manually from the GDrive and place them in `${data_root}/CelebA/raw/`,\n[see here](https://github.com/pytorch/vision/issues/1920#issuecomment-852237902). You can then run\n\n```bash\npython get_datasets.py --data_root=${data_root} --download_celeba=False\n```\n\nTo use your own data, you just need to provide separate csvs containing paths for the train/val/test splits.\n\n### Train models\nExamples here use FashionMNIST as the in-distribution dataset. Commands for other datasets are given\nin [README_additional.md](README_additional.md).\n\n```bash\npython train_ddpm.py \\\n--output_dir=${output_root} \\\n--model_name=fashionmnist \\\n--training_ids=${data_root}/data_splits/FashionMNIST_train.csv \\\n--validation_ids=${data_root}/data_splits/FashionMNIST_val.csv \\\n--is_grayscale=1 \\\n--n_epochs=300 \\\n--beta_schedule=scaled_linear_beta \\\n--beta_start=0.0015 \\\n--beta_end=0.0195\n```\n\nYou can track experiments in tensorboard\n```bash\ntensorboard --logdir=${output_root}\n```\n\nThe code is DistributedDataParallel (DDP) compatible. To train on e.g. 2 GPUs:\n\n```bash\ntorchrun --nproc_per_node=2 --nnodes=1 --node_rank=0 \\\ntrain_ddpm.py \\\n--output_dir=${output_root} \\\n--model_name=fashionmnist \\\n--training_ids=${data_root}/data_splits/FashionMNIST_train.csv \\\n--validation_ids=${data_root}/data_splits/FashionMNIST_val.csv \\\n--is_grayscale=1 \\\n--n_epochs=300 \\\n--beta_schedule=scaled_linear_beta \\\n--beta_start=0.0015 \\\n--beta_end=0.0195\n```\n\n### Reconstruct data\n\n```bash\npython reconstruct.py \\\n--output_dir=${output_root} \\\n--model_name=fashionmnist \\\n--validation_ids=${data_root}/data_splits/FashionMNIST_val.csv \\\n--in_ids=${data_root}/data_splits/FashionMNIST_test.csv \\\n--out_ids=${data_root}/data_splits/MNIST_test.csv,${data_root}/data_splits/FashionMNIST_vflip_test.csv,${data_root}/data_splits/FashionMNIST_hflip_test.csv \\\n--is_grayscale=1 \\\n--beta_schedule=scaled_linear_beta \\\n--beta_start=0.0015 \\\n--beta_end=0.0195 \\\n--num_inference_steps=100 \\\n--inference_skip_factor=4 \\\n--run_val=1 \\\n--run_in=1 \\\n--run_out=1\n```\nThe arg `inference_skip_factor` controls the amount of t starting points that are skipped during reconstruction.\nThis table shows the relationship between values of `inference_skip_factor` and the number of reconstructions, as needed\nto reproduce results in Supplementary Table 4 (for max_t=1000).\n\n| **inference_skip_factor:** | 1   | 2   | 3   | 4   | 5   | 8   | 16  | 32  | 64  |\n|------------------------|-----|-----|-----|-----|-----|-----|-----|-----|-----|\n| **num_reconstructions:**   | 100 | 50  | 34  | 25  | 20  | 13  | 7   | 4   | 2   |\n\nN.B. For a quicker run, you can choose to only reconstruct a subset of the validation set with e.g. `--first_n_val=1000`\nor a subset of the in/out datasets with `--first_n=1000`\n\n\n### Classify samples as OOD\n```bash\npython ood_detection.py \\\n--output_dir=${output_root} \\\n--model_name=fashionmnist\n```\n\n## Run with LDM\nWe'll use the 3D Medical Decathlon Dataset here. In this example we'll use the BraTS dataset as the in-distribution dataset,\nand the other 9 datasets as out-of-distribution datasets.\n\n### Download and process datasets\n```bash\npython src/data/get_decathlon_datasets.py --data_root=${data_root}/Decathlon\n```\n### Train VQVAE\n```bash\npython train_vqvae.py  \\\n--output_dir=${output_root} \\\n--model_name=vqvae_decathlon \\\n--training_ids=${data_root}/data_splits/Task01_BrainTumour_train.csv \\\n--validation_ids=${data_root}/data_splits/Task01_BrainTumour_val.csv  \\\n--is_grayscale=1 \\\n--n_epochs=300 \\\n--batch_size=8  \\\n--eval_freq=10 \\\n--cache_data=0  \\\n--vqvae_downsample_parameters=[[2,4,1,1],[2,4,1,1],[2,4,1,1],[2,4,1,1]] \\\n--vqvae_upsample_parameters=[[2,4,1,1,0],[2,4,1,1,0],[2,4,1,1,0],[2,4,1,1,0]] \\\n--vqvae_num_channels=[256,256,256,256] \\\n--vqvae_num_res_channels=[256,256,256,256] \\\n--vqvae_embedding_dim=128 \\\n--vqvae_num_embeddings=2048 \\\n--vqvae_decay=0.9  \\\n--vqvae_learning_rate=3e-5 \\\n--spatial_dimension=3 \\\n--image_roi=[160,160,128] \\\n--image_size=128\n```\nThe code is DistributedDataParallel (DDP) compatible. To train on e.g. 2 GPUs run with\n`torchrun --nproc_per_node=2 --nnodes=1 --node_rank=0 train_vqvae.py`\n### Train LDM\n```bash\npython train_ddpm.py \\\n  --output_dir=${output_root} \\\n  --model_name=ddpm_decathlon \\\n  --vqvae_checkpoint=${output_root}/vqvae_decathlon/checkpoint.pth \\\n  --training_ids=${data_root}/data_splits/Task01_BrainTumour_train.csv \\\n  --validation_ids=${data_root}/data_splits/Task01_BrainTumour_val.csv  \\\n  --is_grayscale=1 \\\n  --n_epochs=12000 \\\n  --batch_size=6 \\\n  --eval_freq=25 \\\n  --checkpoint_every=1000 \\\n  --cache_data=0  \\\n  --prediction_type=epsilon \\\n  --model_type=small \\\n  --beta_schedule=scaled_linear_beta \\\n  --beta_start=0.0015 \\\n  --beta_end=0.0195 \\\n  --b_scale=1.0 \\\n  --spatial_dimension=3 \\\n  --image_roi=[160,160,128] \\\n  --image_size=128\n```\n### Reconstruct data\n```bash\npython reconstruct.py \\\n  --output_dir=${output_root} \\\n  --model_name=ddpm_decathlon \\\n  --vqvae_checkpoint=${output_root}/decathlon-vqvae-4layer/checkpoint.pth \\\n  --validation_ids=${data_root}/data_splits/Task01_BrainTumour_val.csv  \\\n  --in_ids=${data_root}/data_splits/Task01_BrainTumour_test.csv \\\n  --out_ids=${data_root}/data_splits/Task02_Heart_test.csv,${data_root}/data_splits/Task03_Liver_test.csv,${data_root}/data_splits/Task04_Hippocampus_test.csv,${data_root}/data_splits/Task05_Prostate_test.csv,${data_root}/data_splits/Task06_Lung_test.csv,${data_root}/data_splits/Task07_Pancreas_test.csv,${data_root}/data_splits/Task08_HepaticVessel_test.csv,${data_root}/data_splits/Task09_Spleen_test.csv\\\n  --is_grayscale=1 \\\n  --batch_size=32 \\\n  --cache_data=0 \\\n  --prediction_type=epsilon \\\n  --beta_schedule=scaled_linear_beta \\\n  --beta_start=0.0015 \\\n  --beta_end=0.0195 \\\n  --b_scale=1.0 \\\n  --spatial_dimension=3 \\\n  --image_roi=[160,160,128] \\\n  --image_size=128 \\\n  --num_inference_steps=100 \\\n  --inference_skip_factor=2 \\\n  --run_val=1 \\\n  --run_in=1 \\\n  --run_out=1\n````\n### Classify samples as OOD\n```bash\npython ood_detection.py \\\n--output_dir=${output_root} \\\n--model_name=ddpm_decathlon\n```\n## Acknowledgements\nBuilt with [MONAI Generative](https://github.com/Project-MONAI/GenerativeModels) and [MONAI](https://github.com/Project-MONAI/MONAI).\n\n\n## Citations\nIf you use this codebase, please cite\n```bib\n@InProceedings{Graham_2023_CVPR,\n    author    = {Graham, Mark S. and Pinaya, Walter H.L. and Tudosiu, Petru-Daniel and Nachev, Parashkev and Ourselin, Sebastien and Cardoso, Jorge},\n    title     = {Denoising Diffusion Models for Out-of-Distribution Detection},\n    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR) Workshops},\n    month     = {June},\n    year      = {2023},\n    pages     = {2947-2956}\n}\n@inproceedings{graham2023unsupervised,\n  title={Unsupervised 3D out-of-distribution detection with latent diffusion models},\n  author={Graham, Mark S and Pinaya, Walter Hugo Lopez and Wright, Paul and Tudosiu, Petru-Daniel and Mah, Yee H and Teo, James T and J{\\\"a}ger, H Rolf and Werring, David and Nachev, Parashkev and Ourselin, Sebastien and others},\n  booktitle={International Conference on Medical Image Computing and Computer-Assisted Intervention},\n  pages={446--456},\n  year={2023},\n  organization={Springer}\n}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarksgraham%2Fddpm-ood","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarksgraham%2Fddpm-ood","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarksgraham%2Fddpm-ood/lists"}