Industrial knn-based anomaly detection for images. Visit streamlit link to check out the demo.

Overview

Industrial KNN-based Anomaly Detection

⭐ Now has streamlit support! ⭐ Run $ streamlit run streamlit_app.py

This repo aims to reproduce the results of the following KNN-based anomaly detection methods:

  1. SPADE (Cohen et al. 2021) - knn in z-space and distance to feature maps spade schematic
  2. PaDiM* (Defard et al. 2020) - distance to multivariate Gaussian of feature maps padim schematic
  3. PatchCore (Roth et al. 2021) - knn distance to avgpooled feature maps patchcore schematic

* actually does not have any knn mechanism, but shares many things implementation-wise.


Install

$ pipenv install -r requirements.txt

Note: I used torch cu11 wheels.

Usage

CLI:

$ python indad/run.py METHOD [--dataset DATASET]

Results can be found under ./results/.

Code example:

from indad.model import SPADE

model = SPADE(k=5, backbone_name="resnet18")

# feed healthy dataset
model.fit(...)

# get predictions
img_lvl_anom_score, pxl_lvl_anom_score = model.predict(...)

Custom datasets

πŸ‘οΈ

Check out one of the downloaded MVTec datasets. Naming of images should correspond among folders. Right now there is no support for no ground truth pixel masks.

πŸ“‚datasets
 β”— πŸ“‚your_custom_dataset
  ┣ πŸ“‚ ground_truth/defective
  ┃ ┣ πŸ“‚ defect_type_1
  ┃ β”— πŸ“‚ defect_type_2
  ┣ πŸ“‚ test
  ┃ ┣ πŸ“‚ defect_type_1
  ┃ ┣ πŸ“‚ defect_type_2
  ┃ β”— πŸ“‚ good
  β”— πŸ“‚ train/good
$ python indad/run.py METHOD --dataset your_custom_dataset

Results

πŸ“ = paper, πŸ‘‡ = this repo

Image-level

class SPADE πŸ“ SPADE πŸ‘‡ PaDiM πŸ“ PaDiM πŸ‘‡ PatchCore πŸ“ PatchCore πŸ‘‡
bottle - 98.3 98.3 99.9 100.0 100.0
cable - 88.1 96.7 87.8 99.5 96.2
capsule - 80.4 98.5 87.6 98.1 95.3
carpet - 62.5 99.1 99.5 98.7 98.7
grid - 25.6 97.3 95.5 98.2 93.0
hazelnut - 92.8 98.2 86.1 100.0 100.0
leather - 85.6 99.2 100.0 100.0 100.0
metal_nut - 78.6 97.2 97.6 100.0 98.3
pill - 78.8 95.7 92.7 96.6 92.8
screw - 66.1 98.5 79.6 98.1 96.7
tile - 96.4 94.1 99.5 98.7 99.0
toothbrush - 83.9 98.8 94.7 100.0 98.1
transistor - 89.4 97.5 95.0 100.0 99.7
wood - 85.3 94.7 99.4 99.2 98.8
zipper - 97.1 98.5 93.8 99.4 98.4
averages 85.5 80.6 97.5 93.9 99.1 97.7

Pixel-level

class SPADE πŸ“ SPADE πŸ‘‡ PaDiM πŸ“ PaDiM πŸ‘‡ PatchCore πŸ“ PatchCore πŸ‘‡
bottle 97.5 97.7 94.8 97.6 98.6 97.8
cable 93.7 94.4 88.8 95.5 98.5 97.4
capsule 97.6 98.7 93.5 98.1 98.9 98.3
carpet 87.4 99.0 96.2 98.7 99.1 98.3
grid 88.5 96.4 94.6 96.4 98.7 96.7
hazelnut 98.4 98.4 92.6 97.3 98.7 98.1
leather 97.2 99.1 97.8 98.6 99.3 98.4
metal_nut 99.0 96.1 85.6 95.8 98.4 96.2
pill 99.1 93.5 92.7 94.4 97.6 98.7
screw 98.1 98.9 94.4 97.5 99.4 98.4
tile 96.5 93.1 86.0 92.6 95.9 94.0
toothbrush 98.9 98.9 93.1 98.5 98.7 98.1
transistor 97.9 95.8 84.5 96.9 96.4 97.5
wood 94.1 94.5 91.1 92.9 95.1 91.9
zipper 96.5 98.3 95.9 97.0 98.9 97.6
averages 96.9 96.6 92.1 96.5 98.1 97.2

