Skip to main content

Overview

PatchCore training is configured through command-line arguments organized into four main groups:
  1. Global arguments: GPU, seed, logging, output paths
  2. PatchCore arguments: Backbone, layers, dimensions, patch settings
  3. Sampler arguments: Coreset sampling method and percentage
  4. Dataset arguments: Data loading, image preprocessing, categories

Global Arguments

GPU and Device Settings

int
default:"0"
required
GPU device ID(s) to use for training.
Single GPU
Multiple GPUs (not fully supported)
Location: run_patchcore.py:24
The specified GPU is used for:
  • Feature extraction from backbone
  • FAISS nearest-neighbor search (if --faiss_on_gpu enabled)
  • Coreset sampling operations
Check available GPUs with nvidia-smi.
int
default:"0"
Random seed for reproducibility.
Location: run_patchcore.py:25
Using the same seed with the same configuration should produce identical results. However, FAISS operations may have minor non-determinism.

Model Saving

flag
Save trained PatchCore model(s) to disk.
Location: run_patchcore.py:29Saves:
  • nnscorer_search_index.faiss: Nearest neighbor search index
  • patchcore_params.pkl: Model configuration and parameters
Storage: 50-500 MB per category depending on sampling percentage.
Without this flag, trained models are discarded after evaluation. Always use this flag if you plan to deploy the model.
flag
Save visualization images showing anomaly segmentation results.
Location: run_patchcore.py:28Creates overlay images showing:
  • Original image
  • Predicted anomaly heatmap
  • Ground truth mask
  • Anomaly score

Logging and Output

string
required
Base directory for storing results.
Location: run_patchcore.py:23Creates structure:
string
default:"project"
Project name for organizing experiments.
Location: run_patchcore.py:27
string
default:"group"
Experiment group/run name.
Location: run_patchcore.py:26
Use descriptive names encoding key parameters. Example format:
  • IM224: Image size 224
  • WR50: WideResNet50
  • L2-3: Layers 2 and 3
  • P01: 1% sampling
  • D1024-1024: Dimensions
  • S0: Seed 0
flag
Enable Weights & Biases logging (requires W&B account).
Location: Mentioned in README.md:102,143Set your W&B API key in the script before using.

PatchCore Arguments

All arguments under the patch_core command control the model architecture and feature extraction.

Backbone Configuration

string
required
Pretrained backbone network(s) for feature extraction.
Single backbone
Multiple backbones (ensemble)
Location: run_patchcore.py:242Available backbones (from backbones.py:4-47):
  • resnet50: ResNet-50
  • resnet101: ResNet-101
  • resnet200: ResNet-200 (via timm)
  • wideresnet50: Wide ResNet-50-2 (recommended)
  • wideresnet101: Wide ResNet-101-2
  • resnext101: ResNeXt-101-32x8d
  • resnest50: ResNeSt-50
All backbones are loaded with ImageNet pretrained weights.
string
required
Layer names to extract features from.
Single backbone
Multiple backbones (prefix with index)
Location: run_patchcore.py:243Layer naming by architecture:
Available layers:
  • layer1: 56×56 spatial resolution
  • layer2: 28×28 (recommended)
  • layer3: 14×14 (recommended)
  • layer4: 7×7
Recommended: layer2 + layer3 for multi-scale features
Available layers:
  • features.denseblock1: Early features
  • features.denseblock2: Mid-level (recommended)
  • features.denseblock3: High-level (recommended)
  • features.denseblock4: Final features
Note: Must include features. prefix
Available layers (example for EfficientNet-B7):
  • blocks.2: Early features
  • blocks.4: Mid features
  • blocks.6: High features (recommended)
  • blocks.8: Final features (recommended)
Layer indices vary by EfficientNet variant.
Available layers:
  • blocks.{N}: Transformer blocks (0 to 11 for ViT-Base)
Recommended: Last few blocks (e.g., blocks.9, blocks.11)
ViT support is experimental. Performance may vary.

Feature Dimensions

int
default:"1024"
Dimensionality of features extracted from backbone layers.
Location: run_patchcore.py:245This should match the combined output dimension of selected layers. For WideResNet50 layer2+layer3:
  • layer2: 512 channels
  • layer3: 1024 channels
  • Combined via preprocessing: 1024 dimensions
int
default:"1024"
Final embedding dimension after aggregation.
Location: run_patchcore.py:246Trade-offs:
  • Higher (1024): Better feature representation, more memory
  • Lower (384, 512): Less memory, faster inference, minimal accuracy loss
For ensembles, use lower dimensions (384) to save memory across multiple models.

Patch Settings

int
default:"3"
Local neighborhood aggregation size.
Location: run_patchcore.py:252From PatchMaker class (patchcore.py:278-290):
  • Creates patches via unfold operation
  • Padding: (patchsize - 1) / 2
  • Aggregates local features into patch representations
Recommendations:
  • Detection focus (image-level AUROC): Use 3
  • Segmentation focus (pixel-level AUROC): Use 5
  • Larger patches smooth predictions, better for segmentation
string
default:"max"
Method for aggregating patch scores into image-level score.
Location: run_patchcore.py:253Typically use max (default) - most anomalous patch determines image score.
float
default:"0.0"
Overlap between patches during extraction.
Location: run_patchcore.py:254Usually left at 0.0. Non-zero values increase computation.

