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
Command Structure
The PatchCore training command has four main sections:- Global Arguments
- Backbone Config
- Sampler Config
- Dataset Config
Global Settings
--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 inferenceresults: Base directory where outputs are stored
Step-by-Step Training
Let’s train on a single category first, then scale to all 15:Train on Single Category
Start with one category (bottle) to verify setup:Expected output:
Single Category Training
Scale to All Categories
Once verified, train on all 15 categories:Training will process each category sequentially. Total time: ~1-2 hours on GPU.
All Categories
Understanding Output Files
After training with--save_patchcore_model, you’ll see:
Output Structure
What's in each file?
What's in each file?
nnscorer_search_index.faiss:
- FAISS nearest-neighbor search index
- Contains memory bank of training features
- Size: 50-500 MB depending on sampling percentage
- Model configuration (backbone, layers, dimensions)
- Small file (~1 KB)
- Needed to load model for inference
- 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:GPU device ID. Use
--gpu 0 for first GPU, --gpu 1 for second, etc.Random seed for reproducibility. Same seed = same results.
Save trained model to disk. Required if you want to use the model later.
Experiment name. Creates a subdirectory with this name in results.
Project folder name. Organizes experiments into projects.
Backbone network architecture. Options:
wideresnet50(recommended)wideresnet101resnet50,resnet101- See Backbones for all options
Which layers to extract features from. Common choices:
- WideResNet:
layer2,layer3 - ResNet:
layer2,layer3 - DenseNet:
features.denseblock2,features.denseblock3
Dimensionality of features from backbone layers. Standard: 1024.
Final embedding dimension after aggregation. Lower = less memory.
Neighborhood size for local aggregation. Typical: 3 or 5.
Number of nearest neighbors for anomaly scoring. Usually 1, 3, or 5.
Use GPU for FAISS nearest neighbor search. Faster but uses GPU memory.
Coreset sampling percentage (0.01 = 1%, 0.1 = 10%).
Initial resize dimension before center crop.
Final image size after center crop (input to backbone).
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
CUDA out of memory error
CUDA out of memory error
Problem: GPU doesn’t have enough memory.Solutions:
- Reduce batch size:
dataset [...] --batch_size 1 mvtec $datapath - Lower image resolution: Use 224 instead of 320
- Reduce target embedding dimension:
--target_embed_dimension 512 - Use CPU for FAISS: Remove
--faiss_on_gpu
ModuleNotFoundError: No module named 'patchcore'
ModuleNotFoundError: No module named 'patchcore'
Problem: Python can’t find the patchcore module.Solution:
Training very slow
Training very slow
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.01instead of-p 0.1 - Too many workers: Reduce
--num_workers 4in dataset config
Next Steps
Ensemble Models
Combine multiple backbones for 99.6% AUROC
Configuration Reference
Deep dive into every parameter
