Contrastive Representation Modeling for Anomaly Detection
Willian T. Lunardi, Abdulrahman Banabila, Dania Herzalla, Martin Andreoni · 2025
Read Contrastive Representation Modeling for Anomaly Detection by Willian T. Lunardi, Abdulrahman Banabila, Dania Herzalla, Martin Andreoni (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2501-05130.txt.
Introduction
Willian Lunardi, Abdulrahman Banabila, Dania Herzalla, and Martin Andreoni present a new way to train models that spot odd or faulty items, in a 2025 paper. The method learns from normal examples and must keep them close together in its internal map, while pushing odd items far away. Older training methods often fail at one side of that balance.
Their method reshapes which examples count as alike and which count as unlike during training, with no need for labeled faults. A patch based version is added for small local defects in factory settings. In tests, the method learned faster and matched or beat prior methods on both everyday and factory data sets.
Catalog record
| CREATOR | Willian T. Lunardi, Abdulrahman Banabila, Dania Herzalla, Martin Andreoni |
|---|---|
| YEAR | 2025 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 82 KB |
| VERSION | 1.0 |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2501-05130
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
Contrastive Representation Modeling for Anomaly Detection Willian T. Lunardi, Abdulrahman Banabila, Dania Herzalla, Martin Andreoni Abstract Distance-based anomaly detection methods rely on compact in-distribution (ID) embeddings that are well separated from anomalies. However, conventional contrastive learning strategies often struggle to achieve this balance, either promoting excessive variance among inliers or failing to preserve the diversity of outliers. We begin by analyzing the challenges of representation learning for anomaly detection and identify three essential properties for the pretext task: (1) compact clustering of inliers, (2) strong separation between inliers and anomalies, and (3) preservation of diversity among synthetic outliers. Building on this, we propose a structured contrastive objective that redefines positive and negative relationships during training, promoting these properties without requiring explicit anomaly labels. We extend this framework with a patch-based learning and evaluation strategy specifically designed to improve the detection of localized anomalies in industrial settings. Our approach demonstrates significantly faster convergence and improved performance compared to standard contrastive methods. It matches or surpasses anomaly detection methods on both semantic and industrial benchmarks, including methods that rely on discriminative training or explicit anomaly labels. AUROC Area Under the ROC Curve AULC Area Under the Learning Curve FIRM Focused In-distribution Representation Modeling ID In-Distribution KDE Kernel Density Estimation OE Outlier Exposure OOD Out-of-distribution 1 Introduction Anomaly detection is crucial across numerous domains, including cybersecurity, healthcare, and autonomous systems, where identifying rare or unusual patterns is essential for ensuring robustness, data integrity, and failure prevention. This task is closely related to fields such as OOD ( OOD ) detection, novelty detection, and one-class classification (i.e., semantic anomaly detection), ll of which operate within a specific ID ( ID ) setting to identify data points that do not conform to expected patterns under the open-world assumption ( Yang et al., 2024 ) . While OOD detection typically involves distinguishing between predefined classes with labeled datasets, anomaly detection focuses on identifying deviations from a singular normal class, where minimizing intra-class variance and ensuring tightly clustered inlier representations are particularly crucial for distance-based methods to effectively separate anomalies from normal samples ( Ming et al., 2023 ) . Representation learning plays a critical role in anomaly detection, with many approaches relying on distance-based scores, such as cosine similarity, computed in the learned representation ( Ruff et al., 2018 ; Ruff et al., 2020 ; Tack et al., 2020 ; Sehwag et al., 2021 ; Sun et al., 2022 ; Reiss and Hoshen, 2023 ) . These methods assume that OOD samples are situated far from the compact clusters formed by inlier data, allowing for the classification of anomalies based on their relative positioning. However, their effectiveness is contingent on the quality of the learned embeddings ( Ming et al., 2023 ) , as compact and well-separated ID representations are essential for clear anomaly separation. A key challenge in anomaly detection is structuring learning objectives to balance inlier compactness and outlier separation. Prior work has introduced synthetic outliers to improve anomaly separation, generated through methods such as geometric transformations ( Golan and El-Yaniv, 2018 ; Hendrycks et al., 2019 ) , local perturbations ( Li et al., 2021 ; Schlüter et al., 2022 ) , external datasets ( Hendrycks et al., 2018 ) , and generative outlier synthesis Du et al. (2024) . However, existing contrastive formulations do not fully leverage these methods, as they often fail to incorporate both inlier structure and outlier diversity into the learning objective. The single-positive formulation does not effectively capture the structure of inlier data, leading to increased variance within the normal class. Naive multi-positive extensions fail to capture the diversity among outliers, weakening contrastive signals and hindering effective learning. We begin by diagnosing a fundamental weakness in existing contrastive formulations for anomaly detection, demonstrating that an effective contrastive objective must explicitly enforce three key properties: (1) compact inlier clustering, (2) strong separation between inliers and synthetic outliers, and (3) preserve diversity among synthetic outliers to maintain meaningful contrastive signals. Building on this insight, we reconceptualize how contrastive learning should be applied to anomaly detection by formulating a structured approach that directly integrates these properties. To this end, we propose FIRM ( FIRM ), a representation learning framework that enforces these essential properties to ensure more effective anomaly separation. FIRM explicitly promotes compact clustering of ID representations, reducing intra-class variance and structuring inlier embeddings for improved detection. Simultaneously, it enhances the separation between inliers and synthetic outliers while preserving the diversity among synthetic outliers, preventing representation collapse and ensuring that anomalous variations remain distinct. In summary, our contributions include: • We analyze the limitations of contrastive learning for anomaly detection, particularly the issue of class collision, and formalize key properties for effective anomaly separation. • We introduce FIRM , a contrastive learning objective that encourages inlier compactness, enhances inlier-outlier separation, and preserves synthetic outlier diversity. • We extend FIRM to a patch-based contrastive learning framework for industrial anomaly detection, leveraging foreground-aware sampling and realistic synthetic anomaly injection to enable fine-grained localization without pixel-level supervision. • We validate FIRM on both semantic and industrial anomaly detection tasks, showing substantial performance and convergence improvements. Notably, in industrial settings, our unsupervised patch-based method surpasses several pixel-level supervised approaches, despite requiring no localization labels. The remainder of this paper is organized as follows: Section 2 reviews relevant literature on contrastive learning and anomaly detection. Section 3 introduces the formal problem and examines challenges in existing contrastive methods. In Section 4 , we present our proposed contrastive objective, followed by Section 5 , which describes our patch-based learning strategy for industrial anomalies. Finally, Section 6 reports experimental results across multiple benchmarks. 2 Related Work Self-supervised learning. Self-supervised learning ( Gidaris et al., 2018 ; Hendrycks et al., 2019 ) , particularly contrastive learning ( Oord et al., 2018 ) via instance discrimination ( Wu et al., 2018 ) , has been widely adopted for learning representations without labeled data ( Chen et al., 2020a ; He et al., 2020 ; Chen et al., 2020b ; Chen et al., 2021 ) . Contrastive objectives like InfoNCE ( Oord et al., 2018 ) and NT-Xent ( Chen et al., 2020a ) align positives while separating negatives through self-labeling. SupCon ( Khosla et al., 2020 ; Tian et al., 2024 ) extends this by leveraging labels to cluster positives more tightly and enhance class separation. Representation learning for anomaly detection. The limitations of classical anomaly detection approaches have driven significant interest in developing deep learning methods for anomaly detection and related tasks ( Yi and Yoon, 2021 ; Li et al., 2021 ; Zavrtanik et al., 2021 ; Roth et al., 2022 ; Zhang et al., 2023b ; Zhang et al., 2023a ) . Most of these approaches operate in an unsupervised setting ( Ruff et al., 2018 ; Golan and El-Yaniv, 2018 ) . However, incorporating additional data as negatives can significantly enhance performance ( Hendrycks et al., 2019 ; Hendrycks et al., 2018 ; Du et al., 2022 ; Du et al., 2024 ) . Recent works have explored the application of contrastive learning for anomaly detection ( Sohn et al., 2021 ) , particularly in OOD detection ( Tack et al., 2020 ; Sehwag et