Skip to main content

Overview

Training a single-backbone PatchCore model is the fastest way to get started. This guide uses WideResNet50 as the backbone, which provides excellent performance while remaining computationally efficient.
Recommended for:
  • First-time users learning PatchCore
  • Development and experimentation
  • Production systems with speed/memory constraints
  • Achieving 99.2% AUROC on MVTec AD

Quick Start

Train PatchCore on all MVTec AD categories with recommended settings:
Complete Training Command
Don’t forget to:
  • Set PYTHONPATH before running: export PYTHONPATH=src
  • Replace /path/to/mvtec with your actual dataset path
  • Ensure MVTec AD is properly set up (see MVTec Setup)

Command Structure

The PatchCore training command has four main sections:
Global Settings
Key Parameters:
  • --gpu: Which GPU to use (supports multi-GPU with --gpu 0 --gpu 1)
  • --seed: Controls random initialization for reproducibility
  • --save_patchcore_model: Must include to save the model for inference
  • results: Base directory where outputs are stored

Step-by-Step Training

Let’s train on a single category first, then scale to all 15:
1

Set Environment

Setup
2

Train on Single Category

Start with one category (bottle) to verify setup:
Single Category Training
Expected output:
3

Scale to All Categories

Once verified, train on all 15 categories:
All Categories
Training will process each category sequentially. Total time: ~1-2 hours on GPU.
4

Check Results

After training completes:
View Results
Each category will have a dedicated model directory.

Understanding Output Files

After training with --save_patchcore_model, you’ll see:
Output Structure
nnscorer_search_index.faiss:
  • FAISS nearest-neighbor search index
  • Contains memory bank of training features
  • Size: 50-500 MB depending on sampling percentage
patchcore_params.pkl:
  • Model configuration (backbone, layers, dimensions)
  • Small file (~1 KB)
  • Needed to load model for inference
results.csv:
  • Performance metrics for each category
  • Columns: dataset_name, instance_auroc, full_pixel_auroc, anomaly_pixel_auroc

All CLI Arguments Explained

Here’s every argument used in the training command:
int
default:"0"
GPU device ID. Use --gpu 0 for first GPU, --gpu 1 for second, etc.
int
default:"0"
Random seed for reproducibility. Same seed = same results.
flag
Save trained model to disk. Required if you want to use the model later.
string
Experiment name. Creates a subdirectory with this name in results.
string
default:"project"
Project folder name. Organizes experiments into projects.
string
required
Backbone network architecture. Options:
  • wideresnet50 (recommended)
  • wideresnet101
  • resnet50, resnet101
  • See Backbones for all options
string
required
Which layers to extract features from. Common choices:
  • WideResNet: layer2, layer3
  • ResNet: layer2, layer3
  • DenseNet: features.denseblock2, features.denseblock3
int
default:"1024"
Dimensionality of features from backbone layers. Standard: 1024.
int
default:"1024"
Final embedding dimension after aggregation. Lower = less memory.
int
default:"3"
Neighborhood size for local aggregation. Typical: 3 or 5.
int
default:"1"
Number of nearest neighbors for anomaly scoring. Usually 1, 3, or 5.
flag
Use GPU for FAISS nearest neighbor search. Faster but uses GPU memory.
float
default:"0.1"
Coreset sampling percentage (0.01 = 1%, 0.1 = 10%).
int
default:"256"
Initial resize dimension before center crop.
int
default:"224"
Final image size after center crop (input to backbone).
string
required
MVTec category names. Repeat for each category: -d bottle -d cable ...

Configuration Presets

Try these proven configurations for different use cases:

Monitoring Training Progress

During training, you’ll see progress output:
Training Output

Troubleshooting

Problem: GPU doesn’t have enough memory.Solutions:
  1. Reduce batch size: dataset [...] --batch_size 1 mvtec $datapath
  2. Lower image resolution: Use 224 instead of 320
  3. Reduce target embedding dimension: --target_embed_dimension 512
  4. Use CPU for FAISS: Remove --faiss_on_gpu
Problem: Python can’t find the patchcore module.Solution:
Problem: Training takes much longer than expected.Causes & Solutions:
  • Not using GPU: Verify with nvidia-smi, add --faiss_on_gpu
  • High sampling percentage: Use -p 0.01 instead of -p 0.1
  • Too many workers: Reduce --num_workers 4 in dataset config

Next Steps

Ensemble Models

Combine multiple backbones for 99.6% AUROC

Configuration Reference

Deep dive into every parameter