Prototypical Learning Guided Context-Aware Segmentation Network for Few-Shot Anomaly Detection

Yuxin Jiang, Yunkang Cao, Weiming Shen

TL;DR

Few-shot anomaly detection (FSAD) often suffers from a "domain gap" when general pre-trained models are applied to niche industrial inspection tasks. This paper introduces PCSNet, a framework that uses prototypical learning and synthetic anomalies to adapt pre-trained features into a compact, highly discriminative space. This approach significantly boosts both anomaly detection and pixel-level defect localization using as few as 2 to 8 normal training samples.

What Problem Does This Paper Address?

In modern manufacturing, automated visual anomaly detection is crucial for quality control. While unsupervised methods have advanced significantly, they typically require hundreds of normal images to model a clean distribution. In many real-world scenarios, collecting this much data is impractical, giving rise to Few-Shot Anomaly Detection (FSAD).

Current FSAD methods rely heavily on feature extractors pre-trained on massive datasets like ImageNet. However, a massive domain gap exists between natural images (like cats and dogs) and specialized industrial parts (like specific screws or car components). With only a handful of normal samples available during few-shot training, the pre-trained feature space remains scattered and loose. During testing, this lack of compactness causes normal and abnormal features to overlap, leading to missed defects or false alarms.

Methods

The authors propose the Prototypical Learning Guided Context-Aware Segmentation Network (PCSNet), which bridges the domain gap by splitting the task into two specialized sub-networks:

  1. Prototypical Feature Adaption (PFA) sub-network: Instead of directly using raw pre-trained features, PFA passes them through a lightweight adapter to map them into a compact subspace. To train this adapter without real anomaly data, the framework generates synthetic anomalies using the Natural Synthetic Anomalies (NSA) method. PFA is optimized using three distinct losses:
  2. Normal Features Compacting (NFC) loss: Pulls normal features tightly around calculated prototypical centers.
  3. Abnormal Features Separation (AFS) loss: Pushes synthetic anomalies safely outside the normal boundary margin.
  4. Pixel-level Disparity Classification (PDC) loss: Specifically targets "hard samples" (subtle anomalies that look normal) by isolating the top-$P$ pixels with the highest deviation from the prototypes and optimizing them via an image-level classification objective.
  5. Context-Aware Segmentation (CAS) sub-network: An FPN-like (Feature Pyramid Network) architecture that aggregates multi-scale features and combines them with pixel-wise similarity maps to output precise, fine-grained anomaly localization masks.
Fig. 2. The diagram of the proposed PCSNet model. Firstly, the PFA sub-network extracts discriminative features with three loss functions. Subsequently, the CAS sub-network performs anomaly localization by concatenating the extracted features and similarity maps.

Datasets

The model was evaluated on three distinct datasets:

  • MVTec AD: The gold-standard benchmark for industrial anomaly detection, containing 15 categories of textures and objects.
  • MPDD (Metal Part Defect Detection): A challenging dataset focusing on painted metal parts under variable lighting, orientations, and backgrounds.
  • APPD (Automotive Plastic Parts Dataset): An in-house dataset introduced by the authors to evaluate real-world performance. It contains 4,082 normal training images and a test set with 1,749 normal and 69 abnormal images captured from multiple angles to catch subtle defects.

Key Insights and Ideas

  • Negative Space Engineering via Synthetic Anomalies: Relying solely on normal data makes it difficult to define a sharp decision boundary in a few-shot regime. By introducing synthetic anomalies during training, the model learns exactly what not to be, compressing the normal feature space far more effectively.
  • Focusing on the "Hard Pixels": Subtle defects can easily blend into normal variations. The PDC loss acts like a magnifying glass, forcing the network to focus on the small subset of pixels showing the highest deviation rather than averaging over the whole image.
  • Lightweight Feature Adaptation: Instead of freezing the pre-trained network entirely or fine-tuning the whole model (which risks catastrophic overfitting), adding a lightweight adapter guided by prototypical centers safely bridges the domain gap.

Key Results

  • State-of-the-Art Performance: In an 8-shot setup, PCSNet achieved an image-level AUROC of 94.9% on MVTec AD and 80.2% on MPDD, outperforming competitive baselines like PatchCore and RegAD.
  • High-Precision Localization: Pixel-level AUROC reached 97.1% on MVTec and 96.2% on MPDD, showing excellent performance on both large and tiny defects.
  • Real-World Deployment Ready: On the complex, multi-angle APPD dataset, PCSNet achieved a 92.8% PRO (Per-Region Overlap) score under a 10-shot condition.
  • Efficiency: PCSNet clocked an inference speed of 9.29 FPS, making it 1.9$\times$ faster than CFA and 3.2$\times$ faster than PatchCore, while maintaining a significantly smaller memory footprint than gallery-based methods like SPADE.

Limitations and Future Work

The authors note that the current information exchange between the PFA and CAS sub-networks is quite basic, relying on simple feature concatenation. They suggest that future work should explore more sophisticated feature fusion mechanisms, such as cross-attention layers, to better synergize feature adaptation and segmentation.

Summary updated

BibTeX
@article{jiang2025prototypical,
  author = {Yuxin Jiang and Yunkang Cao and Weiming Shen},
  title = {{Prototypical Learning Guided Context-Aware Segmentation Network for Few-Shot Anomaly Detection}},
  journal = {IEEE Trans. Neural Networks Learn. Syst.},
  year = {2025},
  doi = {10.1109/tnnls.2024.3463495},
}
Figure