official source code for REB:Reducing Biases in Representation for Industrial Anomaly Detection
Install
$ git clone https://github.com/ShuaiLYU/REB
$ cd REB
$ conda create -n reb python==3.10.6
$ conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch
$ pip install -r requirements.txt
Training on Mvtec AD
Run commands below to reproduce results on Mvtec AD
-
you are advised to download Mvtec ad dataset with saliency from BaiduNetdisk (code: 1234) or OneDrive.
you can also generate saliency by yourself with teh EDN saliency model -
modify the dataset path and OUTPUT path in global_param.py according to your personal config.
the best K for Mvtec is 9. set K value in argparse or global_param.py -
Run commands below to train
$ cd REB/projects/reb_mvtec
# Self-supervied learning (fine-turning ImageNet-pretrained model with DefectMaker)) Resnet18
python main.py -exp_name res18_dm_com6_bs1024_epo300 -K 9 -run_name run1 -run_mode 0
# only run LDKNN (after fine-turning ImageNet-pretrained model with DefectMaker) Resnet18
$ python main.py -exp_name res18_dm_com6_bs1024_epo300 -K 9 -run_name run1 -run_mode 1
# DefectMaker + LDKNN (REB) Resnet18
$ python main.py -exp_name res18_dm_com6_bs1024_epo300 -K 9 -run_name run1 -run_mode 2
Training on Mvtec LOCO
Run commands below to reproduce results on Mvtec AD LOCO
-
we don't use saliency for Mvtec LOCO
-
modify the dataset path and OUTPUT path in global_param.py according to your personal config.
the best K for Mvtec LOCO is 45. set K value in argparse or global_param.py -
Run commands below to train
$ cd REB/projects/reb_mvtec_loco
# Self-supervied learning (fine-turning ImageNet-pretrained model with DefectMaker)) Resnet18
python main.py -exp_name res18_dm_com6_bs1024_epo300 -K 45 -run_name run1 -run_mode 0
# only run LDKNN (after fine-turning ImageNet-pretrained model with DefectMaker) Resnet18
$ python main.py -exp_name res18_dm_com6_bs1024_epo300 -K 45 -run_name run1 -run_mode 1
# only run LDKNN ( directly use ImageNet-pretrained model) Resnet18
$ python main.py -exp_name res18_imagenet -K 45 -run_name run1 -run_mode 1
# DefectMaker + LDKNN (REB) Resnet18
$ python main.py -exp_name res18_dm_com6_bs1024_epo300 -K 45 -run_name run1 -run_mode 2
# only run LDKNN ( directly use ImageNet-pretrained model) Resnet18
$ python main.py -exp_name res18_imagenet -K 45 -run_name run1 -run_mode 1
# only run LDKNN ( directly use ImageNet-pretrained model) wideRes50
$ python main.py -exp_name wr50_imagenet -K 45 -run_name run1 -run_mode 1
# only run LDKNN ( directly use ImageNet-pretrained model) wideRes101
$ python main.py -exp_name wr101_imagenet -K 45 -run_name run1 -run_mode 1