PatchCore

On this page 7

PatchCore is an unsupervised visual anomaly detection method that stores features of small image regions from defect-free training images in a coreset memory bank and scores a new image by the distance of its patches to their nearest normal neighbors. In anomaly detection for industrial inspection, it became a reference method because it reaches very high accuracy while training only on normal samples.

Introduced in 2022 by Roth and colleagues under the title “Towards Total Recall in Industrial Anomaly Detection”, PatchCore requires no network training at all. It reuses a convolutional backbone pretrained on ImageNet, and its main contribution is a careful choice of mid-level features, local neighborhood aggregation and coreset subsampling that keeps the memory bank small enough for practical use.

How PatchCore builds its memory bank

PatchCore builds its model in a single pass over the normal training images, with no gradient updates. Each image goes through a frozen backbone, typically WideResNet-50, and the method extracts feature maps from the second and third stages. Earlier layers are too generic, and the deepest layers are biased toward ImageNet classes, so the intermediate layers offer the best balance between local detail and semantic context.

Every spatial position in those feature maps becomes a patch feature. Before storage, each feature is averaged with its neighbors over a small window, usually 3 by 3 positions, which enlarges the receptive field without losing resolution. The features from both layers are upsampled to a common size, concatenated and projected to a fixed dimension.

The resulting pipeline consists of a few distinct stages, summarized in the following table.

StageWhat happensTypical setting
Feature extractionFrozen CNN processes each normal imageWideResNet-50, layers 2 and 3
Local aggregationNeighboring features are averaged3 by 3 patch window
Memory bankAll patch features are collectedTens of millions before reduction
Coreset subsamplingRepresentative subset is selected1 to 10 percent of patches
ScoringNearest-neighbor distance per test patchEuclidean distance, k equal to 1
LocalizationPatch scores form an anomaly mapUpsampled and Gaussian-smoothed
Stages of the PatchCore pipeline

Coreset subsampling and anomaly scoring

Storing every patch from hundreds of images would make nearest-neighbor search slow and memory-hungry. PatchCore therefore applies greedy coreset selection: it repeatedly adds the patch that is farthest from the patches already chosen, an approximation of the minimax facility location problem. The selected subset covers the feature space almost as well as the full bank, often at 1 percent of its size.

At test time, every patch of the new image is compared with its nearest neighbor in the coreset. The image-level anomaly score is the largest patch distance, reweighted by how isolated that nearest neighbor is within the memory bank. The patch-level distances, reshaped to the feature map grid and upsampled, give a pixel-level anomaly map that localizes the defect.

The sampling ratio is the main lever between accuracy and cost, as the approximate figures below illustrate for a typical industrial category.

Sampling ratioMemory bank sizeSearch speedDetection accuracy
100 percentFull set of patch featuresSlowestReference level
10 percentModerateFastPractically identical
1 percentSmallVery fastSlight drop on some categories
0.1 percentVery smallFastestNoticeable drop on textured parts
Effect of the coreset sampling ratio

PatchCore compared with other visual anomaly detectors

PatchCore belongs to the family of feature embedding methods, which compare pretrained features instead of learning to reconstruct images. On the MVTec AD benchmark, it reported an image-level AUROC of about 99.1 percent, which at the time of publication placed it at or near the top of published results.

MethodModel of normalityTraining effortTypical weakness
PatchCoreCoreset of patch features, nearest neighborNone beyond feature extractionMemory and search cost at scale
PaDiMGaussian per patch positionCovariance estimationAssumes aligned images
SPADEk-NN over image and pixel featuresNoneSlow with large banks
AutoencoderReconstruction of normal imagesFull network trainingReconstructs some defects too well
Student-teacher methodsFeature regression mismatchTraining a student networkLonger setup and tuning
PatchCore and related approaches to visual anomaly detection

Compared with PaDiM, PatchCore does not assume that a given pixel position always shows the same part of the object, so it is more tolerant of misalignment. Compared with reconstruction-based detection, it avoids the problem that a capable autoencoder may reproduce anomalies faithfully and thus hide them.

Practical deployment and tuning

In production, PatchCore is usually trained with 50 to 300 defect-free images per product variant, which is attractive in factories where defects are rare and labeling is expensive. Nearest-neighbor search runs on a GPU or with an approximate index library, and inference typically takes tens of milliseconds per image on current hardware.

The decision threshold on the anomaly score is the step that most often goes wrong. It must be calibrated on a validation set with known defects or at least on held-out normal images, because the raw distance scale differs between products and lighting setups.

Limitations and failure modes

PatchCore is strong on local structural defects, but its design leaves known gaps. The most important ones are listed below with their causes.

  • Logical anomalies such as a missing or misplaced component can pass, because each patch still looks normal on its own.
  • Domain shift in lighting, camera position or background raises scores on good parts and causes false alarms.
  • Contaminated training data with even a few defective images teaches the memory bank to accept those defects.
  • Very small defects below the feature map resolution may be averaged away by downsampling.
  • Memory growth with many product variants requires a separate bank per variant or careful sharing.

In simple terms

PatchCore learns what a good product looks like by studying many photos of good products. It cuts each photo into small pieces and remembers a careful selection of them.

When a new photo arrives, it checks every small piece. Does this piece look like something it has seen before? If a piece looks unfamiliar, that spot gets a high score. High scores point to likely defects, and the method can show exactly where they are. It is like an experienced inspector who has memorized thousands of good surfaces and notices anything new.

A practical example

A manufacturer of machined aluminum housings inspects every part with a fixed camera at the end of the line. Defects such as scratches, dents and burrs occur in fewer than one part in 500, so collecting enough labeled defect images for a supervised classifier would take months.

The quality team photographs 200 good housings, builds a PatchCore model with a 10 percent coreset and sets the threshold so that no known defect from a small validation set is missed. In operation, the system flags about 2 percent of parts for manual review, and inspectors confirm that the anomaly maps point directly to the scratch or dent in almost every flagged case. The line keeps its cycle time, and manual inspection effort falls substantially.