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. |
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_pafs |
Render PAF magnitude visualization. |
Source code in sleap_nn/training/utils.py
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_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. |
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
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 | |
__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
|
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
|
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
xavier_init_weights(x)
¶
Function to initilaise the model weights with Xavier initialization method.