Skip to main content

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

The predict() method automatically handles both single images and DataLoaders:
1

Prepare your DataLoader

Create a PyTorch DataLoader with your test images
2

Call predict()

Pass the DataLoader to predict() - it automatically detects the type
3

Process results

Receive anomaly scores, segmentation masks, and ground truth labels

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

The predict() method returns four components:
Image-level anomaly scores
  • Higher scores indicate higher anomaly likelihood
  • Computed as maximum patch score per image
  • Range: typically [0, 1] after normalization
Pixel-level anomaly heatmaps
  • Values represent anomaly scores per pixel
  • Smoothed with Gaussian filter (sigma=4)
  • Can be thresholded for binary segmentation
Ground truth anomaly labels
  • Extracted from dataset metadata
  • Used for computing evaluation metrics
  • Empty if ground truth unavailable
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):
1

Set evaluation mode

Disable dropout and batch normalization updates
2

Iterate through batches

Process each batch with progress tracking
3

Extract metadata

Pull ground truth labels and masks from dataset
4

Run inference

Call _predict() on each batch of images
5

Accumulate results

Collect all scores and masks into lists

Single Batch Prediction

For processing individual batches, use _predict() directly:
Output:

Processing Custom Images

To predict on custom images without creating a dataset:
Ensure your preprocessing matches the training configuration:
  • Same image size (resize, crop)
  • Same normalization (ImageNet stats)
  • RGB format (3 channels)

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_workers to 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