Skip to main content

Overview

The load_and_evaluate_patchcore.py script loads pre-trained PatchCore models and evaluates them on test datasets. This is useful for:
  • Testing saved models without retraining
  • Evaluating on new data
  • Benchmarking model performance
  • Generating segmentation visualizations
Unlike run_patchcore.py, this script only performs inference and evaluation—no training occurs.

Command Structure

Main Command

Arguments

string
required
Path where evaluation results and metrics will be saved

Options

int
default:"0"
GPU device ID(s) to use for evaluation. Can specify multiple GPUs by repeating the flag.
int
default:"0"
Random seed for reproducibility
flag
Save visualization images showing anomaly segmentation results
Unlike run_patchcore.py, this script does not have --log_group, --log_project, or --save_patchcore_model options since it only loads existing models.

Subcommand: patch_core_loader

Loads pre-trained PatchCore models from disk.

Options

string
required
Short flag: -pPath(s) to saved PatchCore model directories. Each path should point to a directory containing .faiss and .pkl files.The loader automatically detects:
  • Single models: Directory with one .faiss file
  • Ensemble models: Directory with multiple .faiss files named Ensemble-{i}-{n}_*.faiss
flag
Use GPU-accelerated FAISS for nearest neighbor search. Significantly speeds up evaluation.
int
default:"8"
Number of CPU workers for FAISS operations

Subcommand: dataset

Configures dataset loading for evaluation.

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 evaluate on. Should match the categories used during training.For MVTec AD: bottle, cable, capsule, carpet, grid, hazelnut, leather, metal_nut, pill, screw, tile, toothbrush, transistor, wood, zipper
int
default:"1"
Batch size for data loading during evaluation. Default is 1 for evaluation.
int
default:"8"
Number of worker processes for data loading
int
default:"256"
Image resize dimension (before center cropping). Must match training configuration.
int
default:"224"
Final image size after center cropping. Must match training configuration.
  • 224 - Standard ImageNet size
  • 320 - Higher resolution models
flag
Apply data augmentation during evaluation (typically not used)
The --resize and --imagesize parameters must match the values used during training, otherwise evaluation results will be incorrect.

Examples

Evaluate Single Model

Evaluate a single trained model on one category:

Evaluate Multiple Categories

Evaluate models for all 15 MVTec AD categories (from sample script):

Evaluate with Segmentation Visualization

Generate visualization images during evaluation:

Evaluate IM224 Model

For models trained at 224x224 resolution:

Evaluate IM320 Model

For models trained at 320x320 resolution:

Model Directory Structure

Expected structure for model directories:

Single Model

Ensemble Model

The script automatically detects ensemble models by counting .faiss files in the directory.

Output Files

The script creates the following structure:

Metrics Computed

The script evaluates the following metrics:
  • 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)
Results are saved to results.csv with per-dataset and mean scores.

Comparison: run_patchcore.py vs load_and_evaluate_patchcore.py


Tips

Match training config: Always use the same --resize and --imagesize values that were used during training
Batch evaluation: Use bash loops to evaluate multiple model directories efficiently (see example above)
GPU acceleration: Always use --faiss_on_gpu for faster evaluation on GPU-enabled systems
Model organization: Organize saved models by configuration (e.g., IM224_WR50_L2-3_P01) for easy identification

Common Issues

Error: “oldString not found in content” - This typically means the model files are missing or the path is incorrect. Verify that .faiss and .pkl files exist in the specified directory.
Mismatched dimensions - If you get dimension errors, ensure --imagesize matches the training configuration.
Number of models mismatch - The script requires either equal numbers of models and datasets, or a single model to evaluate across multiple datasets.