Abstract
Reliable detection of anomalies is crucial when deploying machine learning models in practice, but remains challenging due to the lack of labeled data. To tackle this challenge, contrastive learning approaches are becoming increasingly popular, given the impressive results they have achieved in self-supervised representation learning settings. However, while most existing contrastive anomaly detection and segmentation approaches have been applied to images, none of them can use the contrastive losses directly for both anomaly detection and segmentation. In this paper, we close this gap by making use of the Contrastive Predictive Coding model (arXiv:1807.03748). We show that its patch-wise contrastive loss can directly be interpreted as an anomaly score, and how this allows for the creation of anomaly segmentation masks. The resulting model achieves promising results for both anomaly detection and segmentation on the challenging MVTec-AD dataset.
Introduction
Anomaly detection (AD) involves identifying observations that deviate significantly from the "normal" majority, a task vital for industrial quality control and medical diagnostics. Because anomalous data is inherently rare and unpredictable, researchers typically rely on unsupervised learning. While contrastive learning has revolutionized self-supervised representation learning, most existing AD methods treat it merely as a pre-processing step to train an encoder, requiring a secondary mechanism—such as density estimation—to identify anomalies. This paper proposes a more direct approach: using the contrastive loss function itself as the detection and localization signal.
Methodology
The authors adapt Contrastive Predictive Coding (CPC) for anomaly detection (CPC-AD). In standard CPC, an image is divided into patches, and a model is trained to predict the representation of a "future" patch based on its neighbors. The proposed CPC-AD introduces several key refinements:
- Architecture Simplification: The autoregressive component is removed. Instead of a sequence, a single patch's representation is used to predict its neighbor, simplifying the model.
- Loss as an Anomaly Score: The InfoNCE loss measures how well the model distinguishes a "positive" (neighboring) patch from "negative" (random) patches. In this framework, the loss serves as a natural anomaly score; anomalous patches break learned spatial patterns, resulting in higher loss values.
- Negative Sample Hygiene: During testing, negative samples are drawn from the known-normal training set rather than the current test batch. This prevents potential anomalies in the test set from dampening the contrastive score.
- Multi-directional Context: Four separate models predict patches in different directions (above, below, left, right), and their losses are combined for a robust final score.
Experimental Results
The model was evaluated on the MVTec Anomaly Detection (MVTec-AD) dataset, which contains over 5,000 high-resolution images across 15 categories (10 objects and 5 textures).
Detection Performance
CPC-AD achieved a mean AUROC of 0.901 across all classes, significantly outperforming traditional baselines like Kernel Density Estimation (0.636) and standard Autoencoders (0.631). Performance was particularly strong on categories like "Bottle" (0.998) and "Hazelnut" (0.996).
Segmentation and Localization
By upsampling patch-wise loss values, the model generates anomaly segmentation masks. It achieved a mean pixel-wise AUROC of 0.82. While this is lower than some state-of-the-art models using pre-trained ImageNet features, it is highly competitive for a model trained entirely from scratch. The model is especially effective at detecting breaks in repetitive texture patterns.
Conclusion and Future Work
CPC-AD demonstrates that contrastive objectives can serve as effective "consistency checks" for anomaly detection without needing separate classifiers. While the current patch-based approach results in somewhat coarse segmentation masks (due to the 64x64 pixel sub-patches), it provides an interpretable and robust baseline. Future work may explore integrating pre-trained encoders or refining the resolution to improve fine-grained localization on complex datasets.
Summary updated