mednet.models.segment.losses

Specialized losses for semanatic segmentation.

Classes

BCEWithLogitsLossWeightedPerBatch()

Calculates the binary cross entropy loss for every batch.

MultiLayerBCELogitsLossWeightedPerBatch()

Weighted Binary Cross-Entropy Loss for multi-layered inputs.

MultiLayerSoftJaccardAndBCELogitsLoss([alpha])

Implement Equation 3 in [IGLOVIKOV-2018] for the multi-output networks.

SoftJaccardAndBCEWithLogitsLoss([alpha])

Implement the generalized loss function of Equation (3) at [IGLOVIKOV-2018].

class mednet.models.segment.losses.BCEWithLogitsLossWeightedPerBatch[source]

Bases: Module

Calculates the binary cross entropy loss for every batch.

This loss is similar to torch.nn.BCEWithLogitsLoss, except it updates the pos_weight (ratio between negative and positive target pixels) parameter for the loss term for every batch, based on the accumulated taget pixels for all samples in the batch.

Implements Equation 1 in [MANINIS-2016]. The weight depends on the current proportion between negatives and positives in the ground- truth sample being analyzed.

forward(input_, target)[source]

Forward pass.

Parameters:
  • input – Logits produced by the model to be evaluated, with the shape [n, c, h, w].

  • target (Tensor) – Ground-truth information with the shape [n, c, h, w], containing zeroes and ones.

Return type:

Tensor

Returns:

The average loss for all input data.

class mednet.models.segment.losses.SoftJaccardAndBCEWithLogitsLoss(alpha=0.7)[source]

Bases: Module

Implement the generalized loss function of Equation (3) at [IGLOVIKOV-2018].

At the paper, authors suggest a value of \(\alpha = 0.7\), which we set as default for instances of this type.

\[L = \alpha H + (1-\alpha)(1-J)\]

J is the Jaccard distance, and H, the Binary Cross-Entropy Loss. Our implementation is based on torch.nn.BCEWithLogitsLoss.

Parameters:

alpha (float) – Determines the weighting of J and H. Default: 0.7.

forward(input_, target)[source]

Forward pass.

Parameters:
  • input – Logits produced by the model to be evaluated, with the shape [n, c, h, w].

  • target (Tensor) – Ground-truth information with the shape [n, c, h, w], containing zeroes and ones.

Return type:

Tensor

Returns:

Loss, in a single entry.

class mednet.models.segment.losses.MultiLayerBCELogitsLossWeightedPerBatch[source]

Bases: BCEWithLogitsLossWeightedPerBatch

Weighted Binary Cross-Entropy Loss for multi-layered inputs.

This loss can be used in networks that produce more than one output that has to match output targets. For example, architectures such as as hed.HED or lwnet.LittleWNet require this feature.

It follows the inherited super class applying on-the-fly pos_weight updates per batch.

forward(input_, target)[source]

Forward pass.

Parameters:
  • input – Value produced by the model to be evaluated, with the shape [L, n, c, h, w].

  • target (Tensor) – Ground-truth information with the shape [n, c, h, w].

Return type:

Tensor

Returns:

The average loss for all input data.

class mednet.models.segment.losses.MultiLayerSoftJaccardAndBCELogitsLoss(alpha=0.7)[source]

Bases: SoftJaccardAndBCEWithLogitsLoss

Implement Equation 3 in [IGLOVIKOV-2018] for the multi-output networks.

This loss can be used in networks that produce more than one output that has to match output targets. For example, architectures such as as hed.HED or lwnet.LittleWNet require this feature.

Parameters:

alpha (float) – Determines the weighting of SoftJaccard and BCE. Default: 0.7.

forward(input_, target)[source]

Forward pass.

Parameters:
  • input – Value produced by the model to be evaluated, with the shape [L, n, c, h, w].

  • target (Tensor) – Ground-truth information with the shape [n, c, h, w].

Return type:

Tensor

Returns:

The average loss for all input data.