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)
- 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.
- 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.
- 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.
Modules
Base class for meta architectures. |
|
Common loss functions. |
|
Cross entropy loss. |
|
Embedding distance loss. |
|
Embedding distance loss. |
|
Multi-level segmentation loss. |
|
Multi-positive cross entropy loss. |
|
Orthogonal Transform Loss. |
|
Definitions of loss reducers. |
|
Segmentation cross entropy loss. |