Giter Site home page Giter Site logo

intellabs / dfm Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 1.0 2.24 MB

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

Home Page: https://intellabs.github.io/dfm/

License: MIT License

Python 100.00%

dfm's Introduction

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},
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.