Skip to content

Samplers

Samplers for handling class imbalance when training particle classifiers on cryoET data.

ClassBalancedSampler

Draws samples so that each class is represented roughly equally per mini-batch, using inverse-frequency weighting.

copick_torch.samplers.ClassBalancedSampler

Bases: Sampler

A sampler that balances class distributions during training.

This sampler is designed to address the class imbalance problem by providing a way to balance the frequency of each class in the mini-batches.

__init__

__init__(labels, num_samples=None, replacement=True)

Initialize the class-balanced sampler.

Parameters:

  • labels

    List or tensor of integer class labels for each sample

  • num_samples

    Number of samples to draw (default: len(labels))

  • replacement

    Whether to sample with replacement (default: True)

__iter__

__iter__()

Generate a random sequence of indices based on weighted sampling.

Returns:

  • Iterator over indices

__len__

__len__()

Return the number of samples in the sampler.

Returns:

  • Number of samples