PatchCore-10 was used.

Hyperparams

The following parameters were used to calculate the results. They more or less correspond to the parameters used in the papers.

spade:
  backbone: wide_resnet50_2
  k: 50
padim:
  backbone: wide_resnet50_2
  d_reduced: 250
  epsilon: 0.04
patchcore:
  backbone: wide_resnet50_2
  f_coreset: 0.1
  n_reweight: 3

Progress

  • Datasets
  • Code skeleton
  • Config files
  • CLI
  • Logging
  • SPADE
  • PADIM
  • PatchCore
  • Add custom dataset option
  • Add dataset progress bar
  • Add schematics
  • Unit tests

Design considerations

  • Data is processed in single images to avoid batch statistics interference.
  • I decided to implement greedy kcenter from scratch and there is room for improvement.
  • torch.nn.AdaptiveAvgPool2d for feature map resizing, torch.nn.functional.interpolate for score map resizing.
  • GPU is used for backbones and coreset selection. GPU coreset selection currently runs at:
    • 400-500 it/s @ float32 (RTX3080)
    • 1000+ it/s @ float16 (RTX3080)

Acknowledgements

  • hcw-00 for tipping sklearn.random_projection.SparseRandomProjection

References

SPADE:

@misc{cohen2021subimage,
      title={Sub-Image Anomaly Detection with Deep Pyramid Correspondences}, 
      author={Niv Cohen and Yedid Hoshen},
      year={2021},
      eprint={2005.02357},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

PaDiM:

@misc{defard2020padim,
      title={PaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization}, 
      author={Thomas Defard and Aleksandr Setkov and Angelique Loesch and Romaric Audigier},
      year={2020},
      eprint={2011.08785},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

PatchCore:

@misc{roth2021total,
      title={Towards Total Recall in Industrial Anomaly Detection}, 
      author={Karsten Roth and Latha Pemula and Joaquin Zepeda and Bernhard SchΓΆlkopf and Thomas Brox and Peter Gehler},
      year={2021},
      eprint={2106.08265},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}
Owner
aventau
Into graphics and modelling. Computer Vision / Machine Learning Engineer.
aventau
Implementation for the EMNLP 2021 paper "Interactive Machine Comprehension with Dynamic Knowledge Graphs".

Interactive Machine Comprehension with Dynamic Knowledge Graphs Implementation for the EMNLP 2021 paper. Dependencies apt-get -y update apt-get instal

Xingdi (Eric) Yuan 19 Aug 23, 2022
Variational Attention: Propagating Domain-Specific Knowledge for Multi-Domain Learning in Crowd Counting (ICCV, 2021)

DKPNet ICCV 2021 Variational Attention: Propagating Domain-Specific Knowledge for Multi-Domain Learning in Crowd Counting Baseline of DKPNet is availa

19 Oct 14, 2022
Official implementation of "One-Shot Voice Conversion with Weight Adaptive Instance Normalization".

One-Shot Voice Conversion with Weight Adaptive Instance Normalization By Shengjie Huang, Yanyan Xu*, Dengfeng Ke*, Mingjie Chen, Thomas Hain. This rep

31 Dec 07, 2022
Implementation of Kalman Filter in Python

Kalman Filter in Python This is a basic example of how Kalman filter works in Python. I do plan on refactoring and expanding this repo in the future.

Enoch Kan 35 Sep 11, 2022
PyTorch wrapper for Taichi data-oriented class

Stannum PyTorch wrapper for Taichi data-oriented class PRs are welcomed, please see TODOs. Usage from stannum import Tin import torch data_oriented =

86 Dec 23, 2022
This repository accompanies the ACM TOIS paper "What can I cook with these ingredients?" - Understanding cooking-related information needs in conversational search

In this repository you find data that has been gathered when conducting in-situ experiments in a conversational cooking setting. These data include tr

6 Sep 22, 2022
naked is a Python tool which allows you to strip a model and only keep what matters for making predictions.

naked is a Python tool which allows you to strip a model and only keep what matters for making predictions. The result is a pure Python function with no third-party dependencies that you can simply c

Max Halford 24 Dec 20, 2022
A Python Reconnection Tool for alt:V

altv-reconnect What? It invokes a reconnect in the altV Client Dev Console. You get to determine when your local client should reconnect when developi

8 Jun 30, 2022
Datasets, tools, and benchmarks for representation learning of code.

The CodeSearchNet challenge has been concluded We would like to thank all participants for their submissions and we hope that this challenge provided

GitHub 1.8k Dec 25, 2022
Orbivator AI - To Determine which features of data (measurements) are most important for diagnosing breast cancer and find out if breast cancer occurs or not.

Orbivator_AI Breast Cancer Wisconsin (Diagnostic) GOAL To Determine which features of data (measurements) are most important for diagnosing breast can

anurag kumar singh 1 Jan 02, 2022
Code for Towards Streaming Perception (ECCV 2020) :car:

sAP β€” Code for Towards Streaming Perception ECCV Best Paper Honorable Mention Award Feb 2021: Announcing the Streaming Perception Challenge (CVPR 2021

Martin Li 85 Dec 22, 2022
The Official PyTorch Implementation of "LSGM: Score-based Generative Modeling in Latent Space" (NeurIPS 2021)

The Official PyTorch Implementation of "LSGM: Score-based Generative Modeling in Latent Space" (NeurIPS 2021) Arash Vahdat*   Β·   Karsten Kreis*   Β·  

NVIDIA Research Projects 238 Jan 02, 2023
[CVPR 2021] Involution: Inverting the Inherence of Convolution for Visual Recognition, a brand new neural operator

involution Official implementation of a neural operator as described in Involution: Inverting the Inherence of Convolution for Visual Recognition (CVP

Duo Li 1.3k Dec 28, 2022
Personalized Transfer of User Preferences for Cross-domain Recommendation (PTUPCDR)

Personalized Transfer of User Preferences for Cross-domain Recommendation (PTUPCDR) This is the official implementation of our paper Personalized Tran

Yongchun Zhu 81 Dec 29, 2022
PyTorch implementation of neural style transfer algorithm

neural-style-pt This is a PyTorch implementation of the paper A Neural Algorithm of Artistic Style by Leon A. Gatys, Alexander S. Ecker, and Matthias

770 Jan 02, 2023
On Evaluation Metrics for Graph Generative Models

On Evaluation Metrics for Graph Generative Models Authors: Rylee Thompson, Boris Knyazev, Elahe Ghalebi, Jungtaek Kim, Graham Taylor This is the offic

13 Jan 07, 2023
Orange Chicken: Data-driven Model Generalizability in Crosslinguistic Low-resource Morphological Segmentation

Orange Chicken: Data-driven Model Generalizability in Crosslinguistic Low-resource Morphological Segmentation This repository contains code and data f

Zoey Liu 0 Jan 07, 2022
A novel pipeline framework for multi-hop complex KGQA task. About the paper title: Improving Multi-hop Embedded Knowledge Graph Question Answering by Introducing Relational Chain Reasoning

Rce-KGQA A novel pipeline framework for multi-hop complex KGQA task. This framework mainly contains two modules, answering_filtering_module and relati

ι‡‘δΌŸεΌΊ -上桷倧学人ε·₯智能小渣渣~ 16 Nov 18, 2022
This is the official PyTorch implementation for "Mesa: A Memory-saving Training Framework for Transformers".

Mesa: A Memory-saving Training Framework for Transformers This is the official PyTorch implementation for Mesa: A Memory-saving Training Framework for

Zhuang AI Group 105 Dec 06, 2022
Physics-informed convolutional-recurrent neural networks for solving spatiotemporal PDEs

PhyCRNet Physics-informed convolutional-recurrent neural networks for solving spatiotemporal PDEs Paper link: [ArXiv] By: Pu Ren, Chengping Rao, Yang

Pu Ren 11 Aug 23, 2022