loss_fn.segmentation package

Submodules

loss_fn.segmentation.base_segmentation_criteria module

class loss_fn.segmentation.base_segmentation_criteria.BaseSegmentationCriteria(opts: Namespace, *args, **kwargs)[source]

Bases: BaseCriteria

Base class for defining segmentation loss functions. Sub-classes must implement forward function.

Parameters:

opts – command line arguments

__init__(opts: Namespace, *args, **kwargs) None[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

classmethod add_arguments(parser: ArgumentParser) ArgumentParser[source]

Add criterion-specific arguments to the parser.

loss_fn.segmentation.cross_entropy module

class loss_fn.segmentation.cross_entropy.SegCrossEntropy(opts: Namespace, *args, **kwargs)[source]

Bases: BaseSegmentationCriteria

Cross entropy loss for the task of semantic segmentation.

Parameters:

opts – command-line arguments

__init__(opts: Namespace, *args, **kwargs) None[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

classmethod add_arguments(parser: ArgumentParser) ArgumentParser[source]

Add criterion-specific arguments to the parser.

forward(input_sample: Any, prediction: Mapping[str, Tensor | Tuple[Tensor, Tensor]] | Tensor | Tuple[Tensor, Tensor], target: Tensor, *args, **kwargs) Mapping[str, Tensor][source]

Compute CE segmentation loss

Parameters:
  • input_sample – Input image tensor to model.

  • prediction – Output of model. It can be a * Tensor * Tuple[Tensor, Tensor] * Mapping[segmentation_output, Tensor] * Mapping[segmentation_output, Tuple[Tensor, Tensor]], where segmentation_output is a required key.

  • target – Target label tensor containing values in the range [0, C), where \(C\) is the number of classes

Shapes:

input_sample: This loss function does not care about this argument. prediction:

  • When prediction is a Tensor, then shape is [Batch size, C, Height, Width]

  • When prediction is a Tuple[Tensor, Tensor], then shape of one tensor is [Batch size, C, Height, Width]

    while the other is [Batch size, C, Height / O, Width/ O] where O is the output stride of feature map (typically 4).

  • When prediction is a dictionary, then the shape of prediction[“segmentation_output”] should

    be the same as described in above steps (depending on type).

target: The shape of target tensor is [Batch size, Height, Width]

Returns:

scalar value) is returned with total_loss as mandatory and (seg_loss, aux_loss) as optional keys. total_loss is weighted sum of seg_loss and aux_loss (when applicable).

Return type:

Mapping of the form (string

extra_repr() str[source]

Set the extra representation of the module

To print customized extra information, you should re-implement this method in your own modules. Both single-line and multi-line strings are acceptable.

Module contents