Skip to main content

Overview

The run_patchcore.py script is the primary CLI tool for training PatchCore models on industrial anomaly detection datasets. It uses a command-chaining architecture where you specify a main command followed by three required subcommands: patch_core, sampler, and dataset.

Command Structure

Main Command

Arguments

string
required
Path where training results, models, and evaluation metrics will be saved

Options

int
default:"0"
GPU device ID(s) to use for training. Can specify multiple GPUs by repeating the flag.
int
default:"0"
Random seed for reproducibility across training runs
string
default:"group"
Logging group name for organizing experiment results
string
default:"project"
Logging project name for organizing experiment results
flag
Save visualization images showing anomaly segmentation results. Useful for qualitative analysis.
flag
Save trained PatchCore model to disk for later evaluation or deployment

Subcommand: patch_core

Configures the PatchCore model architecture and training parameters.

Backbone Options

string
Short flag: -bPretrained backbone network(s) to extract features. Supports multiple backbones for ensemble models.Supported backbones:
  • wideresnet50 - Wide ResNet-50
  • wideresnet101 - Wide ResNet-101
  • resnext101 - ResNeXt-101
  • densenet201 - DenseNet-201
string
Short flag: -leLayer names from which to extract feature embeddings. For ensemble models with multiple backbones, prefix with backbone index (e.g., 0.layer2, 1.layer2).

Embedding Dimensions

int
default:"1024"
Dimensionality of pretrained backbone features before projection
int
default:"1024"
Target dimensionality after projection. Lower values reduce memory usage.

Processing Options

choice
default:"mean"
Feature preprocessing methodChoices: mean, conv
choice
default:"mean"
Feature aggregation methodChoices: mean, mlp

Patch Parameters

int
default:"3"
Size of the neighborhood patch for anomaly scoring. Larger values provide more spatial context.
  • Detection tasks: typically 3
  • Segmentation tasks: typically 5
string
default:"max"
Method for computing patch-level anomaly scores
float
default:"0.0"
Overlap ratio between adjacent patches (0.0 = no overlap, 0.5 = 50% overlap)
int
Short flag: -paAdditional patch sizes for multi-scale aggregation

Anomaly Scoring

int
default:"5"
Number of nearest neighbors (k-NN) used for anomaly scoring
  • Detection: typically 1
  • Segmentation: typically 3 or 5

FAISS Options

flag
Use GPU-accelerated FAISS for nearest neighbor search. Significantly speeds up inference.
int
default:"8"
Number of CPU workers for FAISS operations

Subcommand: sampler

Configures coreset sampling strategy for reducing memory bank size.

Arguments

string
required
Sampler type to useOptions:
  • identity - Use all training features (no sampling)
  • greedy_coreset - Greedy coreset selection
  • approx_greedy_coreset - Approximate greedy coreset (faster)

Options

float
default:"0.1"
Short flag: -pPercentage of training features to retain in the memory bank
  • 0.1 = 10% (faster, less memory)
  • 0.01 = 1% (even faster, minimal accuracy loss)

Subcommand: dataset

Configures dataset loading and preprocessing.

Arguments

string
required
Dataset type. Currently supported: mvtec
path
required
Path to the dataset root directory. Must exist.

Options

string
required
Short flag: -dDataset categories to train on. For MVTec AD, this includes:bottle, cable, capsule, carpet, grid, hazelnut, leather, metal_nut, pill, screw, tile, toothbrush, transistor, wood, zipper
float
default:"1.0"
Fraction of training data to use for training (remainder used for validation)
  • 1.0 = use all training data
  • 0.8 = 80% train, 20% validation
int
default:"2"
Batch size for data loading
int
default:"8"
Number of worker processes for data loading
int
default:"256"
Image resize dimension (before center cropping)
int
default:"224"
Final image size after center cropping
  • 224 - Standard ImageNet size
  • 320 - Higher resolution for better segmentation
flag
Apply data augmentation during training

Examples

Basic Training (Single Dataset)

Baseline Detection Model (IM224)

Wide ResNet-50, 10% coreset, optimized for detection:
Expected Performance: Instance AUROC: 0.992, Pixelwise AUROC: 0.981

Ensemble Model (IM224)

Multiple backbones for improved accuracy:
Expected Performance: Instance AUROC: 0.993, Pixelwise AUROC: 0.981

High-Resolution Segmentation (IM320)

Optimized for pixel-level anomaly segmentation:
Expected Performance: Instance AUROC: 0.99, Pixelwise AUROC: 0.984

Output Files

The script creates the following directory structure:

Metrics Computed

The script automatically computes:
  • Instance AUROC: Image-level anomaly detection accuracy
  • Full Pixel AUROC: Pixel-level segmentation accuracy (all images)
  • Anomaly Pixel AUROC: Pixel-level segmentation accuracy (anomalous images only)
All metrics are saved to results.csv with mean scores across all datasets.

Tips

For faster training: Use --faiss_on_gpu and lower coreset percentage (-p 0.01)
For better segmentation: Increase --imagesize to 320, --patchsize to 5, and --anomaly_scorer_num_nn to 3-5
For ensemble models: Use different backbones and prefix layer names with backbone index (e.g., 0.layer2, 1.layer2)