Spatial Contrastive Learning for Anomaly Detection and Localization

Daehwan Kim, Daun Jeong, Hyungmin Kim, Kibong Chong, Seungryong Kim, Hansang ChoIEEE Access 2022

Abstract

With the development of deep learning, abnormal detection methods have been widely presented to improve performances in various applications, including visual inspection systems. However, there remains difficult to be directly applied to real-world applications, which often include the lack of abnormal samples and diversity. This paper proposes contra embedding that adopts progressive autoencoder with contrastive learning to address these difficulties. The autoencoder is trained progressively to reproduce the details of the original images, and modified CutPaste augmentation helps to learn to recover normal images. Especially, contrastive learning based on normal embedding vectors effectively reduces false positives caused by the autoencoder. The proposed method is also helpful when normal data have complex shapes, sizes, and colors. In experiments, MVTec AD dataset is used to show the generalization ability of the proposed method in various real-world applications. It achieves over 98.0% AUROCs in detection and 97.7% AUROCs in the localization, respectively, without using the ImageNet pre-trained model as in previous methods.

Introduction

Unsupervised anomaly detection (UAD) is a critical component of modern visual inspection systems, where normal samples are plentiful but defects are rare and unpredictable. Existing methods typically fall into two categories: reconstruction-based models (like Autoencoders) and embedding-similarity-based models.

However, both approaches face significant hurdles. Autoencoders often suffer from "over-detection," where they fail to reconstruct high-frequency details of normal textures, leading to false positives. Conversely, embedding methods frequently rely on models pre-trained on external datasets like ImageNet, which may not generalize well to specific industrial contexts. This research introduces "contra embedding," a novel method that combines a Progressive Autoencoder (PAE) with patch-level spatial contrastive learning, trained entirely from scratch on the target dataset.

Methodology

The proposed architecture utilizes two networks to extract and refine features for robust anomaly detection.

Progressive Autoencoder (PAE) and M-CutPaste

The PAE is trained progressively from low to high resolutions, improving the reconstruction of fine details. To ensure the model learns a strong "normalcy" prior, the authors introduce Modified CutPaste (M-CutPaste). This augmentation pastes synthetic, anomalous patches onto inputs but requires the PAE to reconstruct the original clean image. This process effectively teaches the network to denoise and ignore anomalies rather than memorizing them.

Spatial Contrastive Learning

To mitigate the false positives common in reconstruction-based models, a separate feature extractor is trained using patch-level contrastive learning. A unique aspect of this approach is the pairing strategy: the positive pair consists of an input image patch and its corresponding reconstructed patch from the PAE. Negative pairs are drawn from different spatial locations. This forces the feature extractor to focus on semantic and structural similarities rather than pixel-level noise.

Overall architecture of the proposed contra embedding method, combining Progressive Autoencoder (PAE) and spatial contrastive learning.

Inference via Multiplicative Masking

During inference, the final anomaly map ($M$) is generated by pixel-wise multiplication of the Structural Similarity Index (SSIM) map ($M_{SSIM}$) from the PAE and the feature distance map ($M_{con}$) from the contrastive network. This multiplication acts as a filter: the contrastive map suppresses the false positives generated by the autoencoder's reconstruction errors in complex normal textures.

Results and Discussion

The method was evaluated on the MVTec AD dataset, the standard benchmark for industrial anomaly detection, and a "non-aligned" version to test robustness against rotation and translation.

Visual comparison of anomaly detection and localization results. The final anomaly map combines the strengths of the progressive autoencoder and contrastive learning.

The proposed method achieved state-of-the-art performance for models trained without external data, reaching 98.0% AUROC for detection and 97.7% AUROC for localization. It outperformed existing methods like Patch-SVDD and CutPaste and remained highly competitive with models utilizing ImageNet pre-training (e.g., PaDiM). Notably, the system demonstrated extreme robustness on non-aligned data, maintaining high localization accuracy despite random rotations and crops, making it suitable for real-world manufacturing environments.

Conclusion

Contra embedding effectively addresses the "over-detection" problem in unsupervised anomaly detection by integrating reconstruction and contrastive learning. While the method excels in most categories, it faces challenges with naturally high-variance textures, such as wood grain or bristles, where normal variations can be mistaken for anomalies. Future work will focus on refining patch-level feature extraction to better handle these naturally noisy textures.

Summary updated

BibTeX
@inproceedings{kim2022spatial,
  author = {Daehwan Kim and Daun Jeong and Hyungmin Kim and Kibong Chong and Seungryong Kim and Hansang Cho},
  title = {{Spatial Contrastive Learning for Anomaly Detection and Localization}},
  booktitle = {IEEE Access},
  year = {2022},
  url = {https://openreview.net/forum?id=l6kwMTMKLfV},
}
Figure