Unsupervised Anomaly Detection via Masked Diffusion Posterior Sampling

Di Wu, Shicai Fan, Xue Zhou, Li Yu, Yuzhong Deng, Jianxiao Zou, Baihong Lin

Abstract

Unsupervised Anomaly Detection (UAD) is a cornerstone of industrial quality control, yet traditional reconstruction-based methods often struggle with blurry outputs or "identical shortcuts" that fail to isolate anomalies. This paper introduces Masked Diffusion Posterior Sampling (MDPS), a framework that conceptualizes anomalies as noisy observations of a normal image distribution. By integrating a masked observation model with a diffusion-based prior, MDPS reconstructs high-fidelity normal versions of test images while preserving healthy textures. Experimental results on MVTec AD and BTAD benchmarks demonstrate that MDPS achieves state-of-the-art performance, significantly outperforming existing generative and diffusion-based approaches.

Introduction

In industrial settings, "normal" data is abundant, while "anomalous" samples—representing defects like scratches, cracks, or missing components—are rare and unpredictable. Unsupervised Anomaly Detection (UAD) addresses this by learning the distribution of normal data and identifying deviations during inference.

Current reconstruction-based methods face significant hurdles. Autoencoders (AEs) often produce blurry reconstructions or inadvertently learn to reconstruct the anomalies they are meant to detect. Generative Adversarial Networks (GANs) suffer from training instability and mode collapse. While Denoising Diffusion Models (DMs) offer high-quality synthesis, their standard application involves global Gaussian noise, which can destroy fine, healthy textures and lead to high false-positive rates in normal regions.

MDPS addresses these limitations by treating anomalies as localized noise. It leverages the generative power of diffusion models while using a masking mechanism to protect known normal regions, ensuring that only suspected anomalous areas are "healed" or reconstructed.

Methodology: Masked Diffusion Posterior Sampling

The MDPS framework operates under a Bayesian paradigm, treating the test image $y$ as a combination of a clean normal region and a "noisy" anomalous region.

Masked Noisy Observation Model

Unlike standard diffusion processes that add noise to the entire image, MDPS uses a mask $m$ to differentiate between pixels. The model protects known normal pixels while allowing the diffusion process to denoise the suspected anomalous pixels. This spatial guidance prevents the degradation of healthy textures that typically occurs in global diffusion-based UAD.

Overview of the MDPS framework, illustrating the iterative denoising process and mask-guided reconstruction.

Diffusion Posterior Sampling

MDPS utilizes a Denoising Diffusion Implicit Model (DDIM) trained exclusively on normal samples as an image prior. During inference, the test image (the "observation") guides the sampling process. This ensures the reconstructed image $x_0$ remains spatially and contextually consistent with the original $y$.

Iterative Mask Generation and Scoring

Since anomaly locations are unknown a priori, MDPS employs a two-stage approach:

  1. Initial Reconstruction: A rough reconstruction is performed assuming the entire image might be anomalous ($m=1$).
  2. Mask Refinement: The initial anomaly map is thresholded to create a refined mask $m$.
  3. Final Reconstruction: The process is repeated with the refined mask to produce a high-fidelity normal version of the image.

Anomalies are finally detected by comparing the original and reconstructed images using a hybrid scoring function that combines pixel-level ($L_1$ norm) and perceptual-level (LPIPS) metrics.

Experimental Results

The authors evaluated MDPS on the MVTec AD and BTAD benchmarks, which represent diverse industrial objects and textures.

Quantitative Performance

MDPS established new state-of-the-art (SOTA) benchmarks:

  • MVTec AD: Achieved 98.8% Image-AUROC and 97.3% Pixel-AUROC, surpassing previous leaders like DRÆM and RAN.
  • BTAD: Achieved 98.4% Image-AUROC and 97.7% Pixel-AUROC.

Qualitative Comparison

Visual analysis confirms that MDPS reconstructions are significantly sharper and more faithful to original "normal" textures compared to AE or GAN-based methods. While other models often produce "washed out" or blurry results, MDPS maintains high-frequency details in healthy regions while effectively removing defects.

Qualitative comparison of MDPS against competing methods (DRÆM, AnoDDPM, AutoDDPM) across various industrial categories.

Efficiency and Insights

A key finding was the importance of the guidance scale ($\rho$). A low scale results in lost texture, while an excessively high scale causes the model to "hallucinate" anomalies. Furthermore, averaging difference maps from multiple posterior samples ($N_s = 16$) was found to significantly improve accuracy by smoothing out stochastic noise. In terms of speed, MDPS is notably more efficient than prior diffusion-based UAD methods, processing images in approximately 0.5 seconds compared to over 30 seconds for methods like AutoDDPM.

Conclusion and Future Work

MDPS demonstrates that treating anomalies as localized noise within a masked diffusion framework provides a robust and mathematically grounded solution for UAD. By preserving healthy textures and leveraging powerful diffusion priors, it overcomes the primary weaknesses of earlier reconstruction-based models.

Future research will focus on reducing the computational overhead of diffusion sampling to meet the demands of ultra-high-speed manufacturing. The authors suggest that knowledge distillation—transferring the capabilities of MDPS into a faster, single-pass neural network—is a promising path forward.

Summary updated

BibTeX
@inproceedings{wu2024unsupervised,
  author = {Di Wu and Shicai Fan and Xue Zhou and Li Yu and Yuzhong Deng and Jianxiao Zou and Baihong Lin},
  title = {{Unsupervised Anomaly Detection via Masked Diffusion Posterior Sampling}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2024},
}
Figure