Overview
Therun_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
Path where training results, models, and evaluation metrics will be saved
Options
GPU device ID(s) to use for training. Can specify multiple GPUs by repeating the flag.
Random seed for reproducibility across training runs
Logging group name for organizing experiment results
Logging project name for organizing experiment results
Save visualization images showing anomaly segmentation results. Useful for qualitative analysis.
Save trained PatchCore model to disk for later evaluation or deployment
Subcommand: patch_core
Configures the PatchCore model architecture and training parameters.Backbone Options
Short flag:
-bPretrained backbone network(s) to extract features. Supports multiple backbones for ensemble models.Supported backbones:wideresnet50- Wide ResNet-50wideresnet101- Wide ResNet-101resnext101- ResNeXt-101densenet201- DenseNet-201
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
Dimensionality of pretrained backbone features before projection
Target dimensionality after projection. Lower values reduce memory usage.
Processing Options
Feature preprocessing methodChoices:
mean, convFeature aggregation methodChoices:
mean, mlpPatch Parameters
Size of the neighborhood patch for anomaly scoring. Larger values provide more spatial context.
- Detection tasks: typically
3 - Segmentation tasks: typically
5
Method for computing patch-level anomaly scores
Overlap ratio between adjacent patches (0.0 = no overlap, 0.5 = 50% overlap)
Short flag:
-paAdditional patch sizes for multi-scale aggregationAnomaly Scoring
Number of nearest neighbors (k-NN) used for anomaly scoring
- Detection: typically
1 - Segmentation: typically
3or5
FAISS Options
Use GPU-accelerated FAISS for nearest neighbor search. Significantly speeds up inference.
Number of CPU workers for FAISS operations
Subcommand: sampler
Configures coreset sampling strategy for reducing memory bank size.Arguments
Sampler type to useOptions:
identity- Use all training features (no sampling)greedy_coreset- Greedy coreset selectionapprox_greedy_coreset- Approximate greedy coreset (faster)
Options
Short flag:
-pPercentage of training features to retain in the memory bank0.1= 10% (faster, less memory)0.01= 1% (even faster, minimal accuracy loss)
Subcommand: dataset
Configures dataset loading and preprocessing.Arguments
Dataset type. Currently supported:
mvtecPath to the dataset root directory. Must exist.
Options
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, zipperFraction of training data to use for training (remainder used for validation)
1.0= use all training data0.8= 80% train, 20% validation
Batch size for data loading
Number of worker processes for data loading
Image resize dimension (before center cropping)
Final image size after center cropping
224- Standard ImageNet size320- Higher resolution for better segmentation
Apply data augmentation during training
Examples
Basic Training (Single Dataset)
Baseline Detection Model (IM224)
Wide ResNet-50, 10% coreset, optimized for detection:Ensemble Model (IM224)
Multiple backbones for improved accuracy:High-Resolution Segmentation (IM320)
Optimized for pixel-level anomaly segmentation: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.