{"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/promptad-learning-prompts-with-only-normal","title":"PromptAD: Learning Prompts with only Normal Samples for Few-Shot Anomaly Detection","arxiv_id":"2404.05231","date":"2024-04-08","proceeding":"CVPR 2024 1","authors":["Xiaofan Li","Zhizhong Zhang","Xin Tan","Chengwei Chen","Yanyun Qu","Yuan Xie","Lizhuang Ma"],"abstract":"The vision-language model has brought great improvement to few-shot industrial anomaly detection, which usually needs to design of hundreds of prompts through prompt engineering. For automated scenarios, we first use conventional prompt learning with many-class paradigm as the baseline to automatically learn prompts but found that it can not work well in one-class anomaly detection. To address the above problem, this paper proposes a one-class prompt learning method for few-shot anomaly detection, termed PromptAD. First, we propose semantic concatenation which can transpose normal prompts into anomaly prompts by concatenating normal prompts with anomaly suffixes, thus constructing a large number of negative samples used to guide prompt learning in one-class setting. Furthermore, to mitigate the training challenge caused by the absence of anomaly images, we introduce the concept of explicit anomaly margin, which is used to explicitly control the margin between normal prompt features and anomaly prompt features through a hyper-parameter. For image-level/pixel-level anomaly detection, PromptAD achieves first place in 11/12 few-shot settings on MVTec and VisA.","url_abs":"https://arxiv.org/abs/2404.05231v2","url_pdf":"https://arxiv.org/pdf/2404.05231v2.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":"promptad-learning-prompts-with-only-normal","repo_url":"https://github.com/funz-0/promptad","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok","spdx":"Unlicense"}}],"tasks":[{"task_slug":"anomaly-detection","task_name":"Anomaly Detection"},{"task_slug":"language-modeling","task_name":"Language Modeling"},{"task_slug":"language-modelling","task_name":"Language Modelling"},{"task_slug":"prompt-engineering","task_name":"Prompt Engineering"},{"task_slug":"prompt-learning","task_name":"Prompt Learning"}],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2404.05231","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2404.05231"}},"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/funz-0/promptad","reach":{"status":"ok","spdx":"Unlicense"}},{"provenance":"deterministic:regex_extraction","url":"https://github.com/FuNz-0/PromptAD","reach":{"status":"ok","spdx":"Unlicense"}}],"summary":{"ran_draft_wrong":2,"ran_violates":1,"ran":1,"unverified":12},"by_repo_kind":{"official":{"samples":16,"ran":4,"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":"4d42af1bb1e8a623","entry":"_convert_to_rgb","repo":"FuNz-0/PromptAD","repo_kind":"official","path":"PromptAD/model.py","file_url":"https://github.com/FuNz-0/PromptAD/blob/HEAD/PromptAD/model.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"MISDECLARED","metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"4d42af1bb1e8a623"}},{"code_sha256_prefix":"f19962ebb134b3d7","entry":"convert_to_custom_text_state_dict","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/model.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/model.py","link_basis":"harvester_set","language":"python","status":"ran_violates","verification_level":1,"contract_check":"VIOLATES","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"f19962ebb134b3d7"}},{"code_sha256_prefix":"dcd422d66b0581d8","entry":"get_cast_dtype","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/model.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/model.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":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"dcd422d66b0581d8"}},{"code_sha256_prefix":"4b3a4d17e1df3140","entry":"load_state_dict","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/factory.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/factory.py","link_basis":"harvester_set","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"4b3a4d17e1df3140"}},{"code_sha256_prefix":"1f60e2ee2a81490c","entry":"PromptAD","repo":"FuNz-0/PromptAD","repo_kind":"official","path":"PromptAD/model.py","file_url":"https://github.com/FuNz-0/PromptAD/blob/HEAD/PromptAD/model.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"1f60e2ee2a81490c"}},{"code_sha256_prefix":"a04cfa7dc81328f3","entry":"PromptLearner","repo":"FuNz-0/PromptAD","repo_kind":"official","path":"PromptAD/model.py","file_url":"https://github.com/FuNz-0/PromptAD/blob/HEAD/PromptAD/model.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"a04cfa7dc81328f3"}},{"code_sha256_prefix":"c92f832f94b5a1c1","entry":"build_model_from_openai_state_dict","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/model.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/model.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"c92f832f94b5a1c1"}},{"code_sha256_prefix":"ddcbd45e940484ee","entry":"gather_features","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/loss.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/loss.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"ddcbd45e940484ee"}},{"code_sha256_prefix":"5c7cf820e99443ff","entry":"get_model_config","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/factory.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/factory.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"5c7cf820e99443ff"}},{"code_sha256_prefix":"f34d8f34635af394","entry":"get_tokenizer","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/factory.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/factory.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"f34d8f34635af394"}},{"code_sha256_prefix":"7dbf4e7ea5607d08","entry":"list_pretrained","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/pretrained.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/pretrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"7dbf4e7ea5607d08"}},{"code_sha256_prefix":"68fec9113b65fcf3","entry":"list_pretrained_models_by_tag","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/pretrained.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/pretrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"68fec9113b65fcf3"}},{"code_sha256_prefix":"7f5bb5f5599e5ad5","entry":"list_pretrained_tags_by_model","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/pretrained.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/pretrained.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"7f5bb5f5599e5ad5"}},{"code_sha256_prefix":"a1693b09a14a516f","entry":"load_openai_model","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/openai.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/openai.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"a1693b09a14a516f"}},{"code_sha256_prefix":"fb651d0a97fd4d3f","entry":"prepare_inputs_for_generation","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/coca_model.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/coca_model.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"fb651d0a97fd4d3f"}},{"code_sha256_prefix":"2a377da4a76a2d44","entry":"register_pooler","repo":"funz-0/promptad","repo_kind":"official","path":"PromptAD/CLIPAD/hf_model.py","file_url":"https://github.com/funz-0/promptad/blob/HEAD/PromptAD/CLIPAD/hf_model.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Unlicense","inline_ok":true,"mcp_get_code":{"code_sha256":"2a377da4a76a2d44"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}