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
Methods
run
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
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
GreedyCoresetSampler
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
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
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
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
Constructor
float
required
Percentage of features to keep (0 < percentage < 1).
Methods
run
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.