DiceLoss
Bases: WeightedBaseLoss
apply_spectral_decouple ¶
Apply spectral decoupling L2 norm after the loss.
https://arxiv.org/abs/2011.09468
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_matrix
|
Tensor
|
Pixelwise losses. A tensor of shape (B, H, W). |
required |
yhat
|
Tensor
|
The pixel predictions of the model. Shape (B, C, H, W). |
required |
lam
|
float, default=0.01
|
Lambda constant. |
0.01
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: SD-regularized loss matrix. Same shape as input. |
apply_ls_to_target ¶
Apply regular label smoothing to the target map.
https://arxiv.org/abs/1512.00567
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target
|
Tensor
|
The target one hot tensor. Shape (B, C, H, W). Dtype: Int64. |
required |
n_classes
|
int
|
Number of classes in the data. |
required |
label_smoothing
|
float, default=0.1
|
The smoothing coeff alpha. |
0.1
|
Retrurns
Torch.Tensor: Label smoothed target. Same shape as input.
apply_svls_to_target ¶
apply_svls_to_target(target: Tensor, n_classes: int, kernel_size: int = 5, sigma: int = 3, **kwargs) -> torch.Tensor
Apply spatially varying label smoothihng to target map.
https://arxiv.org/abs/2104.05788
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target
|
Tensor
|
The target one hot tensor. Shape (B, C, H, W). Dtype: Int64. |
required |
n_classes
|
int
|
Number of classes in the data. |
required |
kernel_size
|
int, default=3
|
Size of a square kernel. |
5
|
sigma
|
int, default=3
|
The std of the gaussian. |
3
|
Retrurns
Torch.Tensor: Label smoothed target. Same shape as input.
apply_class_weights ¶
Multiply pixelwise loss matrix by the class weights.
Note
Does not apply normalization
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_matrix
|
Tensor
|
Pixelwise losses. A tensor of shape (B, H, W). |
required |
target
|
Tensor
|
The target mask. Shape (B, H, W). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The loss matrix scaled with the weight matrix. Shape (B, H, W). |
apply_edge_weights ¶
Apply weights to the object boundaries.
Basically just computes edge_weight
**weight_map
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_matrix
|
Tensor
|
Pixelwise losses. A tensor of shape (B, H, W). |
required |
weight_map
|
Tensor
|
Map that points to the pixels that will be weighted. Shape (B, H, W). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The loss matrix scaled with the nuclear boundary weights. Shape (B, H, W). |
apply_mask_weight ¶
Apply a mask to the loss matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_matrix
|
Tensor
|
Pixelwise losses. A tensor of shape (B, H, W). |
required |
mask
|
Tensor
|
The mask. Shape (B, H, W). |
required |
norm
|
bool, default=True
|
If True, the loss matrix will be normalized by the mean of the mask. |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: The loss matrix scaled with the mask. Shape (B, H, W). |
__init__ ¶
__init__(apply_sd: bool = False, apply_ls: bool = False, apply_svls: bool = False, apply_mask: bool = False, edge_weight: float = None, class_weights: Tensor = None, **kwargs) -> None
Sørensen-Dice Coefficient Loss.
Optionally applies weights at the object edges and classes.
Parameters¶
apply_sd : bool, default=False If True, Spectral decoupling regularization will be applied to the loss matrix. apply_ls : bool, default=False If True, Label smoothing will be applied to the target. apply_svls : bool, default=False If True, spatially varying label smoothing will be applied to the target apply_mask : bool, default=False If True, a mask will be applied to the loss matrix. Mask shape: (B, H, W) edge_weight : float, default=none Weight that is added to object borders. class_weights : torch.Tensor, default=None Class weights. A tensor of shape (n_classes,).
forward ¶
forward(yhat: Tensor, target: Tensor, target_weight: Tensor = None, mask: Tensor = None, **kwargs) -> torch.Tensor
Compute the DICE coefficient.
Parameters yhat (torch.Tensor): The prediction map. Shape (B, C, H, W). target (torch.Tensor): the ground truth annotations. Shape (B, H, W). target_weight (torch.Tensor, default=None): The edge weight map. Shape (B, H, W). mask (torch.Tensor, default=None): The mask map. Shape (B, H, W).
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Computed DICE loss (scalar). |