cosense3d.modules.losses package
Submodules
cosense3d.modules.losses.base_loss module
- class cosense3d.modules.losses.base_loss.BaseLoss(reduction: str = 'mean', activation: str = 'none', loss_weight: float = 1.0)[source]
Bases:
Module
- forward(preds: Tensor, targets: Tensor, weight: Tensor | None = None, avg_factor: int | None = None, reduction_override: str | None = None, *args, **kwargs) Tensor [source]
- Parameters:
preds – prediction tensor.
targets – target tensor.
weight – The weight of loss for each prediction. Defaults to None.
avg_factor – Average factor that is used to average the loss. Defaults to None.
reduction_override – The reduction method used to override the original reduction method of the loss. Defaults to None.
args – additional arguments.
kwargs –
- Returns:
weighted loss.
- property name
- training: bool
cosense3d.modules.losses.common module
- cosense3d.modules.losses.common.cross_entroy_with_logits(preds, tgts, n_cls, weights=None, reduction='none')[source]
- cosense3d.modules.losses.common.focal_loss(preds, tgts, weights=None, reduction='none', gamma=2.0, alpha=0.25, use_sigmoid=True)[source]
Parameters
preds: FloatTensor(…, n_cls) tgts: FloatTensor(…, n_cls) or LongTensor(…,) or LongTensor(…,1), largest label is background weights: same as preds or tgts ——-
- cosense3d.modules.losses.common.indices_to_dense_vector(indices: Tensor, size: int, indices_value: float = 1.0, default_value: float = 0.0) Tensor [source]
Creates dense vector with indices set to specific value and rest to zeros.
This function exists because it is unclear if it is safe to use tf.sparse_to_dense(indices, [size], 1, validate_indices=False) with indices which are not ordered. This function accepts a dynamic size (e.g. tf.shape(tensor)[0])
- Parameters:
indices – 1d Tensor with integer indices which are to be set to indices_values.
size – size of output Tensor.
indices_value – values of elements specified by indices in the output vector.
default_value – values of other elements in the output vector.
- Returns:
dense 1D Tensor of shape [size] with indices set to indices_values and the rest set to default_value.
- cosense3d.modules.losses.common.sigmoid_binary_cross_entropy(preds, tgts, weights=None, reduction='none')[source]
Parameters
preds: Tensor(d1, …, dn) tgts: Tensor(d1, …, dn) weights. Tensor(d1, …, dn) reduction: str(‘none’ | ‘mean’ | ‘sum’) ——-
cosense3d.modules.losses.edl module
- class cosense3d.modules.losses.edl.EDLLoss(n_cls: int, annealing_step: int, **kwargs)[source]
Bases:
BaseLoss
- training: bool
- cosense3d.modules.losses.edl.edl_mse_loss(preds, tgt, n_cls, temp, annealing_step, model_label='edl')[source]
Calculate evidential loss :param model_label: (str) a name to distinguish edl loss of different modules :param preds: (N, n_cls) the logits of each class :param tgt: (N,) labels with values from 0…(n_cls - 1) or (N, n_cls) :param n_cls: (int) number of classes, including background :param temp: current temperature for annealing of KL Divergence term of the loss :param annealing_step: maximum annealing step :return:
cosense3d.modules.losses.focal_loss module
- class cosense3d.modules.losses.focal_loss.FocalLoss(use_sigmoid: bool = True, gamma: float = 2.0, alpha: float = 0.25, activated: bool = False, bg_idx: int | None = None, **kwargs)[source]
Bases:
BaseLoss
- loss(pred: Tensor, target: Tensor, *args, **kwargs)[source]
- Parameters:
pred – prediction.
target – ground truth targets.
args –
kwargs –
- Returns:
- training: bool
- class cosense3d.modules.losses.focal_loss.GaussianFocalLoss(alpha: float = 2.0, gamma: float = 4.0, reduction: str = 'mean', loss_weight: float = 1.0)[source]
Bases:
BaseLoss
GaussianFocalLoss is a variant of focal loss.
More details can be found in the paper Code is modified from kp_utils.py # noqa: E501 Please notice that the target in GaussianFocalLoss is a gaussian heatmap, not 0/1 binary target.
- loss(pred: Tensor, target: Tensor)[source]
Focal Loss for targets in gaussian distribution.
- Parameters:
pred – The prediction.
target – The learning target of the prediction in gaussian distribution.
- Returns:
loss result.
- training: bool
- class cosense3d.modules.losses.focal_loss.QualityFocalLoss(use_sigmoid: bool = True, beta: float = 2.0, activated: bool = False, **kwargs)[source]
Bases:
BaseLoss
- loss(pred: Tensor, target: Tensor)[source]
Forward function.
- Parameters:
pred – Predicted joint representation of classification and quality (IoU) estimation with shape (N, C), C is the number of classes.
target – Target category label with shape (N,) and target quality label with shape (N,).
- Returns:
loss result.
- training: bool
- cosense3d.modules.losses.focal_loss.py_focal_loss_with_prob(pred: Tensor, target: Tensor, gamma: float = 2.0, alpha: float = 0.25)[source]
PyTorch version of Focal Loss. Different from py_sigmoid_focal_loss, this function accepts probability as input.
- Parameters:
pred – The prediction probability with shape (N, C), C is the number of classes.
target – The learning label of the prediction.
gamma – The gamma for calculating the modulating factor. Defaults to 2.0.
alpha – A balanced form for Focal Loss. Defaults to 0.25.
- Returns:
loss result.
- cosense3d.modules.losses.focal_loss.py_sigmoid_focal_loss(pred: Tensor, target: Tensor, gamma: float = 2.0, alpha: float = 0.25)[source]
PyTorch version of Focal Loss. Different from py_sigmoid_focal_loss, this function accepts probability as input.
- Parameters:
pred – The prediction probability with shape (N, C), C is the number of classes.
target – The learning label of the prediction.
gamma – The gamma for calculating the modulating factor. Defaults to 2.0.
alpha – A balanced form for Focal Loss. Defaults to 0.25.
- Returns:
loss result.
- cosense3d.modules.losses.focal_loss.quality_focal_loss(pred: ~torch.Tensor, target: (<class 'torch.Tensor'>, ), beta: float = 2.0) Tensor [source]
Quality Focal Loss (QFL) is from Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection.
- Parameters:
pred – Predicted joint representation of classification and quality (IoU) estimation with shape (N, C), C is the number of classes.
target – Target category label with shape (N,) and target quality label with shape (N,).
beta – The beta parameter for calculating the modulating factor. Defaults to 2.0.
- Returns:
Loss tensor with shape (N,).
- cosense3d.modules.losses.focal_loss.quality_focal_loss_with_prob(pred: ~torch.Tensor, target: (<class 'torch.Tensor'>, ), beta: float = 2.0) Tensor [source]
Quality Focal Loss (QFL) is from Generalized Focal Loss: Learning Qualified and Distributed Bounding Boxes for Dense Object Detection.
- Parameters:
pred – Predicted joint representation of classification and quality (IoU) estimation with shape (N, C), C is the number of classes.
target – Target category label with shape (N,) and target quality label with shape (N,).
beta – The beta parameter for calculating the modulating factor. Defaults to 2.0.
- Returns:
Loss tensor with shape (N,).
cosense3d.modules.losses.iou_loss module
cosense3d.modules.losses.l1_loss module
cosense3d.modules.losses.vanilla_seg_loss module
- class cosense3d.modules.losses.vanilla_seg_loss.VanillaSegLoss(d_weights, s_weights, d_coe, s_coe, l_weights=50, **kwargs)[source]
Bases:
Module
- forward(static_pred=None, dynamic_pred=None, static_gt=None, dynamic_gt=None)[source]
Perform loss function on the prediction.
Parameters
- output_dictdict
The dictionary contains the prediction.
- gt_dictdict
The dictionary contains the groundtruth.
Returns
Loss dictionary.
- training: bool