Skip to content

CELoss

Bases: WeightedBaseLoss

apply_spectral_decouple

apply_spectral_decouple(loss_matrix: Tensor, yhat: Tensor, lam: float = 0.01) -> torch.Tensor

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_ls_to_target(target: Tensor, n_classes: int, label_smoothing: float = 0.1) -> torch.Tensor

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

apply_class_weights(loss_matrix: Tensor, target: Tensor) -> torch.Tensor

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_edge_weights(loss_matrix: Tensor, weight_map: Tensor) -> torch.Tensor

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_mask_weight(loss_matrix: Tensor, mask: Tensor, norm: bool = True) -> torch.Tensor

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).

extra_repr

extra_repr() -> str

Add info to print.

__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

Cross-Entropy loss with weighting.

Parameters:

Name Type Description Default
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 cross entropy 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 CE loss (scalar).