Home

Awesome

MSFlow: Multi-Scale Normalizing Flows for Unsupervised Anomaly Detection

PWC

This is an official implementation of "MSFlow: Multi-Scale Normalizing Flows for Unsupervised Anomaly Detection".

Inmportant Notice

Abstract

Unsupervised anomaly detection (UAD) attracts a lot of research interest and drives widespread applications, where only anomaly-free samples are available for training. Some UAD applications intend to locate the anomalous regions further even without any anomaly information. Although the absence of anomalous samples and annotations deteriorates the UAD performance, an inconspicuous yet powerful statistics model, the normalizing flows, is appropriate for anomaly detection and localization in an unsupervised fashion. The flow-based probabilistic models, only trained on anomaly-free data, can efficiently distinguish unpredictable anomalies by assigning them much lower likelihoods than normal data. Nevertheless, the size variation of unpredictable anomalies introduces another inconvenience to the flow-based methods for high-precision anomaly detection and localization. To generalize the anomaly size variation, we propose a novel Multi-Scale Flows-based framework dubbed MSFlow composed of asymmetrical parallel flows followed by a fusion flow to exchange multi-scale perceptions. Moreover, different multi-scale aggregation strategies are adopted for the image-wise anomaly detection and pixel-wise anomaly localization according to the discrepancy between them. On the challenging MVTec AD benchmark, our MSFlow achieves a new state-of-the-art with detection AUORC score of 99.7%, localization AUROC score of 98.8% and PRO score of 97.1%.

The framework of MSFlow

Enviroment

Prepare datasets

It is recommended to symlink the dataset root to $msflow/data. If your folder structure is different, you may need to change the corresponding paths in default.py.

For MVTec AD data, please download from MVTec AD download. Download and extract them to $msflow/data, and make them look like the following data tree:

MVTec
ā”œā”€ā”€ bottle
ā”‚   ā”œā”€ā”€ ground_truth
ā”‚   ā”‚   ā”œā”€ā”€ broken_large
ā”‚   ā”‚   ā””ā”€ā”€ ...
ā”‚   ā”œā”€ā”€ test
ā”‚   ā”‚   ā”œā”€ā”€ good
ā”‚   ā”‚   ā”œā”€ā”€ broken_large
ā”‚   ā”‚   ā””ā”€ā”€ ...
ā”‚   ā””ā”€ā”€ train
ā”‚       ā””ā”€ā”€ good
ā”œā”€ā”€ cable
ā””ā”€ā”€ ...

For VisA data, please download from VisA download. Download and extract them to $msflow/data, and make them look like the following data tree:

VisA
ā”œā”€ā”€ candle
ā”‚   ā”œā”€ā”€ ground_truth
ā”‚   ā”‚   ā””ā”€ā”€ bad
ā”‚   ā”œā”€ā”€ test
ā”‚   ā”‚   ā”œā”€ā”€ bad
ā”‚   ā”‚   ā””ā”€ā”€ good
ā”‚   ā””ā”€ā”€ train
ā”‚       ā””ā”€ā”€ good
ā”œā”€ā”€ capsules
ā””ā”€ā”€ ...

Thanks spot-diff for providing the code to reorganize the VisA dataset in MVTec AD format. For more details, please refer to this data preparation guide.

Training and Testing

All checkpoints will be saved to the working directory, which is specified by work_dir in the default file.

By default, we evaluate the model on the test set after each meta epoch, you can change the pro evaluation interval by modifying the interval argument in the shell or default file.

Training

For MVTec AD dataset:

CUDA_VISIBLE_DEVICES=0 python main.py --mode train \
    --dataset mvtec --class-names all

For VisA dataset:

CUDA_VISIBLE_DEVICES=0 python main.py --mode train \
    --dataset visa --class-names all --pro-eval

Testing

CUDA_VISIBLE_DEVICES=0 python main.py --mode test --class-name bottle --eval_ckpt $PATH_OF_CKPT 

Results on the MVTec AD benchmark

ClassesDet. AUROCLoc. AUROC
Carpet100.099.4
Grid99.899.4
Leather100.099.7
Tile100.098.2
Wood100.097.1
Bottle100.099.0
Cable99.598.5
Capsule99.299.1
Hazelnut100.098.7
Metal Nut100.099.3
Pill99.698.8
Screw97.899.1
Toothbrush100.098.5
Transistor100.098.3
Zipper100.099.2
Overall Average99.798.8

Results on the VisA benchmark

ClassesDet. AUROCLoc. AUROC
candle97.798.3
capsules98.096.2
cashew94.998.7
chewinggum93.699.7
fryum88.299.6
macaroni197.697.6
macaroni298.089.5
pcb196.098.9
pcb293.597.8
pcb394.498.9
pcb493.099.5
pipe_fryum97.098.9
Overall Average95.297.8

Thanks to

Citation

If you find this work useful for your research, please cite our paper. The formal citation of TNNLS will be updated soon.

@article{zhou2023msflow,
  title={MSFlow: Multi-Scale Flow-based Framework for Unsupervised Anomaly Detection},
  author={Zhou, Yixuan and Xu, Xing and Song, Jingkuan and Shen, Fumin and Shen, Heng Tao},
  journal={arXiv preprint arXiv:2308.15300},
  year={2023}
}