deeplabcut.pose_estimation_pytorch.runners.train
Classes:
| Name | Description |
|---|---|
DetectorTrainingRunner |
Runner to train object detection models. |
PoseTrainingRunner |
Runner to train pose estimation models. |
TrainingRunner |
Base TrainingRunner class. |
Functions:
| Name | Description |
|---|---|
build_optimizer |
Builds an optimizer from a configuration. |
build_training_runner |
Build a runner object according to a pytorch configuration file. |
DetectorTrainingRunner
Bases: TrainingRunner[BaseDetector]
Runner to train object detection models.
Methods:
| Name | Description |
|---|---|
__init__ |
Args: |
step |
Perform a single epoch gradient update or validation step. |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
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 | |
__init__
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
BaseDetector
|
The detector model to train. |
required |
|
Optimizer
|
The optimizer to use to train the model. |
required |
|
TrainingRunner kwargs |
{}
|
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
step
Perform a single epoch gradient update or validation step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, Any]
|
Tuple of input image(s) and target(s) for train or valid single step. |
required |
|
str
|
|
'train'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
"Runner must be in train or eval mode, but {mode} was found." |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Tensor]
|
{ 'total_loss': torch.Tensor, 'aux_loss_1': torch.Tensor, ..., } |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
PoseTrainingRunner
Bases: TrainingRunner[PoseModel]
Runner to train pose estimation models.
Methods:
| Name | Description |
|---|---|
__init__ |
Args: |
load_snapshot |
Loads the state dict for a model from a file. |
step |
Perform a single epoch gradient update or validation step. |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
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 417 418 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 | |
__init__
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
PoseModel
|
The neural network for solving pose estimation task. |
required |
|
Optimizer
|
A PyTorch optimizer for updating model parameters. |
required |
|
bool
|
When |
True
|
|
TrainingRunner kwargs |
{}
|
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
load_snapshot
load_snapshot(snapshot_path: str | Path, device: str, model: PoseModel, weights_only: bool | None = None) -> dict
Loads the state dict for a model from a file.
This method loads a file containing a DeepLabCut PyTorch model snapshot onto a given device, and sets the model weights using the state_dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
the path containing the model weights to load |
required |
|
str
|
the device on which the model should be loaded |
required |
|
PoseModel
|
the model for which the weights are loaded |
required |
|
bool | None
|
Value for torch.load() |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The content of the snapshot file. |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
step
Perform a single epoch gradient update or validation step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, Any]
|
Tuple of input image(s) and target(s) for train or valid single step. |
required |
|
str
|
|
'train'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
"Runner must be in train or eval mode, but {mode} was found." |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Tensor]
|
{ "total_loss": aggregate_loss, "aux_loss_1": loss_value, ..., } |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
TrainingRunner
Bases: Runner, Generic[ModelType]
Base TrainingRunner class.
A TrainingRunner is used to fit models to datasets. Subclasses must implement the
step(self, batch, mode) method, which performs a single training or validation
step on a batch of data. The step is different depending on the model type (e.g.
a pose model step vs. an object detector step).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ModelType
|
The model to fit. |
required |
|
dict | Optimizer
|
The optimizer to use to fit the model. |
required |
|
TorchSnapshotManager
|
Manages how snapshots are saved to disk during training. |
required |
|
str
|
The device on which to run training (e.g. 'cpu', 'cuda', 'cuda:0'). |
'cpu'
|
|
list[int] | None
|
Used to specify the GPU indices for multi-GPU training (e.g. [0, 1, 2, 3] to train on 4 GPUs). When a GPUs list is given, the device must be 'cuda'. |
None
|
|
int
|
The interval at which the model will be evaluated while training
(e.g. |
1
|
|
str | Path | None
|
If continuing to train a model, the path to the snapshot to resume training from. |
None
|
|
dict | LRScheduler | None
|
The learning rate scheduler (or it's configuration), if one should be used. |
None
|
|
bool
|
When resuming training (snapshot_path is not None), attempts to load the scheduler state dict from the snapshot. If you've modified your scheduler, set this to False or the old scheduler parameters might be used. |
True
|
|
BaseLogger | None
|
Logger to monitor training (e.g. a WandBLogger). |
None
|
|
str
|
Name of the file in which to store training stats. |
'learning_stats.csv'
|
|
bool | None
|
Value for the torch.load() |
None
|
Methods:
| Name | Description |
|---|---|
fit |
Train model for the specified number of steps. |
state_dict |
Returns: the state dict for the runner |
step |
Perform a single epoch gradient update or validation step. |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
fit
Train model for the specified number of steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
DataLoader
|
Data loader, which is an iterator over train instances. Each batch contains image tensor and heat maps tensor input samples. |
required |
|
DataLoader
|
Data loader used for validation of the model. |
required |
|
int
|
The number of training epochs. |
required |
|
int
|
The number of iterations between each loss print |
required |
Example
runner = Runner(model, optimizer, cfg, device='cuda') runner.fit(train_loader, valid_loader, "example/models" epochs=50)
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
state_dict
Returns: the state dict for the runner
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
step
abstractmethod
Perform a single epoch gradient update or validation step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, Any]
|
the batch data on which to run a step |
required |
|
str
|
"train" or "eval". Defaults to "train". |
'train'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
if mode is not in {"train", "eval"} |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
A dictionary containing the different losses for the step |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
build_optimizer
Builds an optimizer from a configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Module
|
The model to optimize. |
required |
|
dict
|
The configuration for the optimizer. |
required |
Returns:
| Type | Description |
|---|---|
Optimizer
|
The optimizer for the model built according to the given configuration. |
Source code in deeplabcut/pose_estimation_pytorch/runners/train.py
build_training_runner
build_training_runner(
runner_config: dict,
model_folder: Path,
task: Task,
model: Module,
device: str,
gpus: list[int] | None = None,
snapshot_path: str | Path | None = None,
load_head_weights: bool = True,
logger: BaseLogger | None = None,
) -> TrainingRunner
Build a runner object according to a pytorch configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
the configuration for the runner |
required |
|
Path
|
the folder where models should be saved |
required |
|
Task
|
the task the runner will perform |
required |
|
Module
|
the model to run |
required |
|
str
|
the device to use (e.g. {'cpu', 'cuda:0', 'mps'}) |
required |
|
list[int] | None
|
the list of GPU indices to use for multi-GPU training |
None
|
|
str | Path | None
|
the snapshot from which to load the weights |
None
|
|
bool
|
When |
True
|
|
BaseLogger | None
|
the logger to use, if any |
None
|
Returns:
| Type | Description |
|---|---|
TrainingRunner
|
the runner that was built |