PatchCore anomaly detection

Unofficial implementation of PatchCore(new SOTA) anomaly detection model

Original Paper : Towards Total Recall in Industrial Anomaly Detection (Jun 2021)
Karsten Roth, Latha Pemula, Joaquin Zepeda, Bernhard Schölkopf, Thomas Brox, Peter Gehler

https://arxiv.org/abs/2106.08265
https://paperswithcode.com/sota/anomaly-detection-on-mvtec-ad

notice(21/06/18) :
This code is not yet verified. Any feedback is appreciated.
updates(21/06/21) :

  • I used sklearn's SparseRandomProjection(ep=0.9) for random projection. I'm not confident with this.
  • I think exact value of "b nearest patch-features" is not presented in the paper. I just set 9. (args.n_neighbors)
  • In terms of NN search, author used "faiss". but not implemented in this code yet.
  • sample embeddings/carpet/embedding.pickle => coreset_sampling_ratio=0.001

updates(21/06/26) :

  • A critical issue related to "locally aware patch" raised and fixed. Score table is updated.

Usage

# install python 3.6, torch==1.8.1, torchvision==0.9.1
pip install -r requirements.txt

python train.py --phase train or test --dataset_path .../mvtec_anomaly_detection --category carpet --project_root_path path/to/save/results --coreset_sampling_ratio 0.01 --n_neighbors 9'

# for fast try just specify your dataset_path and run
python train.py --phase test --dataset_path .../mvtec_anomaly_detection --project_root_path ./

MVTecAD AUROC score (PatchCore-1%, mean of n trials)

Category Paper(image-level) This code(image-level) Paper(pixel-level) This code(pixel-level)
carpet 0.980 0.991(1) 0.989 0.989(1)
grid 0.986 0.975(1) 0.986 0.975(1)
leather 1.000 1.000(1) 0.993 0.991(1)
tile 0.994 0.994(1) 0.961 0.949(1)
wood 0.992 0.989(1) 0.951 0.936(1)
bottle 1.000 1.000(1) 0.985 0.981(1)
cable 0.993 0.995(1) 0.982 0.983(1)
capsule 0.980 0.976(1) 0.988 0.989(1)
hazelnut 1.000 1.000(1) 0.986 0.985(1)
metal nut 0.997 0.999(1) 0.984 0.984(1)
pill 0.970 0.959(1) 0.971 0.977(1)
screw 0.964 0.949(1) 0.992 0.977(1)
toothbrush 1.000 1.000(1) 0.985 0.986(1)
transistor 0.999 1.000(1) 0.949 0.972(1)
zipper 0.992 0.995(1) 0.988 0.984(1)
mean 0.990 0.988 0.980 0.977

GitHub

https://github.com/hcw-00/PatchCore_anomaly_detection