{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/diffusion-models-for-counterfactual-1","title":"Diffusion Models for Counterfactual Generation and Anomaly Detection in Brain Images","arxiv_id":"2308.02062","date":"2023-08-03","proceeding":null,"authors":["Alessandro Fontanella","Grant Mair","Joanna Wardlaw","Emanuele Trucco","Amos Storkey"],"abstract":"Segmentation masks of pathological areas are useful in many medical applications, such as brain tumour and stroke management. Moreover, healthy counterfactuals of diseased images can be used to enhance radiologists' training files and to improve the interpretability of segmentation models. In this work, we present a weakly supervised method to generate a healthy version of a diseased image and then use it to obtain a pixel-wise anomaly map. To do so, we start by considering a saliency map that approximately covers the pathological areas, obtained with ACAT. Then, we propose a technique that allows to perform targeted modifications to these regions, while preserving the rest of the image. In particular, we employ a diffusion model trained on healthy samples and combine Denoising Diffusion Probabilistic Model (DDPM) and Denoising Diffusion Implicit Model (DDIM) at each step of the sampling process. DDPM is used to modify the areas affected by a lesion within the saliency map, while DDIM guarantees reconstruction of the normal anatomy outside of it. The two parts are also fused at each timestep, to guarantee the generation of a sample with a coherent appearance and a seamless transition between edited and unedited parts. We verify that when our method is applied to healthy samples, the input images are reconstructed without significant modifications. We compare our approach with alternative weakly supervised methods on the task of brain lesion segmentation, achieving the highest mean Dice and IoU scores among the models considered.","url_abs":"https://arxiv.org/abs/2308.02062v2","url_pdf":"https://arxiv.org/pdf/2308.02062v2.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"diffusion-models-for-counterfactual-1","repo_url":"https://github.com/alessandro-f/dif-fuse","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":null}],"tasks":[{"task_slug":"anatomy","task_name":"Anatomy"},{"task_slug":"anomaly-detection","task_name":"Anomaly Detection"},{"task_slug":"denoising","task_name":"Denoising"},{"task_slug":"lesion-segmentation","task_name":"Lesion Segmentation"},{"task_slug":"segmentation","task_name":"Segmentation"},{"task_slug":null,"task_name":"counterfactual"}],"methods":[{"method_slug":"counterfactuals","method_name":"Counterfactuals"},{"method_slug":"diffusion","method_name":"Diffusion"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2308.02062","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2308.02062"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/alessandro-f/dif-fuse","reach":null}],"summary":{"ran_fixture":2,"ran_honours":1},"by_repo_kind":{"official":{"samples":3,"ran":3,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":3,"samples":[{"code_sha256_prefix":"5a4f7ef54d3e6381","entry":"clean","repo":"alessandro-f/dif-fuse","repo_kind":"official","path":"scripts/image_sample_dif-fuse.py","file_url":"https://github.com/alessandro-f/dif-fuse/blob/HEAD/scripts/image_sample_dif-fuse.py","link_basis":"first_harvest_node","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"5a4f7ef54d3e6381"}},{"code_sha256_prefix":"b8390778485e57ab","entry":"thresholdf","repo":"alessandro-f/dif-fuse","repo_kind":"official","path":"scripts/image_sample_dif-fuse.py","file_url":"https://github.com/alessandro-f/dif-fuse/blob/HEAD/scripts/image_sample_dif-fuse.py","link_basis":"first_harvest_node","language":"python","status":"ran_fixture","verification_level":1,"contract_check":"RAISES","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"b8390778485e57ab"}},{"code_sha256_prefix":"71438f30b0e17975","entry":"to_numpy","repo":"alessandro-f/dif-fuse","repo_kind":"official","path":"scripts/saliency_maps.py","file_url":"https://github.com/alessandro-f/dif-fuse/blob/HEAD/scripts/saliency_maps.py","link_basis":"first_harvest_node","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"invariant","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"71438f30b0e17975"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}