trainer_config
sleap_nn.config.trainer_config
¶
Serializable configuration classes for specifying all trainer config parameters.
These configuration classes are intended to specify all the parameters required to initialize the trainer config.
Classes:
| Name | Description |
|---|---|
CosineAnnealingWarmupConfig |
Configuration for Cosine Annealing with Linear Warmup scheduler. |
DataLoaderConfig |
Train DataLoaderConfig. |
EarlyStoppingConfig |
Configuration for early_stopping. |
EvalConfig |
Configuration for epoch-end evaluation. |
HardKeypointMiningConfig |
Configuration for online hard keypoint mining. |
LRSchedulerConfig |
Configuration for lr_scheduler. |
LinearWarmupLinearDecayConfig |
Configuration for Linear Warmup + Linear Decay scheduler. |
ModelCkptConfig |
Configuration for model checkpoint. |
OptimizerConfig |
Configuration for optimizer. |
ReduceLROnPlateauConfig |
Configuration for ReduceLROnPlateau scheduler. |
StepLRConfig |
Configuration for StepLR scheduler. |
TrainDataLoaderConfig |
Train DataLoaderConfig. |
TrainerConfig |
Configuration for trainer. |
ValDataLoaderConfig |
Validation DataLoaderConfig. |
WandBConfig |
Configuration for WandB. |
ZMQConfig |
Configuration of ZeroMQ-based monitoring of the training. |
Functions:
| Name | Description |
|---|---|
trainer_mapper |
Map the legacy trainer configuration to the new trainer configuration. |
CosineAnnealingWarmupConfig
¶
Configuration for Cosine Annealing with Linear Warmup scheduler.
The learning rate increases linearly during warmup, then decreases following a cosine curve to the minimum value.
Attributes:
| Name | Type | Description |
|---|---|---|
warmup_epochs |
int
|
(int) Number of epochs for linear warmup phase. Default: |
max_epochs |
Optional[int]
|
(int) Total number of training epochs. Will be overridden by
trainer's max_epochs if not specified. Default: |
warmup_start_lr |
float
|
(float) Learning rate at start of warmup. Default: |
eta_min |
float
|
(float) Minimum learning rate at end of cosine decay. Default: |
Source code in sleap_nn/config/trainer_config.py
DataLoaderConfig
¶
Train DataLoaderConfig.
Attributes:
| Name | Type | Description |
|---|---|---|
batch_size |
int
|
(int) Number of samples per batch or batch size for training/validation data. This is the per-GPU batch size; with multi-GPU (DDP) training the effective (global) batch size is |
shuffle |
bool
|
(bool) True to have the data reshuffled at every epoch. Default: |
num_workers |
int
|
(int) Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process. Default: |
Source code in sleap_nn/config/trainer_config.py
EarlyStoppingConfig
¶
Configuration for early_stopping.
Attributes:
| Name | Type | Description |
|---|---|---|
stop_training_on_plateau |
bool
|
(bool) True if early stopping should be enabled. Default: |
min_delta |
float
|
(float) Minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than or equal to min_delta, will count as no improvement. Default: |
patience |
int
|
(int) Number of checks with no improvement after which training will be stopped. Under the default configuration, one check happens after every training epoch. Default: |
Source code in sleap_nn/config/trainer_config.py
EvalConfig
¶
Configuration for epoch-end evaluation.
Attributes:
| Name | Type | Description |
|---|---|---|
enabled |
bool
|
(bool) Enable epoch-end evaluation metrics. Default: |
frequency |
int
|
(int) Evaluate every N epochs. Default: |
oks_stddev |
float
|
(float) OKS standard deviation for evaluation. Default: |
oks_scale |
Optional[float]
|
(float) OKS scale override. If None, uses default. Default: |
match_threshold |
float
|
(float) Match threshold for post-training evaluation.
For centroid models it is the maximum centroid distance in PIXELS
(default |
Source code in sleap_nn/config/trainer_config.py
HardKeypointMiningConfig
¶
Configuration for online hard keypoint mining.
Attributes:
| Name | Type | Description |
|---|---|---|
online_mining |
bool
|
If True, online hard keypoint mining (OHKM) will be enabled. When this is enabled, the loss is computed per keypoint (or edge for PAFs) and sorted from lowest (easy) to highest (hard). The hard keypoint loss will be scaled to have a higher weight in the total loss, encouraging the training to focus on tricky body parts that are more difficult to learn. If False, no mining will be performed and all keypoints will be weighted equally in the loss. Default: |
hard_to_easy_ratio |
float
|
The minimum ratio of the individual keypoint loss with respect to the lowest keypoint loss in order to be considered as "hard". This helps to switch focus on across groups of keypoints during training. Default: |
min_hard_keypoints |
int
|
The minimum number of keypoints that will be considered as "hard", even if they are not below the |
max_hard_keypoints |
Optional[int]
|
The maximum number of hard keypoints to apply scaling to. This can help when there are few very easy keypoints which may skew the ratio and result in loss scaling being applied to most keypoints, which can reduce the impact of hard mining altogether. Default: |
loss_scale |
float
|
Factor to scale the hard keypoint losses by. Default: |
Source code in sleap_nn/config/trainer_config.py
LRSchedulerConfig
¶
Configuration for lr_scheduler.
Only one scheduler should be configured at a time. If multiple are set, priority order is: cosine_annealing_warmup > linear_warmup_linear_decay > step_lr > reduce_lr_on_plateau.
Attributes:
| Name | Type | Description |
|---|---|---|
step_lr |
Optional[StepLRConfig]
|
Configuration for StepLR scheduler. |
reduce_lr_on_plateau |
Optional[ReduceLROnPlateauConfig]
|
Configuration for ReduceLROnPlateau scheduler. |
cosine_annealing_warmup |
Optional[CosineAnnealingWarmupConfig]
|
Configuration for Cosine Annealing with Linear Warmup scheduler. |
linear_warmup_linear_decay |
Optional[LinearWarmupLinearDecayConfig]
|
Configuration for Linear Warmup + Linear Decay scheduler. |
Source code in sleap_nn/config/trainer_config.py
LinearWarmupLinearDecayConfig
¶
Configuration for Linear Warmup + Linear Decay scheduler.
The learning rate increases linearly during warmup, then decreases linearly to the end learning rate.
Attributes:
| Name | Type | Description |
|---|---|---|
warmup_epochs |
int
|
(int) Number of epochs for linear warmup phase. Default: |
max_epochs |
Optional[int]
|
(int) Total number of training epochs. Will be overridden by
trainer's max_epochs if not specified. Default: |
warmup_start_lr |
float
|
(float) Learning rate at start of warmup. Default: |
end_lr |
float
|
(float) Learning rate at end of training. Default: |
Source code in sleap_nn/config/trainer_config.py
ModelCkptConfig
¶
Configuration for model checkpoint.
Any parameters from Lightning's ModelCheckpoint could be used.
Attributes:
| Name | Type | Description |
|---|---|---|
save_top_k |
int
|
(int) If save_top_k == k, the best k models according to the quantity monitored will be saved. If save_top_k == 0, no models are saved. If save_top_k == -1, all models are saved. Please note that the monitors are checked every every_n_epochs epochs. if save_top_k >= 2 and the callback is called multiple times inside an epoch, the name of the saved file will be appended with a version count starting with v1 unless enable_version_counter is set to False. Default: |
save_last |
Optional[bool]
|
(bool) When True, saves a last.ckpt whenever a checkpoint file gets saved. On a local filesystem, this will be a symbolic link, and otherwise a copy of the checkpoint file. This allows accessing the latest checkpoint in a deterministic manner. Default: |
monitor |
str
|
(str) Metric name the checkpoint tracks to pick the "best" model. Any key present in |
mode |
str
|
(str) |
Source code in sleap_nn/config/trainer_config.py
OptimizerConfig
¶
Configuration for optimizer.
Attributes:
| Name | Type | Description |
|---|---|---|
lr |
float
|
(float) Learning rate of type float. Default: |
amsgrad |
bool
|
(bool) Enable AMSGrad with the optimizer. Default: |
Source code in sleap_nn/config/trainer_config.py
ReduceLROnPlateauConfig
¶
Configuration for ReduceLROnPlateau scheduler.
Attributes:
| Name | Type | Description |
|---|---|---|
threshold |
float
|
(float) Threshold for measuring the new optimum, to only focus on significant changes. Default: |
threshold_mode |
str
|
(str) One of "rel", "abs". In rel mode, dynamic_threshold = best * ( 1 + threshold ) in max mode or best * ( 1 - threshold ) in min mode. In abs mode, dynamic_threshold = best + threshold in max mode or best - threshold in min mode. Default: |
cooldown |
int
|
(int) Number of epochs to wait before resuming normal operation after lr has been reduced. Default: |
patience |
int
|
(int) Number of epochs with no improvement after which learning rate will be reduced. For example, if patience = 2, then we will ignore the first 2 epochs with no improvement, and will only decrease the LR after the third epoch if the loss still hasn't improved then. Default: |
factor |
float
|
(float) Factor by which the learning rate will be reduced. new_lr = lr * factor. Default: |
min_lr |
Any
|
(float or List[float]) A scalar or a list of scalars. A lower bound on the learning rate of all param groups or each group respectively. Default: |
Methods:
| Name | Description |
|---|---|
validate_min_lr |
min_lr Validation. |
Source code in sleap_nn/config/trainer_config.py
validate_min_lr()
¶
min_lr Validation.
Ensures min_lr is a float>=0 or list of floats>=0
Source code in sleap_nn/config/trainer_config.py
StepLRConfig
¶
Configuration for StepLR scheduler.
Attributes:
| Name | Type | Description |
|---|---|---|
step_size |
int
|
(int) Period of learning rate decay. If step_size=10, then every 10 epochs, learning rate will be reduced by a factor of gamma. Default: |
gamma |
float
|
(float) Multiplicative factor of learning rate decay. Default: |
Source code in sleap_nn/config/trainer_config.py
TrainDataLoaderConfig
¶
Bases: DataLoaderConfig
Train DataLoaderConfig.
Attributes:
| Name | Type | Description |
|---|---|---|
batch_size |
int
|
(int) Number of samples per batch or batch size for training/validation data. This is the per-GPU batch size; with multi-GPU (DDP) training the effective (global) batch size is |
shuffle |
bool
|
(bool) True to have the data reshuffled at every epoch. Default: |
num_workers |
int
|
(int) Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process. Default: |
Source code in sleap_nn/config/trainer_config.py
TrainerConfig
¶
Configuration for trainer.
Attributes:
| Name | Type | Description |
|---|---|---|
train_data_loader |
TrainDataLoaderConfig
|
(Note: Any parameters from Torch's DataLoader could be used.) |
val_data_loader |
ValDataLoaderConfig
|
(Similar to train_data_loader) |
model_ckpt |
ModelCkptConfig
|
(Note: Any parameters from Lightning's ModelCheckpoint could be used.) |
trainer_num_devices |
ModelCkptConfig
|
(int) Number of devices to use or "auto" to let Lightning decide. If |
trainer_device_indices |
Optional[List[int]]
|
(list) List of device indices to use. For example, |
trainer_accelerator |
str
|
(str) One of the ("cpu", "gpu", "mps", "auto"). "auto" recognises the machine the model is running on and chooses the appropriate accelerator for the Trainer to be connected to. Default: |
profiler |
Optional[str]
|
(str) Profiler for pytorch Trainer. One of ["advanced", "passthrough", "pytorch", "simple"]. Default: |
trainer_strategy |
str
|
(str) Training strategy, one of ["auto", "ddp", "fsdp", "ddp_find_unused_parameters_false", "ddp_find_unused_parameters_true", ...]. This supports any training strategy that is supported by |
enable_progress_bar |
bool
|
(bool) When True, enables printing the logs during training. Default: |
min_train_steps_per_epoch |
int
|
(int) Minimum number of iterations in a single epoch. (Useful if model is trained with very few data points). Refer limit_train_batches parameter of Torch Trainer. Default: |
train_steps_per_epoch |
Optional[int]
|
(int) Number of minibatches (steps) to train for in an epoch. If set to |
visualize_preds_during_training |
bool
|
(bool) If set to |
keep_viz |
bool
|
(bool) If set to |
viz_img_format |
str
|
(str) Image format for the visualization figures saved to the local |
max_epochs |
int
|
(int) Maximum number of epochs to run. Default: |
seed |
Optional[int]
|
(int) Seed value for the current experiment. This ensures deterministic train/val splits across runs, which is critical for safe checkpoint resume. Default: |
use_wandb |
bool
|
(bool) True to enable wandb logging. Default: |
save_ckpt |
bool
|
(bool) True to enable checkpointing. Default: |
ckpt_dir |
Optional[str]
|
(str) Directory path where the |
run_name |
Optional[str]
|
(str) Name of the current run. The ckpts will be created in |
resume_ckpt_path |
Optional[str]
|
(str) Path to |
wandb |
WandBConfig
|
(Only if use_wandb is True, else skip this) |
optimizer_name |
str
|
(str) Optimizer to be used. One of ["Adam", "AdamW"]. Default: |
optimizer |
OptimizerConfig
|
create an optimizer configuration |
lr_scheduler |
Optional[LRSchedulerConfig]
|
create an lr_scheduler configuration |
early_stopping |
EarlyStoppingConfig
|
create an early_stopping configuration |
zmq |
Optional[ZMQConfig]
|
Zmq config with publish and controller port addresses. |
Methods:
| Name | Description |
|---|---|
validate_optimizer_name |
Validate that optimizer_name is one of the allowed values. |
validate_trainer_devices |
Validate the value of trainer_devices. |
Source code in sleap_nn/config/trainer_config.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | |
validate_optimizer_name(value)
staticmethod
¶
Validate that optimizer_name is one of the allowed values.
Source code in sleap_nn/config/trainer_config.py
validate_trainer_devices(value)
staticmethod
¶
Validate the value of trainer_devices.
Source code in sleap_nn/config/trainer_config.py
ValDataLoaderConfig
¶
Bases: DataLoaderConfig
Validation DataLoaderConfig.
Attributes:
| Name | Type | Description |
|---|---|---|
batch_size |
int
|
(int) Number of samples per batch or batch size for training/validation data. This is the per-GPU batch size; with multi-GPU (DDP) training the effective (global) batch size is |
shuffle |
bool
|
(bool) True to have the data reshuffled at every epoch. Default: |
num_workers |
int
|
(int) Number of subprocesses to use for data loading. 0 means that the data will be loaded in the main process. Default: |
Source code in sleap_nn/config/trainer_config.py
WandBConfig
¶
Configuration for WandB.
Only if use_wandb is True, else skip this
Attributes:
| Name | Type | Description |
|---|---|---|
entity |
Optional[str]
|
(str) Entity of wandb project. Default: |
project |
Optional[str]
|
(str) Project name for the wandb project. Default: |
name |
Optional[str]
|
(str) Name of the current run. Default: |
save_viz_imgs_wandb |
bool
|
(bool) If set to |
api_key |
Optional[str]
|
(str) API key. The API key is masked when saved to config files. Default: |
wandb_mode |
Optional[str]
|
(str) "offline" if only local logging is required. Default: |
prv_runid |
Optional[str]
|
(str) Previous run ID if training should be resumed from a previous ckpt. Default: |
group |
Optional[str]
|
(str) Group for wandb logging. Default: |
current_run_id |
Optional[str]
|
(str) Run ID for the current model training. (stored once the training starts). Default: |
viz_enabled |
bool
|
(bool) If True, log pre-rendered matplotlib images to wandb. Default: |
viz_boxes |
bool
|
(bool) If True, log interactive keypoint boxes. Default: |
viz_masks |
bool
|
(bool) If True, log confidence map overlay masks. Default: |
viz_box_size |
float
|
(float) Size of keypoint boxes in pixels (for viz_boxes). Default: |
viz_confmap_threshold |
float
|
(float) Threshold for confidence map masks (for viz_masks). Default: |
log_viz_table |
bool
|
(bool) If True, also log images to a wandb.Table for backwards compatibility. Default: |
delete_local_logs |
Optional[bool]
|
(bool, optional) If True, delete local wandb logs folder after
training. If False, keep the folder. If None (default), automatically delete
if logging online (wandb_mode != "offline") and keep if logging offline.
Default: |
Source code in sleap_nn/config/trainer_config.py
ZMQConfig
¶
Configuration of ZeroMQ-based monitoring of the training.
Attributes:
| Name | Type | Description |
|---|---|---|
controller_port |
Optional[int]
|
Port number of the endpoint to listen for command messages from. "tcp://tcp://127.0.0.1:{port_number}". Set to |
controller_polling_timeout |
int
|
Polling timeout in microseconds specified as an integer. This controls how long the poller should wait to receive a response and should be set to a small value to minimize the impact on training speed. Default: |
publish_port |
Optional[int]
|
Port number of the endpoint to publish updates to. "tcp://tcp://127.0.0.1:{port_number}". Set to |
Source code in sleap_nn/config/trainer_config.py
trainer_mapper(legacy_config)
¶
Map the legacy trainer configuration to the new trainer configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
legacy_config
|
dict
|
A dictionary containing the legacy trainer configuration. |
required |
Returns:
| Type | Description |
|---|---|
TrainerConfig
|
An instance of |
Source code in sleap_nn/config/trainer_config.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | |