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
TheNetworkFeatureAggregator 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
TheMeanMapper module performs adaptive pooling to normalize feature dimensions:
common.py
Preprocessing Module
AppliesMeanMapper to each layer’s features:
common.py
Aggregator: Final Feature Fusion
TheAggregator combines features from all layers into a single vector:
common.py
Supported Backbones
PatchCore supports a wide variety of pretrained backbones:- ResNet Family
- Vision Transformers
- EfficientNet
- Other Architectures
resnet50: Standard ResNet-50resnet101: Deeper ResNet-101resnext101: ResNeXt-101 with grouped convolutionswideresnet50: Recommended - WideResNet-50 (default)wideresnet101: WideResNet-101
backbones.py module:
backbones.py
Layer Selection Strategy
Choosing the right layers is crucial for performance:Early Layers (e.g., layer1)
Early Layers (e.g., layer1)
- Capture low-level features (edges, textures)
- Higher spatial resolution
- Less semantic information
- Best for: Texture-based defects
Middle Layers (e.g., layer2, layer3)
Middle Layers (e.g., layer2, layer3)
- Recommended default:
layer2+layer3 - Balance between semantic and spatial information
- Good generalization across defect types
- Best for: Most industrial inspection tasks
Deep Layers (e.g., layer4)
Deep Layers (e.g., layer4)
- 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:Next Steps
Coreset Sampling
Learn how extracted features are subsampled for efficiency
Anomaly Scoring
Understand how features are used to compute anomaly scores
