losses
sleap_nn.training.losses
¶
Custom loss functions.
Functions:
| Name | Description |
|---|---|
compute_bce_dice_loss |
Compute combined Binary Cross-Entropy and Dice loss for segmentation. |
compute_masked_smooth_l1 |
Compute smooth L1 loss only on masked (foreground) pixels. |
compute_ohkm_loss |
Compute the online hard keypoint mining loss. |
compute_bce_dice_loss(y_pred, y_gt, bce_weight=0.5, dice_weight=0.5, smooth=1.0, pos_weight=None)
¶
Compute combined Binary Cross-Entropy and Dice loss for segmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
Tensor
|
Predicted logits (before sigmoid). Shape: (B, 1, H, W). |
required |
y_gt
|
Tensor
|
Ground truth binary mask. Shape: (B, 1, H, W). |
required |
bce_weight
|
float
|
Weight for the BCE component. |
0.5
|
dice_weight
|
float
|
Weight for the Dice component. |
0.5
|
smooth
|
float
|
Smoothing factor for Dice loss to avoid division by zero. |
1.0
|
pos_weight
|
Optional[float]
|
Optional positive-class weight for the BCE term (passed to
|
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor. |
Source code in sleap_nn/training/losses.py
compute_masked_smooth_l1(y_pred, y_gt, mask)
¶
Compute smooth L1 loss only on masked (foreground) pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
Tensor
|
Predicted offset field. Shape: (B, 2, H, W). |
required |
y_gt
|
Tensor
|
Ground truth offset field. Shape: (B, 2, H, W). |
required |
mask
|
Tensor
|
Binary mask indicating valid pixels. Shape: (B, 1, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar loss tensor. Returns 0 if no foreground pixels. |
Source code in sleap_nn/training/losses.py
compute_ohkm_loss(y_gt, y_pr, hard_to_easy_ratio=2.0, min_hard_keypoints=2, max_hard_keypoints=None, loss_scale=5.0)
¶
Compute the online hard keypoint mining loss.