loss_fn package

Subpackages

Submodules

loss_fn.base_criteria module

class loss_fn.base_criteria.BaseCriteria(opts: Namespace, *args, **kwargs)[source]

Bases: Module, ABC

Base class for defining loss functions. Sub-classes must implement compute_loss 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.

abstract forward(input_sample: Any, prediction: Any, target: Any, *args, **kwargs) Any[source]

Compute the loss.

Parameters:
  • input_sample – Input to the model.

  • prediction – Model’s output

  • target – Ground truth labels

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.

loss_fn.composite_loss module

class loss_fn.composite_loss.CompositeLoss(opts: Namespace, *args, **kwargs)[source]

Bases: BaseCriteria

Combines different loss functions and returns the weighted sum of these losses. loss_category and loss_weight are two mandatory keys that allows us to combine different losses and compute their weighted sum. The loss_category specifies the category of a loss function and is a string (e.g., classification). The loss_weight specifies the contribution of a loss function and is a float value (e.g., 1.0). The sum of `loss_weight`s corresponding to different loss functions is not required to be 1.

Parameters:

opts – command-line arguments

Example:: # Example yaml config for combining classification and neural_augmentation loss function is given below. # Please note that configuration for each loss function should start with - in composite_loss.

loss:

category: “composite_loss” composite_loss:

  • loss_category: “classification” loss_weight: 1.0 classification:

    name: “cross_entropy” cross_entropy:

    label_smoothing: 0.1

  • loss_category: “neural_augmentation” loss_weight: 1.0 neural_augmentation:

    perceptual_metric: “psnr” target_value: [ 40, 10 ] curriculum_method: “cosine”

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

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

classmethod build_composite_loss_fn(opts: Namespace, *args, **kwargs) Tuple[Mapping[str, BaseCriteria], Mapping[str, float]][source]

Build loss functions from command line arguments and loss registry

Parameters:

opts – command-line arguments

Returns:

A tuple of two dictionaries. The first dictionary, task_loss_fn_mapping, contains information about loss function category and module. The second dictionary, task_loss_wts_mapping contains the information about loss function category and weight.

classmethod add_arguments(parser: ArgumentParser) ArgumentParser[source]

Add criterion-specific arguments to the parser.

forward(input_sample: Any, prediction: Any, target: Any, *args, **kwargs) Any[source]

Compute the weighted sum of different loss functions.

Parameters:
  • input_sample – Input to the model.

  • prediction – Model’s output

  • target – Ground truth labels

Returns:

scalar loss value) with total_loss as a mandatory key. The other keys corresponds to loss category names and their values contain category-specific scalar loss values. total_loss is weighted sum of these category-specific losses.

Return type:

A mapping of the form (str

train(mode: bool = True) None[source]

Sets the loss functions in training mode.

eval() None[source]

Sets the loss functions in evaluation mode.

loss_fn.neural_augmentation module

class loss_fn.neural_augmentation.NeuralAugmentation(opts: Namespace, *args, **kwargs)[source]

Bases: BaseCriteria

Compute the augmentation loss, as described in the RangeAugment paper.

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: Tensor | Mapping[str, Tensor | List[Tensor]], prediction: Mapping[str, Tensor], *args, **kwargs) Tensor[source]

Compute the loss between input and augmented image, as described in RangeAugment paper.

Parameters:
  • input_sample – Input sample can either be a Tensor or a dictionary with mandatory key “image”. In case of a dictionary, the values can be a Tensor or list of Tensors.

  • prediction – Output of augmentation model. Mapping of (string: Tensor) with augmented_tensor as the required key.

Shapes:
input_sample:
  • Tensor: The shape of input tensor is [N, C, H, W]

  • Mapping[str, Tensor]: The shape of tensor is [N, C, H, W]

  • Mapping[str, List[Tensor]]: The length of List is N, and the shape of each tensor is [1, C, H, W]

prediction: The shape of prediction[“augmented_tensor”] is [N, C, H, W]

Returns:

A scalar loss value

…note:

During validation or evaluation, neural augmentation loss is not computed and 0 is returned

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.

loss_fn.neural_augmentation.linear_curriculum(start: int, end: int, period: int) Tensor[source]

This function implements linear curriculum

Parameters:
  • start – the starting value for the set of points

  • end – the ending value for the set of points

  • period – size of the constructed tensor

Returns:

A float tensor of length period

loss_fn.neural_augmentation.cosine_curriculum(start: int, end: int, period: int) Tensor[source]

This function implements cosine curriculum :param start: the starting value for the set of points :param end: the ending value for the set of points :param period: size of the constructed tensor

Returns:

A float tensor of length period

Module contents

loss_fn.build_loss_fn(opts: Namespace, category: str | None = '', *args, **kwargs) BaseCriteria[source]

Helper function to build loss function from command-line arguments.

Parameters:
  • opts – command-line arguments

  • category – Optional task category (e.g., classification). Specifying category may be useful for building composite loss functions. See loss_fns.composite_loss.CompositeLoss.build_composite_loss_fn function for an example

Returns:

Loss function module

loss_fn.add_loss_fn_arguments(parser: ArgumentParser) ArgumentParser[source]

This method gets a parser object, and for every loss that is registered in the LOSS_REGISTRY adds its arguments to it.