AnomalyDiffusion: Few-Shot Anomaly Image Generation with Diffusion Model

Teng Hu, Jiangning Zhang, Ran Yi, Yuzhen Du, Xu Chen, Liang Liu, Yabiao Wang, Chengjie Wang

In industrial manufacturing, automated anomaly inspection is critical for quality control. However, deep learning models for this task are often bottlenecked by the extreme scarcity of anomalous data. While factories produce millions of normal samples, defects are rare occurrences. Researchers from Shanghai Jiao Tong University and Tencent's Youtu Lab have developed AnomalyDiffusion, a framework that leverages Latent Diffusion Models (LDMs) to synthesize high-quality, perfectly aligned anomaly data from just a few real-world examples.

The Challenge of Industrial Data Scarcity

Industrial anomaly inspection encompasses detection, localization, and classification. The primary hurdle is the lack of diverse training data for rare defects. Traditional "model-free" methods, such as "Crop&Paste," often result in unrealistic images that lack physical consistency. Meanwhile, GAN-based generative methods frequently struggle to maintain precise alignment between the generated defect and its corresponding ground-truth mask, which is essential for training accurate localization models.

Figure 1: Overview of AnomalyDiffusion's role in the industrial pipeline and qualitative comparison with previous methods like DRAEM and DFMGAN.

Technical Innovation: SAE and AAR

AnomalyDiffusion builds upon pre-trained LDMs, introducing two key innovations to meet the specific requirements of industrial defect generation:

  1. Spatial Anomaly Embedding (SAE): To allow for flexible data augmentation, the model disentangles defect appearance from its location. It uses a learned Anomaly Embedding ($e_a$) to capture the visual essence of a defect (e.g., a crack) via masked textual inversion, and a Spatial Embedding ($e_s$) encoded from an input mask to define exactly where the defect should appear. This enables the generation of any learned defect type in any specified location.
  2. Adaptive Attention Re-weighting (AAR): Diffusion models can sometimes produce faint anomalies or fail to fill the entire target mask. AAR acts as a dynamic feedback loop during the denoising process. It compares the current generation estimate with the original normal image; if the difference in the masked area is insufficient, it boosts the attention weights for those pixels, forcing the model to generate a more prominent and mask-aligned defect.
Figure 2: The overall framework of AnomalyDiffusion, detailing the Spatial Anomaly Embedding (SAE) and Adaptive Attention Re-weighting (AAR) mechanisms.

Performance and Results

The model was evaluated using the MVTec AD dataset, a benchmark for industrial anomaly detection. AnomalyDiffusion achieved superior results in both image authenticity and downstream utility:

  • High Fidelity: The model produced more realistic and diverse anomalies compared to GANs and heuristic augmentation methods.
  • Downstream Utility: Training a simple U-Net on data generated by AnomalyDiffusion yielded a pixel-level 99.1% AUROC and 81.4% Average Precision (AP) on MVTec. This performance exceeds many specialized unsupervised and few-shot models.
  • Versatility: The framework successfully handles both structural anomalies (e.g., squeezed components) and texture-based defects (e.g., surface cracks) with high precision.

Conclusion

AnomalyDiffusion provides a robust solution to the data scarcity problem in industrial AI. By disentangling appearance from location and ensuring strict mask alignment through adaptive attention, it enables the generation of high-quality synthetic datasets that significantly enhance the performance of anomaly detection and localization systems. Future work may explore zero-shot generation and extending the framework to 3D and video data.

Summary updated

BibTeX
@inproceedings{hu2024anomalydiffusion,
  author = {Teng Hu and Jiangning Zhang and Ran Yi and Yuzhen Du and Xu Chen and Liang Liu and Yabiao Wang and Chengjie Wang},
  title = {{AnomalyDiffusion: Few-Shot Anomaly Image Generation with Diffusion Model}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2024},
  doi = {10.1609/aaai.v38i8.28696},
}
Figure