Anomaly Detection using Contrastive Normalizing Flows

Robert Schmier, Ullrich Köthe, Christoph-Nikolas Straehle

Abstract

Detecting test data deviating from training data is a central problem for safe and robust machine learning. Likelihoods learned by a generative model, e.g., a normalizing flow via standard log-likelihood training, perform poorly as an outlier score. We propose to use an unlabelled auxiliary dataset and a probabilistic outlier score for outlier detection. We use a self-supervised feature extractor trained on the auxiliary dataset and train a normalizing flow on the extracted features by maximizing the likelihood on in-distribution data and minimizing the likelihood on the contrastive dataset. We show that this is equivalent to learning the normalized positive difference between the in-distribution and the contrastive feature density. We conduct experiments on benchmark datasets and compare to the likelihood, the likelihood ratio and state-of-the-art anomaly detection methods.

Introduction

Normalizing Flows (NFs) are powerful generative models capable of calculating exact probability densities. Intuitively, high likelihood should indicate in-distribution data, while low likelihood signals an outlier. However, in practice, NFs often fail this task because they are sensitive to low-level pixel statistics—such as local textures or background colors—rather than high-level semantic content. Consequently, a standard NF might assign a higher likelihood to random noise or a completely different dataset (like SVHN) than to its own training data (like CIFAR-10).

Previous attempts to address this, such as the "Likelihood Ratio" method, require training multiple models and can become mathematically unstable in low-density regions. This work proposes Contrastive Normalizing Flows (CNF), a single-model solution that focuses on the unique characteristics of the target distribution relative to a broad background.

Methodology

The proposed method follows a two-stage pipeline designed to prioritize semantic features over pixel-level noise:

  1. Feature Extraction: Instead of raw pixels, the model utilizes a MoCo (Momentum Contrast) feature extractor. Trained in a self-supervised manner on ImageNet, MoCo learns to recognize high-level semantic features while ignoring irrelevant noise.
  2. Contrastive Training: The NF is trained by maximizing the likelihood of the in-distribution data ($p$) while simultaneously minimizing the likelihood of a "contrastive" dataset ($q$)—a diverse, unlabeled collection of images.
The Contrastive Normalizing Flow pipeline: In-distribution and contrastive datasets are processed through a feature extractor before training the Normalizing Flow with a dual objective.

Mathematically, this objective forces the model to learn the normalized positive difference ($p - q$). By modeling the difference rather than the raw density, the model ignores "boring" features common to all images and focuses on what makes the target data unique.

Experimental Results

The researchers evaluated the CNF across several benchmarks, including CIFAR-10, CIFAR-100, CelebA, and tabular data (Credit Card Fraud).

Comparison of outlier score distributions across three methods. Contrastive Normalizing Flow (right) shows superior separation of in-distribution data from various outlier classes compared to standard methods.

Key findings include:

  • Superior Performance: On CIFAR-10, the CNF achieved a mean AUROC of 96.5%, significantly outperforming standard NFs and the Likelihood Ratio method. Fine-tuning the feature extractor (CF-FT) further boosted this to 96.8%.
  • Stability: Unlike the Likelihood Ratio, which can collapse when densities are near zero, the difference-based objective remains stable. This was particularly evident in tabular data, where the CNF maintained a 97.5% AUROC while the Likelihood Ratio method failed (41%).
  • Robustness: The method remains effective even if the contrastive dataset is contaminated with in-distribution samples, as the mathematical objective naturally ignores overlapping regions.

Conclusion

Contrastive Normalizing Flows provide a robust mathematical framework for anomaly detection. By shifting the focus from total density estimation to the positive difference between distributions, the model effectively ignores low-level distractions. While the resulting model is not ideal for image generation due to the nature of the difference distribution, it sets a high standard for safety-critical outlier detection tasks.

Summary updated

BibTeX
@article{schmier2023anomaly,
  author = {Robert Schmier and Ullrich Köthe and Christoph-Nikolas Straehle},
  title = {{Anomaly Detection using Contrastive Normalizing Flows}},
  journal = {Accepted by TMLR},
  year = {2023},
  url = {https://openreview.net/forum?id=B4J40x7NjA},
}
Figure