Utils
get_builtin_activation_type(activation, **kwargs)
Returns activation class by its name from torch.nn namespace. This function support all modules available from torch.nn and also their lower-case aliases. On top of that, it supports a few aliaes: leaky_relu (LeakyReLU), swish (silu).
act_cls = get_activation_type("LeakyReLU", inplace=True, slope=0.01) act = act_cls()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
activation |
Union[str, None]
|
Activation function name (E.g. ReLU). If None - return nn.Identity |
required |
Returns:
Type | Description |
---|---|
Type[nn.Module]
|
Type of the activation function that is ready to be instantiated |
Source code in src/super_gradients/training/utils/activations_utils.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
batch_distance2bbox(points, distance, max_shapes=None)
Decode distance prediction to bounding box for batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
Tensor
|
[B, ..., 2], "xy" format |
required |
distance |
Tensor
|
[B, ..., 4], "ltrb" format |
required |
max_shapes |
Optional[Tensor]
|
[B, 2], "h,w" format, Shape of the image. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Tensor: Decoded bboxes, "x1y1x2y2" format. |
Source code in src/super_gradients/training/utils/bbox_utils.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Callback
Base callback class with all the callback methods. Derived classes may override one or many of the available events to receive callbacks when such events are triggered by the training loop.
The order of the events is as follows:
on_training_start(context) # called once before training starts, good for setting up the warmup LR
for epoch in range(epochs):
on_train_loader_start(context)
for batch in train_loader:
on_train_batch_start(context)
on_train_batch_loss_end(context) # called after loss has been computed
on_train_batch_backward_end(context) # called after .backward() was called
on_train_batch_gradient_step_start(context) # called before the optimizer step about to happen (gradient clipping, logging of gradients)
on_train_batch_gradient_step_end(context) # called after gradient step was done, good place to update LR (for step-based schedulers)
on_train_batch_end(context)
on_train_loader_end(context)
on_validation_loader_start(context)
for batch in validation_loader:
on_validation_batch_start(context)
on_validation_batch_end(context)
on_validation_loader_end(context)
on_validation_end_best_epoch(context)
on_test_start(context)
for batch in test_loader:
on_test_batch_start(context)
on_test_batch_end(context)
on_test_end(context)
on_average_best_models_validation_start
on_average_best_models_validation_end
on_training_end(context) # called once after training ends.
Correspondence mapping from the old callback API:
on_training_start(context) <-> Phase.PRE_TRAINING for epoch in range(epochs): on_train_loader_start(context) <-> Phase.TRAIN_EPOCH_START for batch in train_loader: on_train_batch_start(context) on_train_batch_loss_end(context) on_train_batch_backward_end(context) <-> Phase.TRAIN_BATCH_END on_train_batch_gradient_step_start(context) on_train_batch_gradient_step_end(context) <-> Phase.TRAIN_BATCH_STEP on_train_batch_end(context) on_train_loader_end(context) <-> Phase.TRAIN_EPOCH_END
on_validation_loader_start(context)
for batch in validation_loader:
on_validation_batch_start(context)
on_validation_batch_end(context) <-> Phase.VALIDATION_BATCH_END
on_validation_loader_end(context) <-> Phase.VALIDATION_EPOCH_END
on_validation_end_best_epoch(context) <-> Phase.VALIDATION_END_BEST_EPOCH
on_test_start(context) for batch in test_loader: on_test_batch_start(context) on_test_batch_end(context) <-> Phase.TEST_BATCH_END on_test_end(context) <-> Phase.TEST_END
on_training_end(context) <-> Phase.POST_TRAINING
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 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 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 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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
|
on_average_best_models_validation_end(context)
Called once after the average model validation has finished. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.AVERAGE_BEST_MODELS_VALIDATION_START.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
|
on_average_best_models_validation_start(context)
Called once after the test was end before the training loop has finished. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.AVERAGE_BEST_MODELS_VALIDATION_START.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 |
|
on_test_batch_end(context)
Called after all forward step have been performed for a given batch and there is nothing left to do. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.TEST_BATCH_END.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 |
|
on_test_batch_start(context)
Called at each batch after getting batch of data from test loader and moving it to target device. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_test_loader_end(context)
Called once at the end of test data loader (after processing the last batch). At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.TEST_END.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 |
|
on_test_loader_start(context)
Called once at the start of test data loader (before getting the first batch). At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_train_batch_backward_end(context)
Called after loss.backward() method was called for a given batch At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_train_batch_end(context)
Called after all forward/backward/optimizer steps have been performed for a given batch and there is nothing left to do. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_train_batch_gradient_step_end(context)
Called after gradient step has been performed. Good place to update LR (for step-based schedulers) At this point, the context argument will have the following attributes: - epoch - batch_idx - inputs - target - metrics_compute_fn - loss_avg_meter - criterion - device - stop_training - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.TRAIN_BATCH_STEP.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
|
on_train_batch_gradient_step_start(context)
Called before the graadient step is about to happen. Good place to clip gradients (with respect to scaler), log gradients to data ratio, etc. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_train_batch_loss_end(context)
Called after model forward and loss computation has been done. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names The corresponding Phase enum value for this event is Phase.TRAIN_BATCH_END.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_train_batch_start(context)
Called at each batch after getting batch of data from data loader and moving it to target device. This event triggered AFTER Trainer.pre_prediction_callback call (If it was defined).
At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - target - metrics_compute_fn - loss_avg_meter - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_train_loader_end(context)
Called each epoch at the end of train data loader (after processing the last batch). At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.TRAIN_EPOCH_END.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_train_loader_start(context)
Called each epoch at the start of train data loader (before getting the first batch). At this point, the context argument will have the following attributes: - optimizer - criterion - device - experiment_name - ckpt_dir - net - sg_logger - train_loader - valid_loader - training_params - checkpoint_params - arch_params - metric_to_watch - valid_metrics The corresponding Phase enum value for this event is Phase.TRAIN_EPOCH_START.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
on_training_end(context)
Called once after the training loop has finished (Due to reaching optimization criterion or because of an error.) At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.POST_TRAINING.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
|
on_training_start(context)
Called once before start of the first epoch At this point, the context argument will have the following attributes: - optimizer - criterion - device - experiment_name - ckpt_dir - net - sg_logger - train_loader - valid_loader - training_params - checkpoint_params - arch_params - metric_to_watch - valid_metrics
The corresponding Phase enum value for this event is Phase.PRE_TRAINING.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
on_validation_batch_end(context)
Called after all forward step / loss / metric computation have been performed for a given batch and there is nothing left to do. At this point, the context argument will have the following attributes: - epoch - batch_idx - inputs - preds - target - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.VALIDATION_BATCH_END.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_validation_batch_start(context)
Called at each batch after getting batch of data from validation loader and moving it to target device. At this point, the context argument will have the following attributes: - epoch - batch_idx - inputs - target - metrics_compute_fn - loss_avg_meter - criterion - device - stop_training - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - loss_logging_items_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
|
on_validation_end_best_epoch(context)
Called each epoch after validation has been performed and the best metric has been achieved. At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.VALIDATION_END_BEST_EPOCH.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_validation_loader_end(context)
Called each epoch at the end of validation data loader (after processing the last batch). At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
The corresponding Phase enum value for this event is Phase.VALIDATION_EPOCH_END.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
on_validation_loader_start(context)
Called each epoch at the start of validation data loader (before getting the first batch). At this point, the context argument will have the following attributes: - epoch - batch_idx - optimizer - inputs - preds - target - metrics_dict - metrics_compute_fn - loss_avg_meter - loss_log_items - criterion - device - stop_training - experiment_name - ckpt_dir - net - lr_warmup_epochs - sg_logger - train_loader - valid_loader - training_params - ddp_silent_mode - checkpoint_params - arch_params - metric_to_watch - valid_metrics - loss_logging_items_names
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
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 |
|
CallbackHandler
Bases: Callback
Runs all callbacks
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callbacks |
List[Callback]
|
Callbacks to be run. |
required |
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 |
|
PhaseCallback
Bases: Callback
Kept here to keep backward compatibility with old code. New callbacks should use Callback class instead. This callback supports receiving only a subset of events defined in Phase enum:
PRE_TRAINING = "PRE_TRAINING" TRAIN_EPOCH_START = "TRAIN_EPOCH_START" TRAIN_BATCH_END = "TRAIN_BATCH_END" TRAIN_BATCH_STEP = "TRAIN_BATCH_STEP" TRAIN_EPOCH_END = "TRAIN_EPOCH_END"
VALIDATION_BATCH_END = "VALIDATION_BATCH_END" VALIDATION_EPOCH_END = "VALIDATION_EPOCH_END" VALIDATION_END_BEST_EPOCH = "VALIDATION_END_BEST_EPOCH"
TEST_BATCH_END = "TEST_BATCH_END" TEST_END = "TEST_END" AVERAGE_BEST_MODELS_VALIDATION_START = "AVERAGE_BEST_MODELS_VALIDATION_START" AVERAGE_BEST_MODELS_VALIDATION_END = "AVERAGE_BEST_MODELS_VALIDATION_END" POST_TRAINING = "POST_TRAINING"
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 |
|
PhaseContext
Represents the input for phase callbacks, and is constantly updated after callback calls.
Source code in src/super_gradients/training/utils/callbacks/base_callbacks.py
36 37 38 39 40 41 42 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 |
|
BinarySegmentationVisualizationCallback
Bases: PhaseCallback
A callback that adds a visualization of a batch of segmentation predictions to context.sg_logger
Parameters:
Name | Type | Description | Default |
---|---|---|---|
phase |
Union[Phase, str]
|
When to trigger the callback. |
required |
freq |
int
|
Frequency (in epochs) to perform this callback. |
required |
batch_idx |
int
|
Batch index to perform visualization for. |
0
|
last_img_idx_in_batch |
int
|
Last image index to add to log. (default=-1, will take entire batch). |
-1
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
CosineLRScheduler
Bases: LRCallbackBase
Hard coded step Cosine anealing learning rate scheduling.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
DeciLabUploadCallback
Bases: PhaseCallback
Post-training callback for uploading and optimizing a model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_meta_data |
Model's meta-data object. Type: ModelMetadata |
required | |
optimization_request_form |
Optimization request form object. Type: OptimizationRequestForm |
required | |
ckpt_name |
str
|
Checkpoint filename, inside the checkpoint directory. |
'ckpt_best.pth'
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
__call__(context)
This function will attempt to upload the trained model and schedule an optimization for it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
Training phase context |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
get_optimization_status(optimized_model_name)
This function will do fetch the optimized version of the trained model and check on its benchmark status. The status will be checked against the server every 30 seconds and the process will timeout after 30 minutes or log about the successful optimization - whichever happens first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimized_model_name |
str
|
Optimized model name |
required |
Returns:
Type | Description |
---|---|
Whether or not the optimized model has been benchmarked |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
upload_model(model)
This function will upload the trained model to the Deci Lab
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
The resulting model from the training process |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
DetectionVisualizationCallback
Bases: PhaseCallback
A callback that adds a visualization of a batch of detection predictions to context.sg_logger
Parameters:
Name | Type | Description | Default |
---|---|---|---|
phase |
Union[Phase, str]
|
When to trigger the callback. |
required |
freq |
int
|
Frequency (in epochs) to perform this callback. |
required |
batch_idx |
int
|
Batch index to perform visualization for. |
0
|
classes |
list
|
Class list of the dataset. |
required |
last_img_idx_in_batch |
int
|
Last image index to add to log. (default=-1, will take entire batch). |
-1
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
ExponentialLRScheduler
Bases: LRCallbackBase
Exponential decay learning rate scheduling. Decays the learning rate by lr_decay_factor
every epoch.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
ExtremeBatchCaseVisualizationCallback
Bases: Callback
, ABC
ExtremeBatchCaseVisualizationCallback
A base class for visualizing worst/best validation batches in an epoch according to some metric or loss value, with Full DDP support.
Images are saved with training_hyperparams.sg_logger.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
Optional[Metric]
|
Metric, will be the metric which is monitored. |
None
|
metric_component_name |
Optional[str]
|
In case metric returns multiple values (as Mapping), the value at metric.compute()[metric_component_name] will be the one monitored. |
None
|
loss_to_monitor |
Optional[str]
|
str, loss_to_monitor corresponfing to the 'criterion' passed through training_params in Trainer.train(...). Monitoring loss follows the same logic as metric_to_watch in Trainer.train(..), when watching the loss and should be: if hasattr(criterion, "component_names") and criterion.forward(..) returns a tuple: |
None
|
max |
bool
|
bool, Whether to take the batch corresponding to the max value of the metric/loss or the minimum (default=False). |
False
|
freq |
int
|
int, epoch frequency to perform all of the above (default=1). Inheritors should implement process_extreme_batch which returns an image, as np.ndarray (uint8) with shape BHWC. |
1
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 |
|
__init__(metric=None, metric_component_name=None, loss_to_monitor=None, max=False, freq=1, enable_on_train_loader=False, enable_on_valid_loader=True, max_images=-1)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
Optional[Metric]
|
Metric, will be the metric which is monitored. |
None
|
metric_component_name |
Optional[str]
|
In case metric returns multiple values (as Mapping), the value at metric.compute()[metric_component_name] will be the one monitored. |
None
|
loss_to_monitor |
Optional[str]
|
str, loss_to_monitor corresponding to the 'criterion' passed through training_params in Trainer.train(...). Monitoring loss follows the same logic as metric_to_watch in Trainer.train(..), when watching the loss and should be: if hasattr(criterion, "component_names") and criterion.forward(..) returns a tuple: |
None
|
max |
bool
|
bool, Whether to take the batch corresponding to the max value of the metric/loss or the minimum (default=False). |
False
|
freq |
int
|
int, epoch frequency to perform all of the above (default=1). |
1
|
enable_on_train_loader |
bool
|
Controls whether to enable this callback on the train loader. Default is False. |
False
|
enable_on_valid_loader |
bool
|
Controls whether to enable this callback on the valid loader. Default is True. |
True
|
max_images |
int
|
Maximum images to save. If -1, save all images. |
-1
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
|
process_extreme_batch()
abstractmethod
This method is called right before adding the images to the in SGLoggger (inside the on_validation_loader_end call). It should process self.extreme_batch, self.extreme_preds and self.extreme_targets and output the images, as np.ndarrray. Output should be of shape N,H,W,3 and uint8.
Returns:
Type | Description |
---|---|
np.ndarray
|
images to save, np.ndarray |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
1135 1136 1137 1138 1139 1140 1141 1142 1143 |
|
ExtremeBatchDetectionVisualizationCallback
Bases: ExtremeBatchCaseVisualizationCallback
ExtremeBatchSegVisualizationCallback
Visualizes worst/best batch in an epoch for Object detection. For clarity, the batch is saved twice in the SG Logger, once with the model's predictions and once with ground truth targets.
Assumptions on bbox dormats: - After applying post_prediction_callback on context.preds, the predictions are a list/Tensor s.t: predictions[i] is a tensor of shape nx6 - (x1, y1, x2, y2, confidence, class) where x and y are in pixel units.
- context.targets is a tensor of shape (total_num_targets, 6), in LABEL_CXCYWH format: (index, label, cx, cy, w, h).
Example usage in Yaml config:
training_hyperparams:
phase_callbacks:
- ExtremeBatchDetectionVisualizationCallback:
metric:
DetectionMetrics_050:
score_thres: 0.1
top_k_predictions: 300
num_cls: ${num_classes}
normalize_targets: True
post_prediction_callback:
_target_: super_gradients.training.models.detection_models.pp_yolo_e.PPYoloEPostPredictionCallback
score_threshold: 0.01
nms_top_k: 1000
max_predictions: 300
nms_threshold: 0.7
metric_component_name: 'mAP@0.50'
post_prediction_callback:
_target_: super_gradients.training.models.detection_models.pp_yolo_e.PPYoloEPostPredictionCallback
score_threshold: 0.25
nms_top_k: 1000
max_predictions: 300
nms_threshold: 0.7
normalize_targets: True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
Optional[Metric]
|
Metric, will be the metric which is monitored. |
None
|
metric_component_name |
Optional[str]
|
In case metric returns multiple values (as Mapping), the value at metric.compute()[metric_component_name] will be the one monitored. |
None
|
loss_to_monitor |
Optional[str]
|
str, loss_to_monitor corresponding to the 'criterion' passed through training_params in Trainer.train(...). Monitoring loss follows the same logic as metric_to_watch in Trainer.train(..), when watching the loss and should be: if hasattr(criterion, "component_names") and criterion.forward(..) returns a tuple: |
None
|
max |
bool
|
bool, Whether to take the batch corresponding to the max value of the metric/loss or the minimum (default=False). |
False
|
freq |
int
|
int, epoch frequency to perform all of the above (default=1). |
1
|
classes |
Optional[List[str]]
|
List[str], a list of class names corresponding to the class indices for display. When None, will try to fetch this through a "classes" attribute of the valdiation dataset. If such attribute does not exist an error will be raised (default=None). |
None
|
normalize_targets |
bool
|
bool, whether to scale the target bboxes. If the bboxes returned by the validation data loader are in pixel values range, this needs to be set to True (default=False) |
False
|
enable_on_train_loader |
bool
|
Controls whether to enable this callback on the train loader. Default is False. |
False
|
enable_on_valid_loader |
bool
|
Controls whether to enable this callback on the valid loader. Default is True. |
True
|
max_images |
int
|
Maximum images to save. If -1, save all images. |
-1
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 |
|
process_extreme_batch()
Processes the extreme batch, and returns list of images for visualization. Default implementations stacks GT and prediction overlays horisontally.
Returns:
Type | Description |
---|---|
np.ndarray
|
np.ndarray A 4D tensor of BHWC shape with visualizations of the extreme batch. |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 |
|
universal_undo_preprocessing_fn(inputs)
staticmethod
A universal reversing of preprocessing to be passed to DetectionVisualization.visualize_batch's undo_preprocessing_func kwarg. This function scales input tensor to 0..255 range, and cast it to uint8 dtype.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
torch.Tensor
|
Input 4D tensor of images in BCHW format with unknown normalization. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
Numpy 4D tensor of images in BHWC format, normalized to 0..255 range (uint8). |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
|
ExtremeBatchSegVisualizationCallback
Bases: ExtremeBatchCaseVisualizationCallback
ExtremeBatchSegVisualizationCallback
Visualizes worst/best batch in an epoch, for segmentation. Assumes context.preds in validation is a score tensor of shape BCHW, or a tuple whose first item is one.
True predictions will be marked with green, false ones with red.
Example usage in training_params definition:
training_hyperparams ={
...
"phase_callbacks":
[ExtremeBatchSegVisualizationCallback(
metric=IoU(20, ignore_idx=19)
max=False
ignore_idx=19),
ExtremeBatchSegVisualizationCallback(
loss_to_monitor="CrossEntropyLoss"
max=True
ignore_idx=19)]
...}
Example usage in Yaml config:
training_hyperparams:
phase_callbacks:
- ExtremeBatchSegVisualizationCallback:
loss_to_monitor: DiceCEEdgeLoss/aux_loss0
ignore_idx: 19
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
Optional[Metric]
|
Metric, will be the metric which is monitored. |
None
|
metric_component_name |
Optional[str]
|
In case metric returns multiple values (as Mapping), the value at metric.compute()[metric_component_name] will be the one monitored. |
None
|
loss_to_monitor |
Optional[str]
|
str, loss_to_monitor corresponding to the 'criterion' passed through training_params in Trainer.train(...). Monitoring loss follows the same logic as metric_to_watch in Trainer.train(..), when watching the loss and should be: if hasattr(criterion, "component_names") and criterion.forward(..) returns a tuple: |
None
|
max |
bool
|
bool, Whether to take the batch corresponding to the max value of the metric/loss or the minimum (default=False). |
False
|
freq |
int
|
int, epoch frequency to perform all of the above (default=1). |
1
|
enable_on_train_loader |
bool
|
Controls whether to enable this callback on the train loader. Default is False. |
False
|
enable_on_valid_loader |
bool
|
Controls whether to enable this callback on the valid loader. Default is True. |
True
|
max_images |
int
|
Maximum images to save. If -1, save all images. |
-1
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 |
|
FunctionLRScheduler
Bases: LRCallbackBase
Hard coded rate scheduling for user defined lr scheduling function.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
IllegalLRSchedulerMetric
Bases: Exception
Exception raised illegal combination of training parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric_name |
str
|
Name of the metric that is not supported. |
required |
metrics_dict |
dict
|
Dictionary of metrics that are supported. |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
546 547 548 549 550 551 552 553 554 555 |
|
LRCallbackBase
Bases: PhaseCallback
Base class for hard coded learning rate scheduling regimes, implemented as callbacks.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
is_lr_scheduling_enabled(context)
Predicate that controls whether to perform lr scheduling based on values in context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
PhaseContext: current phase's context. |
required |
Returns:
Type | Description |
---|---|
bool, whether to apply lr scheduling or not. |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
252 253 254 255 256 257 258 259 |
|
perform_scheduling(context)
Performs lr scheduling based on values in context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
PhaseContext: current phase's context. |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
261 262 263 264 265 266 267 |
|
LRSchedulerCallback
Bases: PhaseCallback
Learning rate scheduler callback.
When passing call a metrics_dict, with a key=self.metric_name, the value of that metric will monitored for ReduceLROnPlateau (i.e step(metrics_dict[self.metric_name]).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scheduler |
torch.optim.lr_scheduler._LRScheduler
|
Learning rate scheduler to be called step() with. |
required |
metric_name |
str
|
Metric name for ReduceLROnPlateau learning rate scheduler. |
None
|
phase |
Union[Phase, str]
|
Phase of when to trigger it. |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
LinearBatchLRWarmup
Bases: Callback
LR scheduling callback for linear step warmup on each batch step. LR climbs from warmup_initial_lr with to initial lr.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
317 318 319 320 321 322 323 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 |
|
__init__(warmup_initial_lr, initial_lr, train_loader_len, lr_warmup_steps, training_params, net, **kwargs)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
warmup_initial_lr |
float
|
Starting learning rate |
required |
initial_lr |
float
|
Target learning rate after warmup |
required |
train_loader_len |
int
|
Length of train data loader |
required |
lr_warmup_steps |
int
|
Optional. If passed, will use fixed number of warmup steps to warmup LR. Default is None. |
required |
kwargs |
{}
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.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 |
|
update_lr(optimizer, epoch, batch_idx=None)
Same as in LRCallbackBase
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimizer |
required | ||
epoch |
required | ||
batch_idx |
None
|
Returns:
Type | Description |
---|---|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
381 382 383 384 385 386 387 388 389 390 391 |
|
LinearEpochLRWarmup
Bases: LRCallbackBase
LR scheduling callback for linear step warmup. This scheduler uses a whole epoch as single step. LR climbs from warmup_initial_lr with even steps to initial lr. When warmup_initial_lr is None - LR climb starts from initial_lr/(1+warmup_epochs).
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
ModelConversionCheckCallback
Bases: PhaseCallback
Pre-training callback that verifies model conversion to onnx given specified conversion parameters.
The model is converted, then inference is applied with onnx runtime.
Use this callback with the same args as DeciPlatformCallback to prevent conversion fails at the end of training.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
Model's name |
required |
input_dimensions |
Sequence[int]
|
Model's input dimensions |
required |
primary_batch_size |
int
|
Model's primary batch size |
required |
opset_version |
(default=11) |
required | |
do_constant_folding |
(default=True) |
required | |
dynamic_axes |
(default={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}) |
required | |
input_names |
(default=["input"]) |
required | |
output_names |
(default=["output"]) |
required | |
rtol |
(default=1e-03) |
required | |
atol |
(default=1e-05) |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
40 41 42 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 |
|
PhaseContextTestCallback
Bases: PhaseCallback
A callback that saves the phase context the for testing.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
611 612 613 614 615 616 617 618 619 620 621 |
|
PolyLRScheduler
Bases: LRCallbackBase
Hard coded polynomial decay learning rate scheduling (i.e at specific milestones).
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
|
RoboflowResultCallback
Bases: Callback
Append the training results to a csv file. Be aware that this does not fully overwrite the existing file, just appends.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 |
|
__init__(dataset_name, output_path=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_name |
str
|
Name of the dataset that was used to train the model. |
required |
output_path |
Optional[str]
|
Full path to the output csv file. By default, save at 'checkpoint_dir/results.csv' |
None
|
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
746 747 748 749 750 751 752 753 754 755 756 757 |
|
SlidingWindowValidationCallback
Bases: Callback
Performing single-scale sliding window during inference at the last epoch on the validation set and on the average model.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 |
|
StepLRScheduler
Bases: LRCallbackBase
Hard coded step learning rate scheduling (i.e at specific milestones).
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
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 |
|
TestLRCallback
Bases: PhaseCallback
Phase callback that collects the learning rates in lr_placeholder at the end of each epoch (used for testing). In the case of multiple parameter groups (i.e multiple learning rates) the learning rate is collected from the first one. The phase is VALIDATION_EPOCH_END to ensure all lr updates have been performed before calling this callback.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
768 769 770 771 772 773 774 775 776 777 778 779 780 |
|
TrainingStageSwitchCallbackBase
Bases: PhaseCallback
TrainingStageSwitchCallback
A phase callback that is called at a specific epoch (epoch start) to support multi-stage training. It does so by manipulating the objects inside the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
next_stage_start_epoch |
int
|
Epoch idx to apply the stage change. |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
|
apply_stage_change(context)
This method is called when the callback is fired on the next_stage_start_epoch, and holds the stage change logic that should be applied to the context's objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context |
PhaseContext
|
PhaseContext, context of current phase |
required |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
711 712 713 714 715 716 717 718 |
|
YoloXTrainingStageSwitchCallback
Bases: TrainingStageSwitchCallbackBase
YoloXTrainingStageSwitchCallback
Training stage switch for YoloX training. Disables mosaic, and manipulates YoloX loss to use L1.
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
|
create_lr_scheduler_callback(lr_mode, train_loader, net, training_params, update_param_groups, optimizer)
Creates the phase callback in charge of LR scheduling, to be used by Trainer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lr_mode |
Union[str, Mapping]
|
Union[str, Mapping], When str: Learning rate scheduling policy, one of ['StepLRScheduler','PolyLRScheduler','CosineLRScheduler','FunctionLRScheduler']. 'StepLRScheduler' refers to constant updates at epoch numbers passed through |
required |
train_loader |
DataLoader
|
DataLoader, the Trainer.train_loader used for training. |
required |
net |
torch.nn.Module
|
torch.nn.Module, the Trainer.net used for training. |
required |
training_params |
Mapping
|
Mapping, Trainer.training_params. |
required |
update_param_groups |
bool
|
bool, Whether the Trainer.net has a specific way of updaitng its parameter group. |
required |
optimizer |
torch.optim.Optimizer
|
The optimizer used for training. Will be passed to the LR callback's init (or the torch scheduler's init, depending on the lr_mode value as described above). |
required |
Returns:
Type | Description |
---|---|
PhaseCallback
|
a PhaseCallback instance to be used by Trainer for LR scheduling. |
Source code in src/super_gradients/training/utils/callbacks/callbacks.py
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 |
|
ExtremeBatchPoseEstimationVisualizationCallback
Bases: ExtremeBatchCaseVisualizationCallback
ExtremeBatchPoseEstimationVisualizationCallback
Visualizes worst/best batch in an epoch for pose estimation task. This class visualize horizontally-stacked GT and predicted poses. It requires a key 'gt_samples' (List[PoseEstimationSample]) to be present in additional_batch_items dictionary.
Supported models: YoloNASPose Supported datasets: COCOPoseEstimationDataset
Example usage in Yaml config:
training_hyperparams:
phase_callbacks:
- ExtremeBatchPoseEstimationVisualizationCallback:
keypoint_colors: ${dataset_params.keypoint_colors}
edge_colors: ${dataset_params.edge_colors}
edge_links: ${dataset_params.edge_links}
loss_to_monitor: YoloNASPoseLoss/loss
max: True
freq: 1
max_images: 16
enable_on_train_loader: True
enable_on_valid_loader: True
post_prediction_callback:
_target_: super_gradients.training.models.pose_estimation_models.yolo_nas_pose.YoloNASPosePostPredictionCallback
pose_confidence_threshold: 0.01
nms_iou_threshold: 0.7
pre_nms_max_predictions: 300
post_nms_max_predictions: 30
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
Optional[Metric]
|
Metric, will be the metric which is monitored. |
None
|
metric_component_name |
Optional[str]
|
In case metric returns multiple values (as Mapping), the value at metric.compute()[metric_component_name] will be the one monitored. |
None
|
loss_to_monitor |
Optional[str]
|
str, loss_to_monitor corresponding to the 'criterion' passed through training_params in Trainer.train(...). Monitoring loss follows the same logic as metric_to_watch in Trainer.train(..), when watching the loss and should be: if hasattr(criterion, "component_names") and criterion.forward(..) returns a tuple: |
None
|
max |
bool
|
bool, Whether to take the batch corresponding to the max value of the metric/loss or the minimum (default=False). |
False
|
freq |
int
|
int, epoch frequency to perform all of the above (default=1). |
1
|
classes |
List[str], a list of class names corresponding to the class indices for display. When None, will try to fetch this through a "classes" attribute of the valdiation dataset. If such attribute does not exist an error will be raised (default=None). |
required | |
normalize_targets |
bool, whether to scale the target bboxes. If the bboxes returned by the validation data loader are in pixel values range, this needs to be set to True (default=False) |
required |
Source code in src/super_gradients/training/utils/callbacks/extreme_batch_pose_visualization_callback.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
process_extreme_batch()
Processes the extreme batch, and returns batche of images for visualization - predictions and GT poses stacked horizontally.
Returns:
Type | Description |
---|---|
np.ndarray
|
np.ndarray - the visualization of predictions and GT |
Source code in src/super_gradients/training/utils/callbacks/extreme_batch_pose_visualization_callback.py
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 |
|
universal_undo_preprocessing_fn(inputs)
classmethod
A universal reversing of preprocessing to be passed to DetectionVisualization.visualize_batch's undo_preprocessing_func kwarg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
torch.Tensor
|
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
Source code in src/super_gradients/training/utils/callbacks/extreme_batch_pose_visualization_callback.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
PPYoloETrainingStageSwitchCallback
Bases: TrainingStageSwitchCallbackBase
PPYoloETrainingStageSwitchCallback
Training stage switch for PPYolo training. It changes static bbox assigner to a task aligned assigned after certain number of epochs passed
Source code in src/super_gradients/training/utils/callbacks/ppyoloe_switch_callback.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
DefaultCheckpointSolver
Implements the default behavior from adaptive_load_state_dict. If the model state dict and checkpoint state dict has no 1:1 matching by name, then default solver uses simple ordered matching. It assumes that order of layers in the checkpoint is the same as in the model and iterates over them simultaneously. If shape of the source and recipient tensors are different, solver raises an error.
Source code in src/super_gradients/training/utils/checkpoint_utils.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
__call__(model_state_dict, checkpoint_state_dict)
Map checkpoint state_dict to model state_dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_state_dict |
Mapping[str, Tensor]
|
(Mapping[str, Tensor]) A checkpoint state dict |
required |
checkpoint_state_dict |
Mapping[str, Tensor]
|
(Mapping[str, Tensor]) A model state dict |
required |
Returns:
Type | Description |
---|---|
Mapping[str, Tensor]
|
(Mapping[str, Tensor]) New checkpoint state dict with keys/values converted to match model state_dict |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
MissingPretrainedWeightsException
Bases: Exception
Exception raised by unsupported pretrianed model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
desc |
explanation of the error |
required |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
1537 1538 1539 1540 1541 1542 1543 1544 1545 |
|
YoloXCheckpointSolver
Implementation of checkpoint solver for old YoloX model checkpoints.
Source code in src/super_gradients/training/utils/checkpoint_utils.py
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 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 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 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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 |
|
generate_mapping_table()
classmethod
Helper method to generate mapping table between olx YoloX checkpoints and the current YoloX layer names.
Returns:
Type | Description |
---|---|
Mapping[str, str]
|
A mapping dictionary {checkpoint_key: model_key} |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
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 |
|
adapt_state_dict_to_fit_model_layer_names(model_state_dict, source_ckpt, exclude=[], solver=None)
Given a model state dict and source checkpoints, the method tries to correct the keys in the model_state_dict to fit the ckpt in order to properly load the weights into the model. If unsuccessful - returns None :param model_state_dict: the model state_dict :param source_ckpt: checkpoint dict :param exclude optional list for excluded layers :param solver: callable with signature (ckpt_key, ckpt_val, model_key, model_val) that returns a desired weight for ckpt_val. :return: renamed checkpoint dict (if possible)
Source code in src/super_gradients/training/utils/checkpoint_utils.py
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 |
|
adaptive_load_state_dict(net, state_dict, strict, solver=None)
Adaptively loads state_dict to net, by adapting the state_dict to net's layer names first.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
torch.nn.Module
|
(nn.Module) to load state_dict to |
required |
state_dict |
dict
|
(dict) Checkpoint state_dict |
required |
strict |
Union[bool, StrictLoad]
|
(StrictLoad) key matching strictness |
required |
solver |
callable with signature (ckpt_key, ckpt_val, model_key, model_val) that returns a desired weight for ckpt_val. |
None
|
Returns:
Type | Description |
---|---|
Source code in src/super_gradients/training/utils/checkpoint_utils.py
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 |
|
copy_ckpt_to_local_folder(local_ckpt_destination_dir, ckpt_filename, remote_ckpt_source_dir=None, path_src='local', overwrite_local_ckpt=False, load_weights_only=False)
Copy the checkpoint from any supported source to a local destination path :param local_ckpt_destination_dir: destination where the checkpoint will be saved to :param ckpt_filename: ckpt_best.pth Or ckpt_latest.pth :param remote_ckpt_source_dir: Name of the source checkpoint to be loaded (S3 Modelull URL) :param path_src: S3 / url :param overwrite_local_ckpt: determines if checkpoint will be saved in destination dir or in a temp folder
:return: Path to checkpoint
Source code in src/super_gradients/training/utils/checkpoint_utils.py
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 |
|
get_scheduler_state(scheduler)
Wrapper for getting a torch lr scheduler state dict, resolving some issues with CyclicLR (see https://github.com/pytorch/pytorch/pull/91400)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scheduler |
torch.optim.lr_scheduler._LRScheduler, the scheduler |
required |
Returns:
Type | Description |
---|---|
Dict[str, Tensor]
|
the scheduler's state_dict |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 |
|
load_checkpoint_to_model(net, ckpt_local_path, load_backbone=False, strict=StrictLoad.NO_KEY_MATCHING, load_weights_only=False, load_ema_as_net=False, load_processing_params=False)
Loads the state dict in ckpt_local_path to net and returns the checkpoint's state dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net |
torch.nn.Module
|
Network to load the checkpoint to |
required |
ckpt_local_path |
str
|
Local path to the checkpoint file |
required |
load_ema_as_net |
bool
|
Will load the EMA inside the checkpoint file to the network when set |
False
|
load_backbone |
bool
|
Whether to load the checkpoint as a backbone |
False
|
strict |
Union[str, StrictLoad]
|
See super_gradients.common.data_types.enum.strict_load.StrictLoad class documentation for details (default=NO_KEY_MATCHING to suport SG trained checkpoints) |
StrictLoad.NO_KEY_MATCHING
|
load_weights_only |
bool
|
Whether to ignore all other entries other then "net". |
False
|
load_processing_params |
bool
|
Whether to call set_dataset_processing_params on "processing_params" entry inside the checkpoint file (default=False). |
False
|
Returns:
Type | Description |
---|---|
Source code in src/super_gradients/training/utils/checkpoint_utils.py
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 |
|
load_pretrained_weights(model, architecture, pretrained_weights)
Loads pretrained weights from the MODEL_URLS dictionary to model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
architecture |
str
|
name of the model's architecture |
required |
model |
torch.nn.Module
|
model to load pretrinaed weights for |
required |
pretrained_weights |
str
|
name for the pretrianed weights (i.e imagenet) |
required |
Returns:
Type | Description |
---|---|
None |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 |
|
load_pretrained_weights_local(model, architecture, pretrained_weights)
Loads pretrained weights from the MODEL_URLS dictionary to model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
architecture |
str
|
name of the model's architecture |
required |
model |
torch.nn.Module
|
model to load pretrinaed weights for |
required |
pretrained_weights |
str
|
path tp pretrained weights |
required |
Returns:
Type | Description |
---|---|
None |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 |
|
maybe_remove_module_prefix(state_dict, prefix='module.')
Checks is all the keys in state_dict
start with prefix
and if this is true removes this prefix.
This function is intended to drop a "module." prefix from all keys in checkpoint that was saved
with DataParallel/DistributedDataParallel wrapper.
Since SG 3.1 we changed this behavior and always unwrap the model before saving the state_dict. However, to keep the compatibility with older checkpoints, we must do the 'cleanup' before loading the state_dict.
Returns:
Type | Description |
---|---|
Mapping[str, Tensor]
|
state_dict: The model state_dict after removing the prefix |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
raise_informative_runtime_error(state_dict, checkpoint, exception_msg)
Given a model state dict and source checkpoints, the method calls "adapt_state_dict_to_fit_model_layer_names" and enhances the exception_msg if loading the checkpoint_dict via the conversion method is possible
Source code in src/super_gradients/training/utils/checkpoint_utils.py
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 |
|
read_ckpt_state_dict(ckpt_path, device='cpu')
Reads a checkpoint state dict from a given path or url
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_path |
str
|
Checkpoint path or url |
required |
device |
Target device where tensors should be loaded |
'cpu'
|
Returns:
Type | Description |
---|---|
Mapping[str, torch.Tensor]
|
Checkpoint state dict object |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
transfer_weights(model, model_state_dict)
Copy weights from model_state_dict
to model
, skipping layers that are incompatible (Having different shape).
This method is helpful if you are doing some model surgery and want to load
part of the model weights into different model.
This function will go over all the layers in model_state_dict
and will try to find a matching layer in model
and
copy the weights into it. If shape will not match, the layer will be skipped.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
Model to load weights into |
required |
model_state_dict |
Mapping[str, Tensor]
|
Model state dict to load weights from |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in src/super_gradients/training/utils/checkpoint_utils.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
BaseDatasetAdapterCollateFN
Bases: ABC
Base Collate function that adapts an input data to SuperGradients format
This is done by applying the adapter logic either before or after the original collate function, depending on whether the adapter was set up on a batch or a sample.
Note that the original collate function (if any) will still be used, but will be wrapped into this class.
Source code in src/super_gradients/training/utils/collate_fn/adapters/base_adapter_collate_fn.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(adapter, base_collate_fn)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adapter |
BaseDatasetAdapter
|
Dataset adapter to use |
required |
base_collate_fn |
Callable
|
Collate function to wrap. If None, the default collate function will be used. |
required |
Source code in src/super_gradients/training/utils/collate_fn/adapters/base_adapter_collate_fn.py
22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
ClassificationDatasetAdapterCollateFN
Bases: BaseDatasetAdapterCollateFN
Classification Collate function that adapts an input data to SuperGradients format
This is done by applying the adapter logic either before or after the original collate function, depending on whether the adapter was set up on a batch or a sample.
Note that the original collate function (if any) will still be used, but will be wrapped into this class.
Source code in src/super_gradients/training/utils/collate_fn/adapters/classification_adapter_collate_fn.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
__init__(config=None, config_path=None, base_collate_fn=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
Optional[ClassificationDataConfig]
|
Adapter configuration. Use this if you want to hard code some specificities about your dataset. Mutually exclusive with |
None
|
config_path |
Optional[str]
|
Adapter cache path. Use this if you want to load and/or save the adapter config from a local path. Mutually exclusive with |
None
|
base_collate_fn |
Optional[Callable]
|
Collate function to use. Use this if you .If None, the pytorch default collate function will be used. |
None
|
Source code in src/super_gradients/training/utils/collate_fn/adapters/classification_adapter_collate_fn.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
DetectionDatasetAdapterCollateFN
Bases: BaseDatasetAdapterCollateFN
Detection Collate function that adapts an input data to SuperGradients format for YOLOX, YOLONAS and PPYOLOE.
This is done by applying the adapter logic either before or after the original collate function, depending on whether the adapter was set up on a batch or a sample.
Note that the original collate function (if any) will still be used, but will be wrapped into this class.
Source code in src/super_gradients/training/utils/collate_fn/adapters/detection_adapter_collate_fn.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(config=None, config_path=None, base_collate_fn=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
Optional[DetectionDataConfig]
|
Adapter configuration. Use this if you want to hard code some specificities about your dataset. Mutually exclusive with |
None
|
config_path |
Optional[str]
|
Adapter cache path. Use this if you want to load and/or save the adapter config from a local path. Mutually exclusive with |
None
|
base_collate_fn |
Optional[Callable]
|
Collate function to use. Use this if you .If None, the pytorch default collate function will be used. |
None
|
Source code in src/super_gradients/training/utils/collate_fn/adapters/detection_adapter_collate_fn.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
ensure_flat_bbox_batch(bbox_batch)
Flatten a batched bounding box tensor and prepend the batch ID to each bounding box. Excludes padding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bbox_batch |
torch.Tensor
|
Bounding box tensor of shape (BS, PaddingSize, 5). |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
Flattened tensor of shape (N, 6), where N <= BS * PaddingSize. |
Source code in src/super_gradients/training/utils/collate_fn/adapters/detection_adapter_collate_fn.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
SegmentationDatasetAdapterCollateFN
Bases: BaseDatasetAdapterCollateFN
Segmentation Collate function that adapts an input data to SuperGradients format
This is done by applying the adapter logic either before or after the original collate function, depending on whether the adapter was set up on a batch or a sample.
Note that the original collate function (if any) will still be used, but will be wrapped into this class.
Source code in src/super_gradients/training/utils/collate_fn/adapters/segmentation_adapter_collate_fn.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
__init__(config=None, config_path=None, base_collate_fn=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
Optional[SegmentationDataConfig]
|
Adapter configuration. Use this if you want to hard code some specificities about your dataset. Mutually exclusive with |
None
|
config_path |
Optional[str]
|
Adapter cache path. Use this if you want to load and/or save the adapter config from a local path. Mutually exclusive with |
None
|
base_collate_fn |
Optional[Callable]
|
Collate function to use. Use this if you .If None, the pytorch default collate function will be used. |
None
|
Source code in src/super_gradients/training/utils/collate_fn/adapters/segmentation_adapter_collate_fn.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
CrowdDetectionCollateFN
Bases: DetectionCollateFN
Collate function for Yolox training with additional_batch_items that includes crowd targets
Source code in src/super_gradients/training/utils/collate_fn/crowd_detection_collate_fn.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
CrowdDetectionPPYoloECollateFN
Bases: PPYoloECollateFN
Collate function for Yolox training with additional_batch_items that includes crowd targets
Source code in src/super_gradients/training/utils/collate_fn/crowd_detection_ppyoloe_collate_fn.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
__init__(random_resize_sizes=None, random_resize_modes=None, random_aspect_ratio=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
random_resize_sizes |
Union[List[int], None]
|
List of sizes to randomly resize the image to. If None, will not resize. |
None
|
random_resize_modes |
Union[List[int], None]
|
List of interpolation modes to randomly resize the image to. If None, will not resize. |
None
|
random_aspect_ratio |
bool
|
If True, will randomly choose both width and height from random_resize_sizes. If False, will randomly choose only value which will be the width and height of the images. |
False
|
Source code in src/super_gradients/training/utils/collate_fn/crowd_detection_ppyoloe_collate_fn.py
16 17 18 19 20 21 22 23 24 25 26 |
|
DetectionCollateFN
Collate function for Yolox training
Source code in src/super_gradients/training/utils/collate_fn/detection_collate_fn.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
PPYoloECollateFN
Bases: DetectionCollateFN
Collate function for PPYoloE training
Source code in src/super_gradients/training/utils/collate_fn/ppyoloe_collate_fn.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(random_resize_sizes=None, random_resize_modes=None, random_aspect_ratio=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
random_resize_sizes |
Union[List[int], None]
|
List of single image size dimensions to use for sampling the output image size. If None, random resizing will not be applied. If not None, will randomly sample output shape for entire batch: [B, C, random.choice(random_resize_sizes), random.choice(random_resize_sizes)] The values in random_resize_sizes should be compatible with the model. Example: If the model requires input size to be divisible by 32 then all values in |
None
|
random_resize_modes |
Union[List[int], None]
|
List of interpolation modes to randomly resize the image to. If None, will not resize. Interpolation modes correspond to OpenCV interpolation modes: 0 - INTER_NEAREST 1 - INTER_LINEAR 2 - INTER_CUBIC 3 - INTER_AREA 4 - INTER_LANCZOS4 If None defaults to linear interpolation. |
None
|
random_aspect_ratio |
Union[bool, Tuple[float, float]]
|
If True, will randomly choose both width and height from random_resize_sizes. If False, will randomly choose only value which will be the width and height of the images. If tuple (min_aspect_ratio, max_aspect_ratio), will guarantee that sampled width and height satisfy required aspect ratio range. |
False
|
Source code in src/super_gradients/training/utils/collate_fn/ppyoloe_collate_fn.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
AccessCounterMixin
Implements access counting mechanism for configuration settings (dicts/lists). It is achieved by wrapping underlying config and override getitem, getattr methods to catch read operations and increments access counter for each property.
Source code in src/super_gradients/training/utils/config_utils.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
maybe_wrap_as_counter(value, key, count_usage=True)
Return an attribute value optionally wrapped as access counter adapter to trace read counts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
Attribute value |
required | |
key |
Attribute name |
required | |
count_usage |
bool
|
Whether increment usage count for given attribute. Default is True. |
True
|
Returns:
Type | Description |
---|---|
wrapped value |
Source code in src/super_gradients/training/utils/config_utils.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
raise_if_unused_params(config)
A helper function to check whether all confuration parameters were used on given block of code. Motivation to have this check is to ensure there were no typo or outdated configuration parameters. It at least one of config parameters was not used, this function will raise an UnusedConfigParamException exception. Example usage:
from super_gradients.training.utils import raise_if_unused_params
with raise_if_unused_params(some_config) as some_config: do_something_with_config(some_config)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
Union[HpmStruct, DictConfig, ListConfig, Mapping, list, tuple]
|
A config to check |
required |
Returns:
Type | Description |
---|---|
ConfigInspector
|
An instance of ConfigInspector |
Source code in src/super_gradients/training/utils/config_utils.py
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 |
|
warn_if_unused_params(config)
A helper function to check whether all confuration parameters were used on given block of code. Motivation to have this check is to ensure there were no typo or outdated configuration parameters. It at least one of config parameters was not used, this function will emit warning. Example usage:
from super_gradients.training.utils import warn_if_unused_params
with warn_if_unused_params(some_config) as some_config: do_something_with_config(some_config)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config |
A config to check |
required |
Returns:
Type | Description |
---|---|
An instance of ConfigInspector |
Source code in src/super_gradients/training/utils/config_utils.py
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 |
|
wrap_with_warning(cls, message)
Emits a warning when target class of function is called.
from super_gradients.training.utils.deprecated_utils import wrap_with_warning from super_gradients.training.utils.callbacks import LinearEpochLRWarmup, LinearBatchLRWarmup
LR_WARMUP_CLS_DICT = { "linear": wrap_with_warning( LinearEpochLRWarmup, message=f"Parameter
linear
has been made deprecated and will be removed in the next SG release. Please uselinear_epoch
instead", ), 'linear_epoch`': LinearEpochLRWarmup, }
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Callable
|
A class or function to wrap |
required |
message |
str
|
A message to emit when this class is called |
required |
Returns:
Type | Description |
---|---|
Any
|
A factory method that returns wrapped class |
Source code in src/super_gradients/training/utils/deprecated_utils.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Anchors
A wrapper function to hold the anchors used by detection models such as Yolo
Source code in src/super_gradients/training/utils/detection_utils.py
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 703 704 705 706 707 708 709 |
|
__init__(anchors_list, strides)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
anchors_list |
List[List]
|
of the shape [[w1,h1,w2,h2,w3,h3], [w4,h4,w5,h5,w6,h6] .... where each sublist holds the width and height of the anchors of a specific detection layer. i.e. for a model with 3 detection layers, each containing 5 anchors the format will be a of 3 sublists of 10 numbers each The width and height are in pixels (not relative to image size) |
required |
strides |
List[int]
|
a list containing the stride of the layers from which the detection heads are fed. i.e. if the firs detection head is connected to the backbone after the input dimensions were reduces by 8, the first number will be 8 |
required |
Source code in src/super_gradients/training/utils/detection_utils.py
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
|
DetectionMatching
Bases: ABC
DetectionMatching is an abstract base class that defines the interface for matching detections in object detection models. It includes methods for computing targets for both regular and crowd scenarios, as well as getting thresholds for matching.
Source code in src/super_gradients/training/utils/detection_utils.py
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
|
compute_crowd_targets(preds_box_xyxy, preds_cls, crowd_targets_cls, crowd_target_box_xyxy, preds_matched, preds_to_ignore, preds_idx_to_use)
abstractmethod
Abstract method to compute targets for crowd scenarios.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_box_xyxy |
torch.Tensor
|
(torch.Tensor) Predicted bounding boxes in XYXY format. |
required |
preds_cls |
torch.Tensor
|
(torch.Tensor) Predicted classes. |
required |
crowd_targets_cls |
torch.Tensor
|
(torch.Tensor) Crowd target classes. |
required |
crowd_target_box_xyxy |
torch.Tensor
|
(torch.Tensor) Crowd target bounding boxes in XYXY format. |
required |
preds_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions are matched. |
required |
preds_to_ignore |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions to ignore. |
required |
preds_idx_to_use |
torch.Tensor
|
(torch.Tensor) Indices of predictions to use. |
required |
Returns:
Type | Description |
---|---|
Tuple[torch.Tensor, torch.Tensor]
|
(Tuple[torch.Tensor, torch.Tensor]) Computed targets for crowd scenarios. |
Source code in src/super_gradients/training/utils/detection_utils.py
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
|
compute_targets(preds_box_xyxy, preds_cls, targets_box_xyxy, targets_cls, preds_matched, targets_matched, preds_idx_to_use)
abstractmethod
Abstract method to compute targets for regular scenarios.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_box_xyxy |
torch.Tensor
|
(torch.Tensor) Predicted bounding boxes in XYXY format. |
required |
preds_cls |
torch.Tensor
|
(torch.Tensor) Predicted classes. |
required |
targets_box_xyxy |
torch.Tensor
|
(torch.Tensor) Target bounding boxes in XYXY format. |
required |
targets_cls |
torch.Tensor
|
(torch.Tensor) Target classes. |
required |
preds_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions are matched. |
required |
targets_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which targets are matched. |
required |
preds_idx_to_use |
torch.Tensor
|
(torch.Tensor) Indices of predictions to use. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
(torch.Tensor) Computed targets. |
Source code in src/super_gradients/training/utils/detection_utils.py
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
|
get_thresholds()
abstractmethod
Abstract method to get the thresholds used for detection matching.
Returns:
Type | Description |
---|---|
torch.Tensor
|
(torch.Tensor) The thresholds used in the matching process. |
Source code in src/super_gradients/training/utils/detection_utils.py
820 821 822 823 824 825 826 827 |
|
DetectionPostPredictionCallback
Bases: ABC
, nn.Module
Source code in src/super_gradients/training/utils/detection_utils.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
forward(x, device=None)
abstractmethod
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
the output of your model |
required | |
device |
str
|
(Deprecated) Not used anymore, exists only for sake of keeping the same interface as in the parent class. Will be removed in the SG 3.7.0. A device parameter in case we want to move tensors to a specific device. |
None
|
Returns:
Type | Description |
---|---|
a list with length batch_size, each item in the list is a detections with shape: nx6 (x1, y1, x2, y2, confidence, class) where x and y are in range [0,1] |
Source code in src/super_gradients/training/utils/detection_utils.py
217 218 219 220 221 222 223 224 225 226 227 228 |
|
DetectionTargetsFormat
Bases: Enum
Enum class for the different detection output formats
When NORMALIZED is not specified- the type refers to unnormalized image coordinates (of the bboxes).
For example: LABEL_NORMALIZED_XYXY means [class_idx,x1,y1,x2,y2]
Source code in src/super_gradients/training/utils/detection_utils.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
DetectionVisualization
Source code in src/super_gradients/training/utils/detection_utils.py
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 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 |
|
draw_box_title(color_mapping, class_names, box_thickness, image_np, x1, y1, x2, y2, class_id, pred_conf=None, bbox_prefix='')
staticmethod
Draw a rectangle with class name, confidence on the image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_mapping |
List[Tuple[int]]
|
A list of N RGB colors for each class |
required |
class_names |
List[str]
|
A list of N class names |
required |
box_thickness |
Optional[int]
|
Thickness of the bounding box (in pixels) |
required |
image_np |
np.ndarray
|
Image in RGB format (H, W, C) where to draw the bounding box |
required |
x1 |
int
|
X coordinate of the top left corner of the bounding box |
required |
y1 |
int
|
Y coordinate of the top left corner of the bounding box |
required |
x2 |
int
|
X coordinate of the bottom right corner of the bounding box |
required |
y2 |
int
|
Y coordinate of the bottom right corner of the bounding box |
required |
class_id |
int
|
A corresponding class id |
required |
pred_conf |
float
|
Class confidence score (optional) |
None
|
bbox_prefix |
str
|
Prefix to add to the title of the bounding boxes |
''
|
Source code in src/super_gradients/training/utils/detection_utils.py
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 |
|
visualize_batch(image_tensor, pred_boxes, target_boxes, batch_name, class_names, checkpoint_dir=None, undo_preprocessing_func=undo_image_preprocessing, box_thickness=None, image_scale=1.0, gt_alpha=0.4)
staticmethod
A helper function to visualize detections predicted by a network: saves images into a given path with a name that is {batch_name}_{imade_idx_in_the_batch}.jpg, one batch per call. Colors are generated on the fly: uniformly sampled from color wheel to support all given classes.
Adjustable: * Ground truth box transparency; * Box width; * Image size (larger or smaller than what's provided)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_tensor |
torch.Tensor
|
rgb images, (B, H, W, 3) |
required |
pred_boxes |
List[torch.Tensor]
|
boxes after NMS for each image in a batch, each (Num_boxes, 6), values on dim 1 are: x1, y1, x2, y2, confidence, class |
required |
target_boxes |
torch.Tensor
|
(Num_targets, 6), values on dim 1 are: image id in a batch, class, cx cy w h (coordinates scaled to [0, 1]) |
required |
batch_name |
Union[int, str]
|
id of the current batch to use for image naming |
required |
class_names |
List[str]
|
names of all classes, each on its own index |
required |
checkpoint_dir |
str
|
a path where images with boxes will be saved. if None, the result images will be returns as a list of numpy image arrays |
None
|
undo_preprocessing_func |
Callable[[torch.Tensor], np.ndarray]
|
a function to convert preprocessed images tensor into a batch of cv2-like images |
undo_image_preprocessing
|
box_thickness |
Optional[int]
|
box line thickness in px |
None
|
image_scale |
float
|
scale of an image w.r.t. given image size, e.g. incoming images are (320x320), use scale = 2. to preview in (640x640) |
1.0
|
gt_alpha |
float
|
a value in [0., 1.] transparency on ground truth boxes, 0 for invisible, 1 for fully opaque |
0.4
|
Source code in src/super_gradients/training/utils/detection_utils.py
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 |
|
DistanceMatching
Bases: DetectionMatching
DistanceMatching is a subclass of DetectionMatching that uses a distance metric for matching detections in object detection models.
Source code in src/super_gradients/training/utils/detection_utils.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 |
|
__init__(distance_metric, distance_thresholds)
Initializes the DistanceMatching instance with a distance metric and distance thresholds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distance_metric |
The distance metric to be used for matching. |
required | |
distance_thresholds |
torch.Tensor
|
(torch.Tensor) The distance thresholds for matching. |
required |
Source code in src/super_gradients/training/utils/detection_utils.py
1014 1015 1016 1017 1018 1019 1020 1021 1022 |
|
compute_crowd_targets(preds_box_xyxy, preds_cls, crowd_targets_cls, crowd_target_box_xyxy, preds_matched, preds_to_ignore, preds_idx_to_use)
Computes the matching targets based on the distance metric for crowd scenarios.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_box_xyxy |
torch.Tensor
|
(torch.Tensor) Predicted bounding boxes in XYXY format. |
required |
preds_cls |
torch.Tensor
|
(torch.Tensor) Predicted classes. |
required |
crowd_targets_cls |
torch.Tensor
|
(torch.Tensor) Crowd target classes. |
required |
crowd_target_box_xyxy |
torch.Tensor
|
(torch.Tensor) Crowd target bounding boxes in XYXY format. |
required |
preds_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions are matched. |
required |
preds_to_ignore |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions to ignore. |
required |
preds_idx_to_use |
torch.Tensor
|
(torch.Tensor) Indices of predictions to use. |
required |
Returns:
Type | Description |
---|---|
Tuple[torch.Tensor, torch.Tensor]
|
(Tuple[torch.Tensor, torch.Tensor]) Computed matching targets for crowd scenarios. |
Source code in src/super_gradients/training/utils/detection_utils.py
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 |
|
compute_targets(preds_box_xyxy, preds_cls, targets_box_xyxy, targets_cls, preds_matched, targets_matched, preds_idx_to_use)
Computes the matching targets based on the distance metric for regular scenarios.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_box_xyxy |
torch.Tensor
|
(torch.Tensor) Predicted bounding boxes in XYXY format. |
required |
preds_cls |
torch.Tensor
|
(torch.Tensor) Predicted classes. |
required |
targets_box_xyxy |
torch.Tensor
|
(torch.Tensor) Target bounding boxes in XYXY format. |
required |
targets_cls |
torch.Tensor
|
(torch.Tensor) Target classes. |
required |
preds_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions are matched. |
required |
targets_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which targets are matched. |
required |
preds_idx_to_use |
torch.Tensor
|
(torch.Tensor) Indices of predictions to use. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
(torch.Tensor) Computed matching targets. |
Source code in src/super_gradients/training/utils/detection_utils.py
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
|
get_thresholds()
Returns the distance thresholds used for detection matching.
Returns:
Type | Description |
---|---|
torch.Tensor
|
(torch.Tensor) The distance thresholds. |
Source code in src/super_gradients/training/utils/detection_utils.py
1024 1025 1026 1027 1028 1029 1030 |
|
EuclideanDistance
Bases: DistanceMetric
Source code in src/super_gradients/training/utils/detection_utils.py
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 |
|
calculate_distance(predicted, target)
Calculate the Euclidean distance (L2 distance) between the centers of preds_box and targets_box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predicted |
torch.Tensor
|
(N, 4) tensor for N predicted bounding boxes (x1, y1, x2, y2) |
required |
target |
torch.Tensor
|
(M, 4) tensor for M target bounding boxes (x1, y1, x2, y2) |
required |
Returns:
Type | Description |
---|---|
(N, M) tensor representing pairwise euclidean distances |
Source code in src/super_gradients/training/utils/detection_utils.py
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 |
|
IoUMatching
Bases: DetectionMatching
IoUMatching is a subclass of DetectionMatching that uses Intersection over Union (IoU) for matching detections in object detection models.
Source code in src/super_gradients/training/utils/detection_utils.py
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
__init__(iou_thresholds)
Initializes the IoUMatching instance with IoU thresholds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iou_thresholds |
torch.Tensor
|
(torch.Tensor) The IoU thresholds for matching. |
required |
Source code in src/super_gradients/training/utils/detection_utils.py
886 887 888 889 890 891 892 |
|
compute_crowd_targets(preds_box_xyxy, preds_cls, crowd_targets_cls, crowd_target_box_xyxy, preds_matched, preds_to_ignore, preds_idx_to_use)
Computes the matching targets based on IoU for crowd scenarios.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_box_xyxy |
torch.Tensor
|
(torch.Tensor) Predicted bounding boxes in XYXY format. |
required |
preds_cls |
torch.Tensor
|
(torch.Tensor) Predicted classes. |
required |
crowd_targets_cls |
torch.Tensor
|
(torch.Tensor) Crowd target classes. |
required |
crowd_target_box_xyxy |
torch.Tensor
|
(torch.Tensor) Crowd target bounding boxes in XYXY format. |
required |
preds_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions are matched. |
required |
preds_to_ignore |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions to ignore. |
required |
preds_idx_to_use |
torch.Tensor
|
(torch.Tensor) Indices of predictions to use. |
required |
Returns:
Type | Description |
---|---|
Tuple[torch.Tensor, torch.Tensor]
|
(Tuple[torch.Tensor, torch.Tensor]) Computed matching targets for crowd scenarios. |
Source code in src/super_gradients/training/utils/detection_utils.py
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
compute_targets(preds_box_xyxy, preds_cls, targets_box_xyxy, targets_cls, preds_matched, targets_matched, preds_idx_to_use)
Computes the matching targets based on IoU for regular scenarios.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_box_xyxy |
torch.Tensor
|
(torch.Tensor) Predicted bounding boxes in XYXY format. |
required |
preds_cls |
torch.Tensor
|
(torch.Tensor) Predicted classes. |
required |
targets_box_xyxy |
torch.Tensor
|
(torch.Tensor) Target bounding boxes in XYXY format. |
required |
targets_cls |
torch.Tensor
|
(torch.Tensor) Target classes. |
required |
preds_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which predictions are matched. |
required |
targets_matched |
torch.Tensor
|
(torch.Tensor) Tensor indicating which targets are matched. |
required |
preds_idx_to_use |
torch.Tensor
|
(torch.Tensor) Indices of predictions to use. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
(torch.Tensor) Computed matching targets. |
Source code in src/super_gradients/training/utils/detection_utils.py
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
|
get_thresholds()
Returns the IoU thresholds used for detection matching.
Returns:
Type | Description |
---|---|
torch.Tensor
|
(torch.Tensor) The IoU thresholds. |
Source code in src/super_gradients/training/utils/detection_utils.py
894 895 896 897 898 899 900 |
|
IouThreshold
Bases: tuple
, Enum
Source code in src/super_gradients/training/utils/detection_utils.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
from_bounds(low, high, step=0.05)
classmethod
Create a tensor with values from low (including) to high (including) with a given step size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
low |
float
|
Lower bound |
required |
high |
float
|
Upper bound |
required |
step |
float
|
Step size |
0.05
|
Returns:
Type | Description |
---|---|
torch.Tensor
|
Tensor of [low, low + step, low + 2 * step, ..., high] |
Source code in src/super_gradients/training/utils/detection_utils.py
244 245 246 247 248 249 250 251 252 253 254 |
|
ManhattanDistance
Bases: DistanceMetric
Source code in src/super_gradients/training/utils/detection_utils.py
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 |
|
calculate_distance(predicted, target)
Calculate the Manhattan distance (L1 distance) between the centers of preds_box and targets_box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predicted |
torch.Tensor
|
(N, 4) tensor for N predicted bounding boxes (x1, y1, x2, y2) |
required |
target |
torch.Tensor
|
(M, 4) tensor for M target bounding boxes (x1, y1, x2, y2) |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
(N, M) tensor representing pairwise Manhattan distances |
Source code in src/super_gradients/training/utils/detection_utils.py
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 |
|
NMS_Type
Bases: str
, Enum
Type of non max suppression algorithm that can be used for post processing detection
Source code in src/super_gradients/training/utils/detection_utils.py
393 394 395 396 397 398 399 |
|
adjust_box_anns(bbox, scale_ratio, padw, padh, w_max, h_max)
Adjusts the bbox annotations of rescaled, padded image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bbox |
(np.array) bbox to modify. |
required | |
scale_ratio |
(float) scale ratio between rescale output image and original one. |
required | |
padw |
(int) width padding size. |
required | |
padh |
(int) height padding size. |
required | |
w_max |
(int) width border. |
required | |
h_max |
(int) height border |
required |
Returns:
Type | Description |
---|---|
modified bbox (np.array) |
Source code in src/super_gradients/training/utils/detection_utils.py
771 772 773 774 775 776 777 778 779 780 781 782 783 784 |
|
box_iou(box1, box2)
Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box1 |
torch.Tensor
|
Tensor of shape [N, 4] |
required |
box2 |
torch.Tensor
|
Tensor of shape [M, 4] |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
iou, Tensor of shape [N, M]: the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 |
Source code in src/super_gradients/training/utils/detection_utils.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
calc_bbox_iou_matrix(pred)
calculate iou for every pair of boxes in the boxes vector
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred |
torch.Tensor
|
a 3-dimensional tensor containing all boxes for a batch of images [N, num_boxes, 4], where each box format is [x1,y1,x2,y2] |
required |
Returns:
Type | Description |
---|---|
a 3-dimensional matrix where M_i_j_k is the iou of box j and box k of the i'th image in the batch |
Source code in src/super_gradients/training/utils/detection_utils.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
calculate_bbox_iou_matrix(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-09)
calculate iou matrix containing the iou of every couple iuo(i,j) where i is in box1 and j is in box2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box1 |
a 2D tensor of boxes (shape N x 4) |
required | |
box2 |
a 2D tensor of boxes (shape M x 4) |
required | |
x1y1x2y2 |
boxes format is x1y1x2y2 (True) or xywh where xy is the center (False) |
True
|
Returns:
Type | Description |
---|---|
a 2D iou matrix (shape NxM) |
Source code in src/super_gradients/training/utils/detection_utils.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
change_bbox_bounds_for_image_size(boxes, img_shape, inplace=True)
Clips bboxes to image boundaries. The function may operate both in- and on a copy of the input which is controlled by the inplace parameter. It exists for backward compatibility and will be removed in the SG 3.8.0 and this method will not modify the input. An inplace version of this method is available as change_bbox_bounds_for_image_size_inplace.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes |
(np.ndarray) Input bounding boxes in XYXY format of [..., 4] shape |
required | |
img_shape |
Tuple[int, int]
|
Tuple[int,int] of image shape (height, width). |
required |
inplace |
(bool) If True, the function operates in-place. Otherwise, it returns a modified copy. If True this will trigger a deprecated warning to inform the user to use change_bbox_bounds_for_image_size_inplace instead. |
True
|
Returns:
Type | Description |
---|---|
np.ndarray
|
(np.ndarray)clipped bboxes in XYXY format of [..., 4] shape |
Source code in src/super_gradients/training/utils/detection_utils.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
change_bbox_bounds_for_image_size_inplace(boxes, img_shape)
Clips bboxes to image boundaries. The function operates in-place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes |
(np.ndarray) Input bounding boxes in XYXY format of [..., 4] shape |
required | |
img_shape |
Tuple[int, int]
|
Tuple[int,int] of image shape (height, width). |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
(np.ndarray)clipped bboxes in XYXY format of [..., 4] shape |
Source code in src/super_gradients/training/utils/detection_utils.py
174 175 176 177 178 179 180 181 182 183 184 |
|
compute_box_area(box)
Compute the area of one or many boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box |
torch.Tensor
|
One or many boxes, shape = (4, ?), each box in format (x1, y1, x2, y2) |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
Area of every box, shape = (1, ?) |
Source code in src/super_gradients/training/utils/detection_utils.py
787 788 789 790 791 792 793 794 |
|
compute_detection_matching(output, targets, height, width, denormalize_targets, device, iou_thresholds=None, crowd_targets=None, top_k=100, return_on_cpu=True, matching_strategy=None)
Match predictions (NMS output) and the targets (ground truth) with respect to IoU and confidence score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
List[torch.Tensor]
|
list (of length batch_size) of Tensors of shape (num_predictions, 6) format: (x1, y1, x2, y2, confidence, class_label) where x1,y1,x2,y2 are according to image size |
required |
targets |
torch.Tensor
|
targets for all images of shape (total_num_targets, 6) format: (index, label, x, y, w, h, ) where x,y,w,h are in range [0,1] |
required |
height |
int
|
dimensions of the image |
required |
width |
int
|
dimensions of the image |
required |
iou_thresholds |
torch.Tensor
|
Threshold to compute the mAP |
None
|
device |
str
|
Device |
required |
crowd_targets |
Optional[torch.Tensor]
|
crowd targets for all images of shape (total_num_crowd_targets, 6) format: (index, label, x, y, w, h) where x,y,w,h are in range [0,1] |
None
|
top_k |
int
|
Number of predictions to keep per class, ordered by confidence score |
100
|
denormalize_targets |
bool
|
If True, denormalize the targets and crowd_targets |
required |
return_on_cpu |
bool
|
If True, the output will be returned on "CPU", otherwise it will be returned on "device" |
True
|
matching_strategy |
DetectionMatching
|
Method to match predictions to ground truth targets, IoU, distance based |
None
|
Returns:
Type | Description |
---|---|
List[Tuple]
|
list of the following tensors, for every image: :preds_matched: Tensor of shape (num_img_predictions, n_thresholds) True when prediction (i) is matched with a target with respect to the (j)th IoU threshold :preds_to_ignore: Tensor of shape (num_img_predictions, n_thresholds) True when prediction (i) is matched with a crowd target with respect to the (j)th IoU threshold :preds_scores: Tensor of shape (num_img_predictions), confidence score for every prediction :preds_cls: Tensor of shape (num_img_predictions), predicted class for every prediction :targets_cls: Tensor of shape (num_img_targets), ground truth class for every target |
Source code in src/super_gradients/training/utils/detection_utils.py
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 |
|
compute_detection_metrics(preds_matched, preds_to_ignore, preds_scores, preds_cls, targets_cls, device, recall_thresholds=None, score_threshold=0.1, calc_best_score_thresholds=None)
Compute the list of precision, recall, MaP and f1 for every recall IoU threshold and for every class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_matched |
torch.Tensor
|
Tensor of shape (num_predictions, n_iou_thresholds) True when prediction (i) is matched with a target with respect to the (j)th IoU threshold |
required |
preds_scores |
torch.Tensor
|
Tensor of shape (num_predictions), confidence score for every prediction |
required |
preds_cls |
torch.Tensor
|
Tensor of shape (num_predictions), predicted class for every prediction |
required |
targets_cls |
torch.Tensor
|
Tensor of shape (num_targets), ground truth class for every target box to be detected |
required |
recall_thresholds |
Optional[torch.Tensor]
|
Recall thresholds used to compute MaP. |
None
|
score_threshold |
Optional[float]
|
Minimum confidence score to consider a prediction for the computation of precision, recall and f1 (not MaP) |
0.1
|
device |
str
|
Device |
required |
calc_best_score_thresholds |
bool
|
(Deprecated) If True, the best confidence score threshold is computed for each class This parameter is deprecated and ignore. Function always compute best threshold. |
None
|
Returns:
Type | Description |
---|---|
Tuple
|
:ap, precision, recall, f1: Tensors of shape (n_class, nb_iou_thrs) :unique_classes: Vector with all unique target classes :best_score_threshold: torch.float with the best overall score threshold if calc_best_score_thresholds is True else None :best_score_threshold_per_cls: Array that stores the best score threshold for each class , if calc_best_score_thresholds is True else None |
Source code in src/super_gradients/training/utils/detection_utils.py
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 |
|
compute_detection_metrics_per_cls(preds_matched, preds_to_ignore, preds_scores, n_targets, recall_thresholds, score_threshold, device, calc_best_score_thresholds=None)
Compute the list of precision, recall and MaP of a given class for every recall threshold.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_matched |
torch.Tensor
|
Tensor of shape (num_predictions, n_thresholds) True when prediction (i) is matched with a target with respect to the(j)th threshold |
required |
preds_scores |
torch.Tensor
|
Tensor of shape (num_predictions), confidence score for every prediction |
required |
n_targets |
int
|
Number of target boxes of this class |
required |
recall_thresholds |
torch.Tensor
|
Tensor of shape (max_n_rec_thresh) list of recall thresholds used to compute MaP |
required |
score_threshold |
float
|
Minimum confidence score to consider a prediction for the computation of precision and recall (not MaP) |
required |
device |
str
|
Device |
required |
nb_score_thrs |
Number of score thresholds to consider when calc_best_score_thresholds is True |
required | |
calc_best_score_thresholds |
(Deprecated) If True, the best confidence score threshold is computed for each class This parameter is deprecated and ignore. Function always compute best threshold. |
None
|
Returns:
Type | Description |
---|---|
:ap, precision, recall: Tensors of shape (nb_thrs) :mean_f1_per_threshold: Tensor of shape (nb_score_thresholds) if calc_best_score_thresholds is True else None :best_score_threshold: torch.float if calc_best_score_thresholds is True else None |
Source code in src/super_gradients/training/utils/detection_utils.py
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 |
|
compute_img_detection_matching(preds, targets, crowd_targets, height, width, device, denormalize_targets, iou_thresholds=None, top_k=100, return_on_cpu=True, matching_strategy=None)
Match predictions (NMS output) and the targets (ground truth) with respect to metric and confidence score for a given image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds |
torch.Tensor
|
Tensor of shape (num_img_predictions, 6) format: (x1, y1, x2, y2, confidence, class_label) where x1,y1,x2,y2 are according to image size |
required |
targets |
torch.Tensor
|
targets for this image of shape (num_img_targets, 6) format: (label, cx, cy, w, h) where cx,cy,w,h |
required |
height |
int
|
dimensions of the image |
required |
width |
int
|
dimensions of the image |
required |
device |
str
|
required | |
crowd_targets |
torch.Tensor
|
crowd targets for all images of shape (total_num_crowd_targets, 6) format: (index, x, y, w, h) where x,y,w,h are in range [0,1] |
required |
iou_thresholds |
torch.Tensor
|
Threshold to compute the mAP |
None
|
top_k |
int
|
Number of predictions to keep per class, ordered by confidence score |
100
|
denormalize_targets |
bool
|
If True, denormalize the targets and crowd_targets |
required |
return_on_cpu |
bool
|
If True, the output will be returned on "CPU", otherwise it will be returned on "device" |
True
|
matching_strategy |
DetectionMatching
|
Method to match predictions to ground truth targets: IoU, distance based |
None
|
Returns:
Type | Description |
---|---|
Tuple
|
:preds_matched: Tensor of shape (num_img_predictions, n_thresholds) True when prediction (i) is matched with a target with respect to the (j)th threshold :preds_to_ignore: Tensor of shape (num_img_predictions, n_thresholds) True when prediction (i) is matched with a crowd target with respect to the (j)th threshold :preds_scores: Tensor of shape (num_img_predictions), confidence score for every prediction :preds_cls: Tensor of shape (num_img_predictions), predicted class for every prediction :targets_cls: Tensor of shape (num_img_targets), ground truth class for every target |
Source code in src/super_gradients/training/utils/detection_utils.py
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 |
|
convert_cxcywh_bbox_to_xyxy(input_bbox)
Converts bounding box format from [cx, cy, w, h] to [x1, y1, x2, y2] :param input_bbox: input bbox either 2-dimensional (for all boxes of a single image) or 3-dimensional (for boxes of a batch of images) :return: Converted bbox in same dimensions as the original
Source code in src/super_gradients/training/utils/detection_utils.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
crowd_ioa(det_box, crowd_box)
Return intersection-over-detection_area of boxes, used for crowd ground truths. Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
det_box |
torch.Tensor
|
Tensor of shape [N, 4] |
required |
crowd_box |
torch.Tensor
|
Tensor of shape [M, 4] |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
crowd_ioa, Tensor of shape [N, M]: the NxM matrix containing the pairwise IoA values for every element in det_box and crowd_box |
Source code in src/super_gradients/training/utils/detection_utils.py
797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
|
cxcywh2xyxy(bboxes)
Transforms bboxes from centerized xy wh format to xyxy format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes |
array, shaped (nboxes, 4) |
required |
Returns:
Type | Description |
---|---|
modified bboxes |
Source code in src/super_gradients/training/utils/detection_utils.py
725 726 727 728 729 730 731 732 733 734 735 |
|
get_class_index_in_target(target_format)
Get the label of a given target
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_format |
DetectionTargetsFormat
|
Representation of the target (ex: LABEL_XYXY) |
required |
Returns:
Type | Description |
---|---|
int
|
Position of the class id in a bbox ex: 0 if bbox of format label_xyxy | -1 if bbox of format xyxy_label |
Source code in src/super_gradients/training/utils/detection_utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
get_mosaic_coordinate(mosaic_index, xc, yc, w, h, input_h, input_w)
Returns the mosaic coordinates of final mosaic image according to mosaic image index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mosaic_index |
(int) mosaic image index |
required | |
xc |
(int) center x coordinate of the entire mosaic grid. |
required | |
yc |
(int) center y coordinate of the entire mosaic grid. |
required | |
w |
(int) width of bbox |
required | |
h |
(int) height of bbox |
required | |
input_h |
(int) image input height (should be 1/2 of the final mosaic output image height). |
required | |
input_w |
(int) image input width (should be 1/2 of the final mosaic output image width). |
required |
Returns:
Type | Description |
---|---|
(x1, y1, x2, y2), (x1s, y1s, x2s, y2s) where (x1, y1, x2, y2) are the coordinates in the final mosaic output image, and (x1s, y1s, x2s, y2s) are the coordinates in the placed image. |
Source code in src/super_gradients/training/utils/detection_utils.py
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
|
get_top_k_idx_per_cls(preds_scores, preds_cls, top_k)
Get the indexes of all the top k predictions for every class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds_scores |
torch.Tensor
|
The confidence scores, vector of shape (n_pred) |
required |
preds_cls |
torch.Tensor
|
The predicted class, vector of shape (n_pred) |
required |
top_k |
int
|
Number of predictions to keep per class, ordered by confidence score |
required |
Returns:
Type | Description |
---|---|
Indexes of the top k predictions. length <= (k * n_unique_class) |
Source code in src/super_gradients/training/utils/detection_utils.py
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 |
|
matrix_non_max_suppression(pred, conf_thres=0.1, kernel='gaussian', sigma=3.0, max_num_of_detections=500, class_agnostic_nms=False)
Performs Matrix Non-Maximum Suppression (NMS) on inference results https://arxiv.org/pdf/1912.04488.pdf
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred |
Raw model prediction (in test mode) - a Tensor of shape [batch, num_predictions, 85] where each item format is (x, y, w, h, object_conf, class_conf, ... 80 classes score ...) |
required | |
conf_thres |
float
|
Threshold under which prediction are discarded |
0.1
|
kernel |
str
|
Type of kernel to use ['gaussian', 'linear'] |
'gaussian'
|
sigma |
float
|
Sigma for the gaussian kernel |
3.0
|
max_num_of_detections |
int
|
Maximum number of boxes to output |
500
|
Returns:
Type | Description |
---|---|
List[torch.Tensor]
|
Detections list with shape (x1, y1, x2, y2, object_conf, class_conf, class) |
Source code in src/super_gradients/training/utils/detection_utils.py
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 |
|
non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, multi_label_per_box=True, with_confidence=False, class_agnostic_nms=False)
Performs Non-Maximum Suppression (NMS) on inference results
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction |
raw model prediction. Should be a list of Tensors of shape (cx, cy, w, h, confidence, cls0, cls1, ...) |
required | |
conf_thres |
below the confidence threshold - prediction are discarded |
0.1
|
|
iou_thres |
IoU threshold for the nms algorithm |
0.6
|
|
multi_label_per_box |
bool
|
controls whether to decode multiple labels per box. True - each anchor can produce multiple labels of different classes that pass confidence threshold check (default). False - each anchor can produce only one label of the class with the highest score. |
True
|
with_confidence |
bool
|
whether to multiply objectness score with class score. usually valid for Yolo models only. |
False
|
class_agnostic_nms |
bool
|
indicates how boxes of different classes will be treated during NMS True - NMS will be performed on all classes together. False - NMS will be performed on each class separately (default). |
False
|
Returns:
Type | Description |
---|---|
detections with shape nx6 (x1, y1, x2, y2, object_conf, class_conf, class) |
Source code in src/super_gradients/training/utils/detection_utils.py
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 |
|
undo_image_preprocessing(im_tensor)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im_tensor |
torch.Tensor
|
images in a batch after preprocessing for inference, RGB, (B, C, H, W) |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
images in a batch in cv2 format, BGR, (B, H, W, C) |
Source code in src/super_gradients/training/utils/detection_utils.py
402 403 404 405 406 407 408 409 410 |
|
xyxy2cxcywh(bboxes)
Transforms bboxes from xyxy format to centerized xy wh format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes |
array, shaped (nboxes, 4) |
required |
Returns:
Type | Description |
---|---|
modified bboxes |
Source code in src/super_gradients/training/utils/detection_utils.py
712 713 714 715 716 717 718 719 720 721 722 |
|
DDPNotSetupException
Bases: Exception
Exception raised when DDP setup is required but was not done
Source code in src/super_gradients/training/utils/distributed_training_utils.py
370 371 372 373 374 375 376 377 378 379 380 381 |
|
compute_precise_bn_stats(model, loader, precise_bn_batch_size, num_gpus)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
The model being trained (ie: Trainer.net) |
required |
loader |
torch.utils.data.DataLoader
|
Training dataloader (ie: Trainer.train_loader) |
required |
precise_bn_batch_size |
int
|
The effective batch size we want to calculate the batchnorm on. For example, if we are training a model on 8 gpus, with a batch of 128 on each gpu, a good rule of thumb would be to give it 8192 (ie: effective_batch_size * num_gpus = batch_per_gpu * num_gpus * num_gpus). If precise_bn_batch_size is not provided in the training_params, the latter heuristic will be taken. param num_gpus: The number of gpus we are training on |
required |
Source code in src/super_gradients/training/utils/distributed_training_utils.py
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 |
|
distributed_all_reduce_tensor_average(tensor, n)
This method performs a reduce operation on multiple nodes running distributed training It first sums all of the results and then divides the summation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
The tensor to perform the reduce operation for |
required | |
n |
Number of nodes |
required |
Returns:
Type | Description |
---|---|
Averaged tensor from all of the nodes |
Source code in src/super_gradients/training/utils/distributed_training_utils.py
33 34 35 36 37 38 39 40 41 42 43 44 |
|
get_gpu_mem_utilization()
GPU memory managed by the caching allocator in bytes for a given device.
Source code in src/super_gradients/training/utils/distributed_training_utils.py
360 361 362 363 364 365 366 367 |
|
initialize_ddp()
Initialize Distributed Data Parallel
Important note: (1) in distributed training it is customary to specify learning rates and batch sizes per GPU. Whatever learning rate and schedule you specify will be applied to the each GPU individually. Since gradients are passed and summed (reduced) from all to all GPUs, the effective batch size is the batch you specify times the number of GPUs. In the literature there are several "best practices" to set learning rates and schedules for large batch sizes.
Source code in src/super_gradients/training/utils/distributed_training_utils.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
maybe_all_gather_as_list(inputs)
When in DDP- gathers inputs from all processes. When not in DDP - returns the single-element list of [input].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray, the local rank's tensor to gather |
required |
Returns:
Type | Description |
---|---|
List
|
np.ndarray, the output image as described above |
Source code in src/super_gradients/training/utils/distributed_training_utils.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
|
maybe_all_gather_np_images(image)
When in DDP- gathers images (as np.ndarray objects) from all processes. Returns the concatenated np.array across dim=0. When not in DDP - returns the input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
np.ndarray, the local rank's tensor to gather |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
np.ndarray, the output image as described above |
Source code in src/super_gradients/training/utils/distributed_training_utils.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
maybe_all_reduce_tensor_average(tensor)
When in DDP- mean-reduces tensor from all devices. When not in DDP - returns the input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor |
torch.Tensor
|
tensor to (maybe) reduce |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
Source code in src/super_gradients/training/utils/distributed_training_utils.py
384 385 386 387 388 389 390 391 392 393 394 395 |
|
reduce_results_tuple_for_ddp(validation_results_tuple, device)
Gather all validation tuples from the various devices and average them
Source code in src/super_gradients/training/utils/distributed_training_utils.py
47 48 49 50 51 52 53 54 55 56 57 |
|
restart_script_with_ddp(num_gpus=None)
Launch the same script as the one that was launched (i.e. the command used to start the current process is re-used) but on subprocesses (i.e. with DDP).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_gpus |
int
|
How many gpu's you want to run the script on. If not specified, every available device will be used. |
None
|
Source code in src/super_gradients/training/utils/distributed_training_utils.py
315 316 317 318 319 320 321 322 323 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 |
|
scaled_all_reduce(tensors, num_gpus)
Performs the scaled all_reduce operation on the provided tensors. The input tensors are modified in-place. Currently supports only the sum reduction operator. The reduced values are scaled by the inverse size of the process group (equivalent to num_gpus).
Source code in src/super_gradients/training/utils/distributed_training_utils.py
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 |
|
setup_cpu(multi_gpu=MultiGPUMode.AUTO, num_gpus=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
multi_gpu |
MultiGPUMode
|
DDP, DP, Off or AUTO |
MultiGPUMode.AUTO
|
num_gpus |
int
|
Number of GPU's to use. |
None
|
Source code in src/super_gradients/training/utils/distributed_training_utils.py
211 212 213 214 215 216 217 218 219 220 221 222 223 |
|
setup_device(multi_gpu=None, num_gpus=None, device='cuda')
If required, launch ddp subprocesses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
multi_gpu |
MultiGPUMode
|
DDP, DP, Off or AUTO |
None
|
num_gpus |
int
|
Number of GPU's to use. When None, use all available devices on DDP or only one device on DP/OFF. |
None
|
device |
str
|
The device you want to use ('cpu' or 'cuda') If you only set num_gpus, your device will be set up according to the following logic: - |
'cuda'
|
Source code in src/super_gradients/training/utils/distributed_training_utils.py
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 |
|
setup_gpu(multi_gpu=MultiGPUMode.AUTO, num_gpus=None)
If required, launch ddp subprocesses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
multi_gpu |
MultiGPUMode
|
DDP, DP, Off or AUTO |
MultiGPUMode.AUTO
|
num_gpus |
int
|
Number of GPU's to use. When None, use all available devices on DDP or only one device on DP/OFF. |
None
|
Source code in src/super_gradients/training/utils/distributed_training_utils.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
setup_gpu_mode(gpu_mode=MultiGPUMode.OFF, num_gpus=None)
[DEPRECATED in favor of setup_device] If required, launch ddp subprocesses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gpu_mode |
MultiGPUMode
|
DDP, DP, Off or AUTO |
MultiGPUMode.OFF
|
num_gpus |
int
|
Number of GPU's to use. When None, use all available devices on DDP or only one device on DP/OFF. |
None
|
Source code in src/super_gradients/training/utils/distributed_training_utils.py
165 166 167 168 169 170 171 |
|
wait_for_the_master(local_rank)
Make all processes waiting for the master to do some task.
Source code in src/super_gradients/training/utils/distributed_training_utils.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
EarlyStop
Bases: PhaseCallback
Callback to monitor a metric and stop training when it stops improving. Inspired by pytorch_lightning.callbacks.early_stopping and tf.keras.callbacks.EarlyStopping
Source code in src/super_gradients/training/utils/early_stopping.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(phase, monitor, mode='min', min_delta=0.0, patience=3, check_finite=True, threshold=None, verbose=False, strict=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
phase |
Phase
|
Callback phase event. |
required |
monitor |
str
|
name of the metric to be monitored. |
required |
mode |
str
|
one of 'min', 'max'. In 'min' mode, training will stop when the quantity monitored has stopped decreasing and in 'max' mode it will stop when the quantity monitored has stopped increasing. |
'min'
|
min_delta |
float
|
minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than |
0.0
|
patience |
int
|
number of checks with no improvement after which training will be stopped. One check happens after every phase event. |
3
|
check_finite |
bool
|
When set |
True
|
threshold |
Optional[float]
|
Stop training immediately once the monitored quantity reaches this threshold. For mode 'min' stops training when below threshold, For mode 'max' stops training when above threshold. |
None
|
verbose |
bool
|
If |
False
|
strict |
bool
|
whether to crash the training if |
True
|
Source code in src/super_gradients/training/utils/early_stopping.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
MissingMonitorKeyException
Bases: Exception
Exception raised for missing monitor key in metrics_dict.
Source code in src/super_gradients/training/utils/early_stopping.py
146 147 148 149 150 151 |
|
KDModelEMA
Bases: ModelEMA
Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models Keep a moving average of everything in the model state_dict (parameters and buffers). This is intended to allow functionality like https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage A smoothed version of the weights is necessary for some training schemes to perform well. This class is sensitive where it is initialized in the sequence of model init, GPU assignment and distributed training wrappers.
Source code in src/super_gradients/training/utils/ema.py
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 |
|
__init__(kd_model, decay, decay_function)
Init the EMA
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kd_model |
KDModule
|
KDModule, the training Knowledge distillation model to construct the EMA model by IMPORTANT: WHEN THE APPLICATION OF EMA ONLY ON A SUBSET OF ATTRIBUTES IS DESIRED, WRAP THE NN.MODULE AS SgModule AND OVERWRITE get_include_attributes() AND get_exclude_attributes() AS DESIRED. |
required |
decay |
float
|
the maximum decay value. as the training process advances, the decay will climb towards this value until the EMA_t+1 = EMA_t * decay + TRAINING_MODEL * (1- decay) |
required |
beta |
the exponent coefficient. The higher the beta, the sooner in the training the decay will saturate to its final value. beta=15 is ~40% of the training process. |
required |
Source code in src/super_gradients/training/utils/ema.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
ModelEMA
Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models Keep a moving average of everything in the model state_dict (parameters and buffers). This is intended to allow functionality like https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage A smoothed version of the weights is necessary for some training schemes to perform well. This class is sensitive where it is initialized in the sequence of model init, GPU assignment and distributed training wrappers.
Source code in src/super_gradients/training/utils/ema.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
decay_function = decay_function
instance-attribute
" we hold a list of model attributes (not wights and biases) which we would like to include in each attribute update or exclude from each update. a SgModule declare these attribute using get_include_attributes and get_exclude_attributes functions. for a nn.Module which is not a SgModule all non-private (not starting with '_') attributes will be updated (and only them).
__init__(model, decay, decay_function)
Init the EMA
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
Union[SgModule, nn.Module], the training model to construct the EMA model by IMPORTANT: WHEN THE APPLICATION OF EMA ONLY ON A SUBSET OF ATTRIBUTES IS DESIRED, WRAP THE NN.MODULE AS SgModule AND OVERWRITE get_include_attributes() AND get_exclude_attributes() AS DESIRED. |
required |
decay |
float
|
the maximum decay value. as the training process advances, the decay will climb towards this value until the EMA_t+1 = EMA_t * decay + TRAINING_MODEL * (1- decay) |
required |
beta |
the exponent coefficient. The higher the beta, the sooner in the training the decay will saturate to its final value. beta=15 is ~40% of the training process. |
required |
Source code in src/super_gradients/training/utils/ema.py
37 38 39 40 41 42 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 |
|
update(model, step, total_steps)
Update the state of the EMA model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Current training model |
required | |
step |
int
|
Current training step |
required |
total_steps |
int
|
Total training steps |
required |
Source code in src/super_gradients/training/utils/ema.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
update_attr(model)
This function updates model attributes (not weight and biases) from original model to the ema model. attributes of the original model, such as anchors and grids (of detection models), may be crucial to the model operation and need to be updated. If include_attributes and exclude_attributes lists were not defined, all non-private (not starting with '_') attributes will be updated (and only them).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
the source model |
required |
Source code in src/super_gradients/training/utils/ema.py
143 144 145 146 147 148 149 150 151 152 |
|
ConstantDecay
Bases: IDecayFunction
Constant decay schedule.
Source code in src/super_gradients/training/utils/ema_decay_schedules.py
26 27 28 29 30 31 32 33 34 35 |
|
ExpDecay
Bases: IDecayFunction
Gradually increase EMA decay from 0.1 to the maximum value using following formula: decay * (1 - math.exp(-x * self.beta))
Source code in src/super_gradients/training/utils/ema_decay_schedules.py
50 51 52 53 54 55 56 57 58 59 60 61 |
|
IDecayFunction
Interface for EMA decay schedule. The decay schedule is a function of the maximum decay value and training progress. Usually it gradually increase EMA from to the maximum value. The exact ramp-up schedule is defined by the concrete implementation.
Source code in src/super_gradients/training/utils/ema_decay_schedules.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
__call__(decay, step, total_steps)
abstractmethod
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decay |
float
|
The maximum decay value. |
required |
step |
int
|
Current training step. The unit-range training percentage can be obtained by |
required |
total_steps |
int
|
Total number of training steps. |
required |
Returns:
Type | Description |
---|---|
float
|
Computed decay value for a given step. |
Source code in src/super_gradients/training/utils/ema_decay_schedules.py
14 15 16 17 18 19 20 21 22 23 |
|
ThresholdDecay
Bases: IDecayFunction
Gradually increase EMA decay from 0.1 to the maximum value using following formula: min(decay, (1 + step) / (10 + step))
Source code in src/super_gradients/training/utils/ema_decay_schedules.py
38 39 40 41 42 43 44 45 46 47 |
|
fuse_conv_bn(model, replace_bn_with_identity=False)
Fuses consecutive nn.Conv2d and nn.BatchNorm2d layers recursively inplace in all of the model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
replace_bn_with_identity |
bool
|
if set to true, bn will be replaced with identity. otherwise, bn will be removed |
False
|
model |
nn.Module
|
the target model |
required |
Returns:
Type | Description |
---|---|
the number of fuses executed |
Source code in src/super_gradients/training/utils/export_utils.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
infer_image_shape_from_model(model)
Infer the image shape from the model. This function takes the preprocessing parameters if they are available and gets the input image shape from them. If the preprocessing parameters are not available, the function returns None
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Union[nn.Module, HasPredict]
|
required |
Returns:
Type | Description |
---|---|
Optional[Tuple[int, int]]
|
A tuple of (height, width) or None |
Source code in src/super_gradients/training/utils/export_utils.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
get_input_output_shapes(batch_size, input_dims, output_dims)
Returns input/output shapes for single/multiple input/s output/s
Source code in src/super_gradients/training/utils/get_model_stats.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
get_model_stats(model, input_dims, high_verbosity=True, batch_size=1, device='cuda', dtypes=None, iterations=100)
return the model summary as a string The block(type) column represents the lines (layers) above :param dtypes: The input types (list of inputs types) :param high_verbosity: prints layer by layer information
Source code in src/super_gradients/training/utils/get_model_stats.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
check_image_typing(image)
Check if the given object respects typing of image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ImageSource
|
Image to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the object is an image, False otherwise. |
Source code in src/super_gradients/training/utils/media/image.py
174 175 176 177 178 179 180 181 182 183 184 |
|
generate_image_loader(images)
Generator that loads images one at a time.
Supported types include: - str: A string representing either an image or an URL. - numpy.ndarray: A numpy array representing the image - torch.Tensor: A PyTorch tensor representing the image - PIL.Image.Image: A PIL Image object - List: A list of images of any of the above types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images |
Union[List[ImageSource], ImageSource]
|
Single image or a list of images of supported types. |
required |
Returns:
Type | Description |
---|---|
Iterable[np.ndarray]
|
Generator of images as numpy arrays (H, W, C). If loaded from string, the image will be returned as RGB. |
Source code in src/super_gradients/training/utils/media/image.py
37 38 39 40 41 42 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 |
|
is_image(filename)
Check if the given file name refers to image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
The filename to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the file is an image, False otherwise. |
Source code in src/super_gradients/training/utils/media/image.py
187 188 189 190 191 192 193 |
|
is_url(url)
Check if the given string is a URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
String to check. |
required |
Source code in src/super_gradients/training/utils/media/image.py
152 153 154 155 156 157 158 159 160 |
|
list_images_in_folder(directory)
List all the images in a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory |
str
|
The path to the directory containing the images. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
A list of image file names. |
Source code in src/super_gradients/training/utils/media/image.py
79 80 81 82 83 84 85 86 |
|
load_image(image, input_image_channels=3)
Load a single image and return it as a numpy arrays (H, W, C).
Supported image types include: - numpy.ndarray: A numpy array representing the image - torch.Tensor: A PyTorch tensor representing the image - PIL.Image.Image: A PIL Image object - str: A string representing either a local file path or a URL to an image
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ImageSource
|
Single image of supported types. |
required |
input_image_channels |
int
|
Number of channels that model expects as input. This value helps to infer the layout of the input image array. As of now this argument has default value of 3, but in future it will become mandatory. |
3
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Image as numpy arrays (H, W, C). If loaded from string, the image will be returned as RGB. |
Source code in src/super_gradients/training/utils/media/image.py
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 |
|
load_images(images)
Load a single image or a list of images and return them as a list of numpy arrays.
Supported types include: - str: A string representing either an image or an URL. - numpy.ndarray: A numpy array representing the image - torch.Tensor: A PyTorch tensor representing the image - PIL.Image.Image: A PIL Image object - List: A list of images of any of the above types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
images |
Union[List[ImageSource], ImageSource]
|
Single image or a list of images of supported types. |
required |
Returns:
Type | Description |
---|---|
List[np.ndarray]
|
List of images as numpy arrays. If loaded from string, the image will be returned as RGB. |
Source code in src/super_gradients/training/utils/media/image.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
load_np_image_from_pil(image)
Convert a PIL image to numpy array in RGB format.
Source code in src/super_gradients/training/utils/media/image.py
128 129 130 |
|
load_pil_image_from_str(image_str)
Load an image based on a string (local file path or URL).
Source code in src/super_gradients/training/utils/media/image.py
133 134 135 136 137 138 139 140 141 |
|
save_image(image, path)
Save a numpy array as an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
Image to save, (H, W, C), RGB. |
required |
path |
str
|
Path to save the image to. |
required |
Source code in src/super_gradients/training/utils/media/image.py
144 145 146 147 148 149 |
|
show_image(image)
Show an image using matplotlib.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
Image to show in (H, W, C), RGB. |
required |
Source code in src/super_gradients/training/utils/media/image.py
163 164 165 166 167 168 169 170 171 |
|
FPSCounter
Class for calculating the FPS of a video stream.
Source code in src/super_gradients/training/utils/media/stream.py
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 |
|
fps: float
property
Current FPS value.
__init__(update_frequency=None)
Create a new FPSCounter object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
update_frequency |
Optional[float]
|
Minimum time (in seconds) between updates to the FPS counter. If None, the counter is updated every frame. |
None
|
Source code in src/super_gradients/training/utils/media/stream.py
108 109 110 111 112 113 114 115 116 117 118 |
|
WebcamStreaming
Stream video from a webcam. Press 'q' to quit the streaming.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
window_name |
str
|
Name of the window to display the video stream. |
''
|
frame_processing_fn |
Optional[Callable[[np.ndarray], np.ndarray]]
|
Function to apply to each frame before displaying it. If None, frames are displayed as is. |
None
|
capture |
int
|
ID of the video capture device to use. Default is cv2.CAP_ANY (which selects the first available device). |
cv2.CAP_ANY
|
fps_update_frequency |
Optional[float]
|
Minimum time (in seconds) between updates to the FPS counter. If None, the counter is updated every frame. |
None
|
Source code in src/super_gradients/training/utils/media/stream.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__del__()
Release the video capture device and close the window.
Source code in src/super_gradients/training/utils/media/stream.py
86 87 88 89 |
|
run()
Start streaming video from the webcam and displaying it in a window.
Press 'q' to quit the streaming.
Source code in src/super_gradients/training/utils/media/stream.py
48 49 50 51 52 53 54 |
|
includes_video_extension(file_path)
Check if a file includes a video extension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
Path to the video file. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the file includes a video extension. |
Source code in src/super_gradients/training/utils/media/video.py
218 219 220 221 222 223 |
|
lazy_load_video(file_path, max_frames=None)
Open a video file and returns a generator which yields frames.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
Path to the video file. |
required |
max_frames |
Optional[int]
|
Optional, maximum number of frames to extract. |
None
|
Returns:
Type | Description |
---|---|
Tuple[Iterator[np.ndarray], int, int]
|
|
Source code in src/super_gradients/training/utils/media/video.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
load_video(file_path, max_frames=None)
Open a video file and extract each frame into numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
Path to the video file. |
required |
max_frames |
Optional[int]
|
Optional, maximum number of frames to extract. |
None
|
Returns:
Type | Description |
---|---|
Tuple[List[np.ndarray], int]
|
|
Source code in src/super_gradients/training/utils/media/video.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
save_gif(output_path, frames, fps)
Save a video locally in .gif format. Safe for generator of frames object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Where the video will be saved |
required |
frames |
Iterable[np.ndarray]
|
Frames representing the video, each in (H, W, C), RGB. Note that all the frames are expected to have the same shape. |
required |
fps |
int
|
Frames per second |
required |
Source code in src/super_gradients/training/utils/media/video.py
119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
save_mp4(output_path, frames, fps)
Save a video locally in .mp4 format. Safe for generator of frames object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Where the video will be saved |
required |
frames |
Iterable[np.ndarray]
|
Frames representing the video, each in (H, W, C), RGB. Note that all the frames are expected to have the same shape. |
required |
fps |
int
|
Frames per second |
required |
Source code in src/super_gradients/training/utils/media/video.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
save_video(output_path, frames, fps)
Save a video locally. Depending on the extension, the video will be saved as a .mp4 file or as a .gif file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Where the video will be saved |
required |
frames |
List[np.ndarray]
|
Frames representing the video, each in (H, W, C), RGB. Note that all the frames are expected to have the same shape. |
required |
fps |
int
|
Frames per second |
required |
Source code in src/super_gradients/training/utils/media/video.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
show_video_from_disk(video_path, window_name='Prediction')
Display a video from disk using OpenCV.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
video_path |
str
|
Path to the video file. |
required |
window_name |
str
|
Name of the window to display the video |
'Prediction'
|
Source code in src/super_gradients/training/utils/media/video.py
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 |
|
show_video_from_frames(frames, fps, window_name='Prediction')
Display a video from a list of frames using OpenCV.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
frames |
List[np.ndarray]
|
Frames representing the video, each in (H, W, C), RGB. Note that all the frames are expected to have the same shape. |
required |
fps |
float
|
Frames per second |
required |
window_name |
str
|
Name of the window to display the video |
'Prediction'
|
Source code in src/super_gradients/training/utils/media/video.py
203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
build_optimizer(net, lr, training_params)
Wrapper function for initializing the optimizer :param net: the nn_module to build the optimizer for :param lr: initial learning rate :param training_params: training_parameters
Source code in src/super_gradients/training/utils/optimizer_utils.py
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 |
|
get_initial_lr_from_optimizer(optimizer)
Returns Initial learning rate as:
float - learning rate value when passed as a scalar Dictionary where keys are group names and values are the learning rates. For example {"default": 0.01, "head": 0.1}
Does so by iterating over the optmizer.param_groups and extracting the "lr" vaules. If the optimizer was intiialized with .parameters() and not named_paramters(), names will be assigned to the optimizer parameter groups by index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimizer |
torch.optim.Optimizer
|
torch.optim.Optimizer, The optimizer to extract the lrs from. |
required |
Returns:
Type | Description |
---|---|
Union[Dict[str, float], float]
|
initial_lr as described above. |
Source code in src/super_gradients/training/utils/optimizer_utils.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
initialize_param_groups(model, lr)
Custom param groups for training with specified learning rates for each group in the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
nn.Module model. |
required |
lr |
Union[float, Dict[str, float]]
|
Dictionary where keys are group names and values are the learning rates, or a learning rate value when passed as a scalar. |
required |
Returns:
Type | Description |
---|---|
List[Dict]
|
List of param groups. |
Source code in src/super_gradients/training/utils/optimizer_utils.py
181 182 183 184 185 186 187 188 189 190 191 192 193 |
|
name_optimizer_param_groups_inplace(optimizer)
Convert an optimizer's param_groups to use named parameters, modifying it in place.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
optimizer |
torch.optim.Optimizer
|
torch.optim.Optimizer, The optimizer to be converted. Returns: torch.optim.Optimizer: The same optimizer with modified param_groups. |
required |
Source code in src/super_gradients/training/utils/optimizer_utils.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
separate_lr_groups(model, lr_dict)
Separate parameters based on specified learning rates for each group in the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
nn.Module model. |
required |
lr_dict |
Dict[str, float]
|
Dictionary where keys are group names and values are the learning rates. |
required |
Returns:
Type | Description |
---|---|
List[Dict]
|
List of param groups with named_parameters and corresponding learning rates. |
Source code in src/super_gradients/training/utils/optimizer_utils.py
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 |
|
separate_zero_wd_params_groups_for_optimizer(module, net_named_params, weight_decay)
separate param groups for batchnorm and biases and others with weight decay. return list of param groups in format required by torch Optimizer classes. bias + BN with weight decay=0 and the rest with the given weight decay :param module: train net module. :param net_named_params: list of params groups, output of SgModule.initialize_param_groups :param weight_decay: value to set for the non BN and bias parameters
Source code in src/super_gradients/training/utils/optimizer_utils.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
This implementation is taken from timm's github: https://github.com/rwightman/pytorch-image-models/blob/master/timm/optim/lamb.py
PyTorch Lamb optimizer w/ behaviour similar to NVIDIA FusedLamb
This optimizer code was adapted from the following (starting with latest) * https://github.com/HabanaAI/Model-References/blob/2b435114fe8e31f159b1d3063b8280ae37af7423/PyTorch/nlp/bert/pretraining/lamb.py * https://github.com/NVIDIA/DeepLearningExamples/blob/master/PyTorch/LanguageModeling/Transformer-XL/pytorch/lamb.py * https://github.com/cybertronai/pytorch-lamb
Use FusedLamb if you can (GPU). The reason for including this variant of Lamb is to have a version that is similar in behaviour to APEX FusedLamb if you aren't using NVIDIA GPUs or cannot install/use APEX.
In addition to some cleanup, this Lamb impl has been modified to support PyTorch XLA and has been tested on TPU.
Original copyrights for above sources are below.
Modifications Copyright 2021 Ross Wightman
Copyright (c) 2021, Habana Labs Ltd. All rights reserved.
Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
MIT License
Copyright (c) 2019 cybertronai
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lamb
Bases: Optimizer
Implements a pure pytorch variant of FuseLAMB (NvLamb variant) optimizer from apex.optimizers.FusedLAMB reference: https://github.com/NVIDIA/DeepLearningExamples/blob/master/PyTorch/LanguageModeling/Transformer-XL/pytorch/lamb.py
LAMB was proposed in Large Batch Optimization for Deep Learning: Training BERT in 76 minutes
_.
Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its norm. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) grad_averaging (bool, optional): whether apply (1-beta2) to grad when calculating running averages of gradient. (default: True) max_grad_norm (float, optional): value used to clip global grad norm (default: 1.0) trust_clip (bool): enable LAMBC trust ratio clipping (default: False) always_adapt (boolean, optional): Apply adaptive learning rate to 0.0 weight decay parameter (default: False)
.. _Large Batch Optimization for Deep Learning - Training BERT in 76 minutes: https://arxiv.org/abs/1904.00962 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ
Source code in src/super_gradients/training/utils/optimizers/lamb.py
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 |
|
step(closure=None)
Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss.
Source code in src/super_gradients/training/utils/optimizers/lamb.py
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 |
|
PyTorch implementation of the Lion optimizer. Code adopted from: https://github.com/google/automl/blob/master/lion/lion_pytorch.py
Lion
Bases: Optimizer
Implements Lion algorithm. Generaly, it is recommended to divide lr used by AdamW by 10 and multiply the weight decay by 10.
Source code in src/super_gradients/training/utils/optimizers/lion.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(params, lr=0.0001, betas=(0.9, 0.99), weight_decay=0.0)
Initialize the hyperparameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
Union[Iterable[torch.Tensor], Iterable[dict]]
|
Iterable of parameters to optimize or dicts defining parameter groups |
required |
lr |
float
|
Learning rate (default: 1e-4) |
0.0001
|
betas |
Tuple[float, float]
|
Coefficients used for computing running averages of gradient and its square (default: (0.9, 0.99)) |
(0.9, 0.99)
|
weight_decay |
float
|
Weight decay coefficient (default: 0) |
0.0
|
Source code in src/super_gradients/training/utils/optimizers/lion.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
step(closure=None)
Perform a single optimization step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
closure |
Optional[callable]
|
A closure that reevaluates the model and returns the loss. |
None
|
Returns:
Type | Description |
---|---|
torch.Tensor
|
Loss. |
Source code in src/super_gradients/training/utils/optimizers/lion.py
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 |
|
RMSpropTF
Bases: Optimizer
Implements RMSprop algorithm (TensorFlow style epsilon)
NOTE: This is a direct cut-and-paste of PyTorch RMSprop with eps applied before sqrt
and a few other modifications to closer match Tensorflow for matching hyper-params.
Noteworthy changes include:
1. Epsilon applied inside square-root
2. square_avg initialized to ones
3. LR scaling of update accumulated in momentum buffer
Proposed by G. Hinton in his
course <http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf>
.
The centered version first appears in Generating Sequences
With Recurrent Neural Networks <https://arxiv.org/pdf/1308.0850v5.pdf>
.
Source code in src/super_gradients/training/utils/optimizers/rmsprop_tf.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(params, lr=0.01, alpha=0.9, eps=1e-10, weight_decay=0, momentum=0.0, centered=False, decoupled_decay=False, lr_in_momentum=True)
RMSprop optimizer that follows the tf's RMSprop characteristics
Parameters:
Name | Type | Description | Default |
---|---|---|---|
(iterable) |
params
|
iterable of parameters to optimize or dicts defining parameter groups. |
required |
Source code in src/super_gradients/training/utils/optimizers/rmsprop_tf.py
35 36 37 38 39 40 41 42 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 |
|
step(closure=None)
Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss.
Source code in src/super_gradients/training/utils/optimizers/rmsprop_tf.py
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 |
|
DEKRPoseEstimationDecodeCallback
Bases: AbstractPoseEstimationPostPredictionCallback
Class that implements decoding logic of DEKR's model predictions into poses.
Source code in src/super_gradients/training/utils/pose_estimation/dekr_decode_callbacks.py
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 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
__call__(predictions)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Tuple[Tensor, Tensor]
|
Tuple (heatmap, offset): heatmap - [BatchSize, NumJoints+1,H,W] offset - [BatchSize, NumJoints*2,H,W] |
required |
Returns:
Type | Description |
---|---|
List[PoseEstimationPredictions]
|
Tuple |
Source code in src/super_gradients/training/utils/pose_estimation/dekr_decode_callbacks.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
|
__init__(output_stride, max_num_people, keypoint_threshold, nms_threshold, nms_num_threshold, apply_sigmoid, min_confidence=0.0)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_stride |
int
|
Output stride of the model |
required |
max_num_people |
int
|
Maximum number of decoded poses |
required |
keypoint_threshold |
float
|
(float) A minimum score of a pose center keypoint for pose to be considered as a potential candidate |
required |
nms_threshold |
float
|
The maximum distance between two joints for them to be considered as belonging to the same pose. Given in terms of a percentage of a square root of the area of the pose bounding box. |
required |
nms_num_threshold |
int
|
Number of joints that must pass the NMS check for the pose to be considered as a valid one. |
required |
apply_sigmoid |
bool
|
If True, apply the sigmoid activation on heatmap. This is needed when heatmap is not bound to [0..1] range and trained with logits (E.g focal loss) |
required |
min_confidence |
float
|
Minimum confidence threshold for pose |
0.0
|
Source code in src/super_gradients/training/utils/pose_estimation/dekr_decode_callbacks.py
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 |
|
aggregate_results(heatmap, posemap, output_stride, pose_center_score_threshold, max_num_people)
Get initial pose proposals and aggregate the results of all scale. Not this implementation works only for batch size of 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
heatmap |
Tensor
|
Heatmap at this scale (B, 1+num_joints, w, h) |
required |
posemap |
Tensor
|
Posemap at this scale (B, 2*num_joints, w, h) |
required |
output_stride |
int
|
Ratio of input size / predictions size |
required |
pose_center_score_threshold |
float
|
(float) A minimum score of a pose center keypoint for pose to be considered as a potential candidate |
required |
max_num_people |
int
|
(int) |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, List[Tensor]]
|
|
Source code in src/super_gradients/training/utils/pose_estimation/dekr_decode_callbacks.py
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 |
|
get_locations(output_h, output_w, device)
Generate location map (each pixel contains its own XY coordinate)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_h |
int
|
Feature map height (rows) |
required |
output_w |
int
|
Feature map width (cols) |
required |
device |
Target device to put tensor on |
required |
Returns:
Type | Description |
---|---|
[H * W, 2] |
Source code in src/super_gradients/training/utils/pose_estimation/dekr_decode_callbacks.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
get_reg_poses(offset, num_joints)
Decode offset predictions into absolute locations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset |
Tensor
|
Tensor of [num_joints*2,H,W] shape with offset predictions for each joint |
required |
num_joints |
int
|
Number of joints |
required |
Returns:
Type | Description |
---|---|
[H * W, num_joints, 2] |
Source code in src/super_gradients/training/utils/pose_estimation/dekr_decode_callbacks.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
pose_nms(heatmap_avg, poses, max_num_people, nms_threshold, nms_num_threshold, pose_score_threshold)
NMS for the regressed poses results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
heatmap_avg |
Tensor
|
Avg of the heatmaps at all scales (1, 1+num_joints, w, h) |
required |
poses |
List
|
Gather of the pose proposals [(num_people, num_joints, 3)] |
required |
max_num_people |
int
|
Maximum number of decoded poses |
required |
nms_threshold |
float
|
The maximum distance between two joints for them to be considered as belonging to the same pose. Given in terms of a percentage of a square root of the area of the pose bounding box. |
required |
nms_num_threshold |
int
|
Number of joints that must pass the NMS check for the pose to be considered as a valid one. |
required |
pose_score_threshold |
float
|
Minimum confidence threshold for pose. Pose with confidence lower than this threshold will be discarded. |
required |
Source code in src/super_gradients/training/utils/pose_estimation/dekr_decode_callbacks.py
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 |
|
DEKRVisualizationCallback
Bases: PhaseCallback
A callback that adds a visualization of a batch of segmentation predictions to context.sg_logger
Parameters:
Name | Type | Description | Default |
---|---|---|---|
phase |
Union[Phase, str]
|
When to trigger the callback. |
required |
prefix |
str
|
Prefix to add to the log. |
required |
mean |
List[float]
|
Mean to subtract from image. |
required |
std |
List[float]
|
Standard deviation to subtract from image. |
required |
apply_sigmoid |
bool
|
Whether to apply sigmoid to the output. |
False
|
batch_idx |
int
|
Batch index to perform visualization for. |
0
|
keypoints_threshold |
float
|
Keypoint threshold to use for visualization. |
0.01
|
Source code in src/super_gradients/training/utils/pose_estimation/dekr_visualization_callbacks.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
denormalize_image(image_normalized)
Reverse image normalization image_normalized (image / 255 - mean) / std
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_normalized |
Tensor
|
normalized [3,H,W] |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
Source code in src/super_gradients/training/utils/pose_estimation/dekr_visualization_callbacks.py
49 50 51 52 53 54 55 56 57 58 59 |
|
RescoringPoseEstimationDecodeCallback
A special adapter callback to be used with PoseEstimationMetrics to use the outputs from rescoring model inside metric class.
Source code in src/super_gradients/training/utils/pose_estimation/rescoring_callback.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
__init__(apply_sigmoid)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apply_sigmoid |
bool
|
If True, apply the sigmoid activation on heatmap. This is needed when heatmap is not bound to [0..1] range and trained with logits (E.g focal loss) |
required |
Source code in src/super_gradients/training/utils/pose_estimation/rescoring_callback.py
13 14 15 16 17 18 19 20 |
|
ImagePoseEstimationPrediction
dataclass
Bases: ImagePrediction
Object wrapping an image and a detection model's prediction.
:attr image: Input image :attr predictions: Predictions of the model :attr class_names: List of the class names to predict
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
draw(edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Draw the predicted bboxes on the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Image with predicted bboxes. Note that this does not modify the original image. |
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
save(output_path, edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Path to the output video file. |
required |
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
show(edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Display the image with predicted bboxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
ImagesPoseEstimationPrediction
dataclass
Bases: ImagesPredictions
Object wrapping the list of image detection predictions.
:attr _images_prediction_lst: List of the predictions results
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
save(output_folder, edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_folder |
str
|
Folder path, where the images will be saved. |
required |
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
show(edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Display the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
VideoPoseEstimationPrediction
dataclass
Bases: VideoPredictions
Object wrapping the list of image detection predictions as a Video.
:attr _images_prediction_lst: List of the predictions results :att fps: Frames per second of the video
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
draw(edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Draw the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_folder |
Folder path, where the images will be saved. |
required | |
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Returns:
Type | Description |
---|---|
Iterator[np.ndarray]
|
Iterator of images with predicted bboxes. Note that this does not modify the original image. |
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
save(output_path, edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Path to the output video file. |
required |
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
show(edge_colors=None, joint_thickness=None, keypoint_colors=None, keypoint_radius=None, box_thickness=None, show_confidence=False)
Display the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_colors |
Optional list of tuples representing the colors for each joint. If None, default colors are used. If not None the length must be equal to the number of joint links in the skeleton. |
None
|
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links (in pixels). |
None
|
keypoint_colors |
Optional[List[Tuple]]
|
Optional list of tuples representing the colors for each keypoint. If None, default colors are used. If not None the length must be equal to the number of joints in the skeleton. |
None
|
keypoint_radius |
Optional[int]
|
(Optional) Radius of the keypoints (in pixels). |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
False
|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_pose_estimation_results.py
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 |
|
ImageClassificationPrediction
dataclass
Bases: ImagePrediction
Object wrapping an image and a classification model's prediction.
:attr image: Input image :attr predictions: Predictions of the model :attr class_names: List of the class names to predict
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
draw(show_confidence=True)
Draw the predicted label on the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Image with predicted label. |
Source code in src/super_gradients/training/utils/predict/prediction_results.py
65 66 67 68 69 70 71 72 73 |
|
save(output_path, show_confidence=True)
Save the predicted label on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Path to the output video file. |
required |
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
84 85 86 87 88 89 90 91 92 93 94 95 |
|
show(show_confidence=True)
Display the image with predicted label.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
75 76 77 78 79 80 81 82 |
|
ImageDetectionPrediction
dataclass
Bases: ImagePrediction
Object wrapping an image and a detection model's prediction.
:attr image: Input image :attr predictions: Predictions of the model :attr class_names: List of the class names to predict
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
draw(box_thickness=None, show_confidence=True, color_mapping=None, target_bboxes=None, target_bboxes_format=None, target_class_ids=None, class_names=None)
Draw the predicted bboxes on the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
target_bboxes |
Optional[np.ndarray]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth bounding boxes. Can either be an np.ndarray of shape (image_i_object_count, 4) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. When not None, will plot the predictions and the ground truth bounding boxes side by side (i.e 2 images stitched as one) |
None
|
target_class_ids |
Optional[np.ndarray]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth target class indices. Can either be an np.ndarray of shape (image_i_object_count) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. |
None
|
target_bboxes_format |
Optional[str]
|
Optional[str], bounding box format of target_bboxes, one of ['xyxy','xywh', 'yxyx' 'cxcywh' 'normalized_xyxy' 'normalized_xywh', 'normalized_yxyx', 'normalized_cxcywh']. Will raise an error if not None and target_bboxes is None. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Image with predicted bboxes. Note that this does not modify the original image. |
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
save(output_path, box_thickness=None, show_confidence=True, color_mapping=None, target_bboxes=None, target_bboxes_format=None, target_class_ids=None, class_names=None)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Path to the output video file. |
required |
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
target_bboxes |
Optional[np.ndarray]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth bounding boxes. Can either be an np.ndarray of shape (image_i_object_count, 4) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. When not None, will plot the predictions and the ground truth bounding boxes side by side (i.e 2 images stitched as one) |
None
|
target_class_ids |
Optional[np.ndarray]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth target class indices. Can either be an np.ndarray of shape (image_i_object_count) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. |
None
|
target_bboxes_format |
Optional[str]
|
Optional[str], bounding box format of target_bboxes, one of ['xyxy','xywh', 'yxyx' 'cxcywh' 'normalized_xyxy' 'normalized_xywh', 'normalized_yxyx', 'normalized_cxcywh']. Will raise an error if not None and target_bboxes is None. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
show(box_thickness=None, show_confidence=True, color_mapping=None, target_bboxes=None, target_bboxes_format=None, target_class_ids=None, class_names=None)
Display the image with predicted bboxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
target_bboxes |
Optional[np.ndarray]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth bounding boxes. Can either be an np.ndarray of shape (image_i_object_count, 4) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. When not None, will plot the predictions and the ground truth bounding boxes side by side (i.e 2 images stitched as one) |
None
|
target_class_ids |
Optional[np.ndarray]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth target class indices. Can either be an np.ndarray of shape (image_i_object_count) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. |
None
|
target_bboxes_format |
Optional[str]
|
Optional[str], bounding box format of target_bboxes, one of ['xyxy','xywh', 'yxyx' 'cxcywh' 'normalized_xyxy' 'normalized_xywh', 'normalized_yxyx', 'normalized_cxcywh']. Will raise an error if not None and target_bboxes is None. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
ImagePrediction
dataclass
Bases: ABC
Object wrapping an image and a model's prediction.
:attr image: Input image :attr predictions: Predictions of the model :attr class_names: List of the class names to predict
Source code in src/super_gradients/training/utils/predict/prediction_results.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
draw(*args, **kwargs)
abstractmethod
Draw the predictions on the image.
Source code in src/super_gradients/training/utils/predict/prediction_results.py
36 37 38 39 |
|
save(*args, **kwargs)
abstractmethod
Save the predictions on the image.
Source code in src/super_gradients/training/utils/predict/prediction_results.py
46 47 48 49 |
|
show(*args, **kwargs)
abstractmethod
Display the predictions on the image.
Source code in src/super_gradients/training/utils/predict/prediction_results.py
41 42 43 44 |
|
ImageSegmentationPrediction
dataclass
Bases: ImagePrediction
Object wrapping an image and a segmentation model's prediction.
:attr image: Input image :attr predictions: Predictions of the model :attr class_names: List of the class names to predict
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
draw(alpha=0.6, color_mapping=None, class_names=None)
Draw the predicted segmentation on the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
float
|
Float number between [0,1] denoting the transparency of the masks (0 means full transparency, 1 means opacity). |
0.6
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
class_names |
Optional[List[str]]
|
List of class names to predict (segmentation classes) |
None
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Image with predicted segmentation. Note that this does not modify the original image. |
Source code in src/super_gradients/training/utils/predict/prediction_results.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
save(output_path, alpha=0.6, color_mapping=None)
Save the predicted segmentation on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
Float number between [0,1] denoting the transparency of the masks (0 means full transparency, 1 means opacity). |
0.6
|
|
output_path |
str
|
Path to the output file. |
required |
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
343 344 345 346 347 348 349 350 351 352 |
|
show(alpha=0.6, color_mapping=None)
Display the image with segmentation prediction overlay.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
float
|
Float number between [0,1] denoting the transparency of the masks (0 means full transparency, 1 means opacity). |
0.6
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
333 334 335 336 337 338 339 340 341 |
|
ImagesClassificationPrediction
dataclass
Bases: ImagesPredictions
Object wrapping the list of image classification predictions.
:attr _images_prediction_lst: List of the predictions results
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
save(output_folder, show_confidence=True)
Save the predicted label on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_folder |
str
|
Folder path, where the images will be saved. |
required |
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
423 424 425 426 427 428 429 430 431 432 433 434 |
|
show(show_confidence=True)
Display the predicted labels on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
416 417 418 419 420 421 |
|
ImagesDetectionPrediction
dataclass
Bases: ImagesPredictions
Object wrapping the list of image detection predictions.
:attr _images_prediction_lst: List of the predictions results
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
save(output_folder, box_thickness=None, show_confidence=True, color_mapping=None, target_bboxes=None, target_bboxes_format=None, target_class_ids=None, class_names=None)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_folder |
str
|
Folder path, where the images will be saved. |
required |
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
target_bboxes |
Optional[Union[np.ndarray, List[np.ndarray]]]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth bounding boxes. Can either be an np.ndarray of shape (image_i_object_count, 4) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. When not None, will plot the predictions and the ground truth bounding boxes side by side (i.e 2 images stitched as one) |
None
|
target_class_ids |
Optional[Union[np.ndarray, List[np.ndarray]]]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth target class indices. Can either be an np.ndarray of shape (image_i_object_count) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. |
None
|
target_bboxes_format |
Optional[str]
|
Optional[str], bounding box format of target_bboxes, one of ['xyxy','xywh', 'yxyx' 'cxcywh' 'normalized_xyxy' 'normalized_xywh', 'normalized_yxyx', 'normalized_cxcywh']. Will raise an error if not None and target_bboxes is None. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
show(box_thickness=None, show_confidence=True, color_mapping=None, target_bboxes=None, target_bboxes_format=None, target_class_ids=None, class_names=None)
Display the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
target_bboxes |
Optional[Union[np.ndarray, List[np.ndarray]]]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth bounding boxes. Can either be an np.ndarray of shape (image_i_object_count, 4) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. When not None, will plot the predictions and the ground truth bounding boxes side by side (i.e 2 images stitched as one) |
None
|
target_class_ids |
Optional[Union[np.ndarray, List[np.ndarray]]]
|
Optional[Union[np.ndarray, List[np.ndarray]]], ground truth target class indices. Can either be an np.ndarray of shape (image_i_object_count) when predicting a single image, or a list of length len(target_bboxes), containing such arrays. |
None
|
target_bboxes_format |
Optional[str]
|
Optional[str], bounding box format of target_bboxes, one of ['xyxy','xywh', 'yxyx' 'cxcywh' 'normalized_xyxy' 'normalized_xywh', 'normalized_yxyx', 'normalized_cxcywh']. Will raise an error if not None and target_bboxes is None. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
ImagesPredictions
dataclass
Bases: ABC
Object wrapping the list of image predictions.
:attr _images_prediction_lst: List of results of the run
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
save(*args, **kwargs)
abstractmethod
Save the predictions on the images.
Source code in src/super_gradients/training/utils/predict/prediction_results.py
378 379 380 381 |
|
show(*args, **kwargs)
abstractmethod
Display the predictions on the images.
Source code in src/super_gradients/training/utils/predict/prediction_results.py
373 374 375 376 |
|
ImagesSegmentationPrediction
dataclass
Bases: ImagesPredictions
Object wrapping the list of image segmentation predictions.
:attr _images_prediction_lst: List of the predictions results
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
save(output_folder, color_mapping=None)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_folder |
str
|
Folder path, where the images will be saved. |
required |
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
655 656 657 658 659 660 661 662 663 664 665 666 667 |
|
show(color_mapping=None)
Display the predicted segmentation on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
646 647 648 649 650 651 652 653 |
|
VideoDetectionPrediction
dataclass
Bases: VideoPredictions
Object wrapping the list of image detection predictions as a Video.
:attr _images_prediction_gen: Iterable object of the predictions results :att fps: Frames per second of the video
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 |
|
draw(box_thickness=None, show_confidence=True, color_mapping=None, class_names=None)
Draw the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Returns:
Type | Description |
---|---|
Iterator[np.ndarray]
|
Iterable object of images with predicted bboxes. Note that this does not modify the original image. |
Source code in src/super_gradients/training/utils/predict/prediction_results.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 |
|
save(output_path, box_thickness=None, show_confidence=True, color_mapping=None, class_names=None)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Path to the output video file. |
required |
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
|
show(box_thickness=None, show_confidence=True, color_mapping=None, class_names=None)
Display the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
None
|
show_confidence |
bool
|
Whether to show confidence scores on the image. |
True
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
class_names |
Optional[List[str]]
|
List of class names to show. By default, is None which shows all classes using during training. |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 |
|
VideoPredictions
dataclass
Bases: ABC
Object wrapping the list of image predictions as a Video.
:attr _images_prediction_gen: List of results of the run :att fps: Frames per second of the video
Source code in src/super_gradients/training/utils/predict/prediction_results.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
|
save(*args, **kwargs)
abstractmethod
Save the predictions on the video.
Source code in src/super_gradients/training/utils/predict/prediction_results.py
401 402 403 404 |
|
show(*args, **kwargs)
abstractmethod
Display the predictions on the video.
Source code in src/super_gradients/training/utils/predict/prediction_results.py
396 397 398 399 |
|
VideoSegmentationPrediction
dataclass
Bases: VideoPredictions
Object wrapping the list of image segmentation predictions as a Video.
:attr _images_prediction_lst: List of the predictions results :att fps: Frames per second of the video
Source code in src/super_gradients/training/utils/predict/prediction_results.py
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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
|
draw(alpha=0.6, color_mapping=None, class_names=None)
Draw the predicted segmentation on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
float
|
Float number between [0,1] denoting the transparency of the masks (0 means full transparency, 1 means opacity). |
0.6
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
class_names |
Optional[List[str]]
|
List of class names to predict (segmentation classes). |
None
|
Returns:
Type | Description |
---|---|
List[np.ndarray]
|
List of images with predicted segmentation. Note that this does not modify the original image. |
Source code in src/super_gradients/training/utils/predict/prediction_results.py
681 682 683 684 685 686 687 688 689 690 691 |
|
save(output_path, alpha=0.6, color_mapping=None, class_names=None)
Save the predicted bboxes on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_path |
str
|
Path to the output video file. |
required |
alpha |
float
|
Float number between [0,1] denoting the transparency of the masks (0 means full transparency, 1 means opacity). |
0.6
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
class_names |
Optional[List[str]]
|
List of class names to predict (segmentation classes). |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
704 705 706 707 708 709 710 711 712 713 714 715 716 |
|
show(alpha=0.6, color_mapping=None, class_names=None)
Display the predicted segmentation on the images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
float
|
Float number between [0,1] denoting the transparency of the masks (0 means full transparency, 1 means opacity). |
0.6
|
color_mapping |
Optional[List[Tuple[int, int, int]]]
|
List of tuples representing the colors for each class. Default is None, which generates a default color mapping based on the number of class names. |
None
|
class_names |
Optional[List[str]]
|
List of class names to predict (segmentation classes). |
None
|
Source code in src/super_gradients/training/utils/predict/prediction_results.py
693 694 695 696 697 698 699 700 701 702 |
|
ClassificationPrediction
dataclass
Bases: Prediction
Represents a Classification prediction
Source code in src/super_gradients/training/utils/predict/predictions.py
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 |
|
__init__(confidence, label, image_shape)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
confidence |
float
|
Confidence scores for each bounding box |
required |
label |
int
|
Labels for each bounding box. |
required |
image_shape |
Optional[Tuple[int, int]]
|
Shape of the image the prediction is made on, (H, W). |
required |
Source code in src/super_gradients/training/utils/predict/predictions.py
136 137 138 139 140 141 142 143 144 145 146 147 |
|
DetectionPrediction
dataclass
Bases: Prediction
Represents a detection prediction, with bboxes represented in xyxy format.
Source code in src/super_gradients/training/utils/predict/predictions.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
__init__(bboxes, bbox_format, confidence, labels, image_shape)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes |
np.ndarray
|
BBoxes in the format specified by bbox_format |
required |
bbox_format |
str
|
BBoxes format that can be a string ("xyxy", "cxywh", ...) |
required |
confidence |
np.ndarray
|
Confidence scores for each bounding box |
required |
labels |
np.ndarray
|
Labels for each bounding box. |
required |
image_shape |
Tuple[int, int]
|
Shape of the image the prediction is made on, (H, W). This is used to convert bboxes to xyxy format |
required |
target_bboxes |
np.ndarray, ground truth bounding boxes as np.ndarray of shape (image_i_object_count, 4) When not None, will plot the predictions and the ground truth bounding boxes side by side (i.e 2 images stitched as one) |
required | |
target_labels |
np.ndarray, ground truth target class indices as an np.ndarray of shape (image_i_object_count). |
required | |
target_bbox_format |
str, bounding box format of target_bboxes, one of ['xyxy','xywh', 'yxyx' 'cxcywh' 'normalized_xyxy' 'normalized_xywh', 'normalized_yxyx', 'normalized_cxcywh']. Will raise an error if not None and target_bboxes is None. |
required |
Source code in src/super_gradients/training/utils/predict/predictions.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
PoseEstimationPrediction
dataclass
Bases: Prediction
Represents a pose estimation prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poses |
np.ndarray
|
Numpy array of [Num Poses, Num Joints, 2] shape |
required |
scores |
np.ndarray
|
Numpy array of [Num Poses] shape |
required |
boxes |
Numpy array of [Num Poses, 4] shape which represents the bounding boxes of each pose in xyxy format |
required |
Source code in src/super_gradients/training/utils/predict/predictions.py
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 |
|
__init__(poses, scores, bboxes_xyxy, edge_links, edge_colors, keypoint_colors, image_shape)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poses |
np.ndarray
|
Predicted poses as a numpy array of shape [Num Poses, Num Joints, 2] |
required |
scores |
np.ndarray
|
Confidence scores for each pose [Num Poses] |
required |
bboxes_xyxy |
Optional[np.ndarray]
|
Bounding boxes of each pose in xyxy format [Num Poses, 4] |
required |
image_shape |
Tuple[int, int]
|
Shape of the image the prediction is made on, (H, W). |
required |
Source code in src/super_gradients/training/utils/predict/predictions.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
SegmentationPrediction
dataclass
Bases: Prediction
Represents a segmentation prediction.
Source code in src/super_gradients/training/utils/predict/predictions.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
__init__(segmentation_map, segmentation_map_shape, image_shape)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
segmentation_map |
np.ndarray
|
Segmentation map (predication) in the shape specified segmentation_map_shape |
required |
segmentation_map_shape |
Tuple[int, int]
|
Shape of the prediction (H, W). |
required |
image_shape |
Tuple[int, int]
|
Shape of the image the prediction is made on, (H, W). |
required |
Source code in src/super_gradients/training/utils/predict/predictions.py
167 168 169 170 171 172 173 174 175 176 177 |
|
Quantization utilities
Methods are based on: https://github.com/NVIDIA/TensorRT/blob/51a4297753d3e12d0eed864be52400f429a6a94c/tools/pytorch-quantization/examples/torchvision/classification_flow.py#L385
(Licensed under the Apache License, Version 2.0)
QuantizationCalibrator
Source code in src/super_gradients/training/utils/quantization/calibrator.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
calibrate_model(model, calib_data_loader, method='percentile', num_calib_batches=2, percentile=99.99)
Calibrates torch model with quantized modules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
torch.nn.Module
|
torch.nn.Module, model to perfrom the calibration on. |
required |
calib_data_loader |
torch.utils.data.DataLoader
|
torch.utils.data.DataLoader, data loader of the calibration dataset. Assumes that the first element of the tuple is the input image. |
required |
method |
str
|
str, One of [percentile, mse, entropy, max]. Statistics method for amax computation of the quantized modules (Default=percentile). |
'percentile'
|
num_calib_batches |
int
|
int, number of batches to collect the statistics from. |
2
|
percentile |
float
|
float, percentile value to use when SgModel,quant_modules_calib_method='percentile'. Discarded when other methods are used (Default=99.99). |
99.99
|
Source code in src/super_gradients/training/utils/quantization/calibrator.py
33 34 35 36 37 38 39 40 41 42 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 |
|
QuantizedMapping
Bases: nn.Module
This class wraps a float module instance, and defines a mapping from this instance to the corresponding quantized class, with relevant quant descriptors.
Example: self.my_block = QuantizedMapping(float_module=MyBlock(4, n_classes), quantized_target_class=MyQuantizedBlock)
Source code in src/super_gradients/training/utils/quantization/core.py
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 |
|
QuantizedMetadata
dataclass
This dataclass is responsible for holding the information regarding float->quantized module relation.
It can be both layer-grained and module-grained, e.g.,
module.backbone.conv1 -> QuantConv2d
, nn.Linear -> QuantLinear
, etc...
Parameters:
Name | Type | Description | Default |
---|---|---|---|
float_source |
Union[str, Type]
|
Name of a specific layer (e.g., |
required |
quantized_target_class |
Optional[Union[Type[QuantMixin], Type[QuantInputMixin], Type[SGQuantMixin]]]
|
Quantized type that the source will be converted to |
required |
action |
ReplacementAction
|
how to resolve the conversion, we either: - SKIP: skip it, - UNWRAP: unwrap the instance and work with the wrapped one (i.e., we wrap with a mapper), - REPLACE: replace source with an instance of the quantized type - REPLACE_AND_RECURE: replace source with an instance of the quantized type, then try to recursively quantize the child modules of that type - RECURE_AND_REPLACE: recursively quantize the child modules, then replace source with an instance of the quantized type |
required |
input_quant_descriptor |
QuantDescriptor
|
Quantization descriptor for inputs (None will take the default one) |
None
|
weights_quant_descriptor |
QuantDescriptor
|
Quantization descriptor for weights (None will take the default one) |
None
|
Source code in src/super_gradients/training/utils/quantization/core.py
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 |
|
SGQuantMixin
Bases: nn.Module
A base class for user custom Quantized classes.
Every Quantized class must inherit this mixin, which adds from_float
class-method.
NOTES:
* the Quantized class may also inherit from the native QuantMixin
or QuantInputMixin
* quant descriptors (for inputs and weights) will be passed as kwargs
. The module may ignore them if they are
not necessary
* the default implementation of from_float
is inspecting the init args, and searching for corresponding
properties from the float instance that is passed as argument, e.g., for __init__(self, a)
the mechanism will look for float_instance.a
and pass that value to the __init__
method
Source code in src/super_gradients/training/utils/quantization/core.py
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 |
|
SkipQuantization
Bases: nn.Module
This class wraps a float module instance, and defines that this instance will not be converted to quantized version
Example: self.my_block = SkipQuantization(MyBlock(4, n_classes))
Source code in src/super_gradients/training/utils/quantization/core.py
81 82 83 84 85 86 87 88 89 90 91 92 |
|
export_quantized_module_to_onnx(model, onnx_filename, input_shape, train=False, to_cpu=True, deepcopy_model=False, **kwargs)
Method for exporting onnx after QAT.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
to_cpu |
bool
|
transfer model to CPU before converting to ONNX, dirty workaround when model's tensors are on different devices |
True
|
train |
bool
|
export model in training mode |
False
|
model |
torch.nn.Module
|
torch.nn.Module, model to export |
required |
onnx_filename |
str
|
str, target path for the onnx file, |
required |
input_shape |
tuple
|
tuple, input shape (usually BCHW) |
required |
deepcopy_model |
Whether to export deepcopy(model). Necessary in case further training is performed and prep_model_for_conversion makes the network un-trainable (i.e RepVGG blocks). |
False
|
Source code in src/super_gradients/training/utils/quantization/export.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
QuantBackboneInternalSkipConnection
Bases: QuantSkipConnection
This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between blocks inside the backbone.
Source code in src/super_gradients/training/utils/quantization/modules/quantized_skip_connections.py
40 41 42 43 44 45 |
|
QuantCrossModelSkipConnection
Bases: QuantSkipConnection
This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between backbone and the head.
Source code in src/super_gradients/training/utils/quantization/modules/quantized_skip_connections.py
56 57 58 59 60 61 |
|
QuantHeadInternalSkipConnection
Bases: QuantSkipConnection
This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between blocks inside the head.
Source code in src/super_gradients/training/utils/quantization/modules/quantized_skip_connections.py
48 49 50 51 52 53 |
|
QuantResidual
Bases: SGQuantMixin
This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a residual skip connection within a single block.
Source code in src/super_gradients/training/utils/quantization/modules/quantized_skip_connections.py
16 17 18 19 20 21 22 23 24 25 |
|
QuantSkipConnection
Bases: SGQuantMixin
This is a placeholder module used by the quantization engine only. The module is to be used as a quantized substitute to a skip connection between blocks.
Source code in src/super_gradients/training/utils/quantization/modules/quantized_skip_connections.py
28 29 30 31 32 33 34 35 36 37 |
|
QuantAttentionRefinementModule
Bases: SGQuantMixin
, AttentionRefinementModule
AttentionRefinementModule to apply on the last two backbone stages.
Source code in src/super_gradients/training/utils/quantization/modules/quantized_stdc_blocks.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
QuantBottleneck
Bases: SGQuantMixin
we just insert quantized tensor to the shortcut (=residual) layer, so that it would be quantized NOTE: we must quantize the float instance, so the mode should be QuantizedMetadata.ReplacementAction.RECURE_AND_REPLACE
Source code in src/super_gradients/training/utils/quantization/modules/resnet_bottleneck.py
8 9 10 11 12 13 14 15 16 17 18 19 |
|
ptq(model, selective_quantizer, calibration_loader, calibration_method='percentile', calibration_batches=16, calibration_percentile=99.99, calibration_verbose=False)
Perform Post Training Quantization (PTQ) on the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Input model to quantize. This function always returns a new model, the input model is not modified. |
required | |
selective_quantizer |
Optional[SelectiveQuantizer]
|
An instance of SelectiveQuantizer class that defines what modules to quantize. |
required |
calibration_loader |
Optional[DataLoader]
|
An instance of DataLoader that provides calibration data (optional). |
required |
calibration_method |
str
|
(str) Calibration method for quantized models. See QuantizationCalibrator for details. |
'percentile'
|
calibration_batches |
int
|
(int) Number of batches to use for calibration. Default is 16. |
16
|
calibration_percentile |
float
|
(float) Percentile for percentile calibration method. Default is 99.99. |
99.99
|
calibration_verbose |
bool
|
False
|
Returns:
Type | Description |
---|---|
A quantized model |
Source code in src/super_gradients/training/utils/quantization/ptq.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
SelectiveQuantizer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
custom_mappings |
dict
|
custom mappings that extend the default mappings with extra behaviour |
None
|
default_per_channel_quant_weights |
bool
|
whether quant module weights should be per channel (default=True) |
True
|
default_quant_modules_calibrator_weights |
str
|
default calibrator method for weights (default='max') |
'max'
|
default_quant_modules_calibrator_inputs |
str
|
default calibrator method for inputs (default='histogram') |
'histogram'
|
default_learn_amax |
bool
|
EXPERIMENTAL! whether quant modules should have learnable amax (default=False) |
False
|
Source code in src/super_gradients/training/utils/quantization/selective_quantization_utils.py
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 |
|
register_quantized_module(float_source, action=QuantizedMetadata.ReplacementAction.REPLACE, input_quant_descriptor=None, weights_quant_descriptor=None)
Decorator used to register a Quantized module as a quantized version for Float module
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action |
QuantizedMetadata.ReplacementAction
|
action to perform on the float_source |
QuantizedMetadata.ReplacementAction.REPLACE
|
float_source |
Union[str, Type[nn.Module]]
|
the float module type that is being registered |
required |
input_quant_descriptor |
Optional[QuantDescriptor]
|
the input quantization descriptor |
None
|
weights_quant_descriptor |
Optional[QuantDescriptor]
|
the weight quantization descriptor |
None
|
Source code in src/super_gradients/training/utils/quantization/selective_quantization_utils.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
use_fb_fake_quant
Context manager object to ensure that fake quantization state is preserved
with use_fb_fake_quant(True): do_stuff()
Source code in src/super_gradients/training/utils/quantization/use_fb_fake_quant.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
DropPath
Bases: nn.Module
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
Intended usage of this block is the following:
class ResNetBlock(nn.Module): def init(self, ..., drop_path_rate:float): self.drop_path = DropPath(drop_path_rate)
def forward(self, x): return x + self.drop_path(self.conv_bn_act(x))
Code taken from TIMM (https://github.com/rwightman/pytorch-image-models) Apache License 2.0
Source code in src/super_gradients/training/utils/regularization_utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
__init__(drop_prob=0.0, scale_by_keep=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
drop_prob |
float
|
Probability of zeroing out individual vector (channel dimension) of each feature map |
0.0
|
scale_by_keep |
bool
|
Whether to scale the output by the keep probability. Enable by default and helps to keep output mean & std in the same range as w/o drop path. |
True
|
Source code in src/super_gradients/training/utils/regularization_utils.py
34 35 36 37 38 39 40 41 42 43 |
|
drop_path(x, drop_prob=0.0, scale_by_keep=True)
Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
Source code in src/super_gradients/training/utils/regularization_utils.py
4 5 6 7 8 9 10 11 12 13 14 |
|
BinarySegmentationVisualization
Source code in src/super_gradients/training/utils/segmentation_utils.py
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 |
|
visualize_batch(image_tensor, pred_mask, target_mask, batch_name, checkpoint_dir=None, undo_preprocessing_func=reverse_imagenet_preprocessing, image_scale=1.0)
staticmethod
A helper function to visualize detections predicted by a network: saves images into a given path with a name that is {batch_name}_{imade_idx_in_the_batch}.jpg, one batch per call. Colors are generated on the fly: uniformly sampled from color wheel to support all given classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_tensor |
torch.Tensor
|
rgb images, (B, H, W, 3) |
required |
pred_mask |
torch.Tensor
|
prediction mask in shape [B, 1, H, W] with C number of classes |
required |
target_mask |
torch.Tensor
|
(Num_targets, 6), values on dim 1 are: image id in a batch, class, x y w h (coordinates scaled to [0, 1]) |
required |
batch_name |
Union[int, str]
|
id of the current batch to use for image naming |
required |
checkpoint_dir |
str
|
a path where images with boxes will be saved. if None, the result images will be returns as a list of numpy image arrays |
None
|
undo_preprocessing_func |
Callable[[torch.Tensor], np.ndarray]
|
a function to convert preprocessed images tensor into a batch of cv2-like images |
reverse_imagenet_preprocessing
|
image_scale |
float
|
scale factor for output image |
1.0
|
Source code in src/super_gradients/training/utils/segmentation_utils.py
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 |
|
forward_with_sliding_window_wrapper(forward, img, sliding_window_stride, sliding_window_crop_size, num_classes)
Inference by sliding-window with overlap. It involves systematically moving a window with a fixed crop-size over the input image. As the window moves across the image, features or patterns within the window are extracted by running a forward pass of the crop image through the net.
If h_crop > h_img or w_crop > w_img, the small patch will be used to decode without padding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
forward |
Callable[[torch.Tensor], torch.Tensor]
|
a model's forward function. |
required |
img |
torch.Tensor
|
a batch of images to benchmark the model on using sliding window. |
required |
sliding_window_stride |
tuple
|
(height, width) the stride size between crops for forward with sliding window |
required |
sliding_window_crop_size |
tuple
|
(height, width) the crop size to take from the image for forward with sliding window |
required |
num_classes |
int
|
the number of classes. return: predictions tensor |
required |
Source code in src/super_gradients/training/utils/segmentation_utils.py
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 |
|
one_hot_to_binary_edge(x, kernel_size, flatten_channels=True)
Utils function to create edge feature maps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
torch.Tensor
|
input tensor, must be one_hot tensor with shape [B, C, H, W] |
required |
kernel_size |
int
|
kernel size of dilation erosion convolutions. The result edge widths depends on this argument as follows: |
required |
flatten_channels |
bool
|
Whether to apply logical_or across channels dimension, if at least one pixel class is considered as edge pixel flatten value is 1. If set as |
True
|
Returns:
Type | Description |
---|---|
torch.Tensor
|
one_hot edge torch.Tensor. |
Source code in src/super_gradients/training/utils/segmentation_utils.py
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 |
|
reverse_imagenet_preprocessing(im_tensor)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im_tensor |
torch.Tensor
|
images in a batch after preprocessing for inference, RGB, (B, C, H, W) |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
images in a batch in cv2 format, BGR, (B, H, W, C) |
Source code in src/super_gradients/training/utils/segmentation_utils.py
59 60 61 62 63 64 65 66 67 68 69 |
|
target_to_binary_edge(target, num_classes, kernel_size, ignore_index=None, flatten_channels=True)
Utils function to create edge feature maps from target.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
torch.Tensor
|
Class labels long tensor, with shape [N, H, W] |
required |
num_classes |
int
|
num of classes in datasets excluding ignore label, this is the output channels of the one hot result. |
required |
kernel_size |
int
|
kernel size of dilation erosion convolutions. The result edge widths depends on this argument as follows: |
required |
flatten_channels |
bool
|
Whether to apply logical or across channels dimension, if at least one pixel class is considered as edge pixel flatten value is 1. If set as |
True
|
ignore_index |
int
|
the index of the class in the dataset to ignore |
None
|
Returns:
Type | Description |
---|---|
torch.Tensor
|
one_hot edge torch.Tensor. |
Source code in src/super_gradients/training/utils/segmentation_utils.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
to_one_hot(target, num_classes, ignore_index=None)
Target label to one_hot tensor. labels and ignore_index must be consecutive numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
torch.Tensor
|
Class labels long tensor, with shape [N, H, W] |
required |
num_classes |
int
|
num of classes in datasets excluding ignore label, this is the output channels of the one hot result. |
required |
ignore_index |
int
|
the index of the class in the dataset to ignore |
None
|
Returns:
Type | Description |
---|---|
one hot tensor with shape [N, num_classes, H, W] |
Source code in src/super_gradients/training/utils/segmentation_utils.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
ImprovementType
Bases: Enum
Type of improvement compared to previous value, i.e. if the value is better, worse or the same.
Difference with "increase": If a loss goes from 1 to 0.5, the value is smaller (decreased), but the result is better (improvement). For accuracy from 1 to 0.5, the value is smaller, but this time the result decreased, because greater is better.
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
to_color()
Get the color representing the current improvement type
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
64 65 66 67 68 69 70 71 72 73 |
|
IncreaseType
Bases: Enum
Type of increase compared to previous value, i.e. if the value is greater, smaller or the same.
Difference with "improvement": If a loss goes from 1 to 0.5, the value is smaller (decreased), but the result is better (improvement). For accuracy from 1 to 0.5, the value is smaller, but this time the result decreased, because greater is better.
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
to_symbol()
Get the symbol representing the current increase type
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
39 40 41 42 43 44 45 46 47 48 |
|
MonitoredValue
dataclass
Store a value and some indicators relative to its past iterations.
The value can be a metric/loss, and the iteration can be epochs/batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of the metric |
required |
greater_is_better |
Optional[bool]
|
True, a greater value is considered better. ex: (greater_is_better=True) For Accuracy 1 is greater and therefore better than 0.4 ex: (greater_is_better=False) For Loss 1 is greater and therefore worse than 0.4 None when unknown |
None
|
current |
Optional[float]
|
Current value of the metric |
None
|
previous |
Optional[float]
|
Value of the metric in previous iteration |
None
|
best |
Optional[float]
|
Value of the metric in best iteration (best according to greater_is_better) |
None
|
change_from_previous |
Optional[float]
|
Change compared to previous iteration value |
None
|
change_from_best |
Optional[float]
|
Change compared to best iteration value |
None
|
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
has_improved_from_best: ImprovementType
property
Type of improvement compared to best value, i.e. if the value is better, worse or the same.
has_improved_from_previous: ImprovementType
property
Type of improvement compared to previous value, i.e. if the value is better, worse or the same.
has_increased_from_best: IncreaseType
property
Type of increase compared to best value, i.e. if the value is greater, smaller or the same.
has_increased_from_previous: IncreaseType
property
Type of increase compared to previous value, i.e. if the value is greater, smaller or the same.
add_log_to_file(filename, results_titles_list, results_values_list, epoch, max_epochs)
Add a message to the log file
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
340 341 342 343 344 345 346 347 348 |
|
display_epoch_summary(epoch, n_digits, monitored_values_dict)
Display a summary of loss/metric of interest, for a given epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch |
int
|
the number of epoch. |
required |
n_digits |
int
|
number of digits to display on screen for float values |
required |
monitored_values_dict |
Dict[str, Dict[str, MonitoredValue]]
|
Dict of Dict. The first one represents the splut, and the second one a loss/metric. |
required |
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
get_callable_param_names(obj)
Get the param names of a given callable (function, class, ...)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
callable
|
Object to inspect |
required |
Returns:
Type | Description |
---|---|
Tuple[str]
|
Param names of that object |
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
439 440 441 442 443 444 |
|
get_lr_info(model, param_groups)
Generate a string with information about the model and learning rates for each parameter group.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
(nn.Module): The PyTorch model. |
required |
param_groups |
List[Dict[str, Union[str, float, List[tuple]]]]
|
(List[Dict[str, Union[str, float, List[tuple]]]]): List of dictionaries containing information about each parameter group, including the group name, learning rate, and named parameters. Returns: str: A formatted string with information about the model and learning rates. |
required |
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
init_summary_writer(tb_dir, checkpoint_loaded, user_prompt=False)
Remove previous tensorboard files from directory and launch a tensor board process
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
launch_tensorboard_process(checkpoints_dir_path, sleep_postpone=True, port=None)
launch_tensorboard_process - Default behavior is to scan all free ports from 6006-6016 and try using them unless port is defined by the user :param checkpoints_dir_path: :param sleep_postpone: :param port: :return: tuple of tb process, port
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
log_main_training_params(multi_gpu, num_gpus, batch_size, batch_accumulate, train_dataset_length, train_dataloader_len, model, param_groups, max_train_batches=None)
Log training parameters
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
log_uncaught_exceptions(logger)
Makes logger log uncaught exceptions
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger |
logging.Logger |
required |
Returns:
Type | Description |
---|---|
None |
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
|
parse_args(cfg, arg_names)
parse args from a config. unlike get_param(), in this case only parameters that appear in the config will override default params from the function's signature
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
424 425 426 427 428 429 430 431 432 433 434 435 436 |
|
try_port(port)
try_port - Helper method for tensorboard port binding
Parameters:
Name | Type | Description | Default |
---|---|---|---|
port |
required |
Returns:
Type | Description |
---|---|
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
unpack_batch_items(batch_items)
Adds support for unpacking batch items in train/validation loop.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_items |
Union[tuple, torch.Tensor]
|
(Union[tuple, torch.Tensor]) returned by the data loader, which is expected to be in one of the following formats: 1. torch.Tensor or tuple, s.t inputs = batch_items[0], targets = batch_items[1] and len(batch_items) = 2 2. tuple: (inputs, targets, additional_batch_items) where inputs are fed to the network, targets are their corresponding labels and additional_batch_items is a dictionary (format {additional_batch_item_i_name: additional_batch_item_i ...}) which can be accessed through the phase context under the attribute additional_batch_item_i_name, using a phase callback. |
required |
Returns:
Type | Description |
---|---|
inputs, target, additional_batch_items |
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
update_monitored_value(previous_monitored_value, new_value)
Update the given ValueToMonitor object (could be a loss or a metric) with the new value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
previous_monitored_value |
MonitoredValue
|
The stats about the value that is monitored throughout epochs. |
required |
new_value |
float
|
The value of the current epoch that will be used to update previous_monitored_value |
required |
Returns:
Type | Description |
---|---|
MonitoredValue
|
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
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 |
|
update_monitored_values_dict(monitored_values_dict, new_values_dict)
Update the given ValueToMonitor object (could be a loss or a metric) with the new value
Parameters:
Name | Type | Description | Default |
---|---|---|---|
monitored_values_dict |
Dict[str, MonitoredValue]
|
Dict mapping value names to their stats throughout epochs. |
required |
new_values_dict |
Dict[str, float]
|
Dict mapping value names to their new (i.e. current epoch) value. |
required |
Returns:
Type | Description |
---|---|
Dict[str, MonitoredValue]
|
Updated monitored_values_dict |
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
write_hpms(writer, hpmstructs=[], special_conf={})
Stores the training and dataset hyper params in the tensorboard file
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
360 361 362 363 364 365 366 367 368 369 |
|
write_training_results(writer, results_titles_list, results_values_list, epoch)
Stores the training and validation loss and accuracy for current epoch in a tensorboard file
Source code in src/super_gradients/training/utils/sg_trainer_utils.py
351 352 353 354 355 356 357 |
|
DefaultBoxes
Bases: object
Default Boxes, (aka: anchor boxes or priors boxes) used by SSD model
Source code in src/super_gradients/training/utils/ssd_utils.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(fig_size, feat_size, scales, aspect_ratios, scale_xy=0.1, scale_wh=0.2)
For each feature map i (each predicting level, grids) the anchors (a.k.a. default boxes) will be: [ [s, s], [sqrt(s * s_next), sqrt(s * s_next)], [s * sqrt(alpha1), s / sqrt(alpha1)], [s / sqrt(alpha1), s * sqrt(alpha1)], ... [s * sqrt(alphaN), s / sqrt(alphaN)], [s / sqrt(alphaN), s * sqrt(alphaN)] ] / fig_size where: * s = scale[i] - this level's scale * s_next = scale[i + 1] - next level's scale * alpha1, ... alphaN - this level's alphas, e.g. [2, 3] * fig_size - input image resolution
Because of division by image resolution, the anchors will be in image coordinates normalized to [0, 1]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fig_size |
int
|
input image resolution |
required |
feat_size |
List[int]
|
resolution of all feature maps with predictions (grids) |
required |
scales |
List[int]
|
anchor sizes in pixels for each feature level; one value per level will be used to generate anchors based on the formula above |
required |
aspect_ratios |
List[List[int]]
|
lists of alpha values for each feature map |
required |
scale_xy |
predicted boxes will be with a factor scale_xy so will be multiplied by scale_xy during post-prediction processing; e.g. scale 0.1 means that prediction will be 10 times bigger (improves predictions quality) |
0.1
|
|
scale_wh |
same logic as in scale_xy, but for width and height. |
0.2
|
Source code in src/super_gradients/training/utils/ssd_utils.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
SSDPostPredictCallback
Bases: DetectionPostPredictionCallback
post prediction callback module to convert and filter predictions coming from the SSD net to a format used by all other detection models
Source code in src/super_gradients/training/utils/ssd_utils.py
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 |
|
__init__(conf=0.001, iou=0.6, classes=None, max_predictions=300, nms_type=NMS_Type.ITERATIVE, multi_label_per_box=True)
Predictions of SSD contain unnormalized probabilities for a background class, together with confidences for all the dataset classes. Background will be utilized and discarded, so this callback will return 0-based classes without background
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conf |
float
|
confidence threshold |
0.001
|
iou |
float
|
IoU threshold |
0.6
|
classes |
list
|
(optional list) filter by class |
None
|
nms_type |
NMS_Type
|
the type of nms to use (iterative or matrix) |
NMS_Type.ITERATIVE
|
multi_label_per_box |
controls whether to decode multiple labels per box. True - each anchor can produce multiple labels of different classes that pass confidence threshold check (default). False - each anchor can produce only one label of the class with the highest score. |
True
|
Source code in src/super_gradients/training/utils/ssd_utils.py
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 |
|
AverageMeter
A class to calculate the average of a metric, for each batch during training/testing
Source code in src/super_gradients/training/utils/utils.py
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 |
|
HpmStruct
Source code in src/super_gradients/training/utils/utils.py
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 |
|
to_dict(include_schema=True)
Convert this HpmStruct instance into a dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
include_schema |
If True, also return the field "schema" |
True
|
Returns:
Type | Description |
---|---|
dict
|
Dict representation of this HpmStruct instance. |
Source code in src/super_gradients/training/utils/utils.py
60 61 62 63 64 65 66 67 68 |
|
validate()
Validate the current dict values according to the provided schema
Source code in src/super_gradients/training/utils/utils.py
70 71 72 73 74 75 76 77 78 79 80 81 |
|
Timer
A class to measure time handling both GPU & CPU processes Returns time in milliseconds
Source code in src/super_gradients/training/utils/utils.py
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 |
|
__init__(device)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device |
str
|
str 'cpu''cuda' |
required |
Source code in src/super_gradients/training/utils/utils.py
159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
arch_params_deprecated(func)
Since initialization of arch_params is deprecated and will be removed, this decorator will be used to wrap the init function of some models. It will unwrap the parameters of the function and will log a warning.
Source code in src/super_gradients/training/utils/utils.py
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 |
|
check_img_size_divisibility(img_size, stride=32)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_size |
int
|
Int, the size of the image (H or W). |
required |
stride |
int
|
Int, the number to check if img_size is divisible by. |
32
|
Returns:
Type | Description |
---|---|
Tuple[bool, Optional[Tuple[int, int]]]
|
(True, None) if img_size is divisble by stride, (False, Suggestions) if it's not. Note: Suggestions are the two closest numbers to img_size that are divisible by stride. For example if img_size=321, stride=32, it will return (False,(352, 320)). |
Source code in src/super_gradients/training/utils/utils.py
565 566 567 568 569 570 571 572 573 574 575 576 577 |
|
check_model_contains_quantized_modules(model)
Check if the model contains any quantized modules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
Model to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the model contains any quantized modules, False otherwise. |
Source code in src/super_gradients/training/utils/utils.py
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
|
check_models_have_same_weights(model_1, model_2, skip_bn_stats=False)
Checks whether two networks have the same weights
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_1 |
torch.nn.Module
|
Net to be checked |
required |
model_2 |
torch.nn.Module
|
Net to be checked |
required |
skip_bn_stats |
bool
|
bool, whether to skip batch normazliation related stats |
False
|
Returns:
Type | Description |
---|---|
True iff the two networks have the same weights |
Source code in src/super_gradients/training/utils/utils.py
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 |
|
convert_to_tensor(array, dtype=None, device=None)
Converts numpy arrays and lists to Torch tensors before calculation losses
Parameters:
Name | Type | Description | Default |
---|---|---|---|
array |
torch.tensor / Numpy array / List |
required |
Source code in src/super_gradients/training/utils/utils.py
36 37 38 39 40 41 42 43 44 45 46 |
|
download_and_untar_from_url(urls, dir='.')
Download a file from url and untar.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
urls |
List[str]
|
Url to download the file from. |
required |
dir |
Union[str, Path]
|
Destination directory. |
'.'
|
Source code in src/super_gradients/training/utils/utils.py
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 |
|
download_and_unzip_from_url(url, dir='.', unzip=True, delete=True)
Downloads a zip file from url to dir, and unzips it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
Url to download the file from. |
required | |
dir |
Destination directory. |
'.'
|
|
unzip |
Whether to unzip the downloaded file. |
True
|
|
delete |
Whether to delete the zip file. used to downlaod VOC. Source: https://github.com/ultralytics/yolov5/blob/master/data/VOC.yaml |
True
|
Source code in src/super_gradients/training/utils/utils.py
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 |
|
empty_list()
Instantiate an empty list. This is a workaround to generate a list with a function call in hydra, instead of the "[]".
Source code in src/super_gradients/training/utils/utils.py
31 32 33 |
|
ensure_is_tuple_of_two(inputs)
Checks input and converts it to a tuple of length two. If input is None returns None.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Union[Any, Iterable[Any], None]
|
Input argument, either a number or a tuple of two numbers. |
required |
Returns:
Type | Description |
---|---|
Union[Tuple[Any, Any], None]
|
Tuple of two numbers if input is not None, otherwise - None. |
Source code in src/super_gradients/training/utils/utils.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
|
exif_size(image)
Get the size of image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
Image
|
The image to get size from |
required |
Returns:
Type | Description |
---|---|
Tuple[int, int]
|
(height, width) |
Source code in src/super_gradients/training/utils/utils.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
fuzzy_idx_in_list(name, lst)
Returns the index of name in lst, with non sensitivity to symbols, uppercase and lowercase.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
str, the name to be searched in lst. |
required |
lst |
List[str]
|
List[str], the list as described above. |
required |
Returns:
Type | Description |
---|---|
int
|
int, index of name in lst in the matter discussed above. |
Source code in src/super_gradients/training/utils/utils.py
293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
fuzzy_keys(params)
Returns params.key() removing leading and trailing white space, lower-casing and dropping symbols.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
Mapping
|
Mapping, the mapping containing the keys to be returned. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List[str], list of keys as discussed above. |
Source code in src/super_gradients/training/utils/utils.py
246 247 248 249 250 251 252 |
|
fuzzy_str(s)
Returns s removing leading and trailing white space, lower-casing and drops non word chars (except for '/')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s |
str
|
str, string to apply the manipulation discussed above. |
required |
Returns:
Type | Description |
---|---|
str, s after the manipulation discussed above. |
Source code in src/super_gradients/training/utils/utils.py
255 256 257 258 259 260 261 |
|
generate_batch(iterable, batch_size)
Batch data into tuples of length n. The last batch may be shorter.
Source code in src/super_gradients/training/utils/utils.py
633 634 635 636 637 638 639 640 641 |
|
get_filename_suffix_by_framework(framework)
Return the file extension of framework.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
framework |
str
|
(str) |
required |
Returns:
Type | Description |
---|---|
(str) the suffix for the specific framework |
Source code in src/super_gradients/training/utils/utils.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
get_fuzzy_attr(params, name)
Returns attribute (same functionality as getattr), but non sensitive to symbols, uppercase and lowercase.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
Any
|
Any, any object which wed looking for the attribute name in. |
required |
name |
str
|
str, the attribute of param to be returned. |
required |
Returns:
Type | Description |
---|---|
Any, the attribute value or None when not fuzzy matching of the attribute is found |
Source code in src/super_gradients/training/utils/utils.py
283 284 285 286 287 288 289 290 |
|
get_fuzzy_mapping_param(name, params)
Returns parameter value, with key=name with no sensitivity to lowercase, uppercase and symbols.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
str, the key in params which is fuzzy-matched and retruned. |
required |
params |
Mapping
|
Mapping, the mapping containing param. |
required |
Returns:
Type | Description |
---|---|
Source code in src/super_gradients/training/utils/utils.py
272 273 274 275 276 277 278 279 280 |
|
get_image_size_from_path(img_path)
Get the image size of an image at a specific path
Source code in src/super_gradients/training/utils/utils.py
614 615 616 617 |
|
get_orientation_key()
cached
Get the orientation key according to PIL, which is useful to get the image size for instance
Returns:
Type | Description |
---|---|
int
|
Orientation key according to PIL |
Source code in src/super_gradients/training/utils/utils.py
580 581 582 583 584 585 586 |
|
get_param(params, name, default_val=None)
Retrieves a param from a parameter object/dict . If the parameter does not exist, will return default_val. In case the default_val is of type dictionary, and a value is found in the params - the function will return the default value dictionary with internal values overridden by the found value IMPORTANT: Not sensitive to lowercase, uppercase and symbols.
i.e. default_opt_params = {'lr':0.1, 'momentum':0.99, 'alpha':0.001} training_params = {'optimizer_params': {'lr':0.0001}, 'batch': 32 .... } get_param(training_params, name='OptimizerParams', default_val=default_opt_params) will return {'lr':0.0001, 'momentum':0.99, 'alpha':0.001}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
an object (typically HpmStruct) or a dict holding the params |
required | |
name |
name of the searched parameter |
required | |
default_val |
assumed to be the same type as the value searched in the params |
None
|
Returns:
Type | Description |
---|---|
the found value, or default if not found |
Source code in src/super_gradients/training/utils/utils.py
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 339 340 341 342 343 344 345 346 |
|
infer_model_device(model)
Get the device where the model's parameters are stored. This function returns device of the first parameter of the model, assuming there is no cross-device parameter movement inside the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
Model to get the device from. |
required |
Returns:
Type | Description |
---|---|
Optional[torch.device]
|
Device where the model's parameters are stored. The function may return None if the model has no parameters or buffers. |
Source code in src/super_gradients/training/utils/utils.py
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 |
|
infer_model_dtype(model)
Get the device where the model's parameters are stored. This function returns device of the first parameter of the model, assuming there is no cross-device parameter movement inside the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
Model to get the device from. |
required |
Returns:
Type | Description |
---|---|
Optional[torch.device]
|
Device where the model's parameters are stored. The function may return None if the model has no parameters or buffers. |
Source code in src/super_gradients/training/utils/utils.py
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
|
load_func(dotpath)
load function in module. function is right-most segment.
Used for passing functions (without calling them) in yaml files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dotpath |
str
|
path to module. |
required |
Returns:
Type | Description |
---|---|
a python function |
Source code in src/super_gradients/training/utils/utils.py
391 392 393 394 395 396 397 398 399 400 401 402 |
|
make_divisible(x, divisor, ceil=True)
Returns x evenly divisible by divisor. If ceil=True it will return the closest larger number to the original x, and ceil=False the closest smaller number.
Source code in src/super_gradients/training/utils/utils.py
554 555 556 557 558 559 560 561 562 |
|
move_state_dict_to_device(model_sd, device)
Moving model state dict tensors to target device (cuda or cpu)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_sd |
model state dict |
required | |
device |
either cuda or cpu |
required |
Source code in src/super_gradients/training/utils/utils.py
365 366 367 368 369 370 371 372 373 |
|
override_default_params_without_nones(params, default_params)
Helper method for overriding default dictionary's entries excluding entries with None values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params |
Dict
|
dict, output dictionary which will take the defaults. |
required |
default_params |
Mapping
|
dict, dictionary for the defaults. |
required |
Returns:
Type | Description |
---|---|
Dict
|
dict, params after manipulation, |
Source code in src/super_gradients/training/utils/utils.py
620 621 622 623 624 625 626 627 628 629 630 |
|
random_seed(is_ddp, device, seed)
Sets random seed of numpy, torch and random.
When using ddp a seed will be set for each process according to its local rank derived from the device number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_ddp |
bool, will set different random seed for each process when using ddp. |
required | |
device |
'cuda','cpu', 'cuda: |
required | |
seed |
int, random seed to be set |
required |
Source code in src/super_gradients/training/utils/utils.py
376 377 378 379 380 381 382 383 384 385 386 387 388 |
|
resolve_torch_device(device)
Resolve the specified torch device. It accepts either a string or a torch.device object.
This function takes the provided device identifier and returns a corresponding torch.device object, which represents the device where a torch.Tensor will be allocated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device |
Union[str, torch.device]
|
A string or torch.device object representing the device (e.g., 'cpu', 'cuda', 'cuda:0'). |
required |
Returns:
Type | Description |
---|---|
torch.device
|
A torch.device object representing the resolved device. Example: >>> torch.cuda.set_device(5) >>> str(resolve_torch_device("cuda")) 'cuda:5' |
Source code in src/super_gradients/training/utils/utils.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
|
safe_untar(tar_file, extract_path)
Protect against Tar Slip vulnerability. Calling extractall to extract all files from a tar file without sanitization may result files outside destination directory to be overwritten, resulting in an arbitrary file write. CVE-2007-4559 https://nvd.nist.gov/vuln/detail/CVE-2007-4559
Source code in src/super_gradients/training/utils/utils.py
512 513 514 515 516 517 518 519 520 521 522 523 |
|
tensor_container_to_device(obj, device, non_blocking=True, detach=False)
Recursively send compounded objects to device (sending all tensors to device and maintaining structure)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device |
str
|
device to send the tensors to |
required |
non_blocking |
used for DistributedDataParallel |
True
|
|
detach |
bool
|
detach the tensors from the graph |
False
|
Source code in src/super_gradients/training/utils/utils.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
unwrap_model(model)
Get the real model from a model wrapper (DataParallel, DistributedDataParallel)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Union[nn.Module, nn.DataParallel, DistributedDataParallel]
|
required |
Returns:
Type | Description |
---|---|
nn.Module
|
Source code in src/super_gradients/training/utils/utils.py
103 104 105 106 107 108 109 110 111 112 113 114 |
|
draw_label(image, label, confidence)
Draw a label and confidence on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
The image on which to draw the label and confidence, in RGB format, and Channel Last (H, W, C) |
required |
label |
str
|
The label to draw. |
required |
confidence |
float
|
The confidence of the label. |
required |
Source code in src/super_gradients/training/utils/visualization/classification.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
DepthVisualization
Source code in src/super_gradients/training/utils/visualization/depth_estimation.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
process_depth_map_for_visualization(depth_map, color_scheme=None, drop_extreme_percentage=0, inverse=False, ignored_val=None)
staticmethod
Process a depth map for visualization.
This method enhances the visual representation of a depth map by: 1. Clipping extreme values based on the specified percentage. 2. Normalizing the depth map to the 0-255 range. 3. Optionally inverting the depth map (inversion is done as 1 / depth). 4. Applying a color map using OpenCV's applyColorMap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
depth_map |
np.ndarray
|
Input depth map as a NumPy array. |
required |
color_scheme |
Optional[int]
|
OpenCV color scheme for the depth map visualization. If not specified: - If |
None
|
drop_extreme_percentage |
float
|
Percentage of extreme values to drop. |
0
|
inverse |
bool
|
Apply inversion (1 / depth) if True. |
False
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Processed colormap of the depth map for visualization. |
Source code in src/super_gradients/training/utils/visualization/depth_estimation.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
draw_bbox(image, title, color, box_thickness, x1, y1, x2, y2)
Draw a bounding box on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
Image on which to draw the bounding box. |
required |
color |
Tuple[int, int, int]
|
RGB values of the color of the bounding box. |
required |
title |
Optional[str]
|
Title to display inside the bounding box. |
required |
box_thickness |
Optional[int]
|
Thickness of the bounding box border. |
required |
x1 |
int
|
x-coordinate of the top-left corner of the bounding box. |
required |
y1 |
int
|
y-coordinate of the top-left corner of the bounding box. |
required |
x2 |
int
|
x-coordinate of the bottom-right corner of the bounding box. |
required |
y2 |
int
|
y-coordinate of the bottom-right corner of the bounding box. |
required |
Source code in src/super_gradients/training/utils/visualization/detection.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
get_recommended_box_thickness(x1, y1, x2, y2)
Get a nice box thickness for a given bounding box.
Source code in src/super_gradients/training/utils/visualization/detection.py
48 49 50 51 52 53 54 55 56 57 58 59 |
|
get_recommended_text_size(x1, y1, x2, y2)
Get a nice text size for a given bounding box.
Source code in src/super_gradients/training/utils/visualization/detection.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
LabelInfo
dataclass
Hold information about labels.
:attr name: Label name. :attr color: Color of the label. :attr text_size: Size of the label text.
Source code in src/super_gradients/training/utils/visualization/legend.py
14 15 16 17 18 19 20 21 22 23 24 25 |
|
Row
dataclass
Represent a row of labels.
Source code in src/super_gradients/training/utils/visualization/legend.py
28 29 30 31 32 33 |
|
add_to_row_or_create_new(rows, label, image_width)
Adds a label to a row or creates a new row if the current one is full.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rows |
List[Row]
|
Existing rows of labels. |
required |
label |
LabelInfo
|
Label to add. |
required |
image_width |
int
|
Width of the image. |
required |
Returns:
Type | Description |
---|---|
List[Row]
|
Updated rows of labels. |
Source code in src/super_gradients/training/utils/visualization/legend.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
draw_label_on_canvas(canvas, label, position, font_size)
Draws a label on the canvas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
canvas |
np.ndarray
|
The canvas to draw on. |
required |
label |
LabelInfo
|
The label to draw. |
required |
position |
Tuple[int, int]
|
Position to draw the label. |
required |
font_size |
int
|
Font size of the label. |
required |
Returns:
Type | Description |
---|---|
Tuple[np.ndarray, int]
|
The updated canvas and horizontal position for next label. |
Source code in src/super_gradients/training/utils/visualization/legend.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
draw_legend_on_canvas(image, class_color_tuples)
Draws a legend on the canvas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
The image to draw the legend on. |
required |
class_color_tuples |
Iterable[Tuple[str, Tuple[int, int, int]]]
|
Iterable of tuples containing class name and its color. |
required |
Returns:
Type | Description |
---|---|
np.ndarray
|
The canvas with the legend drawnOops, it seems like the response got cut off. |
Source code in src/super_gradients/training/utils/visualization/legend.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
get_label_info(name, color)
Creates a LabelInfo object for a given name and color.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Label name. |
required |
color |
Tuple[int, int, int]
|
Label color. |
required |
Returns:
Type | Description |
---|---|
LabelInfo
|
An object of LabelInfo. |
Source code in src/super_gradients/training/utils/visualization/legend.py
45 46 47 48 49 50 51 52 |
|
get_label_rows(labels, image_width)
Arranges labels in rows to fit into the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
List[LabelInfo]
|
List of labels. |
required |
image_width |
int
|
Width of the image. |
required |
Returns:
Type | Description |
---|---|
List[Row]
|
List of label rows. |
Source code in src/super_gradients/training/utils/visualization/legend.py
83 84 85 86 87 88 89 90 91 92 93 |
|
get_sorted_labels(class_color_tuples)
Sorts and creates LabelInfo for class-color tuples.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_color_tuples |
Sequence[Tuple[str, Tuple[int, int, int]]]
|
Tuples of class names and associated colors. |
required |
Returns:
Type | Description |
---|---|
List[LabelInfo]
|
A sorted list of LabelInfo objects. |
Source code in src/super_gradients/training/utils/visualization/legend.py
73 74 75 76 77 78 79 80 |
|
get_text_size(text)
Calculate the size of a given text using the CV2 getTextSize function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
Input text. |
required |
Returns:
Type | Description |
---|---|
Tuple[int, int]
|
A tuple of width and height of the text box. |
Source code in src/super_gradients/training/utils/visualization/legend.py
36 37 38 39 40 41 42 |
|
PoseVisualization
Source code in src/super_gradients/training/utils/visualization/pose_estimation.py
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 |
|
draw_poses(*, image, poses, boxes, scores, is_crowd, edge_links, edge_colors, keypoint_colors, show_keypoint_confidence=False, joint_thickness=None, box_thickness=None, keypoint_radius=None, keypoint_confidence_threshold=0.5)
classmethod
Draw multiple poses on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
Image on which to draw the poses. This image will not be modified, instead a new image will be returned. |
required |
poses |
np.ndarray
|
Predicted poses. Shape [Num Poses, Num Joints, 2] or [Num Poses, Num Joints, 3] if confidence scores are available. |
required |
boxes |
Optional[np.ndarray]
|
Optional bounding boxes for each pose. Shape [Num Poses, 4] in XYXY format. |
required |
scores |
Optional[np.ndarray]
|
Optional confidence scores for each pose. Shape [Num Poses] |
required |
is_crowd |
Optional[np.ndarray]
|
Optional array of booleans indicating whether each pose is crowd or not. Shape [Num Poses] |
required |
edge_links |
Union[np.ndarray, List[Tuple[int, int]]]
|
Array of [Num Links, 2] containing the links between joints to draw. |
required |
edge_colors |
Union[None, np.ndarray, List[Tuple[int, int, int]]]
|
Array of shape [Num Links, 3] or list of tuples containing the (r,g,b) colors for each joint link. |
required |
keypoint_colors |
Union[None, np.ndarray, List[Tuple[int, int, int]]]
|
Array of shape [Num Joints, 3] or list of tuples containing the (r,g,b) colors for each keypoint. |
required |
show_keypoint_confidence |
bool
|
Whether to show the confidence score for each keypoint individually. |
False
|
keypoint_confidence_threshold |
float
|
A minimal confidence score for individual keypoint to be drawn. |
0.5
|
joint_thickness |
Optional[int]
|
(Optional) Thickness of the joint links |
None
|
Returns:
Type | Description |
---|---|
A new image with the poses drawn on it. |
Source code in src/super_gradients/training/utils/visualization/pose_estimation.py
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 |
|
draw_skeleton(image, keypoints, score, edge_links, edge_colors, joint_thickness, keypoint_colors, keypoint_radius, show_confidence, box_thickness, keypoint_confidence_threshold=0.0, show_keypoint_confidence=False)
Draw a skeleton on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
Input image (will not be modified) |
required |
keypoints |
np.ndarray
|
Array of [Num Joints, 2] or [Num Joints, 3] containing the keypoints to draw. First two values are the x and y coordinates, the third (optional, not used) is the confidence score. |
required |
score |
float
|
Confidence score of the whole pose |
required |
edge_links |
Union[None, np.ndarray, List[Tuple[int, int]]]
|
Array of [Num Links, 2] containing the links between joints to draw. Can be None, in which case no links will be drawn. |
required |
edge_colors |
Union[None, np.ndarray, List[Tuple[int, int, int]]]
|
Array of shape [Num Links, 3] or list of tuples containing the (r,g,b) colors for each joint link. |
required |
joint_thickness |
int
|
(Optional) Thickness of the joint links |
required |
keypoint_colors |
Union[None, np.ndarray, List[Tuple[int, int, int]]]
|
Array of shape [Num Joints, 3] or list of tuples containing the (r,g,b) colors for each keypoint. |
required |
keypoint_radius |
int
|
(Optional) Radius of the keypoints (in pixels) |
required |
show_confidence |
bool
|
Whether to show the bounding box around the pose and confidence score on top of it. |
required |
box_thickness |
Optional[int]
|
(Optional) Thickness of bounding boxes. If None, will adapt to the box size. |
required |
keypoint_confidence_threshold |
float
|
If keypoints contains confidence scores (Shape is [Num Joints, 3]), this function will draw keypoints with confidence score > threshold. |
0.0
|
show_keypoint_confidence |
bool
|
Whether to show the confidence score for each keypoint individually. |
False
|
Returns:
Type | Description |
---|---|
A new image with the skeleton drawn on it |
Source code in src/super_gradients/training/utils/visualization/pose_estimation.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
overlay_segmentation(image, pred_mask, num_classes, alpha, colors=None, class_names=None)
Draw a bounding box on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
Image on which to draw the segmentation. |
required |
pred_mask |
torch.Tensor
|
Image on which to draw the segmentation. |
required |
num_classes |
int
|
Image on which to draw the segmentation. |
required |
alpha |
float
|
Float number between [0,1] denoting the transparency of the masks (0 means full transparency, 1 means opacity). |
required |
colors |
Optional[Union[List[Union[str, Tuple[int, int, int]]], str, Tuple[int, int, int]]]
|
List containing the colors of the masks or single color for all masks. By default, random colors are generated for each mask. |
None
|
class_names |
Optional[List[str]]
|
List containing the class names of cityscapes classes used for model training |
None
|
Source code in src/super_gradients/training/utils/visualization/segmentation.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
best_text_color(background_color)
Determine the best color for text to be visible on a given background color.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
background_color |
Tuple[int, int, int]
|
RGB values of the background color. |
required |
Returns:
Type | Description |
---|---|
Tuple[int, int, int]
|
RGB values of the best text color for the given background color. |
Source code in src/super_gradients/training/utils/visualization/utils.py
38 39 40 41 42 43 44 45 46 47 48 49 |
|
compute_brightness(color)
Computes the brightness of a given color in RGB format. From https://alienryderflex.com/hsp.html
Parameters:
Name | Type | Description | Default |
---|---|---|---|
color |
Tuple[int, int, int]
|
A tuple of three integers representing the RGB values of the color. |
required |
Returns:
Type | Description |
---|---|
float
|
The brightness of the color. |
Source code in src/super_gradients/training/utils/visualization/utils.py
52 53 54 55 56 57 58 |
|
draw_text_box(image, text, x, y, font, font_size, background_color, thickness=1)
Draw a text inside a box
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
np.ndarray
|
The image on which to draw the text box. |
required |
text |
str
|
The text to display in the text box. |
required |
x |
int
|
The x-coordinate of the top-left corner of the text box. |
required |
y |
int
|
The y-coordinate of the top-left corner of the text box. |
required |
font |
int
|
The font to use for the text. |
required |
font_size |
float
|
The size of the font to use. |
required |
background_color |
Tuple[int, int, int]
|
The color of the text box and text as a tuple of three integers representing RGB values. |
required |
thickness |
int
|
The thickness of the text. |
1
|
Returns:
Type | Description |
---|---|
np.ndarray
|
Image with the text inside the box. |
Source code in src/super_gradients/training/utils/visualization/utils.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
generate_color_mapping(num_classes)
Generate a unique BGR color for each class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_classes |
int
|
The number of classes in the dataset. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[int, ...]]
|
List of RGB colors for each class. |
Source code in src/super_gradients/training/utils/visualization/utils.py
61 62 63 64 65 66 67 68 69 |
|
ModelWeightAveraging
Utils class for managing the averaging of the best several snapshots into a single model. A snapshot dictionary file and the average model will be saved / updated at every epoch and evaluated only when training is completed. The snapshot file will only be deleted upon completing the training. The snapshot dict will be managed on cpu.
Source code in src/super_gradients/training/utils/weight_averaging_utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 |
|
__init__(ckpt_dir, greater_is_better, metric_to_watch, load_checkpoint=False, number_of_models_to_average=10)
Init the ModelWeightAveraging
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ckpt_dir |
str
|
The directory where the checkpoints are saved |
required |
metric_to_watch |
str
|
Monitoring loss or acc, will be identical to that which determines best_model |
required |
load_checkpoint |
bool
|
Whether to load pre-existing snapshot dict. |
False
|
number_of_models_to_average |
int
|
Number of models to average |
10
|
Source code in src/super_gradients/training/utils/weight_averaging_utils.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
cleanup()
Delete snapshot file when reaching the last epoch
Source code in src/super_gradients/training/utils/weight_averaging_utils.py
99 100 101 102 103 |
|
get_average_model(model, validation_results_dict=None)
Returns the averaged model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
will be used to determine arch |
required | |
validation_results_dict |
if provided, will update the average model before returning |
None
|
|
target_device |
if provided, return sd on target device |
required |
Source code in src/super_gradients/training/utils/weight_averaging_utils.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
update_snapshots_dict(model, validation_results_dict)
Update the snapshot dict and returns the updated average model for saving
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
the latest model |
required |
validation_results_dict |
Mapping[str, float]
|
performance of the latest model |
required |
Source code in src/super_gradients/training/utils/weight_averaging_utils.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|