Preprocessing and Aggregation

choice
default:"mean"
Method for preprocessing multi-layer features.
Location: run_patchcore.py:247Options: mean, convmean is standard and recommended.
choice
default:"mean"
Method for aggregating features across layers.
Location: run_patchcore.py:248Options: mean, mlpmean is standard (no learnable parameters).

Anomaly Scoring

int
default:"5"
Number of nearest neighbors for anomaly score computation.
Location: run_patchcore.py:250From NearestNeighbourScorer (referenced in patchcore.py:69-71):
  • Computes distance to k nearest neighbors in memory bank
  • Anomaly score = average distance to k-NN
Recommendations:
  • 1-NN: Best for detection, fastest
  • 3-NN: Balanced, more robust to outliers
  • 5-NN: Better segmentation, smoother scores

FAISS Configuration

flag
Use GPU for FAISS nearest neighbor search.
Location: run_patchcore.py:257Benefits:
  • 5-10x faster inference
  • Essential for real-time applications
Requirements:
  • Additional GPU memory (~500 MB per model)
  • FAISS GPU support installed
int
default:"8"
Number of CPU threads for FAISS operations.
Location: run_patchcore.py:258Only relevant when not using --faiss_on_gpu.

Sampler Arguments

Controls coreset subsampling of the feature memory bank.
string
required
Coreset sampling algorithm.
Location: run_patchcore.py:319Options:
Approximate Greedy Coreset Sampling (recommended)
From ApproximateGreedyCoresetSampler (sampler.py:118-171):
  • Uses approximate distance matrix (10 starting points)
  • Much faster than exact greedy
  • Minimal performance loss
Speed: ~1-2 min for 1% of ~17k features
float
default:"0.1"
Percentage of features to keep after sampling.
Location: run_patchcore.py:320Trade-offs:Recommendation: Use 1% (-p 0.01) for production - excellent balance.

Dataset Arguments

Controls data loading and preprocessing.
string
required
Dataset type.
Location: run_patchcore.py:334Currently only mvtec is supported. Custom datasets need implementation.
path
required
Path to dataset root directory.
Location: run_patchcore.py:335Must contain subdirectories for each category (see MVTec Setup).
string
required
MVTec category names to train on.
Single category
Multiple categories
Location: run_patchcore.py:336Valid categories (from mvtec.py:8-24):
  • bottle, cable, capsule, carpet, grid
  • hazelnut, leather, metal_nut, pill, screw
  • tile, toothbrush, transistor, wood, zipper

Image Preprocessing

int
default:"256"
Initial resize dimension (before center crop).
Location: run_patchcore.py:340From MVTecDataset (mvtec.py:74-87):
  1. Load image
  2. transforms.Resize(resize) → Resize to (resize, resize)
  3. transforms.CenterCrop(imagesize) → Crop to (imagesize, imagesize)
Common combinations:
  • Resize 256 → Crop 224
  • Resize 366 → Crop 320
int
default:"224"
Final image size after center crop (input to backbone).
Location: run_patchcore.py:341Impact:
  • 224: Standard ImageNet size, fastest
  • 320: Better localization, 1.5-2x slower
  • Higher: Requires more GPU memory
flag
Enable data augmentation during training.
Location: run_patchcore.py:342
Data augmentation is typically not used with PatchCore, as the method assumes consistent image distributions. Only use for experimentation.

Data Loading

int
default:"2"
Number of images per batch.
Location: run_patchcore.py:338Larger batches can speed up training but require more GPU memory.
int
default:"8"
Number of CPU workers for data loading.
Location: run_patchcore.py:339More workers = faster data loading, but uses more CPU/RAM.
float
default:"1.0"
Fraction of training data to use for training (vs. validation).
Location: run_patchcore.py:337Typically use 1.0 (all data for training, no validation split).

Configuration Examples

Here are complete, working configurations for common scenarios:
Simplest possible training command:
Uses all defaults, trains single category.
Recommended production configuration (99.2% AUROC):
Best performance configuration (99.6% AUROC):
Configuration for limited GPU memory (under 11 GB):
Changes:
  • Smaller backbone (ResNet50 vs WideResNet50)
  • Lower target dimension (512 vs 1024)
  • Batch size 1
  • No --faiss_on_gpu

Parameter Interactions

Memory Usage Factors

GPU memory usage is primarily determined by:
Memory Formula (approximate)
To reduce memory:
  1. Lower --batch_size
  2. Smaller --imagesize
  3. Reduce --target_embed_dimension
  4. Remove --faiss_on_gpu
  5. Use smaller backbone

Training Time Factors

Training time per category:
Time Formula (approximate)
To reduce time:
  1. Lower sampling percentage (-p 0.01 vs -p 0.1)
  2. Use approx_greedy_coreset (not greedy_coreset)
  3. Enable --faiss_on_gpu
  4. Smaller backbone (ResNet50 vs ResNet101)

Accuracy vs. Efficiency

Next Steps

Train Single Model

Apply these configurations to train your first model

Ensemble Training

Combine multiple backbones for best performance