{"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/counterfactual-contrastive-learning-robust","title":"Counterfactual contrastive learning: robust representations via causal image synthesis","arxiv_id":"2403.09605","date":"2024-03-14","proceeding":null,"authors":["Melanie Roschewitz","Fabio De Sousa Ribeiro","Tian Xia","Galvin Khara","Ben Glocker"],"abstract":"Contrastive pretraining is well-known to improve downstream task performance and model generalisation, especially in limited label settings. However, it is sensitive to the choice of augmentation pipeline. Positive pairs should preserve semantic information while destroying domain-specific information. Standard augmentation pipelines emulate domain-specific changes with pre-defined photometric transformations, but what if we could simulate realistic domain changes instead? In this work, we show how to utilise recent progress in counterfactual image generation to this effect. We propose CF-SimCLR, a counterfactual contrastive learning approach which leverages approximate counterfactual inference for positive pair creation. Comprehensive evaluation across five datasets, on chest radiography and mammography, demonstrates that CF-SimCLR substantially improves robustness to acquisition shift with higher downstream performance on both in- and out-of-distribution data, particularly for domains which are under-represented during training.","url_abs":"https://arxiv.org/abs/2403.09605v2","url_pdf":"https://arxiv.org/pdf/2403.09605v2.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":"counterfactual-contrastive-learning-robust","repo_url":"https://github.com/biomedia-mira/counterfactual-contrastive","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"contrastive-learning","task_name":"Contrastive Learning"},{"task_slug":"counterfactual-inference","task_name":"Counterfactual Inference"},{"task_slug":"image-generation","task_name":"Image Generation"},{"task_slug":null,"task_name":"counterfactual"}],"methods":[{"method_slug":"contrastive-learning","method_name":"Contrastive Learning"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2403.09605","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2403.09605"}},"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/biomedia-mira/counterfactual-contrastive","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran_violates":2,"ran":6,"ran_draft_wrong":1,"unverified":4},"by_repo_kind":{"official":{"samples":13,"ran":9,"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":0,"samples":[{"code_sha256_prefix":"aa5486a3650902d8","entry":"exists","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/utils.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/utils.py","link_basis":"harvester_set","language":"python","status":"ran_violates","verification_level":1,"contract_check":"VIOLATES","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"aa5486a3650902d8"}},{"code_sha256_prefix":"227af6c3aae15736","entry":"gaussian_kl","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/hvae.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/hvae.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"227af6c3aae15736"}},{"code_sha256_prefix":"1ed19b773b317db5","entry":"is_float_dtype","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/utils.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/utils.py","link_basis":"plan_row","language":"python","status":"ran_violates","verification_level":1,"contract_check":"VIOLATES","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"1ed19b773b317db5"}},{"code_sha256_prefix":"9893d00262df0805","entry":"linear_warmup","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/utils.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"9893d00262df0805"}},{"code_sha256_prefix":"81e6698064c711ac","entry":"plot_counterfactual_viz_mimic","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/plotting_utils.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/plotting_utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"81e6698064c711ac"}},{"code_sha256_prefix":"0e203533cf38cc3f","entry":"plot_cxr_grid","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/plotting_utils.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/plotting_utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"0e203533cf38cc3f"}},{"code_sha256_prefix":"ad5fda5f07b480d3","entry":"preprocess_batch","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/trainer.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/trainer.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"ad5fda5f07b480d3"}},{"code_sha256_prefix":"bda5911e32c3e618","entry":"undo_norm","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/plotting_utils.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/plotting_utils.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"bda5911e32c3e618"}},{"code_sha256_prefix":"4719c763c53be3fe","entry":"zero_module","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/hvae.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/hvae.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"4719c763c53be3fe"}},{"code_sha256_prefix":"ce5ff9ee4488ec4c","entry":"add_arguments","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/hps.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/hps.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"ce5ff9ee4488ec4c"}},{"code_sha256_prefix":"f71d1270abe6581b","entry":"sample_gaussian","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/hvae.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/hvae.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"f71d1270abe6581b"}},{"code_sha256_prefix":"a5c976876f53f736","entry":"scanner_counterfactuals","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/save_padchest_scanner_cf.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/save_padchest_scanner_cf.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"a5c976876f53f736"}},{"code_sha256_prefix":"772421466c02275b","entry":"setup_hparams","repo":"biomedia-mira/counterfactual-contrastive","repo_kind":"official","path":"causal_models/hps.py","file_url":"https://github.com/biomedia-mira/counterfactual-contrastive/blob/HEAD/causal_models/hps.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"772421466c02275b"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}