Home

Awesome

One-for-All: Proposal Masked Cross-Class Anomaly Detection

<!-- (https://arxiv.org/abs/2106.08254) -->

PyTorch implementation and pretrained models for AAAI2023 paper, One-for-All: Proposal Masked Cross-Class Anomaly Detection.


Download Pretrained Weights and Models

Download checkpoints that are self-supervised pretrained on ImageNet-22k and then used for fine-tuning on MVTecAD dataset:

Download pretrained visual tokenizer(discrite VAE) from: encoder, decoder, and put them to the directory weights/tokenizer.

Or download pretrained ViT visual tokenizer from: vit_tokenizer, and put them to the directory weights/tokenizer.

Download offline generated prototype feature from: there, and put it to the directory weights/prototypes.

Download pretrained protoflow from: there, and put it to the directory weights/protoflow.

Setup

Install all packages with this command:

$ python3 -m pip install -U -r requirements.txt

Download MVTecAD dataset from there, put it to the directory data/mvtec_anomaly_detection, and then run following code to convert this dataset to ImageNet format.

python setup_train_dataset.py --data_path /path/to/dataset

This script will create a ImageNet format dataset for training at the data/Mvtec-ImageNet directory. Then please download foreground masks, and put it to the directory data/Mvtec-ImageNet/fg_mask.

Training

Run code for training MVTecAD dataset.

bash scripts/train_multi_class.sh  // training for multi-class setting
bash scripts/train_cross_class.sh  // training for cross-class setting

For cross-class setting objects-to-textures, please set --from_obj_to_texture in train_cross_class.sh. If not setted, the code will run cross-class setting textures-to-objects.

Testing

Run code for testing MVTecAD dataset.

bash scripts/test_multi_class.sh  // testing for multi-class setting
bash scripts/test_multi_class.sh  // testing for multi-class setting

You can download trained ViT-base-16 models for multi-class setting: multi-classes-model, the trained models are provided in Download Pretrained Weights and Models section. You can download trained ViT-base-16 model for cross-class setting from: objects-to-textures and textures-to-objects.

We summarize the validation results as follows.

Multi-Class Setting:

CategoryImage/Pixel AUCCategoryImage/Pixel AUCCategoryImage/Pixel AUC
Carpet0.999/0.988Bottle1.000/0.978Pill0.965/0.952
Grid0.982/0.962Cable0.975/0.963Screw0.807/0.954
Leather1.000/0.990Capsule0.912/0.962Toothbrush0.894/0.980
Tile1.000/0.956Hazelnut1.000/0.980Transistor0.963/0.940
Wood1.000/0.908Metal nut1.000/0.888Zipper0.967/0.942
Mean0.964/0.956

Cross-Class Setting(objects-to-textures):

CategoryImage/Pixel AUC
Carpet0.986/0.967
Grid0.901/0.913
Leather1.000/0.978
Tile0.998/0.935
Wood0.995/0.862
Mean0.976/0.931

Cross-Class Setting(textures-to-objects):

CategoryImage/Pixel AUCCategoryImage/Pixel AUC
Bottle0.977/0.932Pill0.805/0.860
Cable0.893/0.940Screw0.580/0.911
Capsule0.767/0.956Toothbrush0.917/0.965
Hazelnut0.939/0.928Transistor0.834/0.801
Metal nut0.787/0.716Zipper0.945/0.927
Mean0.844/0.894

Citation

If you find this repository useful, please consider citing our work:

@article{PMAD,
      title={One-for-All: Proposal Masked Cross-Class Anomaly Detection}, 
      author={Xincheng Yao and Chongyang Zhang and Ruoqi Li and Jun Sun and Zhenyu Liu},
      year={2023},
      conference={Proceedings of the AAAI Conference on Artificial Intelligence, 37(4), 4792-4800.},
      doi={https://doi.org/10.1609/aaai.v37i4.25604},
      primaryClass={cs.CV}
}

Acknowledgement

This repository is built using the timm library, the BEiT repository and the DALL-E repository.

If you are interested in our work, you can also follow our other works: BGAD (CVPR2023), FOD (ICCV2023), HGAD (ECCV2024), ResAD (NeurIPS2024). Or, you can follow our github page xcyao00.