{"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/anomalydiffusion-few-shot-anomaly-image","title":"AnomalyDiffusion: Few-Shot Anomaly Image Generation with Diffusion Model","arxiv_id":"2312.05767","date":"2023-12-10","proceeding":null,"authors":["Teng Hu","Jiangning Zhang","Ran Yi","Yuzhen Du","Xu Chen","Liang Liu","Yabiao Wang","Chengjie Wang"],"abstract":"Anomaly inspection plays an important role in industrial manufacture. Existing anomaly inspection methods are limited in their performance due to insufficient anomaly data. Although anomaly generation methods have been proposed to augment the anomaly data, they either suffer from poor generation authenticity or inaccurate alignment between the generated anomalies and masks. To address the above problems, we propose AnomalyDiffusion, a novel diffusion-based few-shot anomaly generation model, which utilizes the strong prior information of latent diffusion model learned from large-scale dataset to enhance the generation authenticity under few-shot training data. Firstly, we propose Spatial Anomaly Embedding, which consists of a learnable anomaly embedding and a spatial embedding encoded from an anomaly mask, disentangling the anomaly information into anomaly appearance and location information. Moreover, to improve the alignment between the generated anomalies and the anomaly masks, we introduce a novel Adaptive Attention Re-weighting Mechanism. Based on the disparities between the generated anomaly image and normal sample, it dynamically guides the model to focus more on the areas with less noticeable generated anomalies, enabling generation of accurately-matched anomalous image-mask pairs. Extensive experiments demonstrate that our model significantly outperforms the state-of-the-art methods in generation authenticity and diversity, and effectively improves the performance of downstream anomaly inspection tasks. The code and data are available in https://github.com/sjtuplayer/anomalydiffusion.","url_abs":"https://arxiv.org/abs/2312.05767v2","url_pdf":"https://arxiv.org/pdf/2312.05767v2.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":"anomalydiffusion-few-shot-anomaly-image","repo_url":"https://github.com/sjtuplayer/anomalydiffusion","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}}],"tasks":[{"task_slug":"image-generation","task_name":"Image Generation"}],"methods":[{"method_slug":"diffusion","method_name":"Diffusion"},{"method_slug":"focus","method_name":"Focus"},{"method_slug":"latent-diffusion-model","method_name":"Latent Diffusion Model"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2312.05767","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2312.05767"}},"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/sjtuplayer/anomalydiffusion","reach":{"status":"ok","spdx":"MIT"}}],"summary":{"ran":1,"ran_honours":2,"unverified":7},"by_repo_kind":{"official":{"samples":10,"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":0,"samples":[{"code_sha256_prefix":"1398a2cb64d8b5aa","entry":"check_mask","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"generate_mask.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/generate_mask.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":"1398a2cb64d8b5aa"}},{"code_sha256_prefix":"c56b7ef16f309a45","entry":"gaussian","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"unet_utils/loss.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/unet_utils/loss.py","link_basis":"harvester_set","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"well_formed","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c56b7ef16f309a45"}},{"code_sha256_prefix":"7337f1f5ff01dcd0","entry":"get_lr","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"test-classification.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/test-classification.py","link_basis":"harvester_set","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"well_formed","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"7337f1f5ff01dcd0"}},{"code_sha256_prefix":"18c9bfa7e94951e9","entry":"collect_anomaly_scores","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"unet_utils/au_pro_util.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/unet_utils/au_pro_util.py","link_basis":"harvester_set","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":"18c9bfa7e94951e9"}},{"code_sha256_prefix":"49b5692178958afe","entry":"compute_pro","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"unet_utils/au_pro_util.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/unet_utils/au_pro_util.py","link_basis":"harvester_set","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":"49b5692178958afe"}},{"code_sha256_prefix":"443341084ce76798","entry":"create_window","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"unet_utils/loss.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/unet_utils/loss.py","link_basis":"harvester_set","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":"443341084ce76798"}},{"code_sha256_prefix":"c23b317a405ae12c","entry":"make_batch","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"generate_mask.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/generate_mask.py","link_basis":"harvester_set","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":"c23b317a405ae12c"}},{"code_sha256_prefix":"87fbedd0c27cf378","entry":"nondefault_trainer_args","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"train_mask.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/train_mask.py","link_basis":"harvester_set","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":"87fbedd0c27cf378"}},{"code_sha256_prefix":"bff794bd9bd73f94","entry":"ssim","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"unet_utils/loss.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/unet_utils/loss.py","link_basis":"harvester_set","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":"bff794bd9bd73f94"}},{"code_sha256_prefix":"9d72c1f1f0d6dea4","entry":"trapezoid","repo":"sjtuplayer/anomalydiffusion","repo_kind":"official","path":"unet_utils/au_pro_util.py","file_url":"https://github.com/sjtuplayer/anomalydiffusion/blob/HEAD/unet_utils/au_pro_util.py","link_basis":"harvester_set","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":"9d72c1f1f0d6dea4"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}