Skip to main content
PatchCore is a state-of-the-art anomaly detection method that achieves up to 99.6% image-level AUROC and 98.4% pixel-level localization AUROC on industrial inspection tasks. It works by building a memory bank of patch-level features from normal training images and using nearest neighbor search to identify anomalies.

Algorithm Overview

The PatchCore algorithm operates in two distinct phases:
1

Training Phase

Extract patch features from normal images, apply coreset subsampling, and build a memory bank
2

Inference Phase

Compare test image patches to the memory bank using nearest neighbor search to compute anomaly scores

Architecture

PatchCore Architecture The PatchCore pipeline consists of several key components:
  1. Pretrained Backbone: Extracts multi-scale features (typically WideResNet50)
  2. Patch-level Aggregation: Converts feature maps into locally aware patch representations
  3. Coreset Subsampling: Reduces memory bank size while preserving diversity
  4. Nearest Neighbor Search: Uses FAISS for efficient similarity matching

Core Class Structure

The main PatchCore class inherits from torch.nn.Module and manages the entire pipeline:
patchcore.py

Key Methods

Configures the PatchCore model with backbone network, feature extraction layers, and scoring parameters.
patchcore.py
Key Parameters:
  • 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
Computes embeddings from training data and fills the memory bank.
patchcore.py
The training process:
  1. Extracts features from all normal training images
  2. Applies coreset subsampling to reduce memory footprint
  3. Builds FAISS index for fast nearest neighbor search
Computes anomaly scores and segmentation masks for test images.
patchcore.py

PatchMaker: Local Aggregation

The PatchMaker 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:
Using --faiss_on_gpu significantly accelerates nearest neighbor search, especially for large memory banks.

Performance Characteristics

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