sleap.nn.training#

Training functionality and high level APIs.

class sleap.nn.training.BottomUpModelTrainer(data_readers: sleap.nn.training.DataReaders, model: sleap.nn.model.Model, config: sleap.nn.config.training_job.TrainingJobConfig, initial_config: Optional[sleap.nn.config.training_job.TrainingJobConfig] = None)[source]#

Trainer for models that output multi-instance confidence maps and PAFs.

property has_offsets: bool#

Whether model is configured to output refinement offsets.

property input_keys: List[str]#

Return example keys to be mapped to model inputs.

property output_keys: List[str]#

Return example keys to be mapped to model outputs.

class sleap.nn.training.BottomUpMultiClassModelTrainer(data_readers: sleap.nn.training.DataReaders, model: sleap.nn.model.Model, config: sleap.nn.config.training_job.TrainingJobConfig, initial_config: Optional[sleap.nn.config.training_job.TrainingJobConfig] = None)[source]#

Trainer for models that output multi-instance confidence maps and class maps.

property has_offsets: bool#

Whether model is configured to output refinement offsets.

property input_keys: List[str]#

Return example keys to be mapped to model inputs.

property output_keys: List[str]#

Return example keys to be mapped to model outputs.

class sleap.nn.training.CentroidConfmapsModelTrainer(data_readers: sleap.nn.training.DataReaders, model: sleap.nn.model.Model, config: sleap.nn.config.training_job.TrainingJobConfig, initial_config: Optional[sleap.nn.config.training_job.TrainingJobConfig] = None)[source]#

Trainer for models that output centroid confidence maps.

property has_offsets: bool#

Whether model is configured to output refinement offsets.

property input_keys: List[str]#

Return example keys to be mapped to model inputs.

property output_keys: List[str]#

Return example keys to be mapped to model outputs.

class sleap.nn.training.DataReaders(training_labels_reader: sleap.nn.data.providers.LabelsReader, validation_labels_reader: sleap.nn.data.providers.LabelsReader, test_labels_reader: Optional[sleap.nn.data.providers.LabelsReader] = None)[source]#

Container class for SLEAP labels that serve as training data sources.

training_labels_reader#

LabelsReader pipeline provider for a training data from a sleap.Labels instance.

Type

sleap.nn.data.providers.LabelsReader

validation_labels_reader#

LabelsReader pipeline provider for a validation data from a sleap.Labels instance.

Type

sleap.nn.data.providers.LabelsReader

test_labels_reader#

LabelsReader pipeline provider for a test set data from a sleap.Labels instance. This is not necessary for training.

Type

Optional[sleap.nn.data.providers.LabelsReader]

classmethod from_config(labels_config: sleap.nn.config.data.LabelsConfig, training: Union[str, sleap.io.dataset.Labels], validation: Union[str, sleap.io.dataset.Labels, float], test: Optional[Union[str, sleap.io.dataset.Labels]] = None, video_search_paths: Optional[List[str]] = None, update_config: bool = False, with_track_only: bool = False) sleap.nn.training.DataReaders[source]#

Create data readers from a (possibly incomplete) configuration.

classmethod from_labels(training: Union[str, sleap.io.dataset.Labels], validation: Union[str, sleap.io.dataset.Labels, float], test: Optional[Union[str, sleap.io.dataset.Labels]] = None, video_search_paths: Optional[List[str]] = None, labels_config: Optional[sleap.nn.config.data.LabelsConfig] = None, update_config: bool = False, with_track_only: bool = False) sleap.nn.training.DataReaders[source]#

Create data readers from sleap.Labels datasets as data providers.

property test_labels: sleap.io.dataset.Labels#

Return the sleap.Labels underlying the test data reader.

property training_labels: sleap.io.dataset.Labels#

Return the sleap.Labels underlying the training data reader.

property validation_labels: sleap.io.dataset.Labels#

Return the sleap.Labels underlying the validation data reader.

