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 ininput_keys
.- Returns:
A dataset that generates examples with the tensors in
input_keys
mapped to keys inoutput_keys
according to the structure inkey_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 assplit_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 aLabels
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 assplit_fractions
. The indices will be stored in theexample_indices
in eachLabelsReader
instance.The actual
Labels
instance will be the same for each instance, only theexample_indices
that are iterated over will change across splits.If the input
labels_reader
already hasexample_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
andlabels_val
aresleap.Label
objects containing the selected frames for each split. Theirvideos
,tracks
andprovenance
attributes are identical tolabels
even if the split does not contain instances with a particular video or track.idx_train
andidx_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.