Skip to content

A clean Pytorch re-implementation of the paper Deep One-Class Classification extending it to 128x128 resolution.

License

Notifications You must be signed in to change notification settings

GabrielDornelles/deep-anomaly-classification

Repository files navigation

deep-anomaly-classification

This repository contains an implementation of the paper Deep One-Class Classification, an image anomaly classification method.

The original paper proposes its method to find anomalies on CIFAR10 and MNIST datasets, the authors also benchmark it on adversarial attacks. Here, I extend their work to higher resolution datasets, like 128x128 pixels.

Original Repo and implementation: https://github.com/lukasruff/Deep-SVDD-PyTorch

Deep SVDD

In this repo, I'll use the Deep SVDD as an anomaly classification for golden sample datasets.

So, if you have something that you need to classify when anomalies happen but you don't know the anomalies (i.e you only have good samples, bad samples rarely occur), this is a good Unsupervised setup and might suit you!

Deep Support Vector Data Description (Deep SVDD)

Deep SVDD is a method that utilizes deep convolutional neural networks to learn a function that maps the input space into a smaller vector space. The goal is to make non-anomalous points close to each other in that space. The convolutional neural network learns to map these points close to each other based on the most important characteristics of the class.

Deep SVDD

Objective

The main objective of Deep SVDD is to train a model to find the best representation of a dataset in a smaller vector space. It then optimizes a hypersphere that contains the samples of good data inside it. During testing, everything outside the hypersphere is considered an anomaly.

Training

Step 1: Deep Auto Encoder Training

In the first step, we train a Deep Auto Encoder to reconstruct our dataset. A good reconstruction implies that the encoder has learned good features, and the bottleneck has a strong representation of the trained class.

Step 2: Latent Space Extraction

In the second step, we take only the encoder part of our AutoEncoder and its last convolutional layer as a latent space, which contains the smallest representation of our data (typically 512 dimensions).

Step 3: Hypersphere Center Calculation

We create a 512-dimensional vector and calculate the mean of our dataset at the latent dimension (last conv layer). This mean serves as the center of our 512-dimensional hypersphere.

Step 4: Hypersphere Radius Optimization

In this step, we optimize the hypersphere radius to ensure it contains our good samples inside it.

Step 5: Anomaly Detection

To detect anomalies, we forward new images through our encoder, which outputs a 512-dimensional vector (the image in the latent space). We check whether this 512-dimensional point is inside the hypersphere or not. Scores smaller than 0 are considered inside the hypersphere, while scores higher than 0 are anomalies.

Results

The results can vary based on training and data, and ideal outcomes should involve negative scores for good samples. However, it's common to have very small values as the output. Also, Its recommended to apply some augmentations to help the model not overfit your data (it will happen if you train with 1 or 2 images).

Example Results:

Image Score
Golden Sample 0.012
Darker 0.11
Handmade Defects 32.15
Shifted Painting 159.10
F12 Instead of F8 2.33

Citation

I'am a research engineer, so I don't really write papers on the subject, instead I write the models and make them work on real world scenarios. If you find that repository useful, please make a reference to it!

# Original Author
@InProceedings{pmlr-v80-ruff18a,
  title     = {Deep One-Class Classification},
  author    = {Ruff, Lukas and Vandermeulen, Robert A. and G{\"o}rnitz, Nico and Deecke, Lucas and Siddiqui, Shoaib A. and Binder, Alexander and M{\"u}ller, Emmanuel and Kloft, Marius},
  booktitle = {Proceedings of the 35th International Conference on Machine Learning},
  pages     = {4393--4402},
  year      = {2018},
  volume    = {80},
}

# Mine
@misc{deep-anomaly-classification,
  title={Deep Anomaly Classification},
  author={Gabriel Dornelles Monteiro},
  year={2022},
  howpublished={GitHub Repository},
  url={https://github.com/GabrielDornelles/deep-anomaly-classification},
}

About

A clean Pytorch re-implementation of the paper Deep One-Class Classification extending it to 128x128 resolution.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages