Skip to content

IntelLabs/dfm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Anomaly Detection using Feature Reconstruction Error from Deep Features

Introduction

This repo presents a fast, principled approach for detecting anomalous and out-of-distribution (OOD) samples in deep neural networks (DNN). We propose the application of linear statistical dimensionality reduction techniques on the semantic features produced by a DNN, in order to capture the low-dimensional subspace truly spanned by said features. We show that the feature reconstruction error (FRE), which is the l2-norm of the difference between the original feature in the high-dimensional space and the pre-image of its low-dimensional reduced embedding, is highly effective for OOD and anomaly detection. This dimensionality reduction can be performed using either Principal Component Analysis (PCA), or via a shallow (single layer) linear auto-encoder (AE). In case of the AE, if the weights are shared between the encoder and decoder, i.e $W_{dec} = W_{enc}^T$, then it converges to the same solution as the PCA. Experiments using standard image datasets and DNN architectures demonstrate that our method meets or exceeds best-in-class quality performance, but at a fraction of the computational and memory cost required by the state of the art. It can be trained and run very efficiently.

Dependencies

Following modules are needed and can be installed using python -m pip install -r requirements.txt:

  • numpy
  • scikit-learn
  • PIL
  • torch
  • torchvision
  • pandas

Dataset

Download the entire MVTec Anomaly Detection dataset (about 4.9 GB) from the MVTec website and store it in a local folder. Recommended default folder location: ./mvtec. Direct link to dataset.

Running the code

To run the code:

python run_mvtec.py

By default, python run_mvtec.py will run all 15 object categories in the MVTEC dataset with an EfficientNet B5 backbone assuming that the dataset is stored in ./mvtec folder (default location assumed by the script).

If the dataset is stored in a different folder, its location can be specified by:

python run_mvtec.py --dataset_directory <MVTec dataset path>

Two other backbones are supported: Resnet18 and Resnet50. To run with a different backbone:

python run_mvtec.py --model resnet18

Three dimensionality reduction methods are supported: pca, tae (Tied AE), ae (plain AE without shared weights) and any one or more of them can be optionally specified (default: all three modes are run). For example, the command line below will generate results for both PCA and Tied AE. Additionally, the number of training epochs can be optionally be specified if one of the modes being run is a type of AE (default: 250):

python run_mvtec.py --modes pca tae --epochs 200

To save the heatmaps used for anomaly segmentation, use the --save_heatmaps option and specify an optional output path (default: .output):

python run_mvtec.py --save_heatmaps --output_path results

To run on GPU:

python run_mvtec.py --gpu

To run a specific category from MVTec, provide category name. For example, :

python run_mvtec.py --object_category hazelnut

Sample commands using all options

python run_mvtec.py --dataset_directory <MVTec dataset path> --model resnet18 --object_category hazelnut --gpu --modes pca --save_heatmaps

Reference

This code is based on the following work. Please cite if you use it.

@inproceedings{fre2023,
  title={FRE: A Fast Method For Anomaly Detection And Segmentation},
  author={Ibrahima J. Ndiour and
          Nilesh A. Ahuja and
          Utku Genc and
          Omesh Tickoo}
  booktitle={British Machine Vision Conference (BMVC)},
  year={2023},
}

About

DFM (Deep Feature Modeling) is an efficient and principled method for out-of-distribution detection, novelty and anomaly detection.

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •  

Languages