labels#

voxel.labels.recode(tensor, mapping, reverse=False, default=0, background=False) → Tensor#

Remap the values of a label tensor through an ordered mapping.

In the forward direction, tensor is treated as an index map and each value i is replaced with mapping[i] (a lookup-table gather); the mapping values may be of any dtype. In the reverse direction, each value is replaced with its position within mapping, which requires an integer mapping; values not present in mapping become default.

A LabelLookup is treated as its ordered integer indices.

Parameters:
  • tensor (Tensor) – An integer label tensor.

  • mapping (Tensor or list or LabelLookup) – The ordered values to map index positions to.

  • reverse (bool, optional) – Map values back to their index positions.

  • default (int, optional) – The index assigned to reverse-mapped values that are not present in mapping.

  • background (bool, optional) – Prepend the background label 0 to mapping (unless already present) so it occupies the first index position.

Returns:

Tensor – The recoded tensor, with the same shape as tensor.

voxel.labels.onehot(tensor, labels=-1, background=False) → Tensor#

One-hot encode an integer label tensor, placing classes on a leading dimension.

Parameters:
  • tensor (Tensor) – An integer label tensor.

  • labels (int or Tensor or LabelLookup, optional) – The classes to encode. If an integer, it is the total number of classes (-1 infers one greater than the largest value). If a tensor or lookup of label values, the tensor is first reverse-recoded so those values map to the channels.

  • background (bool, optional) – When labels is a tensor or lookup, reserve the first channel for the background label 0 (unless already present).

Returns:

Tensor – The one-hot encoded tensor with the class dimension inserted first.

voxel.labels.collapse(tensor, labels=None, background=False) → Tensor#

Collapse a one-hot (or probabilistic) tensor into a label tensor.

This is the inverse of onehot: the leading class dimension is reduced with an argmax, and the resulting channel indices are optionally recoded into label values.

Parameters:
  • tensor (Tensor) – A tensor with classes on the leading dimension.

  • labels (Tensor or LabelLookup, optional) – The label values the channels correspond to. If None, the channel indices are returned directly, otherwise they are forward-recoded into label values.

  • background (bool, optional) – When labels is provided, treat the first channel as the background label 0 (unless already present), matching onehot.

Returns:

Tensor – The label tensor with the class dimension removed.