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
Deep generative models, particularly Normalizing Flows (NFs), are theoretically well-suited for outlier detection because they provide exact likelihood estimation. However, in practice, these models often assign higher likelihoods to out-of-distribution (OOD) data than to the training data itself. For instance, a model trained on CIFAR-10 might assign higher likelihood to SVHN images. This phenomenon occurs because the models tend to capture low-level pixel statistics—such as local correlations and background textures—rather than the high-level semantic features that define a class.
Previous attempts to solve this, such as the Likelihood Ratio method, involve training a second "background" model on a generic dataset and using the ratio $p(x)/q(x)$ as an outlier score. While conceptually sound, this approach is numerically unstable in the distribution's tails, where both $p(x)$ and $q(x)$ are extremely small, leading to noisy and unreliable scores.
Methodology
This paper introduces Contrastive Normalizing Flows, which address these issues through two primary innovations: a new training objective and the use of a semantic feature space.
The Positive Difference Objective
Instead of training two separate models, the authors propose a single model trained with a contrastive objective. The model is optimized to maximize the log-likelihood of in-distribution data ($x \sim p$) while simultaneously minimizing the log-likelihood of a contrastive dataset ($y \sim q$). The authors demonstrate that this objective is equivalent to learning the normalized positive difference ($p - q$) between the two densities. Unlike the likelihood ratio, the difference $p - q$ is numerically stable; in regions where data is sparse, the difference naturally approaches zero rather than exploding.
Self-Supervised Feature Space
To prevent the model from focusing on low-level pixel noise, the training is performed in a high-level semantic space. A frozen MoCo (Momentum Contrast) feature extractor, pre-trained on ImageNet, is used to transform raw images into 128-dimensional feature vectors. The Normalizing Flow then learns the distribution of these semantic features, effectively ignoring background noise that is common across both in-distribution and contrastive datasets.
Experimental Setup and Results
The method was evaluated across several benchmarks, including CIFAR-10/100, CelebA, and SVHN. In "one-vs-rest" experiments on CIFAR-10, the Contrastive NF achieved a state-of-the-art mean AUROC of 96.8%.
The robustness of the "Positive Difference" score is evident when comparing it to standard likelihoods and likelihood ratios. As shown in Figure 3, the Contrastive NF provides much clearer separation between in-distribution data (e.g., 'deer') and difficult OOD samples (e.g., 'horse') compared to standard methods.
Beyond accuracy, the method is computationally efficient. By operating in a 128-dimensional feature space, a single forward pass takes approximately 11ms, significantly faster than pixel-based models like GLOW (~162ms).
Discussion and Conclusion
The Contrastive Normalizing Flow demonstrates that focusing on the difference between distributions in a semantic feature space is a powerful strategy for outlier detection. The method is robust to "contamination" in the contrastive dataset and avoids the numerical instabilities of ratio-based scores.
However, challenges remain. The model still finds it difficult to distinguish between semantically similar classes (e.g., cats vs. dogs) if the underlying feature extractor places them close together. Future work may involve exploring more complex high-dimensional difference distributions and investigating alternative ways to generate contrastive data in domains where large auxiliary datasets are unavailable.
Summary updated