Self-supervised Autoencoder Anomaly Detection

Machine Learning
Introduced by
Golan & El-Yaniv; broader self-supervised anomaly detection community
Year
2018–2020
Type
Unsupervised / self-supervised deep learning
Family
Machine learning

What does it do?

Self-supervised autoencoder anomaly detection trains an autoencoder on unlabeled normal data together with a self-supervised pretext task, such as predicting which geometric transformation was applied to an input.

It then flags inputs whose reconstruction error deviates substantially from the distribution learned on normal data.

The intuition behind the method

A standard autoencoder for anomaly detection is trained to reconstruct normal data and raises an alarm when reconstruction error is high. The self-supervised variant adds an auxiliary pretext task, for example predicting a rotation applied to an image, so the network must also learn structure that is specific to normal samples. The working hypothesis is that the pretext task can make the encoder reconstruct some anomalies poorly that a vanilla autoencoder would reconstruct well, so that they receive high scores. This is a hypothesis rather than an established result, and it must be checked on each dataset.

History

Reconstruction-based anomaly detection with autoencoders is a well-established deep approach, and the self-supervised variant described here extends it with an auxiliary pretext task. Ruff et al. (2021, Proceedings of the IEEE) provide a unifying review of shallow and deep anomaly detection, covering generative-model, one-class and reconstruction-based methods and the connections between them. Golan and El-Yaniv's 2018 NeurIPS paper, Deep one-class classification via geometric transformations, is a related self-supervised one-class method that uses geometric transformations; it is not itself an autoencoder reconstruction method and is cited here as a neighbouring idea.

When to use it

Choose self-supervised autoencoder anomaly detection when labelled anomalies are unavailable or very scarce, when there is a substantial body of unlabeled normal data (as a rough guide, many hundreds of samples, though the requirement depends on the data and task), and when the inputs are high-dimensional and structured, such as images, video, or time-series, so that a suitable pretext task can be defined. It is also a candidate when plain reconstruction-based autoencoders are judged to miss anomalies that lie close to the normal data. Avoid it for small tabular datasets, where classical detectors such as Isolation Forest or One-Class SVM are simpler to run. Avoid it also when the decision must be explained in terms of individual features, because the deep model is hard to interpret.

Limitations and considerations

  • Requires a substantial amount of unlabeled normal data and, for deep models, GPU resources; it is impractical for very small datasets.
  • Architecture, pretext task, and threshold all need tuning, and the best choices are domain-dependent.
  • The anomaly score is a reconstruction-error magnitude with no natural unit, so the threshold must be calibrated on the specific data rather than read off directly.
  • Performance degrades if the training data contain anomalies, because the model then learns a contaminated notion of normality.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Trains only on unlabeled normal data, so no labelled anomaly examples are needed.
  • The pretext task adds a second learning signal to reconstruction, which can give the encoder more discriminative features than reconstruction alone.
  • Suited to high-dimensional inputs such as images, audio, and multivariate time-series, where hand-designed features are hard to specify.
  • The pretext task can be adapted to the modality, so domain knowledge about which transformations preserve normality can be used directly.
  • Because the model learns what normal data looks like rather than a list of known fault types, it does not require examples of each anomaly type in training.

Applications

  • Visual inspection of manufactured parts, where normal images are plentiful and defect examples are rare.
  • Anomaly detection in unlabeled network or system logs.
  • Screening of medical images for unusual scans when diseased examples are scarce.
  • Monitoring of multivariate sensor time-series from machinery.
  • Flagging unusual events in mostly normal video footage.

How it works

How it works

1

One or more self-supervised pretext tasks are defined over the training inputs without labels. Common choices include geometric transformations (such as rotations or flips), masked prediction, contrastive pairs, and temporal ordering of time-series windows. A task is useful only if solving it depends on structure that normal data has and that the task's transformation actually changes.

2

The autoencoder, or a companion prediction head, is trained on unlabeled normal data to solve the pretext task alongside or instead of plain reconstruction. The aim is for the encoder to capture features of normal data that go beyond low-level pixel or feature statistics. Training typically combines the reconstruction loss and the pretext-task loss in a weighted sum, and the relative weight is a hyperparameter that is tuned on held-out normal data.

3

After pretraining, each test sample is passed through the trained encoder and decoded back to the input space. Samples that resemble the normal training data should compress and reconstruct well, whereas samples far from that learned distribution should reconstruct poorly.

4

The anomaly score is the squared reconstruction error s(x) = ||x - x_hat||^2, so higher values indicate greater deviation from the learned normal distribution. Scoring on this page relies on reconstruction error alone; any combination with a pretext-task score would need its own definition and validation on held-out normal data.

Formula
s(x) = \|x - \hat{x}\|^2
5

A decision threshold is applied to the anomaly score. It is usually set from the empirical distribution of scores on a held-out set of normal validation samples, for example at the 95th or 99th percentile. Test samples above the threshold are flagged as anomalies.

Common pitfalls

  • Setting the threshold on the same data used for training instead of a held-out normal validation set, which gives overoptimistic results.
  • Choosing a pretext task whose transformation does not change the normal data in a meaningful way, for example rotating samples whose orientation carries no information, so the auxiliary signal is uninformative.
  • Reporting only AUROC when the anomaly class is extremely rare; precision-recall metrics are more informative in that case.
  • Skipping contamination checks: even a small fraction of anomalies in the training set can distort the learned normal distribution.
  • Treating reconstruction error as a full explanation; individual scores and reconstructions should be inspected, since a high score does not say which part of the input is anomalous.

Frequently asked questions

How is this different from a standard autoencoder for anomaly detection?

A standard autoencoder is trained only on reconstruction loss. The self-supervised variant adds a pretext task, such as predicting an applied rotation, which asks the encoder to learn further structure of normal data. The expected benefit is better detection of subtle anomalies that a vanilla autoencoder still reconstructs reasonably well, but this has to be verified on the target data.

What pretext task should I choose?

The choice depends on the data modality. Geometric transformations are a natural option for images, temporal shuffling or masking for time-series, and masked feature prediction for large tabular datasets. A good task is one whose solution depends on normal patterns and that anomalous inputs would be expected to fail. Candidate tasks should be compared on held-out data.

How do I set the anomaly threshold?

Hold out a clean subset of normal training data, compute anomaly scores on it, and set the threshold at a high percentile such as the 95th or 99th. A higher percentile reduces false positives, which suits cases where false alarms are costly; a lower one reduces missed anomalies, which suits cases where misses are costly.

My dataset is small. Should I use this method?

Probably not. Deep autoencoder methods depend on a large body of normal samples to learn stable representations and score estimates; many hundreds is a typical rough guide, though the requirement depends on the data and task. For small tabular datasets, Isolation Forest, One-Class SVM, or Local Outlier Factor are simpler and faster to run; whether they are more or less accurate depends on the data and should be checked.

Can I use this when I have some labeled anomalies?

Yes. A small number of labelled anomalies can be used to add a supervised term to the self-supervised objective, or to tune the threshold more precisely. If labels are plentiful, fully supervised or semi-supervised approaches may be a better fit than the purely self-supervised variant.

Related methods

Sources

  1. Golan, I. & El-Yaniv, R. (2018). Deep one-class classification via geometric transformations. Advances in Neural Information Processing Systems (NeurIPS), 31.

    Open publication reference
  2. Ruff, L., Kauffmann, J. R., Vandermeulen, R. A., Montavon, G., Samek, W., Kloft, M., Dietterich, T. G., & Müller, K.-R. (2021). A unifying review of deep and shallow anomaly detection. Proceedings of the IEEE, 109(5), 756–795.

    Open DOI reference