Skip to main content
PatchCore leverages pretrained CNN backbones to extract rich, hierarchical features from images. The feature extraction process is central to the algorithm’s success, enabling it to capture both semantic and spatial information at multiple scales.

Overview

Feature extraction in PatchCore happens through several stages:
1

Backbone Feature Extraction

Pass images through a pretrained CNN and extract features from intermediate layers
2

Patch-level Aggregation

Convert feature maps into locally aggregated patch representations using unfold operations
3

Multi-scale Alignment

Resize and align features from different layers to the same spatial resolution
4

Dimensionality Reduction

Normalize and reduce feature dimensions for efficient storage and comparison

NetworkFeatureAggregator

The NetworkFeatureAggregator class efficiently extracts features from multiple backbone layers:
common.py

Forward Hook Mechanism

PatchCore uses PyTorch’s forward hooks to extract intermediate layer outputs without modifying the backbone:
common.py
The hook mechanism allows early stopping - computation halts after the last required layer is reached, saving compute time.

The _embed() Method

The core feature embedding process happens in the _embed() method:
patchcore.py

Multi-scale Feature Alignment

One of the key innovations is aligning features from different layers to the same spatial resolution:
patchcore.py
Bilinear interpolation is used to resize lower-resolution features to match the highest resolution layer. This preserves spatial correspondence across scales.

Preprocessing Pipeline

MeanMapper

The MeanMapper module performs adaptive pooling to normalize feature dimensions:
common.py

Preprocessing Module

Applies MeanMapper to each layer’s features:
common.py

Aggregator: Final Feature Fusion

The Aggregator combines features from all layers into a single vector:
common.py
The aggregator uses adaptive average pooling, which automatically handles varying input dimensions and produces a fixed-size output.

Supported Backbones

PatchCore supports a wide variety of pretrained backbones:
  • resnet50: Standard ResNet-50
  • resnet101: Deeper ResNet-101
  • resnext101: ResNeXt-101 with grouped convolutions
  • wideresnet50: Recommended - WideResNet-50 (default)
  • wideresnet101: WideResNet-101
Backbones are loaded using the backbones.py module:
backbones.py

Layer Selection Strategy

Choosing the right layers is crucial for performance:
  • Capture low-level features (edges, textures)
  • Higher spatial resolution
  • Less semantic information
  • Best for: Texture-based defects
  • Recommended default: layer2 + layer3
  • Balance between semantic and spatial information
  • Good generalization across defect types
  • Best for: Most industrial inspection tasks
  • High-level semantic features
  • Lower spatial resolution
  • Strong classification capability
  • Best for: Object-level anomalies

Feature Dimension Flow

Here’s how feature dimensions transform through the pipeline:

Complete Embedding Example

Here’s a complete example of the embedding process:
All features are extracted with torch.no_grad() - PatchCore does not perform any backpropagation during training or inference.

Next Steps

Coreset Sampling

Learn how extracted features are subsampled for efficiency

Anomaly Scoring

Understand how features are used to compute anomaly scores