class sleap.nn.training.SingleInstanceModelTrainer(data_readers: sleap.nn.training.DataReaders, model: sleap.nn.model.Model, config: sleap.nn.config.training_job.TrainingJobConfig, initial_config: Optional[sleap.nn.config.training_job.TrainingJobConfig] = None)[source]#

Trainer for models that output single-instance confidence maps.

property has_offsets: bool#

Whether model is configured to output refinement offsets.

property input_keys: List[str]#

Return example keys to be mapped to model inputs.

property output_keys: List[str]#

Return example keys to be mapped to model outputs.

class sleap.nn.training.TopDownMultiClassModelTrainer(data_readers: sleap.nn.training.DataReaders, model: sleap.nn.model.Model, config: sleap.nn.config.training_job.TrainingJobConfig, initial_config: Optional[sleap.nn.config.training_job.TrainingJobConfig] = None)[source]#

Trainer for models that output multi-instance confidence maps and class maps.

property has_offsets: bool#

Whether model is configured to output refinement offsets.

property input_keys: List[str]#

Return example keys to be mapped to model inputs.

property output_keys: List[str]#

Return example keys to be mapped to model outputs.

class sleap.nn.training.TopdownConfmapsModelTrainer(data_readers: sleap.nn.training.DataReaders, model: sleap.nn.model.Model, config: sleap.nn.config.training_job.TrainingJobConfig, initial_config: Optional[sleap.nn.config.training_job.TrainingJobConfig] = None)[source]#

Trainer for models that output instance centered confidence maps.

property has_offsets: bool#

Whether model is configured to output refinement offsets.

property input_keys: List[str]#

Return example keys to be mapped to model inputs.

property output_keys: List[str]#

Return example keys to be mapped to model outputs.

class sleap.nn.training.Trainer(data_readers: sleap.nn.training.DataReaders, model: sleap.nn.model.Model, config: sleap.nn.config.training_job.TrainingJobConfig, initial_config: Optional[sleap.nn.config.training_job.TrainingJobConfig] = None)[source]#

Base trainer class that provides general model training functionality.

This class is intended to be instantiated using the from_config() class method, which will return the appropriate subclass based on the input configuration.

This class should not be used directly. It is intended to be subclassed by a model output type-specific trainer that provides more specific functionality.

data_readers#

A DataReaders instance that contains training data providers.

Type

sleap.nn.training.DataReaders

model#

A Model instance describing the SLEAP model to train.

Type

sleap.nn.model.Model

config#

A TrainingJobConfig that describes the training parameters.

Type

sleap.nn.config.training_job.TrainingJobConfig

initial_config#

This attribute will contain a copy of the input configuration before any attributes are updated in config.

Type

Optional[sleap.nn.config.training_job.TrainingJobConfig]

pipeline_builder#

A model output type-specific data pipeline builder to create pipelines that generate data used for training. This must be specified in subclasses.

Type

sleap.nn.training.PipelineBuilder

training_pipeline#

The data pipeline that generates examples from the training set for optimization.

Type

sleap.nn.data.pipelines.Pipeline

validation_pipeline#

The data pipeline that generates examples from the validation set for optimization.

Type

sleap.nn.data.pipelines.Pipeline

training_viz_pipeline#

The data pipeline that generates examples from the training set for visualization.

Type

sleap.nn.data.pipelines.Pipeline

validation_viz_pipeline#

The data pipeline that generates examples from the validation set for visualization.

Type

sleap.nn.data.pipelines.Pipeline

optimization_callbacks#

Keras callbacks related to optimization.

Type

List[keras.callbacks.Callback]

output_callbacks#

Keras callbacks related to outputs.

Type

List[keras.callbacks.Callback]

visualization_callbacks#

Keras callbacks related to visualization.

Type

List[keras.callbacks.Callback]

run_path#

The path to the run folder that will contain training results, if any.

Type

Optional[str]

