Patch-aware Vector Quantized Codebook Learning for Unsupervised Visual Defect Detection

Qisen Cheng, Shuhui Qu, Janghwan Lee · DOI

Abstract

In unsupervised visual defect detection, models are typically trained on normal data to identify anomalies as deviations from the norm. However, standard Autoencoders face a "Goldilocks" dilemma: insufficient representation capacity leads to poor reconstruction of normal samples, while excessive capacity allows the model to reconstruct defects, a phenomenon known as "mode collapse." We introduce PVQAE (Patch-aware Vector Quantized Autoencoder), which dynamically allocates representation capacity. By using high-resolution codes for complex regions and low-resolution codes for simple ones, PVQAE effectively flags defects that cannot be reconstructed within the learned "budget." Our approach achieves state-of-the-art performance on major industrial benchmarks.

The Problem: The Representation Capacity Dilemma

Unsupervised defect detection relies on the model's ability to reconstruct normal images while failing to reconstruct anomalous ones. This creates a significant challenge in balancing representation capacity. If the capacity is too low, the model cannot accurately represent normal samples, leading to false positives. Conversely, if the capacity is too high, the model becomes too proficient at reconstructing everything, including defects, making them invisible during inference.

The Goldilocks Dilemma: Standard vector quantization vs. the proposed dynamic approach.

Existing methods often apply a uniform resolution across the entire image, which is inefficient. For instance, a smooth surface requires very little data to describe, whereas a complex texture or logo needs significantly more.

Proposed Method: PVQAE

PVQAE extends the Vector Quantized Variational Autoencoder (VQ-VAE) framework with three novel components designed to optimize capacity allocation:

  • Multi-resolution Dynamic Code Allocation: Instead of a fixed grid, a Dynamic Routing Module selects the optimal resolution for each image patch from a hierarchy of candidates. It assigns "fine" codes to information-rich areas and "coarse" codes to simple regions.
  • Progressive Budget Learning: To prevent the model from defaulting to high resolution everywhere, a budget loss is introduced. Using Discrete Wavelet Transform (DWT) entropy to estimate "context richness," the model is penalized more heavily for using high-resolution codes on simple patches.
  • Normal Budget Priors: The model learns the spatial pattern of code usage. A Budget Prior Transformer predicts the expected resolution for each patch. During inference, if a patch requires a budget significantly different from the Transformer's prediction, it serves as a strong indicator of a defect.
Overview of the PVQAE architecture, featuring Patch-aware Codebook Learning and Normal Budget Prior Learning.

Key Insights

A central innovation of PVQAE is treating the "budget"—the amount of information used to describe a patch—as an anomaly indicator. If a normally smooth area suddenly requires a high-resolution code for reconstruction, it likely indicates the presence of a defect like a crack or scratch. By forcing the model to be economical during training, it loses the ability to reconstruct anomalous details, thereby increasing reconstruction error and improving defect visibility.

Results and Evaluation

The model was evaluated on three standard industrial benchmarks: MVTecAD, BTAD (Bean Tech Anomaly Dataset), and MTSD (Magnetic Tile Surface Defect). PVQAE achieved state-of-the-art or competitive results across all datasets, outperforming previous VQ-based methods. Qualitative visualizations demonstrate that PVQAE produces cleaner heatmaps highly concentrated on actual defects compared to standard VQ-VAEs.

Ablation studies revealed that a linear schedule for the budget weight ($\lambda$) is most effective. Starting at zero allows the model to learn basic reconstruction before gradually increasing the pressure for efficiency.

Conclusion and Future Work

PVQAE introduces a dynamic and efficient way to handle representation capacity in industrial anomaly detection. While the multi-stage architecture adds some complexity, the performance gains and improved interpretability of defect heatmaps are significant. Future work may focus on streamlining the architecture for real-time constraints and exploring its effectiveness on smaller datasets.

Summary updated

BibTeX
@inproceedings{cheng2024patch,
  author = {Qisen Cheng and Shuhui Qu and Janghwan Lee},
  title = {{Patch-aware Vector Quantized Codebook Learning for Unsupervised Visual Defect Detection}},
  booktitle = {ICTAI},
  year = {2024},
  doi = {10.1109/ictai62512.2024.00088},
}
Figure