An open API service indexing awesome lists of open source software.

https://github.com/narcissusex/hetmm

[IJCV 2024] Hard-normal Example-aware Template Mutual Matching for Industrial Anomaly Detection
https://github.com/narcissusex/hetmm

anomaly-detection anomaly-localization ijcv pytorch training-free unsupervised

Last synced: about 2 months ago
JSON representation

[IJCV 2024] Hard-normal Example-aware Template Mutual Matching for Industrial Anomaly Detection

Awesome Lists containing this project

README

        

# Hard-normal Example-aware Template Mutual Matching for Industrial Anomaly Detection

[Zixuan Chen](https://narcissusex.github.io), [Xiaohua Xie](https://cse.sysu.edu.cn/content/2478), [Lingxiao Yang](https://zjjconan.github.io/), [Jian-Huang Lai](https://cse.sysu.edu.cn/content/2498),

*International Journal of Computer Vision (**IJCV**)*, 2024.

**TL;DR:** HETMM is a simple yet effective framework for industrial anomaly detection based on template matching, which can accurately detect and locate unknown anomalies in a training-free manner.

---
[![Paper](https://img.shields.io/badge/Paper-1?logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAADIAAAAuCAYAAABqK0pRAAAKAElEQVRo3u1aWW8b1xlNkfahQG0nLlwg6IIW%2FQnuH%2Bjv6PbsN%2F8Bo08BGsBLU%2BexNYq2iW35wW%2FKQ2HXjrWLnBmSM9xFiqS4WItJStRCUvLXc747Q116QUPJdRIgA1zM8M72Leec77sjvfPOt9u329drE5Fzg8HgV%2F3%2B8MN%2Bv%2F%2Fv%2Ff39eYy5%2F8c4ODiwfh%2Fo7z28r1Zb%2BytM%2Be6Xsfc7luHvDYfDD4%2BOjq5hf%2FXw8DCG47685Q3vxHguOzs9SSSTPkz7fmTgu3t7ez%2BDt7%2FgwO%2Bfc48bPsNYhsFL4Ui8%2BNDnz58L5k88aBSfYY9er6fz0dYfDGRzc2v0e2dnB9fsysbGJh1xRo7gxg9wvoybe7zOGq%2FcXnzxacb29rbu7Y1Gn8iRzc3NH%2FP%2Btw0ROvDs2TMejM0D99J%2B1j6xI%2BtfhSPl1ob0Bodj81P37slfbt4cZeVr7whckblSU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Aj%2B7tSqa5OLr%2BmFrhh45cO60BGI7%2BJmzfW1%2FW6TexpQLlY1FWgj5dzT55wAWZqUUyW5xcNb3RlGYyp1tOnL38qoMgwaFFGuAxm2zKxI37Y9JGkhAOrNAtaNmwiWSuYJf7mS7iWZ8T9MFtOWP1NXYlJsZCRUhXdby0nZYzBcGAtrFq6yrRlnv0Yl8a2%2FJ6oaYzW6TTGtBaGLwotQi0eD9sNX7G9Wlwx58JrymgxVDDYcEIslPi4lo5yXWITmS2MB%2Bm22%2FgglGqbI6snWepG3S4HW3MqD2ESW1jS4YVtOSOuioLud3WlrM2jccCsZeiYp%2BsXo1q694MxPiSpdAiWXUfIRyLBzsjElb0OR%2FywtfZ0TRGuP8IPCrGFRXWKqsTFEslu1vmmnyLEKBaUWha1IAAEs0G4RnFDslvdL%2BbieKb9OaiITqFeq4%2F9fWRjY8JPpr2dbf2uRTiZFZ4pjnkYzcJH%2FLaaLfRgLbxsTSHBjJg236xfeE8a6w8SO0iYjxHRh4tMgMpufXxgxW5vPRuT5An%2FYuW%2B9ksjH8IWhBBgxY1alGiuH35CZTHUvwVGLUx4Pvr7oN43HI4d252v%2FsWq3X5JtWig7dgAx93u8ULLNKuic3HHcd%2FIR%2BzDw%2BGpPmJ3ut2X5jud8TlmMJ8vVre2tgqwtZjNZpcfPPjP32%2FduvXHK1eu%2FBouvBs58hNc3%2F4q%2FqzQbLW6UKUmRosD4tFaWFj45%2BPHj3%2Fz6NGj3z98%2BPAP09PTv7148eIvz549e%2F7MmTM%2FhMlnX%2FmH9Gaz%2BaNer%2Feo0%2Bn43W439bYG33f%2F%2Fv3fXb58%2BYNLly79NBrnz7%2FG0C%2FzTwEXLlz4wblz597HeO8tjvfx7u%2B9qf%2FE%2BC%2FkEJZILUQcyQAAAABJRU5ErkJggg%3D%3D&label=IJCV)](https://link.springer.com/article/10.1007/s11263-024-02323-0)
[![ArXiv](https://img.shields.io/badge/cs.CV-2303.16191-b31b1b?logo=arxiv&logoColor=red)](https://arxiv.org/abs/2303.16191)
[![Google Drive](https://img.shields.io/badge/Material-1?logo=googledrive&label=Google%20Drive)](https://drive.google.com/drive/folders/1c4XvmugX-ryP168bDMFcScdiYWgYktlu?usp=drive_link)
[![Baidu Cloud](https://img.shields.io/badge/Materials-ryP168bDMFcScdiYWgYktlu%3Fusp%3Ddrive_link?logo=Baidu&label=Baidu%20Cloud)](https://pan.baidu.com/s/1HH_3FQo1K72HbUvZpfylxw?pwd=eeg9)


[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/hard-nominal-example-aware-template-mutual/anomaly-detection-on-mvtec-ad)](https://paperswithcode.com/sota/anomaly-detection-on-mvtec-ad?p=hard-nominal-example-aware-template-mutual)
[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/hard-nominal-example-aware-template-mutual/anomaly-detection-on-visa)](https://paperswithcode.com/sota/anomaly-detection-on-visa?p=hard-nominal-example-aware-template-mutual)
[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/hard-nominal-example-aware-template-mutual/anomaly-detection-on-surface-defect-saliency)](https://paperswithcode.com/sota/anomaly-detection-on-surface-defect-saliency?p=hard-nominal-example-aware-template-mutual)

## Motivation


framework

Visualization of training data (ball) and queries (cube) via t-SNE. Visually, existing methods' decision boundaries are dominated by the overwhelming number of easy-normal examples (blue balls). Hence, the normal queries (green cubes) near the hard-normal examples (orange balls) are prone to be erroneously identified as anomalies (purple cubes), resulting in a high false-positive or missed-detection rate. To address this issue, we propose HETMM to construct a robust prototype-based decision boundary, which can accurately distinguish hard-normal examples from anomalies.

## Framework


framework

The overall framework of our methods. In stage I, the original template set $\mathcal{T}^{(j)}$ is the aggregation of the features extracted by feeding $N$ collected normal images $\mathcal{Z}$ into the pre-trained backbone $\Phi$ with $M$ layers, where each color on $\mathcal{T}^{(j)}$ denotes that the feature is extracted from different normal images. To streamline $\mathcal{T}^{(j)}$ into a tiny set $\mathcal{T}^{(j)K}$ with $K$ sheets ($N\ge K$), PTS selects $K$ significant prototypes from $\mathcal{T}^{(j)}$ at each pixel coordinate through the sliding windows. In stage II, given a query image $q$, we first extract its features by the same pre-trained backbone $\Phi$ and then employ ATMM to obtain hierarchical anomaly maps $S^{(j)}$, where each $S^{(j)}$ is generated at the $j$-th layer. $S^\dagger$ is obtained as the final outputs.

## Code Usage
### 1) Get start

* Python 3.9.x
* CUDA 11.1 or *higher*
* NVIDIA RTX 3090
* Torch 1.8.0 or *higher*

**Create a python env using conda**
```bash
conda create -n hetmm python=3.9 -y
conda activate hetmm
```

**Install the required libraries**
```bash
bash setup.sh
```

### 2) Template Generation
**Original template set on MVTec AD:**
```bash
python run.py --mode temp --ttype ALL --dataset MVTec_AD --datapath
```
**Tiny set formed by PTS (60 sheets) on MVTec AD:**
```bash
python run.py --mode temp --ttype PTS --tsize 60 --dataset MVTec_AD --datapath
```
Since generating pixel-level OPTICS clusters is time-consuming, you can download the "*template*" folder from [Google Drive](https://drive.google.com/drive/folders/1c4XvmugX-ryP168bDMFcScdiYWgYktlu?usp=drive_link) / [Baidu Cloud](https://pan.baidu.com/s/1HH_3FQo1K72HbUvZpfylxw?pwd=eeg9) and copy it into our main folder as:
```
HETMM/
├── configs/
├── template/
├── src/
├── run.py
└── ...
```

### 3) Anomaly Prediction
**Original template set on MVTec AD:**
```bash
python run.py --mode test --ttype ALL --dataset MVTec_AD --datapath
```
**Tiny set formed by PTS (60 sheets) on MVTec AD:**
```bash
python run.py --mode test --ttype PTS --tsize 60 --dataset MVTec_AD --datapath
```
Please see "*run.sh*" and "*run.py*" for more details.

## Citation

```tex
@article{Chen_2024_hetmm,
author = {Chen, Zixuan and Xie, Xiaohua and Yang, Lingxiao and Lai, Jianhuang},
title = {Hard-normal Example-aware Template Mutual Matching for Industrial Anomaly Detection},
journal = {International Journal of Computer Vision (IJCV)},
publisher = {Springer},
year = {2024},
doi = {10.1007/s11263-024-02323-0},
}
```