tfm.vision.sampling_ops.BalancedPositiveNegativeSampler

Subsamples minibatches to a desired balance of positives and negatives.

positive_fraction desired fraction of positive examples (scalar in [0,1]) in the batch.
is_static If True, uses an implementation with static shape guarantees.

ValueError if positive_fraction < 0, or positive_fraction > 1

Methods

subsample

View source

Returns subsampled minibatch.

Args
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. It cannot be None is is_static is True.
labels boolean tensor of shape [N] denoting positive(=True) and negative (=False) examples.
scope name scope.

Returns
sampled_idx_indicator boolean tensor of shape [N], True for entries which are sampled.

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

subsample_indicator

View 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.

Args
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