Skip to main content

Overview

Samplers reduce the size of the feature memory bank by selecting a representative subset of features from the training data. This improves inference speed and memory efficiency while maintaining detection accuracy.

IdentitySampler

A no-op sampler that returns all features without any subsampling. Use this when you want to keep all training features in the memory bank.

Methods

run

Returns the input features unchanged.
torch.Tensor or np.ndarray
required
Input features with shape [N, D] where N is the number of samples and D is the feature dimension.

Returns

torch.Tensor or np.ndarray
The same features that were input, with no modification.

Example


BaseSampler

Abstract base class for all sampling strategies that perform subsampling.

Constructor

float
required
Percentage of features to keep, must be in the range (0, 1). For example, 0.1 means keep 10% of features.

Methods

run

Abstract method that must be implemented by subclasses.

GreedyCoresetSampler

Greedy coreset subsampling algorithm that iteratively selects the most representative features. This produces high-quality subsamples but requires computing the full N×N distance matrix.

Constructor

float
required
Percentage of features to keep (0 < percentage < 1). For example, 0.1 keeps 10% of features.
torch.device
required
Device on which to perform coreset computation (CPU or CUDA).
int
default:"128"
Features are projected to this dimension before computing distances to speed up computation.

Methods

run

Subsamples features using the greedy coreset algorithm.
torch.Tensor or np.ndarray
required
Input features with shape [N, D].

Returns

torch.Tensor or np.ndarray
Subsampled features with shape [N*percentage, D].

Example

Memory Usage: This sampler computes a full N×N distance matrix, which requires O(N²) memory. For very large feature sets (>50,000 samples), consider using ApproximateGreedyCoresetSampler instead.

ApproximateGreedyCoresetSampler

Approximate greedy coreset subsampling that avoids computing the full N×N distance matrix. This is more memory-efficient but slower than the exact version.

Constructor

float
required
Percentage of features to keep (0 < percentage < 1).
torch.device
required
Device on which to perform coreset computation.
int
default:"10"
Number of random starting points to use for distance approximation. Higher values improve accuracy but increase computation time.
int
default:"128"
Target dimension for feature projection before distance computation.

Methods

run

Subsamples features using approximate greedy coreset algorithm.
torch.Tensor or np.ndarray
required
Input features with shape [N, D].

Returns

torch.Tensor or np.ndarray
Subsampled features with shape [N*percentage, D].

Example

Performance Trade-off: This sampler uses less memory than GreedyCoresetSampler but takes longer to compute. It’s ideal for datasets with >50,000 training images where memory is a constraint.

RandomSampler

Randomly samples a subset of features. This is the fastest sampling method but may not preserve the feature distribution as well as coreset methods.

Constructor

float
required
Percentage of features to keep (0 < percentage < 1).

Methods

run

Randomly samples features without replacement.
torch.Tensor or np.ndarray
required
Input features with shape [N, D].

Returns

torch.Tensor or np.ndarray
Randomly sampled features with shape [N*percentage, D].

Example


Comparison


Usage with PatchCore

Recommendation: For most industrial anomaly detection tasks, use ApproximateGreedyCoresetSampler with percentage=0.1 (10%). This provides the best balance of speed, memory, and accuracy.