Overview
PatchCore supports efficient batch prediction on multiple images using PyTorch DataLoaders. This enables processing large datasets with automatic batching, GPU acceleration, and parallel data loading.Basic Batch Prediction
Thepredict() method automatically handles both single images and DataLoaders:
DataLoader Configuration
Create a DataLoader for efficient batch processing:Batch Size Recommendations:
- 224x224 images: batch_size=8-16
- 320x320 images: batch_size=4-8
- Adjust based on GPU memory (11GB recommended)
Output Format
Thepredict() method returns four components:
scores: List of floats
scores: List of floats
Image-level anomaly scores
- Higher scores indicate higher anomaly likelihood
- Computed as maximum patch score per image
- Range: typically [0, 1] after normalization
masks: List of numpy arrays
masks: List of numpy arrays
Pixel-level anomaly heatmaps
- Values represent anomaly scores per pixel
- Smoothed with Gaussian filter (sigma=4)
- Can be thresholded for binary segmentation
labels_gt: List of booleans
labels_gt: List of booleans
Ground truth anomaly labels
- Extracted from dataset metadata
- Used for computing evaluation metrics
- Empty if ground truth unavailable
masks_gt: List of numpy arrays
masks_gt: List of numpy arrays
Ground truth segmentation masks
- Binary masks indicating defect locations
- 1 = anomalous pixel, 0 = normal
- Used for pixel-level metrics (AUROC, PRO)
Internal Implementation
The prediction pipeline for DataLoaders (source:patchcore.py:183):Single Batch Prediction
For processing individual batches, use_predict() directly:
Processing Custom Images
To predict on custom images without creating a dataset:Feature Extraction Details
Under the hood, batch prediction extracts and processes features:Performance Optimization
GPU Memory Management
For large datasets, manage GPU memory carefully:Parallel Data Loading
Speed up preprocessing with multiple workers:Worker Recommendations:
- Set
num_workersto number of CPU cores (typically 4-16) - Higher values speed up data loading but use more memory
- Set to 0 for debugging to see data loading errors
Interpreting Results
Anomaly Score Thresholding
Determine optimal threshold for classification:Segmentation Mask Visualization
Visualize anomaly heatmaps:Next Steps
Load Models
Learn how to load pretrained models
Evaluation Metrics
Compute AUROC and PRO scores
