Overview
ThePatchCore class is the core implementation of the PatchCore algorithm for industrial anomaly detection. It provides methods for loading models, training on normal data, and predicting anomalies in images.
Class Definition
torch.nn.Module.
torch.device
required
The device (CPU or CUDA) on which to run the model.
Methods
load
Parameters
torch.nn.Module
required
Pre-trained neural network backbone for feature extraction (e.g., ResNet, WideResNet).
list[str]
required
List of layer names from which to extract features from the backbone.
torch.device
required
Device on which to load the model.
tuple
required
Shape of input images (e.g.,
(3, 224, 224) for RGB images).int
required
Dimension to which features are projected before aggregation.
int
required
Target dimension for final feature embeddings.
int
default:"3"
Size of patches to extract from feature maps.
int
default:"1"
Stride for patch extraction from feature maps.
int
default:"1"
Number of nearest neighbors to use for anomaly scoring.
Sampler
default:"IdentitySampler()"
Sampler to use for subsampling features during training. Can be
IdentitySampler, GreedyCoresetSampler, or RandomSampler.FaissNN
default:"FaissNN(False, 4)"
Nearest neighbor search method for anomaly scoring.
Example
fit
DataLoader or iterable
required
DataLoader or iterable containing normal (non-anomalous) training images. Images should be dictionaries with an
"image" key or raw tensors.Example
The
fit method only requires normal (non-anomalous) images. PatchCore learns what “normal” looks like and detects deviations from this during inference.predict
torch.Tensor or DataLoader
required
Input image tensor or DataLoader. If a tensor, shape should be
[B, C, H, W]. If a DataLoader, images can be dictionaries with "image", "is_anomaly", and "mask" keys.Returns
list[float]
List of anomaly scores for each image. Higher scores indicate higher likelihood of anomaly.
list[np.ndarray]
List of anomaly segmentation masks for each image. Values are normalized between 0 and 1.
list[int]
Ground truth labels (only returned when using DataLoader with labeled data).
list[np.ndarray]
Ground truth masks (only returned when using DataLoader with labeled data).
Example
embed
torch.Tensor or DataLoader
required
Input images as tensor
[B, C, H, W] or DataLoader.Returns
np.ndarray or list[np.ndarray]
Feature embeddings extracted from the images. Shape depends on the number of patches and target embedding dimension.
save_to_path
str
required
Directory path where the model will be saved.
str
default:""
Optional prefix to prepend to saved filenames.
Example
load_from_path
str
required
Directory path from which to load the model.
torch.device
required
Device on which to load the model.
FaissNN
required
Nearest neighbor search method to use.
str
default:""
Optional prefix used when saving the model.
Example
PatchMaker Class
Internal utility class for patch extraction and scoring.int
required
Size of patches to extract.
int
Stride for patch extraction. If None, defaults to patchsize.
Methods
patchify
torch.Tensor
required
Input feature tensor with shape
[B, C, H, W].bool
default:"False"
Whether to return spatial information about patches.
