Algorithm Overview
The PatchCore algorithm operates in two distinct phases:Training Phase
Extract patch features from normal images, apply coreset subsampling, and build a memory bank
Architecture
The PatchCore pipeline consists of several key components:
- Pretrained Backbone: Extracts multi-scale features (typically WideResNet50)
- Patch-level Aggregation: Converts feature maps into locally aware patch representations
- Coreset Subsampling: Reduces memory bank size while preserving diversity
- Nearest Neighbor Search: Uses FAISS for efficient similarity matching
Core Class Structure
The mainPatchCore class inherits from torch.nn.Module and manages the entire pipeline:
patchcore.py
Key Methods
load() - Initialize the Model
load() - Initialize the Model
Configures the PatchCore model with backbone network, feature extraction layers, and scoring parameters.Key Parameters:
patchcore.py
backbone: Pretrained CNN (e.g., WideResNet50, ResNet101)layers_to_extract_from: Which layers to extract features from (e.g., [‘layer2’, ‘layer3’])patchsize: Size of local neighborhood aggregation (default: 3)anomaly_score_num_nn: Number of nearest neighbors for scoring (default: 1)featuresampler: Coreset sampling strategy
fit() - Train the Model
fit() - Train the Model
Computes embeddings from training data and fills the memory bank.The training process:
patchcore.py
- Extracts features from all normal training images
- Applies coreset subsampling to reduce memory footprint
- Builds FAISS index for fast nearest neighbor search
predict() - Detect Anomalies
predict() - Detect Anomalies
Computes anomaly scores and segmentation masks for test images.
patchcore.py
PatchMaker: Local Aggregation
ThePatchMaker class handles the conversion of feature maps into locally aggregated patches:
patchcore.py
The default
patchsize=3 with stride=1 creates overlapping patches that capture local spatial context. This is crucial for precise anomaly localization.Forward Modules Pipeline
PatchCore uses a modular forward pipeline:patchcore.py
Training Example
Here’s how to train PatchCore on MVTec AD:Performance Characteristics
| Model | Mean AUROC | Mean Seg. AUROC | Mean PRO |
|---|---|---|---|
| WR50-baseline | 99.2% | 98.1% | 94.4% |
| Ensemble | 99.6% | 98.2% | 94.9% |
PatchCore is extremely efficient - training requires only a single forward pass through normal images without any gradient computation!
Next Steps
Feature Extraction
Learn how PatchCore extracts multi-scale features
Coreset Sampling
Understand memory bank compression techniques
Anomaly Scoring
Explore nearest neighbor-based scoring
Quick Start
Start using PatchCore in your project
