{"id":19880089,"url":"https://github.com/tiskw/patchcore-ad","last_synced_at":"2025-05-02T13:31:57.965Z","repository":{"id":43450938,"uuid":"456090372","full_name":"tiskw/patchcore-ad","owner":"tiskw","description":"Unofficial implementation of PatchCore and several additional experiments.","archived":false,"fork":false,"pushed_at":"2024-03-03T00:29:49.000Z","size":794,"stargazers_count":20,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T02:21:35.099Z","etag":null,"topics":["anomaly-detection","deep-learning","image-processing","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tiskw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-02-06T08:01:23.000Z","updated_at":"2025-04-02T06:40:32.000Z","dependencies_parsed_at":"2024-11-12T17:10:16.452Z","dependency_job_id":"6e6e4027-0dac-4a0a-85cf-74b036de79f5","html_url":"https://github.com/tiskw/patchcore-ad","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiskw%2Fpatchcore-ad","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiskw%2Fpatchcore-ad/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiskw%2Fpatchcore-ad/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiskw%2Fpatchcore-ad/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiskw","download_url":"https://codeload.github.com/tiskw/patchcore-ad/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252046227,"owners_count":21685975,"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":["anomaly-detection","deep-learning","image-processing","pytorch"],"created_at":"2024-11-12T17:10:13.305Z","updated_at":"2025-05-02T13:31:57.364Z","avatar_url":"https://github.com/tiskw.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"PatchCore Anomaly Detection\n================================================================================\n\nThis repository provides an unofficial PyTorch implementation\nof the PatchCore anomaly detection model [1] and several additional experiments.\n\nPatchCore is an anomaly detection algorithm that has the following features:\n\n* uses a memory-bank of nominal features extracted from a pre-trained\n  backbone network (like SPADE and PaDiM), where the memory back is\n  coreset-subsampled to ensure low inference cost at higher performance,\n* uses an approximated nearest neighbor search for evaluating pixel-wise\n  anomaly score on inference,\n* shows state-of-the-art performance on the MVTec AD dataset (as of Jun 2021).\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg width=\"750px\" src=\"experiments/figures/patchcore_sketch.jpg\" /\u003e\n    \u003cimg width=\"640px\" src=\"experiments/figures/samples_mvtec_ad.jpg\" /\u003e\n\u003c/div\u003e\n\n\nUsage\n--------------------------------------------------------------------------------\n\n### Installation\n\nThe author recommends using Docker for keeping your environment clean.\nFor example, you can create a new Docker container and enter into it\nby the following command in the root directory of this repository:\n\n```console\ndocker run --rm -it -v `pwd`:/workspace -w /workspace --name patchcore tiskw/patchcore:cpu-2022-03-01\n```\n\nIf you need GPU support, please use the Docker image with CUDA libraries:\n\n```console\ndocker run --rm -it -v `pwd`:/workspace -w /workspace --name patchcore tiskw/patchcore:gpu-2022-03-01\n```\n\nSee [this document](SETUP.md) for more details.\n\n### Dataset\n\nYou need to get the MVTec AD dataset [2] if you will reproduce [our experiments](experiments/README.md).\nIf you don't have a plan to use the dataset, you can skip this subsection.\nYou can download the MVTec AD dataset from\n[the official website](https://www.mvtec.com/company/research/datasets/mvtec-ad)\n(or [direct link to the data file](https://www.mydrive.ch/shares/38536/3830184030e49fe74747669442f0f282/download/420938113-1629952094/mvtec_anomaly_detection.tar.xz))\nand put it under `data/mvtec_ad/` directory.\n\nAt first, please move to the `data/mvtec_ad/` directory.\n\n```console\ncd data/mvtec_ad/\n```\n\nThen, run the following command to download the MVTec AD dataset:\n\n```console\nwget \"https://www.mydrive.ch/shares/38536/3830184030e49fe74747669442f0f282/download/420938113-1629952094/mvtec_anomaly_detection.tar.xz\"\n```\n\nFinally, extract the downloaded data:\n\n```console\ntar fJx mvtec_anomaly_detection.tar.xz\n```\n\nSee [this document](SETUP.md) for more details.\n\n\n### Train and predict on your dataset\n\nYou can train and run predictions on your dataset using `main.py`.\nIn the following, we assume:\n\n- your training images (good images) are stored under `data_train/` directory,\n- your test images (good or failure images) are stored under `data_test/` directory,\n- the training result will be stored at `./index.faiss`,\n- the test results will be dumped under `output_test/` directory.\n\nYou can train your model by the following command:\n\n```console\npython3 main.py train -i data_train -o ./index.faiss\n```\n\nThen, you can predict anomaly score for test images by the following command:\n\n```console\npython3 main.py predict -i data_test -o output_test\n```\n\nOn the output directory `output_test/`, two types of files will be dumped:\n- `.jpg`: anomaly heatmap overlayed on the input image,\n- `.npy`: matrix of anomaly heatmap with shape `(height, width)`.\n\n### Replicate the experiments\n\nIf you want to replicate [the experiments](experiments/README.md),\nrun the following command at the root directory of this repository:\n\n```console\npython3 main_mvtecad.py runall\npython3 main_mvtecad.py summarize\n```\n\nThe `python3 main_mvtecad.py runall` command will take a quite long time,\ntherefore it is a good idea to wrap the above command by `nohup`.\n\n### Anomalous area visualization\n\nIf you want the visualizatino of the anomalous area for each sample like\nthe following figure, you can try `--contour` option in the prediction\nstep. The following is the details of steps to generate the anomalous\narea visualization.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg width=\"672px\" src=\"experiments/figures/anomalous_area_visualization.jpg\" /\u003e\n\u003c/div\u003e\n\nAt first, the training data is located under `data_train/` directory,\nand we assume that you completed the training of the model:\n\n```console\n# Train the PatchCore model.\npython3 main.py train -i data_train -o ./index.faiss\n```\n\nNext, we need to compute the threshold for determining the anomalous area\nfor each samples. You can compute the threshold by the following command:\n\n```console\n# Compute threshold.\npython3 main.py thresh -i data_train\n```\n\nFinally, you can get the anomalouse area visualization by the following command\nwhere we assume that the test data is located under `data_test/` directory\nand `THRESH` is the threshold value computed in the previous step:\n\n```console\n# Visualize contour map using the threshold value obtained by the above.\npython3 main.py predict --contour THRESH -i data_test -o output_test\n```\n\n\nExperiments\n--------------------------------------------------------------------------------\n\n### Experiment 1: Comparison with the original paper on MVTec AD dataset\n\nThe following figures are summaries of the comparison of the anomaly detection\nscores on MVTec AD dataset [2] with the original paper of the PatchCore [1].\nThe performance of our implementation is quite close to the paper's score,\ntherefore our implementation may have no serious issue.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg width=\"480px\" src=\"experiments/figures/MVTecAD_averaged_image-level_roc_auc_score.svg\" /\u003e\n    \u003cimg width=\"480px\" src=\"experiments/figures/MVTecAD_averaged_pixel-level_roc_auc_score.svg\" /\u003e\n\u003c/div\u003e\n\nSee [this document](experiments/summary_comparison_with_the_paper.md)\nfor more details.\n\n### Experiment 2: Comparison of backbone networks\n\nWe compared the image/pixel-level scores on the MVTec AD dataset with\ndifferent backbone networks. Some networks show a better speed/performance\ntradeoff than Wide ResNet50 x2 which is used as a default backbone network\nin the original paper.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg width=\"480px\" src=\"experiments/figures/MVTecAD_image-level_roc_auc_score_backbones.svg\" /\u003e\n    \u003cimg width=\"480px\" src=\"experiments/figures/MVTecAD_pixel-level_roc_auc_score_backbones.svg\" /\u003e\n\u003c/div\u003e\n\nSee [this document](experiments/summary_comparison_with_backbones.md)\nfor more details.\n\n### Experiment 3: Comparison of pre-trainings\n\nWe compared several different pre-trained ResNet50 as a backbone of PatchCore.\nWe hypothesize that a well-trained neural network achieves higher performance.\nWe tried the normal ImageNet pre-training ResNet50, DeepLabV3 resNet50 pre-trained\nwith COCO, and ResNet50-SSL/SWSL model that are pre-trained on ImageNet [3]\nwith semi-supervise or un-supervised manner. The result is quite interesting,\nhowever, basically, we can say that the normal ImageNet pre-trained model is enough\ngood for PatchCore purposes.\n\n\u003cdiv align=\"center\"\u003e\n    \u003cimg width=\"480px\" src=\"experiments/figures/MVTecAD_ResNet50_with_different_pretrainings.svg\" /\u003e\n\u003c/div\u003e\n\nSee [this document](experiments/summary_comparison_pretraining.md)\nfor more details.\n\n\u003c!--\n### Experiment 4: Approcimated k-NN vs One-class SVM\n\nPatchCore uses k-nearest neighbor algorithm [4] in the anomaly score\ncomputation part. However, it is commonly know that k-nearest neighbor\nalgotithm is sensitive for outlier data points. What happen if we use\nthe other types of anomaly detection algorithm as a backend of anomaly\nscore computation part, like one-class SVM [5]?\nDoes it contribute to the scores?\n--\u003e\n\nNotes\n--------------------------------------------------------------------------------\n \n* This implementation refers to another PatchCore implementation [6] which\n  is released under Apache 2.0 license. The author has learned a lot\n  from the implementation.\n\n\nReferences\n--------------------------------------------------------------------------------\n\n[1] K. Roth, L. Pemula, J. Zepeda, B. Scholkopf, T. Brox, and P. Gehler,\n    \"Towards Total Recall in Industrial Anomaly Detection\",\n    arXiv, 2021.\n    [PDF](https://arxiv.org/pdf/2106.08265.pdf)\n\n[2] P. Bergmann M. Fauser D. Sattlegger, and C. Steger,\n    \"MVTec AD - A Comprehensive Real-World Dataset for Unsupervised Anomaly Detection\",\n    CVPR, 2019.\n    [PDF](https://openaccess.thecvf.com/content_CVPR_2019/papers/Bergmann_MVTec_AD_--_A_Comprehensive_Real-World_Dataset_for_Unsupervised_Anomaly_CVPR_2019_paper.pdf)\n\n[3] I. Yalniz, H. Jegou, K. Chen, M. Paluri and D. Mahajan,\n    \"Billion-scale semi-supervised learning for image classification\",\n    arXiv, 2019.\n    [PDF](https://arxiv.org/pdf/1905.00546.pdf)\n\n[4] E. Fix and J. Hodges,\n    ”Discriminatory Analysis. Nonparametric Discrimination: Consistency Properties”,\n    USAF School of Aviation Medicine, Randolph Field, Texas, 1951.\n\n[5] B. Scholkopf, R. Williamson, A. Smola, J. Shawe-Taylor and J. Platt,\n    \"Support Vector Method for Novelty Detection\",\n    NIPS, 1999.\n    [PDF](https://proceedings.neurips.cc/paper/1999/file/8725fb777f25776ffa9076e44fcfd776-Paper.pdf)\n\n[6] [hcw-00/PatchCore_anomaly_detection](https://github.com/hcw-00/PatchCore_anomaly_detection), GitHub.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiskw%2Fpatchcore-ad","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiskw%2Fpatchcore-ad","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiskw%2Fpatchcore-ad/lists"}