easy_vision.python.core.sampler

easy_vision.python.core.sampler.balanced_positive_negative_sampler

Class to subsample minibatches by balancing positives and negatives.

Subsamples minibatches based on a pre-specified positive fraction in range [0,1]. The class presumes there are many more negatives than positive examples: if the desired batch_size cannot be achieved with the pre-specified positive fraction, it fills the rest with negative examples. If this is not sufficient for obtaining the desired batch_size, it returns fewer examples.

The main function to call is Subsample(self, indicator, labels). For convenience one can also call SubsampleWeights(self, weights, labels) which is defined in the minibatch_sampler base class.

class easy_vision.python.core.sampler.balanced_positive_negative_sampler.BalancedPositiveNegativeSampler(positive_fraction=0.5)[source]

Bases: easy_vision.python.core.sampler.minibatch_sampler.MinibatchSampler

Subsamples minibatches to a desired balance of positives and negatives.

__init__(positive_fraction=0.5)[source]

Constructs a minibatch sampler.

Parameters:positive_fraction – desired fraction of positive examples (scalar in [0,1]) in the batch.
Raises:ValueError – if positive_fraction < 0, or positive_fraction > 1
subsample(indicator, batch_size, labels)[source]

Returns subsampled minibatch.

Parameters:
  • indicator – boolean tensor of shape [N] whose True entries can be sampled.
  • batch_size – desired batch size. If None, keeps all positive samples and randomly selects negative samples so that the positive sample fraction matches self._positive_fraction.
  • labels – boolean tensor of shape [N] denoting positive(=True) and negative (=False) examples.
Returns:

boolean tensor of shape [N], True for entries which are

sampled.

Return type:

is_sampled

Raises:

ValueError – if labels and indicator are not 1D boolean tensors.

easy_vision.python.core.sampler.minibatch_sampler

Base minibatch sampler module.

The job of the minibatch_sampler is to subsample a minibatch based on some criterion.

The main function call is:
subsample(indicator, batch_size, **params).

Indicator is a 1d boolean tensor where True denotes which examples can be sampled. It returns a boolean indicator where True denotes an example has been sampled..

Subclasses should implement the Subsample function and can make use of the @staticmethod SubsampleIndicator.

class easy_vision.python.core.sampler.minibatch_sampler.MinibatchSampler[source]

Bases: object

Abstract base class for subsampling minibatches.

__init__()[source]

Constructs a minibatch sampler.

subsample(indicator, batch_size, **params)[source]

Returns subsample of entries in indicator.

Parameters:
  • indicator – boolean tensor of shape [N] whose True entries can be sampled.
  • batch_size – desired batch size.
  • **params – additional keyword arguments for specific implementations of the MinibatchSampler.
Returns:

boolean tensor of shape [N] whose True entries have been sampled. If sum(indicator) >= batch_size, sum(is_sampled) = batch_size

Return type:

sample_indicator

static subsample_indicator(indicator, num_samples)[source]

Subsample indicator vector.

Given a boolean indicator vector with M elements set to True, the function assigns all but num_samples of these previously True elements to False. If num_samples is greater than M, the original indicator vector is returned.

Parameters:
  • indicator – a 1-dimensional boolean tensor indicating which elements are allowed to be sampled and which are not.
  • num_samples – int32 scalar tensor
Returns:

a boolean tensor with the same shape as input (indicator) tensor