sleap.nn.data.training#

Transformers and utilities for training-related operations.

class sleap.nn.data.training.KeyMapper(key_maps: Any | None)[source]#

Maps example keys to specified outputs.

This is useful for transforming examples into tuples that map onto specific layer names for training.

key_maps#

Dictionary or list of dictionaries with string keys and values of the form: {input_key: output_key}. If a list, the examples will be in tuples in the same order.

Type:

List[Dict[str, str]]

property input_keys: List[str]#

Return the keys that incoming elements are expected to have.

property output_keys: List[str]#

Return the keys that outgoing elements will have. These may be nested.

transform_dataset(ds_input: DatasetV2) DatasetV2[source]#

Create a dataset with input keys mapped to new key names.

Parameters:

ds_input – Any tf.data.Dataset that generates examples as a dictionary of tensors with the keys in input_keys.

Returns:

A dataset that generates examples with the tensors in input_keys mapped to keys in output_keys according to the structure in key_maps.

sleap.nn.data.training.split_labels(labels: Labels, split_fractions: Sequence[float]) Tuple[Labels][source]#

Split a Labels into multiple new ones with random subsets of the data.

Parameters:
  • labels – An instance of Labels.

  • split_fractions – One or more floats between 0 and 1 that specify the fraction of examples that should be in each dataset. These should add up to <= 1.0. Fractions of less than 1 element will be rounded up to ensure that is at least 1 element in each split. One of the fractions may be -1 to indicate that it should contain all elements left over from the other splits.

Returns:

A tuple of new Labels instances of the same length as split_fractions.

Raises:
  • ValueError – If more than one split fraction is specified as -1.

  • ValueError – If the splits add up to more than the total available examples.

Note

Sampling is done without replacement.

sleap.nn.data.training.split_labels_reader(labels_reader: LabelsReader, split_fractions: Sequence[float]) Tuple[LabelsReader][source]#

Split a LabelsReader into multiple new ones with random subsets of the data.

Parameters:
  • labels_reader – An instance of sleap.nn.data.providers.LabelsReader. This is a provider that generates datasets that contain elements read from a Labels instance.

  • split_fractions – One or more floats between 0 and 1 that specify the fraction of examples that should be in each dataset. These should add up to <= 1.0. Fractions of less than 1 element will be rounded up to ensure that is at least 1 element in each split. One of the fractions may be -1 to indicate that it should contain all elements left over from the other splits.

Returns:

A tuple of LabelsReader instances of the same length as split_fractions. The indices will be stored in the example_indices in each LabelsReader instance.

The actual Labels instance will be the same for each instance, only the example_indices that are iterated over will change across splits.

If the input labels_reader already has example_indices, a subset of these will be sampled to generate the splits.

Raises:
  • ValueError – If more than one split fraction is specified as -1.

  • ValueError – If the splits add up to more than the total available examples.

Note

Sampling is done without replacement.

sleap.nn.data.training.split_labels_train_val(labels: Labels, validation_fraction: float) Tuple[Labels, List[int], Labels, List[int]][source]#

Make a train/validation split from a labels dataset.

Parameters:
  • labels – A Labels dataset with labeled frames.

  • validation_fraction – Fraction of frames to use for validation.

Returns:

A tuple of (labels_train, idx_train, labels_val, idx_val).

labels_train and labels_val are sleap.Label objects containing the selected frames for each split. Their videos, tracks and provenance attributes are identical to labels even if the split does not contain instances with a particular video or track.

idx_train and idx_val are list indices of the labeled frames within the input labels that were assigned to each split, i.e.:

labels[idx_train] == labels_train[:]

If there is only one labeled frame in labels, both of the labels will contain the same frame.

If validation_fraction would result in fewer than one label for either split, it will be rounded to ensure there is at least one label in each.