utils
sleap_nn.training.utils
¶
Miscellaneous utility functions for training.
Classes:
| Name | Description |
|---|---|
MatplotlibRenderer |
Renders VisualizationData to matplotlib figures. |
VisualizationData |
Container for visualization data from a single sample. |
WandBRenderer |
Renders VisualizationData to wandb.Image objects. |
Functions:
| Name | Description |
|---|---|
get_dist_rank |
Return the rank of the current process if torch.distributed is initialized. |
get_gpu_memory |
Get the available memory on each GPU. |
imgfig |
Create a tight figure for image plotting. |
is_distributed_initialized |
Check if distributed processes are initialized. |
plot_confmaps |
Plot confidence maps reduced over channels. |
plot_img |
Plot an image in a tight figure. |
plot_peaks |
Plot ground truth and detected peaks. |
plot_tile_grid |
Visualize the sliding-window tile grid (and blend coverage) over a frame. |
xavier_init_weights |
Function to initilaise the model weights with Xavier initialization method. |
MatplotlibRenderer
¶
Renders VisualizationData to matplotlib figures.
Methods:
| Name | Description |
|---|---|
render |
Render visualization data to a matplotlib figure. |
render_gt_mask |
Render the predicted foreground vs the ground-truth mask overlay. |
render_instance_masks |
Render grouped predicted per-instance masks as a colored overlay. |
render_offsets |
Render the center-offset field DIRECTION for segmentation models. |
render_pafs |
Render PAF magnitude visualization. |
Source code in sleap_nn/training/utils.py
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 | |
render(data)
¶
Render visualization data to a matplotlib figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
VisualizationData
|
VisualizationData containing image, confmaps, peaks, etc. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib Figure object. |
Source code in sleap_nn/training/utils.py
render_gt_mask(data)
¶
Render the predicted foreground vs the ground-truth mask overlay.
Draws the image, overlays the predicted foreground probability (as for the standard confmap render), then outlines the ground-truth foreground mask so the prediction can be compared against truth each epoch (the segmentation analog of pose's GT-keypoint overlay).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
VisualizationData
|
VisualizationData with |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib Figure object. |
Source code in sleap_nn/training/utils.py
render_instance_masks(data)
¶
Render grouped predicted per-instance masks as a colored overlay.
Each predicted instance's mask (from the offset-grouping that already runs
in get_visualization_data) is drawn in a distinct color on the input
image, so instance separation — the model's actual output, and the single
most informative seg diagnostic — is visible each epoch. When a GT
foreground mask is present (gt_mask), its silhouette is outlined for
reference. Empty instance lists render the plain image (no instances
detected this epoch).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
VisualizationData
|
VisualizationData with |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib Figure object. |
Source code in sleap_nn/training/utils.py
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 | |
render_offsets(data)
¶
Render the center-offset field DIRECTION for segmentation models.
Colors each pixel by the ANGLE of its center-offset vector (hue =
atan2(dy, dx), the standard optical-flow color wheel), so the field
reads as "which way does each foreground pixel point to its instance
center." Opacity is modulated by the offset magnitude so near-zero
(background) offsets stay transparent and only meaningful vectors show
their direction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
VisualizationData
|
VisualizationData with |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib Figure object showing the per-pixel offset direction. |
Source code in sleap_nn/training/utils.py
render_pafs(data)
¶
Render PAF magnitude visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
VisualizationData
|
VisualizationData with pred_pafs populated. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib Figure object showing PAF magnitudes. |
Source code in sleap_nn/training/utils.py
VisualizationData
dataclass
¶
Container for visualization data from a single sample.
This dataclass decouples data extraction from rendering, allowing the same data to be rendered to different output targets (matplotlib, wandb, etc.).
Attributes:
| Name | Type | Description |
|---|---|---|
image |
ndarray
|
Input image as (H, W, C) numpy array, normalized to [0, 1]. |
pred_confmaps |
ndarray
|
Predicted confidence maps as (H, W, nodes) array, values in [0, 1]. |
pred_peaks |
ndarray
|
Predicted keypoints as (instances, nodes, 2) or (nodes, 2) array. |
pred_peak_values |
ndarray
|
Confidence values as (instances, nodes) or (nodes,) array. |
gt_instances |
ndarray
|
Ground truth keypoints, same shape as pred_peaks. |
node_names |
List[str]
|
List of node/keypoint names, e.g., ["head", "thorax", ...]. |
output_scale |
float
|
Ratio of confmap size to image size (confmap_h / image_h). |
is_paired |
bool
|
Whether GT and predictions can be paired for error visualization. |
pred_pafs |
Optional[ndarray]
|
Part affinity fields for bottom-up models, optional. |
pred_class_maps |
Optional[ndarray]
|
Class maps for multi-class models, optional. |
pred_center_heatmap |
Optional[ndarray]
|
Center heatmap for segmentation models, optional. |
pred_offsets |
Optional[ndarray]
|
Center-offset field (H, W, 2) for segmentation models, optional. Rendered as a per-pixel offset-magnitude map. |
gt_mask |
Optional[ndarray]
|
Ground-truth foreground mask (H, W) or (H, W, 1) for segmentation models, optional. Rendered as a translucent overlay so the predicted foreground probability can be compared against truth each epoch. |
instance_masks |
Optional[List[ndarray]]
|
Grouped predicted per-instance masks for bottom-up
segmentation, as a list of |
Source code in sleap_nn/training/utils.py
WandBRenderer
¶
Renders VisualizationData to wandb.Image objects.
Supports multiple rendering modes: - "direct": Pre-render with matplotlib, convert to wandb.Image - "boxes": Use wandb boxes for interactive keypoint visualization - "masks": Use wandb masks for confidence map overlay
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the renderer. |
render |
Render visualization data to a wandb.Image. |
Source code in sleap_nn/training/utils.py
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 | |
__init__(mode='direct', box_size=5.0, confmap_threshold=0.1, min_size=512)
¶
Initialize the renderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Rendering mode - "direct", "boxes", or "masks". |
'direct'
|
box_size
|
float
|
Size of keypoint boxes in pixels (for "boxes" mode). |
5.0
|
confmap_threshold
|
float
|
Threshold for confmap mask (for "masks" mode). |
0.1
|
min_size
|
int
|
Minimum image dimension. Smaller images will be upscaled. |
512
|
Source code in sleap_nn/training/utils.py
render(data, caption=None)
¶
Render visualization data to a wandb.Image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
VisualizationData
|
VisualizationData containing image, confmaps, peaks, etc. |
required |
caption
|
Optional[str]
|
Optional caption for the image. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
A wandb.Image object. |
Source code in sleap_nn/training/utils.py
get_dist_rank()
¶
get_gpu_memory()
¶
Get the available memory on each GPU.
Returns:
| Type | Description |
|---|---|
List[int]
|
A list of the available memory on each GPU in MiB. |
Source code in sleap_nn/training/utils.py
imgfig(size=6, dpi=72, scale=1.0)
¶
Create a tight figure for image plotting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
float | tuple
|
Scalar or 2-tuple specifying the (width, height) of the figure in inches. If scalar, will assume equal width and height. |
6
|
dpi
|
int | float
|
Dots per inch, controlling the resolution of the image. |
72
|
scale
|
float
|
Factor to scale the size of the figure by. This is a convenience for increasing the size of the plot at the same DPI. |
1.0
|
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib.figure.Figure to use for plotting. |
Source code in sleap_nn/training/utils.py
is_distributed_initialized()
¶
plot_confmaps(confmaps, output_scale=1.0)
¶
Plot confidence maps reduced over channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
confmaps
|
ndarray
|
Confidence maps to plot with shape (height, width, channel). |
required |
output_scale
|
float
|
Factor to scale the size of the figure by. |
1.0
|
Returns:
| Type | Description |
|---|---|
|
A matplotlib.figure.Figure to use for plotting. |
Source code in sleap_nn/training/utils.py
plot_img(img, dpi=72, scale=1.0)
¶
Plot an image in a tight figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Image to plot of shape (height, width, channel). |
required |
dpi
|
int | float
|
Dots per inch, controlling the resolution of the image. |
72
|
scale
|
float
|
Factor to scale the size of the figure by. This is a convenience for increasing the size of the plot at the same DPI. |
1.0
|
Returns:
| Type | Description |
|---|---|
Figure
|
A matplotlib.figure.Figure to use for plotting. |
Source code in sleap_nn/training/utils.py
plot_peaks(pts_gt, pts_pr=None, paired=False)
¶
Plot ground truth and detected peaks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pts_gt
|
ndarray
|
Ground-truth keypoints of shape (num_instances, nodes, 2). To plot centroids, shape: (1, num_instances, 2). |
required |
pts_pr
|
ndarray | None
|
Predicted keypoints of shape (num_instances, nodes, 2). To plot centroids, shape: (1, num_instances, 2) |
None
|
paired
|
bool
|
True if error lines should be plotted else False. |
False
|
Returns:
| Type | Description |
|---|---|
|
A matplotlib.figure.Figure to use for plotting. |
Source code in sleap_nn/training/utils.py
plot_tile_grid(img, tile_size, overlap, output_stride, max_stride=1, min_overlap_fraction=0.25, blend='gaussian', sigma_scale=0.125, show_coverage=True, dpi=72, scale=1.0)
¶
Visualize the sliding-window tile grid (and blend coverage) over a frame.
Draws each tile's extent as a rectangle over the image and, optionally,
overlays the summed importance-window coverage (the CNT denominator used
by the tiled-inference stitcher) as a heatmap so seams and edge
down-weighting are visible. Useful for choosing tile_size / overlap
and for documentation figures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
ndarray
|
Frame image; accepts |
required |
tile_size
|
int
|
Square tile side length in pixels. |
required |
overlap
|
int
|
Tile overlap in pixels. |
required |
output_stride
|
int
|
Model output stride (grid snapping + coverage resolution). |
required |
max_stride
|
int
|
Backbone max stride (used to snap the inter-tile step). |
1
|
min_overlap_fraction
|
float
|
Minimum overlap as a fraction of |
0.25
|
blend
|
str
|
Importance-window mode, one of |
'gaussian'
|
sigma_scale
|
float
|
Gaussian importance-window sigma scale. |
0.125
|
show_coverage
|
bool
|
If |
True
|
dpi
|
int | float
|
Dots per inch passed to :func: |
72
|
scale
|
float
|
Figure size scale passed to :func: |
1.0
|
Returns:
| Type | Description |
|---|---|
Figure
|
A |
Source code in sleap_nn/training/utils.py
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 | |
xavier_init_weights(x)
¶
Function to initilaise the model weights with Xavier initialization method.