项目作者: Pangoraw

项目描述 :
Unofficial re-implementation of PaDiM: A Patch Distribution Modeling Framework for Anomaly Detection and Localization
高级语言: Python
项目地址: git://github.com/Pangoraw/PaDiM.git
创建时间: 2021-03-03T13:37:50Z
项目社区:https://github.com/Pangoraw/PaDiM

开源协议:MIT License

下载


PaDiM

A Patch Distribution Modeling Framework for Anomaly Detection and Localization

This is an unofficial re-implementation of the paper PaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization available on arxiv.

Features

The key features of this implementation are:

  • Constant memory footprint - training on more images does not result in more memory required
  • Resumable learning - the training step can be stopped and then resumed with inference in-between
  • Limited dependencies - apart from PyTorch, Torchvision and Numpy

Variants

This repository also contains variants on the original PaDiM model:

  • PaDiMSVDD uses a Deep-SVDD model instead of a multi-variate Gaussian distribution for the normal patch representation.
  • PaDiMShared shares the multi-variate Gaussian distribution between all patches instead of learning it only for specific coordinates.

Installation

  1. git clone https://github.com/Pangoraw/PaDiM.git padim

Getting started

Training

  1. from torch.utils.data import DataLoader
  2. from padim import PaDiM
  3. # i) Initialize
  4. padim = PaDiM(num_embeddings=100, device="cpu", backbone="resnet18")
  5. # ii) Create a dataloader producing image tensors
  6. dataloader = DataLoader(...)
  7. # iii) Consume the data to learn the normal distribution
  8. # Use PaDiM.train(...)
  9. padim.train(dataloader)
  10. # Or PaDiM.train_one_batch(...)
  11. for imgs in dataloader:
  12. padim.train_one_batch(imgs)

Testing

With the same PaDiM instance as in the Training section:

  1. for new_imgs in test_dataloader:
  2. distances = padim.predict(new_imgs)
  3. # distances is a (n * c) matrix of the mahalanobis distances
  4. # Compute metrics...

Acknowledgements

This implementation was built on the work of: