PaDiM

Also known as: Patch Distribution Modeling

On this page 7

PaDiM, short for Patch Distribution Modeling, is a visual anomaly detection method that describes every image position with a multivariate Gaussian distribution of pretrained CNN features learned from normal images only, then flags patches whose features lie far from that distribution. In anomaly detection for industrial inspection, PaDiM provides both an image-level anomaly score and a pixel-level anomaly map that localizes defects.

The method was published by Defard and colleagues in 2020 and quickly became a standard baseline on the MVTec AD benchmark. It requires no neural network training: a frozen backbone extracts features, and only simple statistics are estimated, which makes PaDiM fast to set up on small sets of defect-free samples.

How PaDiM models normal image patches

PaDiM works in three stages: feature extraction, per-position distribution fitting and distance-based scoring. Each training image passes through a convolutional network pretrained on ImageNet, such as ResNet-18 or Wide ResNet-50-2. Feature maps from the first three stages are resized to a common resolution and concatenated into one embedding vector for every spatial position.

For each position across all normal training images, PaDiM estimates a mean vector and a covariance matrix. A small regularization term, typically 0.01 times the identity matrix, is added so the covariance stays invertible. The result is a grid of Gaussians, one per patch location, that captures what normal texture and structure look like at that place in the image.

The table below lists the stages of the pipeline and what each one produces.

StageOperationOutput
Feature extractionFrozen CNN, layers 1 to 3Multi-scale patch embeddings
Dimension reductionRandom selection of channelsSmaller embeddings
Distribution fittingMean and covariance per positionGrid of Gaussian models
ScoringMahalanobis distance per patchRaw anomaly map
Post-processingUpsampling and Gaussian smoothingFinal heat map and image score
Stages of the PaDiM pipeline

Scoring with the Mahalanobis distance

At test time, PaDiM computes the Mahalanobis distance between each patch embedding and the Gaussian fitted for that position. Unlike Euclidean distance, it accounts for correlations between feature channels, so deviations along rarely varying directions weigh more heavily. The distance map is upsampled to the input resolution and smoothed, typically with a Gaussian filter of sigma about 4 pixels.

The image-level score is usually the maximum value of the anomaly map. A threshold on this score separates normal from defective parts, while a threshold on the map itself produces a segmentation mask of the defect region.

Key parameters and their typical values are summarized below.

ParameterTypical valueEffect
BackboneResNet-18 or Wide ResNet-50-2Feature quality and speed
Reduced dimension100 for ResNet-18, 550 for Wide ResNet-50-2Accuracy versus memory
Input size256 pixels resized, 224 center cropSpatial detail of the map
Covariance regularization0.01 times identityNumerical stability
Smoothing sigmaAbout 4 pixelsNoise in the heat map
Main PaDiM parameters

The original study found that random channel selection performs almost as well as principal component analysis for dimension reduction, while being cheaper. This result surprised many readers and reflects how redundant pretrained features are for this task.

PaDiM belongs to the family of embedding-based visual anomaly detection methods, which rely on pretrained features instead of reconstruction. Its main distinction is the position-specific parametric model, while neighbors such as PatchCore use a non-parametric memory bank.

MethodModel of normalityMain strengthMain weakness
PaDiMOne Gaussian per patch positionSimple, fast inferenceNeeds aligned images
PatchCoreMemory bank of patch featuresHigher accuracy, position-freeNearest-neighbor search cost
SPADEk-NN over stored featuresNo distribution assumptionSlow at large scale
Autoencoder detectionReconstruction of normal imagesLearns domain-specific featuresReconstructs some defects too well
Deep SVDDHypersphere around normal embeddingsCompact modelTraining can collapse
PaDiM and neighboring approaches

On the MVTec AD benchmark, PaDiM reported about 95 percent image-level AUROC and above 97 percent pixel-level AUROC with a Wide ResNet backbone. Later methods such as PatchCore reached higher scores, but PaDiM remains a strong, interpretable baseline.

When to use PaDiM in practice

PaDiM fits inspection tasks where images are well aligned and only normal samples are easy to collect. Typical settings include fixed-camera production lines that inspect printed circuit boards, bottles, metal parts, textiles or packaging. It is implemented in open-source libraries such as Anomalib, which simplifies deployment.

SituationSuitabilityReason
Fixed camera, aligned partsWell suitedPosition-specific statistics hold
Uniform textures such as fabricWell suitedPatterns repeat across positions
Objects in random orientationsPoorly suitedPositions no longer correspond
Very limited memory on edge devicesUse with a small backboneCovariance storage grows quickly
Highest possible accuracy requiredConsider PatchCoreUsually ranks higher on benchmarks
Deciding whether PaDiM is a good fit

A few dozen to a few hundred defect-free training images are usually enough. The decision threshold should be chosen on a validation set that contains at least some real defects, because the anomaly score has no absolute meaning.

Limitations and common pitfalls

PaDiM trades flexibility for simplicity, and several weaknesses follow directly from its per-position Gaussian assumption.

  • Memory grows with resolution and embedding size, because each position stores a full covariance matrix.
  • Misaligned or rotated objects produce false alarms, since features shift to other positions.
  • Multimodal normal appearance, such as product variants, fits poorly into a single Gaussian.
  • ImageNet features may miss subtle defects in unusual domains such as X-ray or thermal images.
  • Uncalibrated thresholds lead to unstable false positive rates when lighting changes.

Careful image registration, stable lighting and periodic refitting on fresh normal samples address most of these issues in production.

The simple explanation

PaDiM learns what a good product looks like, spot by spot. It studies many photos of flawless parts. For each small area of the picture, it remembers the typical look and how much it normally varies.

When a new part arrives, it checks every small area against that memory. Areas that look far more unusual than normal light up on a heat map. The brighter the spot, the more likely a scratch, dent or stain is there.

How it is used in practice

A factory that produces machined aluminum housings inspects every part with a fixed overhead camera. Defects are rare, so collecting labeled defect images is difficult. The quality team gathers about 300 images of approved parts and fits PaDiM with a Wide ResNet-50-2 backbone.

Fitting takes only minutes on a single GPU, because the network is not trained. On a validation set with real scratches and dents, the team sets a threshold that catches nearly all visible defects while flagging only a small share of good parts for manual review.

In operation, the heat map shows inspectors exactly where each suspected defect lies, which shortens manual checks. When a new housing variant enters production, the team simply refits the model on fresh normal images.