TverskyLoss
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__(alpha: float = 0.7, beta: float = 0.3, 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
Tversky loss.
https://arxiv.org/abs/1706.05721
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
float, default=0.7
|
False positive dice coefficient. |
0.7
|
beta
|
float, default=0.3
|
False negative tanimoto coefficient. |
0.3
|
apply_sd
|
bool, default=False
|
If True, Spectral decoupling regularization will be applied to the loss matrix. |
False
|
apply_ls
|
bool, default=False
|
If True, Label smoothing will be applied to the target. |
False
|
apply_svls
|
bool, default=False
|
If True, spatially varying label smoothing will be applied to the target |
False
|
apply_mask
|
bool, default=False
|
If True, a mask will be applied to the loss matrix. Mask shape: (B, H, W) |
False
|
edge_weight
|
float, default=none
|
Weight that is added to object borders. |
None
|
class_weights
|
torch.Tensor, default=None
|
Class weights. A tensor of shape (n_classes,). |
None
|
forward ¶
forward(yhat: Tensor, target: Tensor, target_weight: Tensor = None, mask: Tensor = None, **kwargs) -> torch.Tensor
Compute the Tversky loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
yhat
|
Tensor
|
The prediction map. Shape (B, C, H, W). |
required |
target
|
Tensor
|
the ground truth annotations. Shape (B, H, W). |
required |
target_weight
|
torch.Tensor, default=None
|
The edge weight map. Shape (B, H, W). |
None
|
mask
|
torch.Tensor, default=None
|
The mask map. Shape (B, H, W). |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: Computed Tversky loss (scalar). |