property callbacks: List[keras.callbacks.Callback]#

Return all callbacks currently configured.

cleanup()[source]#

Delete visualization images subdirectory.

evaluate()[source]#

Compute evaluation metrics on data splits and save them.

classmethod from_config(config: sleap.nn.config.training_job.TrainingJobConfig, training_labels: Optional[Union[str, sleap.io.dataset.Labels]] = None, validation_labels: Optional[Union[str, sleap.io.dataset.Labels, float]] = None, test_labels: Optional[Union[str, sleap.io.dataset.Labels]] = None, video_search_paths: Optional[List[str]] = None) sleap.nn.training.Trainer[source]#

Initialize the trainer from a training job configuration.

Parameters
  • config – A TrainingJobConfig instance.

  • training_labels – Training labels to use instead of the ones in the config, if any. If a path is specified, it will overwrite the one in the config.

  • validation_labels – Validation labels to use instead of the ones in the config, if any. If a path is specified, it will overwrite the one in the config.

  • test_labels – Teset labels to use instead of the ones in the config, if any. If a path is specified, it will overwrite the one in the config.

abstract property input_keys#

Implement in subclasses.

property keras_model: keras.engine.training.Model#

Alias for self.model.keras_model.

abstract property output_keys#

Implement in subclasses.

package()[source]#

Package model folder into a zip file for portability.

setup()[source]#

Set up data pipeline and model for training.

train()[source]#

Execute the optimization loop to train the model.

sleap.nn.training.get_timestamp() str[source]#

Return the date and time as a string.

sleap.nn.training.main()[source]#

Create CLI for training and run.

sleap.nn.training.sanitize_scope_name(name: str) str[source]#

Sanitizes string which will be used as TensorFlow scope name.

sleap.nn.training.setup_checkpointing(config: sleap.nn.config.outputs.CheckpointingConfig, run_path: str) List[keras.callbacks.Callback][source]#

Set up model checkpointing callbacks from config.

sleap.nn.training.setup_losses(config: sleap.nn.config.optimization.OptimizationConfig) Callable[[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor], tensorflow.python.framework.ops.Tensor][source]#

Set up model loss function from config.

sleap.nn.training.setup_metrics(config: sleap.nn.config.optimization.OptimizationConfig, part_names: Optional[List[str]] = None) List[Union[keras.losses.Loss, keras.metrics.Metric]][source]#

Set up training metrics from config.

sleap.nn.training.setup_new_run_folder(config: sleap.nn.config.outputs.OutputsConfig, base_run_name: Optional[str] = None) str[source]#

Create a new run folder from config.

sleap.nn.training.setup_optimization_callbacks(config: sleap.nn.config.optimization.OptimizationConfig) List[keras.callbacks.Callback][source]#

Set up optimization callbacks from config.

sleap.nn.training.setup_optimizer(config: sleap.nn.config.optimization.OptimizationConfig) keras.optimizer_v2.optimizer_v2.OptimizerV2[source]#

Set up model optimizer from config.

sleap.nn.training.setup_output_callbacks(config: sleap.nn.config.outputs.OutputsConfig, run_path: Optional[str] = None) List[keras.callbacks.Callback][source]#

Set up training outputs callbacks from config.

sleap.nn.training.setup_tensorboard(config: sleap.nn.config.outputs.TensorBoardConfig, run_path: str) List[keras.callbacks.Callback][source]#

Set up TensorBoard callbacks from config.

sleap.nn.training.setup_visualization(config: sleap.nn.config.outputs.OutputsConfig, run_path: str, viz_fn: Callable[[], matplotlib.figure.Figure], name: str) List[keras.callbacks.Callback][source]#

Set up visualization callbacks from config.

sleap.nn.training.setup_zmq_callbacks(zmq_config: sleap.nn.config.outputs.ZMQConfig) List[keras.callbacks.Callback][source]#

Set up ZeroMQ callbacks from config.