vis4d.op.loss

This module contains commonly used loss functions.

The losses do not follow a common API, but have a reducer as attribute, which is a function to aggregate loss values into a single tensor value.

class EmbeddingDistanceLoss(reducer=<function identity_loss>, neg_pos_ub=3.0, pos_margin=0.0, neg_margin=0.3, hard_mining=True)[source]

Embedding distance loss for learning appearance similarity.

Computes the difference between the target distances and the predicted distances of two sets of embedding vectors. Uses hard negative mining based on the loss values to select pairs for overall loss computation.

Creates an instance of the class.

forward(pred, target, weight=None)[source]

Forward function.

Parameters:
  • pred (torch.Tensor) – The predicted distances between two sets of predictions. Shape [N, M].

  • target (torch.Tensor) – The corresponding target distances. Either zero (different identity) or one (same identity).

  • weight (torch.Tensor, optional) – The weight of loss for each prediction. Defaults to None.

Returns:

embedding distance loss.

Return type:

loss_bbox (torch.Tensor)

update_weight(pred, target, weight)[source]

Update element-wise loss weights.

Exclude negatives according to maximum fraction of samples and/or hard negative mining.

Return type:

tuple[Tensor, Tensor, Tensor]

class IoULoss(reducer=<function identity_loss>, mode='log', eps=1e-06)[source]

IoU loss.

Computing the IoU loss between a set of predicted bboxes and target bboxes. The loss is calculated depending on the mode:

  • linear: 1 - IoU

  • square: 1 - IoU^2

  • log: -log(IoU)

Parameters:
  • reducer (LossReducer) – Reducer to reduce the loss value. Defaults to identy_loss, which is no reduction.

  • mode (str, optional) – Mode to calculate the loss. Defaults to “log”.

  • eps (float, optional) – Epsilon value to avoid division by zero.

Creates an instance of the class.

forward(pred, target)[source]

Forward function.

Parameters:
  • pred (torch.Tensor) – Predicted bboxes.

  • target (torch.Tensor) – Target bboxes.

Returns:

The reduced IoU loss.

Return type:

torch.Tensor

class Loss(reducer=<function identity_loss>)[source]

Base loss class.

Initialize a loss functor.

Parameters:
  • reducer (LossReducer) – A function to aggregate the loss values into

  • prediction (a single tensor value. It is commonly used for dense)

  • loss. (tasks to merge pixel-wise loss to a final)

  • Example::

    def mean_loss(loss: torch.Tensor) -> torch.Tensor:

    return loss.mean()

class MultiLevelSegLoss(reducer=<function mean_loss>, feature_idx=(0, ), weights=None)[source]

Multi-level segmentation loss class.

Applies the segmentation loss function to multiple levels of predictions to provide auxiliary losses for intermediate outputs in addition to the final output, used in FCN.

Creates an instance of the class.

Parameters:
  • reducer (LossReducer) – Reducer for the loss function. Defaults to mean_loss.

  • feature_idx (tuple[int]) – Indices for the level of features to compute losses. Defaults to (0,).

  • weights (list[float], optional) –

    The weights of each feature level. If None passes, it will set to 1 for all levels. Defaults to

    None.

forward(outputs, target, ignore_index=255)[source]

Forward pass.

Parameters:
  • outputs (list[Tensor]) – Multi-level outputs.

  • target (Tensor) – Assigned segmentation target mask.

  • ignore_index (int) – Ignore class id. Default to 255.

Returns:

Computed losses for each level.

Return type:

LossesType

class MultiPosCrossEntropyLoss(reducer=<function identity_loss>)[source]

Multi-positive cross entropy loss.

Used for appearance similiary learning in QDTrack.

Initialize a loss functor.

Parameters:
  • reducer (LossReducer) – A function to aggregate the loss values into

  • prediction (a single tensor value. It is commonly used for dense)

  • loss. (tasks to merge pixel-wise loss to a final)

  • Example::

    def mean_loss(loss: torch.Tensor) -> torch.Tensor:

    return loss.mean()

forward(pred, target, weight, avg_factor)[source]

Multi-positive cross entropy loss.

Parameters:
  • pred (Tensor) – Similarity scores before softmax. Shape [N, M]

  • target (Tensor) – Target for each pair. Either one, meaning same identity or zero, meaning different identity. Shape [N, M]

  • weight (Tensor) – The weight of loss for each prediction.

  • avg_factor (float) – Averaging factor for the loss.

Returns:

Scalar loss value.

Return type:

Tensor

class OrthogonalTransformRegularizationLoss(reducer=<function identity_loss>)[source]

Loss that punishes linear transformations that are not orthogonal.

Calculates difference of X’*X and identity matrix using norm( X’*X - I)

Initialize a loss functor.

Parameters:
  • reducer (LossReducer) – A function to aggregate the loss values into

  • prediction (a single tensor value. It is commonly used for dense)

  • loss. (tasks to merge pixel-wise loss to a final)

  • Example::

    def mean_loss(loss: torch.Tensor) -> torch.Tensor:

    return loss.mean()

__call___(transforms)[source]

Calculates the loss.

Calculates difference of X’*X and the identity matrix using norm(X’*X - I) for each transformation

Parameters:

transforms (list[Tensor]) – (list(torch.tensor)) list with transformation matrices batched ([N, 3, 3], [N, x, x], ….)

Return type:

Tensor

Returns:

torch.Tensor containing the mean loss value (mean(norm(X’*X - I)))

forward(transforms)[source]

Calculates the loss.

Calculates difference of X’*X and the identity matrix using norm(X’*X - I) for each transformation

Parameters:

transforms (list[Tensor]) – (list(torch.tensor)) list with transformation matrices batched ([N, 3, 3], [N, x, x], ….)

Return type:

Tensor

Returns:

torch.Tensor containing the mean loss value (mean(norm(X’*X - I)))

class SegCrossEntropyLoss(reducer=<function mean_loss>)[source]

Segmentation cross entropy loss class.

Wrapper for nn.CrossEntropyLoss that additionally clips the output to the target size and converts the target mask tensor to long.

Creates an instance of the class.

Parameters:

reducer (LossReducer) – Reducer for the loss function. Defaults to mean_loss.

forward(output, target, ignore_index=255)[source]

Forward pass.

Parameters:
  • output (list[Tensor]) – Model output.

  • target (Tensor) – Assigned segmentation target mask.

  • ignore_index (int) – Ignore class id. Default to 255.

Returns:

Computed loss.

Return type:

LossesType

Modules

vis4d.op.loss.base

Base class for meta architectures.

vis4d.op.loss.common

Common loss functions.

vis4d.op.loss.cross_entropy

Cross entropy loss.

vis4d.op.loss.embedding_distance

Embedding distance loss.

vis4d.op.loss.iou_loss

Embedding distance loss.

vis4d.op.loss.multi_level_seg_loss

Multi-level segmentation loss.

vis4d.op.loss.multi_pos_cross_entropy

Multi-positive cross entropy loss.

vis4d.op.loss.orthogonal_transform_loss

Orthogonal Transform Loss.

vis4d.op.loss.reducer

Definitions of loss reducers.

vis4d.op.loss.seg_cross_entropy_loss

Segmentation cross entropy loss.