Losses
BCEDiceLoss
Bases: torch.nn.Module
Binary Cross Entropy + Dice Loss
Weighted average of BCE and Dice loss
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_weights |
List[float]
|
List of size 2 s.t loss_weights[0], loss_weights[1] are the weights for BCE, Dice respectively. |
[0.5, 0.5]
|
logits |
bool
|
Whether to use logits or not. |
True
|
Source code in V3_4/src/super_gradients/training/losses/bce_dice_loss.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
forward(input, target)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
torch.Tensor
|
Network's raw output shaped (N,1,H,W) |
required |
target |
torch.Tensor
|
Ground truth shaped (N,H,W) |
required |
Source code in V3_4/src/super_gradients/training/losses/bce_dice_loss.py
27 28 29 30 31 32 33 34 |
|
BCE
Bases: BCEWithLogitsLoss
Binary Cross Entropy Loss
Source code in V3_4/src/super_gradients/training/losses/bce_loss.py
5 6 7 8 9 10 11 12 13 14 15 16 |
|
forward(input, target)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input |
torch.Tensor
|
Network's raw output shaped (N,1,*) |
required |
target |
torch.Tensor
|
Ground truth shaped (N,*) |
required |
Source code in V3_4/src/super_gradients/training/losses/bce_loss.py
10 11 12 13 14 15 16 |
|
ChannelWiseKnowledgeDistillationLoss
Bases: nn.Module
Implementation of Channel-wise Knowledge distillation loss.
paper: "Channel-wise Knowledge Distillation for Dense Prediction", https://arxiv.org/abs/2011.13256 Official implementation: https://github.com/irfanICMLL/TorchDistiller/tree/main/SemSeg-distill
Source code in V3_4/src/super_gradients/training/losses/cwd_loss.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
__init__(normalization_mode='channel_wise', temperature=4.0, ignore_index=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalization_mode |
str
|
default is for |
'channel_wise'
|
temperature |
float
|
temperature relaxation value applied upon the logits before the normalization. default value is set to |
4.0
|
Source code in V3_4/src/super_gradients/training/losses/cwd_loss.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
DDRNetLoss
Bases: OhemCELoss
Source code in V3_4/src/super_gradients/training/losses/ddrnet_loss.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
__init__(threshold=0.7, ohem_percentage=0.1, weights=[1.0, 0.4], ignore_label=255, num_pixels_exclude_ignored=False)
This loss is an extension of the Ohem (Online Hard Example Mining Cross Entropy) Loss.
as define in paper: Accurate Semantic Segmentation of Road Scenes ( https://arxiv.org/pdf/2101.06085.pdf )
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
float
|
threshold to th hard example mining algorithm |
0.7
|
ohem_percentage |
float
|
minimum percentage of total pixels for the hard example mining algorithm (taking only the largest) losses |
0.1
|
weights |
List[float]
|
weights per each input of the loss. This loss supports a multi output (like in DDRNet with an auxiliary head). the losses of each head can be weighted. |
[1.0, 0.4]
|
ignore_label |
int
|
targets label to be ignored |
255
|
num_pixels_exclude_ignored |
bool
|
whether to exclude ignore pixels when calculating the mining percentage. see OhemCELoss doc for more details. |
False
|
Source code in V3_4/src/super_gradients/training/losses/ddrnet_loss.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
DEKRLoss
Bases: nn.Module
Implementation of the loss function from the "Bottom-Up Human Pose Estimation Via Disentangled Keypoint Regression" paper (https://arxiv.org/abs/2104.02300)
This loss should be used in conjunction with DEKRTargetsGenerator.
Source code in V3_4/src/super_gradients/training/losses/dekr_loss.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
component_names
property
Names of individual loss components for logging during training.
__init__(heatmap_loss_factor=1.0, offset_loss_factor=0.1, heatmap_loss='mse')
Instantiate the DEKR loss function. It is two-component loss function, consisting of a heatmap (MSE) loss and an offset (Smooth L1) losses. The total loss is the sum of the two individual losses, weighted by the corresponding factors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
heatmap_loss_factor |
float
|
Weighting factor for heatmap loss |
1.0
|
offset_loss_factor |
float
|
Weighting factor for offset loss |
0.1
|
heatmap_loss |
str
|
Type of heatmap loss to use. Can be "mse" (Used in DEKR paper) or "qfl" (Quality Focal Loss). We use QFL in our recipe as it produces better results. |
'mse'
|
Source code in V3_4/src/super_gradients/training/losses/dekr_loss.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
forward(predictions, targets)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Tuple[Tensor, Tensor]
|
Tuple of (heatmap, offset) predictions. heatmap is of shape (B, NumJoints + 1, H, W) offset is of shape (B, NumJoints * 2, H, W) |
required |
targets |
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple of (heatmap, mask, offset, offset_weight). heatmap is of shape (B, NumJoints + 1, H, W) mask is of shape (B, NumJoints + 1, H, W) offset is of shape (B, NumJoints * 2, H, W) offset_weight is of shape (B, NumJoints * 2, H, W) |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Tuple of (loss, loss_components) loss is a scalar tensor with the total loss loss_components is a tensor of shape (3,) containing the individual loss components for logging (detached from the graph) |
Source code in V3_4/src/super_gradients/training/losses/dekr_loss.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
DiceCEEdgeLoss
Bases: _Loss
Source code in V3_4/src/super_gradients/training/losses/dice_ce_edge_loss.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
__init__(num_classes, num_aux_heads=2, num_detail_heads=1, weights=(1, 1, 1, 1), dice_ce_weights=(1, 1), ignore_index=-100, edge_kernel=3, ce_edge_weights=(0.5, 0.5))
Total loss is computed as follows:
Loss-cls-edge = λ1 * CE + λ2 * M * CE , where [λ1, λ2] are ce_edge_weights.
For each Main feature maps and auxiliary heads the loss is calculated as:
Loss-main-aux = λ3 * Loss-cls-edge + λ4 * Loss-Dice, where [λ3, λ4] are dice_ce_weights.
For Feature maps defined as detail maps that predicts only the edge mask, the loss is computed as follow:
Loss-detail = BinaryCE + BinaryDice
Finally the total loss is computed as follows for the whole feature maps:
Loss = Σw[i] * Loss-main-aux[i] + Σw[j] * Loss-detail[j], where `w` is defined as the `weights` argument
`i` in [0, 1 + num_aux_heads], 1 is for the main feature map.
`j` in [1 + num_aux_heads, 1 + num_aux_heads + num_detail_heads].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_aux_heads |
int
|
num of auxiliary heads. |
2
|
num_detail_heads |
int
|
num of detail heads. |
1
|
weights |
Union[tuple, list]
|
Loss lambda weights. |
(1, 1, 1, 1)
|
dice_ce_weights |
Union[tuple, list]
|
weights lambdas between (Dice, CE) losses. |
(1, 1)
|
edge_kernel |
int
|
kernel size of dilation erosion convolutions for creating the edge feature map. |
3
|
ce_edge_weights |
Union[tuple, list]
|
weights lambdas between regular CE and edge attention CE. |
(0.5, 0.5)
|
Source code in V3_4/src/super_gradients/training/losses/dice_ce_edge_loss.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
forward(preds, target)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds |
Tuple[torch.Tensor]
|
Model output predictions, must be in the followed format: [Main-feats, Aux-feats[0], ..., Aux-feats[num_auxs-1], Detail-feats[0], ..., Detail-feats[num_details-1] |
required |
Source code in V3_4/src/super_gradients/training/losses/dice_ce_edge_loss.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
BinaryDiceLoss
Bases: DiceLoss
Compute Dice Loss for binary class tasks (1 class only). Except target to be a binary map with 0 and 1 values.
Source code in V3_4/src/super_gradients/training/losses/dice_loss.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
__init__(apply_sigmoid=True, smooth=1.0, eps=1e-05)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apply_sigmoid |
bool
|
Whether to apply sigmoid to the predictions. |
True
|
smooth |
float
|
laplace smoothing, also known as additive smoothing. The larger smooth value is, closer the dice coefficient is to 1, which can be used as a regularization effect. As mentioned in: https://github.com/pytorch/pytorch/issues/1249#issuecomment-337999895 |
1.0
|
eps |
float
|
epsilon value to avoid inf. |
1e-05
|
Source code in V3_4/src/super_gradients/training/losses/dice_loss.py
50 51 52 53 54 55 56 57 58 59 |
|
DiceLoss
Bases: AbstarctSegmentationStructureLoss
Compute average Dice loss between two tensors, It can support both multi-classes and binary tasks. Defined in the paper: "V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation"
Source code in V3_4/src/super_gradients/training/losses/dice_loss.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
GeneralizedDiceLoss
Bases: DiceLoss
Compute the Generalised Dice loss, contribution of each label is normalized by the inverse of its volume, in order to deal with class imbalance. Defined in the paper: "Generalised Dice overlap as a deep learning loss function for highly unbalanced segmentations"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
smooth |
float
|
default value is 0, smooth laplacian is not recommended to be used with GeneralizedDiceLoss. because the weighted values to be added are very small. |
0.0
|
eps |
float
|
default value is 1e-17, must be a very small value, because weighted |
1e-17
|
Source code in V3_4/src/super_gradients/training/losses/dice_loss.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
__init__(apply_softmax=True, ignore_index=None, smooth=0.0, eps=1e-17, reduce_over_batches=False, reduction='mean')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apply_softmax |
bool
|
Whether to apply softmax to the predictions. |
True
|
smooth |
float
|
laplace smoothing, also known as additive smoothing. The larger smooth value is, closer the dice coefficient is to 1, which can be used as a regularization effect. As mentioned in: https://github.com/pytorch/pytorch/issues/1249#issuecomment-337999895 |
0.0
|
eps |
float
|
epsilon value to avoid inf. |
1e-17
|
reduce_over_batches |
bool
|
Whether to apply reduction over the batch axis if set True, default is |
False
|
reduction |
Union[LossReduction, str]
|
Specifies the reduction to apply to the output: |
'mean'
|
Source code in V3_4/src/super_gradients/training/losses/dice_loss.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
FocalLoss
Bases: _Loss
Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
Source code in V3_4/src/super_gradients/training/losses/focal_loss.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
bbox_ciou_loss(pred_bboxes, target_bboxes, eps)
Compute CIoU loss between predicted and target bboxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_bboxes |
Tensor
|
Predicted boxes in xyxy format of [D0, D1,...Di, 4] shape |
required |
target_bboxes |
Tensor
|
Target boxes in xyxy format of [D0, D1,...Di, 4] shape |
required |
Returns:
Type | Description |
---|---|
Tensor
|
CIoU loss per each box as tensor of shape [D0, D1,...Di] |
Source code in V3_4/src/super_gradients/training/losses/functional.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
bbox_overlap(box1, box2, eps=1e-10)
Calculate the iou of box1 and box2. Shape of box1 and box2 should be the same, or broadcastable to the same shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box1 |
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple containing the x1, y1, x2, y2 coordinates of box1 |
required |
box2 |
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple containing the x1, y1, x2, y2 coordinates of box2 |
required |
eps |
float
|
epsilon to avoid divide by zero |
1e-10
|
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor, Tensor]
|
Tuple containing (iou, overlap, union) - iou: iou of box1 and box2 - overlap: overlap of box1 and box2 - union: union of box1 and box2 |
Source code in V3_4/src/super_gradients/training/losses/functional.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
get_bbox_center(bbox)
Compute the center of a bounding box from X1, Y1, X2, Y2 coordinates
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bbox |
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple of (x1, y1, x2, y2) tensors of arbitrary shape |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Tuple of (cx, cy) tensors of the same shape as bbox |
Source code in V3_4/src/super_gradients/training/losses/functional.py
58 59 60 61 62 63 64 65 66 67 |
|
get_bbox_width_height(bbox)
Compute the width and height of the bounding box from X1, Y1, X2, Y2 coordinates
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bbox |
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple of (x1, y1, x2, y2) tensors of arbitrary shape |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Tuple of (w, h) tensors of the same shape as bbox |
Source code in V3_4/src/super_gradients/training/losses/functional.py
70 71 72 73 74 75 76 77 78 79 |
|
get_convex_bbox(box1, box2)
Compute the convex bounding box around box1 and box2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box1 |
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple containing the x1, y1, x2, y2 coordinates of box1 |
required |
box2 |
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple containing the x1, y1, x2, y2 coordinates of box2 |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor, Tensor, Tensor]
|
Tuple containing the x1, y1, x2, y2 coordinates of the convex bounding box |
Source code in V3_4/src/super_gradients/training/losses/functional.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
BinaryIoULoss
Bases: IoULoss
Compute IoU Loss for binary class tasks (1 class only). Except target to be a binary map with 0 and 1 values.
Source code in V3_4/src/super_gradients/training/losses/iou_loss.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
|
__init__(apply_sigmoid=True, smooth=1.0, eps=1e-05)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apply_sigmoid |
bool
|
Whether to apply sigmoid to the predictions. |
True
|
smooth |
float
|
laplace smoothing, also known as additive smoothing. The larger smooth value is, closer the IoU coefficient is to 1, which can be used as a regularization effect. As mentioned in: https://github.com/pytorch/pytorch/issues/1249#issuecomment-337999895 |
1.0
|
eps |
float
|
epsilon value to avoid inf. |
1e-05
|
Source code in V3_4/src/super_gradients/training/losses/iou_loss.py
49 50 51 52 53 54 55 56 57 58 |
|
GeneralizedIoULoss
Bases: IoULoss
Compute the Generalised IoU loss, contribution of each label is normalized by the inverse of its volume, in order to deal with class imbalance.
FIXME: Why duplicate some parats in class and init docstring ? (+they have different description)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
(float) |
smooth
|
default value is 0, smooth laplacian is not recommended to be used with GeneralizedIoULoss. because the weighted values to be added are very small. |
required |
Source code in V3_4/src/super_gradients/training/losses/iou_loss.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
__init__(apply_softmax=True, ignore_index=None, smooth=0.0, eps=1e-17, reduce_over_batches=False, reduction='mean')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apply_softmax |
bool
|
Whether to apply softmax to the predictions. |
True
|
smooth |
float
|
laplace smoothing, also known as additive smoothing. The larger smooth value is, closer the iou coefficient is to 1, which can be used as a regularization effect. As mentioned in: https://github.com/pytorch/pytorch/issues/1249#issuecomment-337999895 |
0.0
|
eps |
float
|
epsilon value to avoid inf. |
1e-17
|
reduce_over_batches |
bool
|
Whether to apply reduction over the batch axis if set True, default is |
False
|
reduction |
Union[LossReduction, str]
|
Specifies the reduction to apply to the output: |
'mean'
|
Source code in V3_4/src/super_gradients/training/losses/iou_loss.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
IoULoss
Bases: AbstarctSegmentationStructureLoss
Compute average IoU loss between two tensors, It can support both multi-classes and binary tasks.
Source code in V3_4/src/super_gradients/training/losses/iou_loss.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
KDLogitsLoss
Bases: _Loss
Knowledge distillation loss, wraps the task loss and distillation loss
Source code in V3_4/src/super_gradients/training/losses/kd_losses.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
__init__(task_loss_fn, distillation_loss_fn=KDklDivLoss(), distillation_loss_coeff=0.5)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_loss_fn |
_Loss
|
task loss. E.g., CrossEntropyLoss |
required |
distillation_loss_fn |
_Loss
|
distillation loss. E.g., KLDivLoss |
KDklDivLoss()
|
distillation_loss_coeff |
float
|
0.5
|
Source code in V3_4/src/super_gradients/training/losses/kd_losses.py
24 25 26 27 28 29 30 31 32 33 34 35 |
|
KDklDivLoss
Bases: KLDivLoss
KL divergence wrapper for knowledge distillation
Source code in V3_4/src/super_gradients/training/losses/kd_losses.py
10 11 12 13 14 15 16 17 |
|
CrossEntropyLoss
Bases: nn.CrossEntropyLoss
CrossEntropyLoss - with ability to recieve distrbution as targets, and optional label smoothing
Source code in V3_4/src/super_gradients/training/losses/label_smoothing_cross_entropy_loss.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
cross_entropy(inputs, target, weight=None, ignore_index=-100, reduction='mean', smooth_eps=None, smooth_dist=None, from_logits=True)
cross entropy loss, with support for target distributions and label smoothing https://arxiv.org/abs/1512.00567
Source code in V3_4/src/super_gradients/training/losses/label_smoothing_cross_entropy_loss.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
onehot(indexes, N=None, ignore_index=None)
Creates a one-hot representation of indexes with N possible entries if N is not specified, it will suit the maximum index appearing. indexes is a long-tensor of indexes ignore_index will be zero in onehot representation
Source code in V3_4/src/super_gradients/training/losses/label_smoothing_cross_entropy_loss.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
MaskAttentionLoss
Bases: _Loss
Pixel mask attention loss. For semantic segmentation usages with 4D tensors.
Source code in V3_4/src/super_gradients/training/losses/mask_loss.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
__init__(criterion, loss_weights=(1.0, 1.0), reduction='mean')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
criterion |
_Loss
|
_Loss object, loss function that apply per pixel cost penalty are supported, i.e CrossEntropyLoss, BCEWithLogitsLoss, MSELoss, SL1Loss. criterion reduction must be |
required |
loss_weights |
Union[list, tuple]
|
Weight to apply for each part of the loss contributions, [regular loss, masked loss] respectively. |
(1.0, 1.0)
|
reduction |
Union[LossReduction, str]
|
Specifies the reduction to apply to the output: |
'mean'
|
Source code in V3_4/src/super_gradients/training/losses/mask_loss.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
OhemBCELoss
Bases: OhemLoss
OhemBCELoss - Online Hard Example Mining Binary Cross Entropy Loss
Source code in V3_4/src/super_gradients/training/losses/ohem_ce_loss.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
OhemCELoss
Bases: OhemLoss
OhemLoss - Online Hard Example Mining Cross Entropy Loss
Source code in V3_4/src/super_gradients/training/losses/ohem_ce_loss.py
64 65 66 67 68 69 70 71 72 73 74 |
|
OhemLoss
Bases: _Loss
OhemLoss - Online Hard Example Mining Cross Entropy Loss
Source code in V3_4/src/super_gradients/training/losses/ohem_ce_loss.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
__init__(threshold, mining_percent=0.1, ignore_lb=-100, num_pixels_exclude_ignored=True, criteria=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
float
|
Sample below probability threshold, is considered hard. |
required |
num_pixels_exclude_ignored |
bool
|
How to calculate total pixels from which extract mining percent of the samples. |
True
|
ignore_lb |
int
|
label index to be ignored in loss calculation. |
-100
|
criteria |
_Loss
|
loss to mine the examples from. i.e for num_pixels=100, ignore_pixels=30, mining_percent=0.1: num_pixels_exclude_ignored=False => num_mining = 100 * 0.1 = 10 num_pixels_exclude_ignored=True => num_mining = (100 - 30) * 0.1 = 7 |
None
|
Source code in V3_4/src/super_gradients/training/losses/ohem_ce_loss.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
ATSSAssigner
Bases: nn.Module
Bridging the Gap Between Anchor-based and Anchor-free Detection via Adaptive Training Sample Selection
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
|
__init__(topk=9, num_classes=80, force_gt_matching=False, eps=1e-09)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topk |
Maximum number of achors that is selected for each gt box |
9
|
|
num_classes |
80
|
||
force_gt_matching |
Guarantee that each gt box is matched to at least one anchor. If two gt boxes match to the same anchor, the one with the larger area will be selected. And the second-best achnor will be assigned to the other gt box. |
False
|
|
eps |
Small constant for numerical stability |
1e-09
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
forward(anchor_bboxes, num_anchors_list, gt_labels, gt_bboxes, pad_gt_mask, bg_index, gt_scores=None, pred_bboxes=None)
This code is based on https://github.com/fcjian/TOOD/blob/master/mmdet/core/bbox/assigners/atss_assigner.py
The assignment is done in following steps 1. compute iou between all bbox (bbox of all pyramid levels) and gt 2. compute center distance between all bbox and gt 3. on each pyramid level, for each gt, select k bbox whose center are closest to the gt center, so we total select k*l bbox as candidates for each gt 4. get corresponding iou for the these candidates, and compute the mean and std, set mean + std as the iou threshold 5. select these candidates whose iou are greater than or equal to the threshold as positive 6. limit the positive sample's center in gt 7. if an anchor box is assigned to multiple gts, the one with the highest iou will be selected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
anchor_bboxes |
Tensor
|
Tensor(float32) - pre-defined anchors, shape(L, 4), "xmin, xmax, ymin, ymax" format |
required |
num_anchors_list |
list
|
Number of anchors in each level |
required |
gt_labels |
Tensor
|
Tensor (int64|int32) - Label of gt_bboxes, shape(B, n, 1) |
required |
gt_bboxes |
Tensor
|
Tensor (float32) - Ground truth bboxes, shape(B, n, 4) |
required |
pad_gt_mask |
Optional[Tensor]
|
Tensor (float32) - 1 means bbox, 0 means no bbox, shape(B, n, 1) |
required |
bg_index |
int
|
Background index |
required |
gt_scores |
Optional[Tensor]
|
Tensor (float32) - Score of gt_bboxes, shape(B, n, 1), if None, then it will initialize with one_hot label |
None
|
pred_bboxes |
Optional[Tensor]
|
Tensor (float32) - predicted bounding boxes, shape(B, L, 4) |
None
|
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor, Tensor]
|
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
|
GIoULoss
Bases: object
Generalized Intersection over Union, see https://arxiv.org/abs/1902.09630
Parameters:
Name | Type | Description | Default |
---|---|---|---|
loss_weight |
float
|
giou loss weight, default as 1 |
1.0
|
eps |
float
|
epsilon to avoid divide by zero, default as 1e-10 |
1e-10
|
reduction |
str
|
Options are "none", "mean" and "sum". default as none |
'none'
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 |
|
bbox_overlap(box1, box2, eps=1e-10)
Calculate the iou of box1 and box2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box1 |
Tensor
|
box1 with the shape (..., 4) |
required |
box2 |
Tensor
|
box1 with the shape (..., 4) |
required |
eps |
float
|
epsilon to avoid divide by zero |
1e-10
|
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor, Tensor]
|
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
|
PPYoloELoss
Bases: nn.Module
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 |
|
__init__(num_classes, use_varifocal_loss=True, use_static_assigner=True, reg_max=16, classification_loss_weight=1.0, iou_loss_weight=2.5, dfl_loss_weight=0.5, use_batched_assignment=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_classes |
int
|
Number of classes |
required |
use_varifocal_loss |
bool
|
Whether to use Varifocal loss for classification loss; otherwise use Focal loss |
True
|
classification_loss_weight |
float
|
Classification loss weight |
1.0
|
iou_loss_weight |
float
|
IoU loss weight |
2.5
|
dfl_loss_weight |
float
|
DFL loss weight |
0.5
|
reg_max |
int
|
Number of regression bins (Must match the number of bins in the PPYoloE head) |
16
|
use_batched_assignment |
bool
|
Whether to use batched targets assignment or sequential (per-image). Default is True (batched). Batched assignment can be faster when number of the target per image is more or less the same across the batch, but it has higher peak GPU memory usage. Sequential assignment has lower peak GPU memory usage and preferable for cases when number of targets per image varies a lot. |
True
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 |
|
forward(outputs, targets)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outputs |
Union[Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor], Tuple[Tuple[Tensor, Tensor], Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]]]
|
Tuple of pred_scores, pred_distri, anchors, anchor_points, num_anchors_list, stride_tensor |
required |
targets |
Tensor
|
(Dictionary [str,Tensor]) with keys: - gt_class: (Tensor, int64|int32): Label of gt_bboxes, shape(B, n, 1) - gt_bbox: (Tensor, float32): Ground truth bboxes, shape(B, n, 4) in x1y1x2y2 format - pad_gt_mask (Tensor, float32): 1 means bbox, 0 means no bbox, shape(B, n, 1) |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
|
TaskAlignedAssigner
Bases: nn.Module
TOOD: Task-aligned One-stage Object Detection
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
__init__(topk=13, alpha=1.0, beta=6.0, eps=1e-09)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topk |
Maximum number of achors that is selected for each gt box |
13
|
|
alpha |
Power factor for class probabilities of predicted boxes (Used compute alignment metric) |
1.0
|
|
beta |
Power factor for IoU score of predicted boxes (Used compute alignment metric) |
6.0
|
|
eps |
Small constant for numerical stability |
1e-09
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
forward(pred_scores, pred_bboxes, anchor_points, num_anchors_list, gt_labels, gt_bboxes, pad_gt_mask, bg_index, gt_scores=None)
This code is based on https://github.com/fcjian/TOOD/blob/master/mmdet/core/bbox/assigners/task_aligned_assigner.py
The assignment is done in following steps 1. compute alignment metric between all bbox (bbox of all pyramid levels) and gt 2. select top-k bbox as candidates for each gt 3. limit the positive sample's center in gt (because the anchor-free detector only can predict positive distance) 4. if an anchor box is assigned to multiple gts, the one with the highest iou will be selected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_scores |
Tensor
|
Tensor (float32): predicted class probability, shape(B, L, C) |
required |
pred_bboxes |
Tensor
|
Tensor (float32): predicted bounding boxes, shape(B, L, 4) |
required |
anchor_points |
Tensor
|
Tensor (float32): pre-defined anchors, shape(L, 2), "cxcy" format |
required |
num_anchors_list |
list
|
List ( num of anchors in each level, shape(L) |
required |
gt_labels |
Tensor
|
Tensor (int64|int32): Label of gt_bboxes, shape(B, n, 1) |
required |
gt_bboxes |
Tensor
|
Tensor (float32): Ground truth bboxes, shape(B, n, 4) |
required |
pad_gt_mask |
Optional[Tensor]
|
Tensor (float32): 1 means bbox, 0 means no bbox, shape(B, n, 1). Can be None, which means all gt_bboxes are valid. |
required |
bg_index |
int
|
int ( background index |
required |
gt_scores |
Optional[Tensor]
|
Tensor (one, float32) Score of gt_bboxes, shape(B, n, 1) |
None
|
Returns:
Type | Description |
---|---|
|
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
batch_iou_similarity(box1, box2, eps=1e-09)
Calculate iou of box1 and box2 in batch. Bboxes are expected to be in x1y1x2y2 format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box1 |
torch.Tensor
|
box with the shape [N, M1, 4] |
required |
box2 |
torch.Tensor
|
box with the shape [N, M2, 4] |
required |
Returns:
Type | Description |
---|---|
float
|
iou between box1 and box2 with the shape [N, M1, M2] |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
bbox_center(boxes)
Get bbox centers from boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes |
Tensor
|
Boxes with shape (..., 4), "xmin, ymin, xmax, ymax" format. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Boxes centers with shape (..., 2), "cx, cy" format. |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
232 233 234 235 236 237 238 239 240 241 |
|
bbox_overlaps(bboxes1, bboxes2, mode='iou', is_aligned=False, eps=1e-06)
Calculate overlap between two set of bboxes.
If is_aligned
is False
, then calculate the overlaps between each
bbox of bboxes1 and bboxes2, otherwise the overlaps between each aligned
pair of bboxes1 and bboxes2.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes1 |
torch.Tensor
|
shape (B, m, 4) in |
required |
bboxes2 |
torch.Tensor
|
shape (B, n, 4) in |
required |
mode |
str
|
Either "iou" (intersection over union) or "iof" (intersection over foreground). |
'iou'
|
is_aligned |
bool
|
If True, then m and n must be equal. Default False. |
False
|
eps |
float
|
A value added to the denominator for numerical stability. Default 1e-6. |
1e-06
|
Returns:
Type | Description |
---|---|
torch.Tensor
|
Tensor of shape (m, n) if |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
check_points_inside_bboxes(points, bboxes, center_radius_tensor=None, eps=1e-09)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
points |
Tensor
|
Tensor (float32) of shape[L, 2], "xy" format, L: num_anchors |
required |
bboxes |
Tensor
|
Tensor (float32) of shape[B, n, 4], "xmin, ymin, xmax, ymax" format |
required |
center_radius_tensor |
Optional[Tensor]
|
Tensor (float32) of shape [L, 1]. Default: None. |
None
|
eps |
float
|
Default: 1e-9 |
1e-09
|
Returns:
Type | Description |
---|---|
Tensor
|
Tensor (float32) of shape[B, n, L], value=1. means selected |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
compute_max_iou_anchor(ious)
For each anchor, find the GT with the largest IOU.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ious |
Tensor
|
Tensor (float32) of shape[B, n, L], n: num_gts, L: num_anchors |
required |
Returns:
Type | Description |
---|---|
Tensor
|
is_max_iou is Tensor (float32) of shape[B, n, L], value=1. means selected |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
164 165 166 167 168 169 170 171 172 173 174 |
|
compute_max_iou_gt(ious)
For each GT, find the anchor with the largest IOU.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ious |
Tensor
|
Tensor (float32) of shape[B, n, L], n: num_gts, L: num_anchors |
required |
Returns:
Type | Description |
---|---|
Tensor
|
is_max_iou, Tensor (float32) of shape[B, n, L], value=1. means selected |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
244 245 246 247 248 249 250 251 252 253 254 |
|
gather_topk_anchors(metrics, topk, largest=True, topk_mask=None, eps=1e-09)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metrics |
Tensor
|
Tensor(float32) of shape[B, n, L], n: num_gts, L: num_anchors |
required |
topk |
int
|
The number of top elements to look for along the axis. |
required |
largest |
bool
|
If set to true, algorithm will sort by descending order, otherwise sort by ascending order. |
True
|
topk_mask |
Optional[Tensor]
|
Tensor(float32) of shape[B, n, 1], ignore bbox mask, |
None
|
eps |
float
|
Default: 1e-9 |
1e-09
|
Returns:
Type | Description |
---|---|
Tensor
|
is_in_topk, Tensor (float32) of shape[B, n, L], value=1. means selected |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
iou_similarity(box1, box2, eps=1e-10)
Calculate iou of box1 and box2. Bboxes are expected to be in x1y1x2y2 format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
box1 |
torch.Tensor
|
box with the shape [M1, 4] |
required |
box2 |
torch.Tensor
|
box with the shape [M2, 4] |
required |
Returns:
Type | Description |
---|---|
float
|
iou between box1 and box2 with the shape [M1, M2] |
Source code in V3_4/src/super_gradients/training/losses/ppyolo_loss.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
RSquaredLoss
Bases: _Loss
Source code in V3_4/src/super_gradients/training/losses/r_squared_loss.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
forward(output, target)
Computes the R-squared for the output and target values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output |
Tensor / Numpy / List The prediction |
required | |
target |
Tensor / Numpy / List The corresponding lables |
required |
Source code in V3_4/src/super_gradients/training/losses/r_squared_loss.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
RescoringLoss
Bases: nn.Module
Source code in V3_4/src/super_gradients/training/losses/rescoring_loss.py
10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
forward(predictions, targets)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Tuple[Tensor, Tensor]
|
Tuple of (poses, scores) |
required |
targets |
Target scores |
required |
Returns:
Type | Description |
---|---|
KD loss between predicted scores and target scores |
Source code in V3_4/src/super_gradients/training/losses/rescoring_loss.py
15 16 17 18 19 20 21 22 |
|
SegKDLoss
Bases: nn.Module
Wrapper loss for semantic segmentation KD.
This loss includes two loss components, ce_loss
i.e CrossEntropyLoss, and KDLogitsLoss
i.e
ChannelWiseKnowledgeDistillationLoss
.
Source code in V3_4/src/super_gradients/training/losses/seg_kd_loss.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
__init__(kd_loss, ce_loss, weights, kd_loss_weights)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kd_loss |
nn.Module
|
knowledge distillation criteria, such as, ChannelWiseKnowledgeDistillationLoss. This loss should except as input a triplet of the predictions from the model with shape [B, C, H, W], the teacher model predictions with shape [B, C, H, W] and the target labels with shape [B, H, W]. |
required |
ce_loss |
nn.Module
|
classification criteria, such as, CE, OHEM, MaskAttention, SL1, etc. This loss should except as input the predictions from the model with shape [B, C, H, W], and the target labels with shape [B, H, W]. |
required |
weights |
Union[tuple, list]
|
lambda weights to apply upon each prediction map heads. |
required |
kd_loss_weights |
Union[tuple, list]
|
lambda weights to apply upon each criterion. 2 values are excepted as follows, [ce_loss_weight, kd_loss_weight]. |
required |
Source code in V3_4/src/super_gradients/training/losses/seg_kd_loss.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
ShelfNetOHEMLoss
Bases: OhemCELoss
Source code in V3_4/src/super_gradients/training/losses/shelfnet_ohem_loss.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
__init__(threshold=0.7, mining_percent=0.0001, ignore_lb=255)
This loss is an extension of the Ohem (Online Hard Example Mining Cross Entropy) Loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
float
|
threshold to th hard example mining algorithm |
0.7
|
mining_percent |
float
|
minimum percentage of total pixels for the hard example mining algorithm (taking only the largest) losses. Default is 1e-4, according to legacy settings, number of 400 pixels for typical input of (512x512) and batch of 16. |
0.0001
|
ignore_lb |
int
|
targets label to be ignored |
255
|
Source code in V3_4/src/super_gradients/training/losses/shelfnet_ohem_loss.py
10 11 12 13 14 15 16 17 18 19 20 |
|
ShelfNetSemanticEncodingLoss
Bases: nn.CrossEntropyLoss
2D Cross Entropy Loss with Auxilary Loss
Source code in V3_4/src/super_gradients/training/losses/shelfnet_semantic_encoding_loss.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
HardMiningCrossEntropyLoss
Bases: _Loss
L_cls = [CE of all positives] + [CE of the hardest backgrounds] where the second term is built from [neg_pos_ratio * positive pairs] background cells with the highest CE (the hardest background cells)
Source code in V3_4/src/super_gradients/training/losses/ssd_loss.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
__init__(neg_pos_ratio)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
neg_pos_ratio |
float
|
a ratio of negative samples to positive samples in the loss (unlike positives, not all negatives will be used: for each positive the [neg_pos_ratio] hardest negatives will be selected) |
required |
Source code in V3_4/src/super_gradients/training/losses/ssd_loss.py
20 21 22 23 24 25 26 27 28 |
|
SSDLoss
Bases: _Loss
Implements the loss as the sum of the followings:
1. Confidence Loss: All labels, with hard negative mining
2. Localization Loss: Only on positive labels
L = (2 - alpha) * L_l1 + alpha * L_cls, where * L_cls is HardMiningCrossEntropyLoss * L_l1 = [SmoothL1Loss for all positives]
Source code in V3_4/src/super_gradients/training/losses/ssd_loss.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
__init__(dboxes, alpha=1.0, iou_thresh=0.5, neg_pos_ratio=3.0)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dboxes |
DefaultBoxes
|
model anchors, shape [Num Grid Cells * Num anchors x 4] |
required |
alpha |
float
|
a weighting factor between classification and regression loss |
1.0
|
iou_thresh |
float
|
a threshold for matching of anchors in each grid cell to GTs (a match should have IoU > iou_thresh) |
0.5
|
neg_pos_ratio |
float
|
a ratio for HardMiningCrossEntropyLoss |
3.0
|
Source code in V3_4/src/super_gradients/training/losses/ssd_loss.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
forward(predictions, targets)
Compute the loss :param predictions - predictions tensor coming from the network, tuple with shapes ([Batch Size, 4, num_dboxes], [Batch Size, num_classes + 1, num_dboxes]) were predictions have logprobs for background and other classes :param targets - targets for the batch. [num targets, 6] (index in batch, label, x,y,w,h)
Source code in V3_4/src/super_gradients/training/losses/ssd_loss.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
match_dboxes(targets)
creates tensors with target boxes and labels for each dboxes, so with the same len as dboxes.
- Each GT is assigned with a grid cell with the highest IoU, this creates a pair for each GT and some cells;
- The rest of grid cells are assigned to a GT with the highest IoU, assuming it's > self.iou_thresh; If this condition is not met the grid cell is marked as background
GT-wise: one to many Grid-cell-wise: one to one
Parameters:
Name | Type | Description | Default |
---|---|---|---|
targets |
a tensor containing the boxes for a single image; shape [num_boxes, 6] (image_id, label, x, y, w, h) |
required |
Returns:
Type | Description |
---|---|
two tensors boxes - shape of dboxes [4, num_dboxes] (x,y,w,h) labels - sahpe [num_dboxes] |
Source code in V3_4/src/super_gradients/training/losses/ssd_loss.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
DetailAggregateModule
Bases: nn.Module
DetailAggregateModule to create ground-truth spatial details map. Given ground-truth segmentation masks and using laplacian kernels this module create feature-maps with special attention to classes edges aka details.
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
__init__(num_classes, ignore_label, detail_threshold=1.0, learnable_fusing_kernel=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detail_threshold |
float
|
threshold to define a pixel as edge after laplacian. must be a value between 1 and 8, lower value for smooth edges, high value for fine edges. |
1.0
|
learnable_fusing_kernel |
bool
|
whether the 1x1 conv map of strided maps is learnable or not. |
True
|
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
DetailLoss
Bases: _Loss
STDC DetailLoss applied on details features from higher resolution and ground-truth details map. Loss combination of BCE loss and BinaryDice loss
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
__init__(weights=[1.0, 1.0])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weights |
list
|
weight to apply for each part of the loss contributions, [BCE, Dice] respectively. |
[1.0, 1.0]
|
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
94 95 96 97 98 99 100 101 102 |
|
forward(detail_out, detail_target)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detail_out |
torch.Tensor
|
predicted detail map. |
required |
detail_target |
torch.Tensor
|
ground-truth detail loss, output of DetailAggregateModule. |
required |
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
104 105 106 107 108 109 110 111 |
|
STDCLoss
Bases: _Loss
Loss class of STDC-Seg training.
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
component_names
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
__init__(num_classes, threshold=0.7, num_aux_heads=2, num_detail_heads=1, weights=(1, 1, 1, 1), detail_weights=(1, 1), mining_percent=0.1, detail_threshold=1.0, learnable_fusing_kernel=True, ignore_index=None, ohem_criteria=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
threshold |
float
|
Online hard-mining probability threshold. |
0.7
|
num_aux_heads |
int
|
num of auxiliary heads. |
2
|
num_detail_heads |
int
|
num of detail heads. |
1
|
weights |
Union[tuple, list]
|
Loss lambda weights. |
(1, 1, 1, 1)
|
detail_weights |
Union[tuple, list]
|
weights for (Dice, BCE) losses parts in DetailLoss. |
(1, 1)
|
mining_percent |
float
|
mining percentage. |
0.1
|
detail_threshold |
float
|
detail threshold to create binary details features in DetailLoss. |
1.0
|
learnable_fusing_kernel |
bool
|
whether DetailAggregateModule params are learnable or not. |
True
|
ohem_criteria |
OhemLoss
|
OhemLoss criterion component of STDC. When none is given, it will be derrived according to num_classes (i.e OhemCELoss if num_classes > 1 and OhemBCELoss otherwise). |
None
|
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
forward(preds, target)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds |
Tuple[torch.Tensor]
|
Model output predictions, must be in the followed format: [Main-feats, Aux-feats[0], ..., Aux-feats[num_auxs-1], Detail-feats[0], ..., Detail-feats[num_details-1] |
required |
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
get_train_named_params()
Expose DetailAggregateModule learnable parameters to be passed to the optimizer.
Source code in V3_4/src/super_gradients/training/losses/stdc_loss.py
209 210 211 212 213 214 |
|
AbstarctSegmentationStructureLoss
Bases: _Loss
, ABC
Abstract computation of structure loss between two tensors, It can support both multi-classes and binary tasks.
Source code in V3_4/src/super_gradients/training/losses/structure_loss.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
__init__(apply_softmax=True, ignore_index=None, smooth=1.0, eps=1e-05, reduce_over_batches=False, generalized_metric=False, weight=None, reduction='mean')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apply_softmax |
bool
|
Whether to apply softmax to the predictions. |
True
|
smooth |
float
|
laplace smoothing, also known as additive smoothing. The larger smooth value is, closer the metric coefficient is to 1, which can be used as a regularization effect. As mentioned in: https://github.com/pytorch/pytorch/issues/1249#issuecomment-337999895 |
1.0
|
eps |
float
|
epsilon value to avoid inf. |
1e-05
|
reduce_over_batches |
bool
|
Whether to average metric over the batch axis if set True, default is |
False
|
generalized_metric |
bool
|
Whether to apply normalization by the volume of each class. |
False
|
weight |
Optional[torch.Tensor]
|
a manual rescaling weight given to each class. If given, it has to be a Tensor of size |
None
|
reduction |
Union[LossReduction, str]
|
Specifies the reduction to apply to the output: |
'mean'
|
Source code in V3_4/src/super_gradients/training/losses/structure_loss.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
CIoULoss
Bases: nn.Module
Complete IoU loss
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
__init__(eps=1e-10, reduction='none')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
epsilon to avoid divide by zero, default as 1e-10 |
1e-10
|
reduction |
str
|
Options are "none", "mean" and "sum". default as none |
'none'
|
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
252 253 254 255 256 257 258 259 260 261 262 |
|
forward(predictions, targets, loc_weights=None)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
Tensor
|
Predicted boxes in xyxy format of [D0, D1,...Di, 4] shape |
required |
targets |
Tensor
|
Target boxes in xyxy format of [D0, D1,...Di, 4] shape |
required |
loc_weights |
Optional[Tensor]
|
Optional tensor of [D0, D1,...Di] shape with weights for each prediction |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
CIOU loss |
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
|
YoloNASPoseLoss
Bases: nn.Module
Loss for training YoloNASPose model
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
|
__init__(oks_sigmas, classification_loss_type='focal', regression_iou_loss_type='ciou', classification_loss_weight=1.0, iou_loss_weight=2.5, dfl_loss_weight=0.5, pose_cls_loss_weight=1.0, pose_reg_loss_weight=1.0, pose_classification_loss_type='bce', bbox_assigner_topk=13, bbox_assigned_alpha=1.0, bbox_assigned_beta=6.0, assigner_multiply_by_pose_oks=False, rescale_pose_loss_with_assigned_score=False, average_losses_in_ddp=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
oks_sigmas |
Union[List[float], np.ndarray, Tensor]
|
OKS sigmas for pose estimation. Array of [Num Keypoints]. |
required |
classification_loss_type |
str
|
Classification loss type. One of "focal" or "bce" |
'focal'
|
regression_iou_loss_type |
str
|
Regression IoU loss type. One of "giou" or "ciou" |
'ciou'
|
classification_loss_weight |
float
|
Classification loss weight |
1.0
|
iou_loss_weight |
float
|
IoU loss weight |
2.5
|
dfl_loss_weight |
float
|
DFL loss weight |
0.5
|
pose_cls_loss_weight |
float
|
Pose classification loss weight |
1.0
|
pose_reg_loss_weight |
float
|
Pose regression loss weight |
1.0
|
average_losses_in_ddp |
bool
|
Whether to average losses in DDP mode. In theory, enabling this option should have the positive impact on model accuracy since it would smooth out influence of batches with small number of objects. However, it needs to be proven empirically. |
False
|
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
forward(outputs, targets)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outputs |
Tuple[Tuple[Tensor, Tensor, Tensor, Tensor], Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]]
|
Tuple of pred_scores, pred_distri, anchors, anchor_points, num_anchors_list, stride_tensor |
required |
targets |
Tuple[Tensor, Tensor, Tensor]
|
A tuple of (boxes, joints, crowd) tensors where - boxes: [N, 5] (batch_index, x1, y1, x2, y2) - joints: [N, num_joints, 4] (batch_index, x, y, visibility) - crowd: [N, 2] (batch_index, is_crowd) |
required |
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Tuple of two tensors where first element is main loss for backward and second element is stacked tensor of all individual losses |
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
|
YoloNASPoseTaskAlignedAssigner
Bases: nn.Module
Task-aligned assigner repurposed from YoloNAS for pose estimation task
This class is almost identical to TaskAlignedAssigner, but it also assigns poses and unlike in object detection where assigned scores are product of IoU and class confidence, in pose estimation final assignment score is product of pose OKS and bbox IoU. This was empirically found to provide superior performance that the original approach.
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
__init__(sigmas, topk=13, alpha=1.0, beta=6.0, eps=1e-09, multiply_by_pose_oks=False)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sigmas |
Tensor
|
Sigmas for OKS calculation |
required |
topk |
int
|
Maximum number of anchors that is selected for each gt box |
13
|
alpha |
float
|
Power factor for class probabilities of predicted boxes (Used compute alignment metric) |
1.0
|
beta |
Power factor for IoU score of predicted boxes (Used compute alignment metric) |
6.0
|
|
eps |
Small constant for numerical stability |
1e-09
|
|
multiply_by_pose_oks |
bool
|
Whether to multiply alignment metric by pose OKS |
False
|
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
forward(pred_scores, pred_bboxes, pred_pose_coords, anchor_points, gt_labels, gt_bboxes, gt_poses, gt_crowd, pad_gt_mask, bg_index)
This code is based on https://github.com/fcjian/TOOD/blob/master/mmdet/core/bbox/assigners/task_aligned_assigner.py
The assignment is done in following steps 1. compute alignment metric between all bbox (bbox of all pyramid levels) and gt 2. select top-k bbox as candidates for each gt 3. limit the positive sample's center in gt (because the anchor-free detector only can predict positive distance) 4. if an anchor box is assigned to multiple gts, the one with the highest iou will be selected.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_scores |
Tensor
|
Tensor (float32): predicted class probability, shape(B, L, C) |
required |
pred_bboxes |
Tensor
|
Tensor (float32): predicted bounding boxes, shape(B, L, 4) |
required |
pred_pose_coords |
Tensor
|
Tensor (float32): predicted poses, shape(B, L, Num Keypoints, 2) |
required |
anchor_points |
Tensor
|
Tensor (float32): pre-defined anchors, shape(L, 2), xy format |
required |
gt_labels |
Tensor
|
Tensor (int64|int32): Label of gt_bboxes, shape(B, n, 1) |
required |
gt_bboxes |
Tensor
|
Tensor (float32): Ground truth bboxes, shape(B, n, 4) |
required |
gt_poses |
Tensor
|
Tensor (float32): Ground truth poses, shape(B, n, Num Keypoints, 3) |
required |
gt_crowd |
Tensor
|
Tensor (int): Whether the gt is crowd, shape(B, n, 1) |
required |
pad_gt_mask |
Tensor
|
Tensor (float32): 1 means bbox, 0 means no bbox, shape(B, n, 1) |
required |
bg_index |
int
|
int ( background index |
required |
gt_scores |
Tensor (one, float32) Score of gt_bboxes, shape(B, n, 1) |
required |
Returns:
Type | Description |
---|---|
YoloNASPoseYoloNASPoseBoxesAssignmentResult
|
|
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
|
YoloNASPoseYoloNASPoseBoxesAssignmentResult
dataclass
This dataclass stores result of assignment of predicted boxes to ground truth boxes for YoloNASPose model. It produced by YoloNASPoseTaskAlignedAssigner and is used by YoloNASPoseLoss to compute the loss.
For all fields, first dimension is batch dimension, second dimension is number of anchors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
assigned_labels |
Tensor
|
Tensor of shape (B, L) - Assigned gt labels for each anchor location |
required |
assigned_bboxes |
Tensor
|
Tensor of shape (B, L, 4) - Assigned groundtruth boxes in XYXY format for each anchor location |
required |
assigned_scores |
Tensor
|
Tensor of shape (B, L, C) - Assigned scores for each anchor location |
required |
assigned_poses |
Tensor
|
Tensor of shape (B, L, 17, 3) - Assigned groundtruth poses for each anchor location |
required |
assigned_gt_index |
Tensor
|
Tensor of shape (B, L) - Index of assigned groundtruth box for each anchor location |
required |
assigned_crowd |
Tensor
|
Tensor of shape (B, L) - Whether the assigned groundtruth box is crowd |
required |
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
batch_pose_oks(gt_keypoints, pred_keypoints, gt_bboxes_xyxy, sigmas, eps=1e-09)
Calculate batched OKS (Object Keypoint Similarity) between two sets of keypoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gt_keypoints |
torch.Tensor
|
Joints with the shape [N, M1, Num Joints, 3] |
required |
gt_bboxes_xyxy |
torch.Tensor
|
Array of bboxes with the shape [N, M1, 4] in XYXY format |
required |
pred_keypoints |
torch.Tensor
|
Joints with the shape [N, M1, Num Joints, 3] |
required |
sigmas |
torch.Tensor
|
Sigmas with the shape [Num Joints] |
required |
(float) |
eps
|
Small constant for numerical stability |
required |
Returns:
Type | Description |
---|---|
float
|
OKS between gt_keypoints and pred_keypoints with the shape [N, M1, M2] |
Source code in V3_4/src/super_gradients/training/losses/yolo_nas_pose_loss.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
Based on https://github.com/Megvii-BaseDetection/YOLOX (Apache-2.0 license)
IOUloss
Bases: nn.Module
IoU loss with the following supported loss types:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reduction |
str
|
One of ["mean", "sum", "none"] reduction to apply to the computed loss (Default="none") |
'none'
|
loss_type |
str
|
One of ["iou", "giou"] where: * 'iou' for (1 - iou^2) * 'giou' according to "Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression" (1 - giou), where giou = iou - (cover_box - union_box)/cover_box |
'iou'
|
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
YoloXDetectionLoss
Bases: _Loss
Calculate YOLOX loss: L = L_objectivness + L_iou + L_classification + 1[use_l1]*L_l1
where: * L_iou, L_classification and L_l1 are calculated only between cells and targets that suit them; * L_objectivness is calculated for all cells.
L_classification:
for cells that have suitable ground truths in their grid locations add BCEs
to force a prediction of IoU with a GT in a multi-label way
Coef: 1.
L_iou:
for cells that have suitable ground truths in their grid locations
add (1 - IoU^2), IoU between a predicted box and each GT box, force maximum IoU
Coef: 5.
L_l1:
for cells that have suitable ground truths in their grid locations
l1 distance between the logits and GTs in “logits” format (the inverse of “logits to predictions” ops)
Coef: 1[use_l1]
L_objectness:
for each cell add BCE with a label of 1 if there is GT assigned to the cell
Coef: 1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strides |
List[int]
|
List of Yolo levels output grid sizes (i.e [8, 16, 32]). |
required |
num_classes |
int
|
Number of classes. |
required |
use_l1 |
bool
|
Controls the L_l1 Coef as discussed above (default=False). |
False
|
center_sampling_radius |
float
|
Sampling radius used for center sampling when creating the fg mask (default=2.5). |
2.5
|
iou_type |
str
|
Iou loss type, one of ["iou","giou"] (deafult="iou"). |
'iou'
|
iou_weight |
float
|
Weight to apply to the iou loss term. |
5.0
|
obj_weight |
float
|
Weight to apply to the obj loss term. |
1.0
|
cls_weight |
float
|
Weight to apply to the cls loss term. |
1.0
|
cls_pos_weight |
Optional[torch.Tensor]
|
Class weights for the cls loss. Passed on to torch.nn.BCEWithLogitsLoss |
None
|
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
|
component_names: List[str]
property
Component names for logging during training. These correspond to 2nd item in the tuple returned in self.forward(...). See super_gradients.Trainer.train() docs for more info.
dynamic_k_matching(cost, pair_wise_ious, gt_classes, num_gt, fg_mask)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cost |
pairwise cost, [num_FGs x num_GTs] |
required | |
pair_wise_ious |
pairwise IoUs, [num_FGs x num_GTs] |
required | |
gt_classes |
class of each GT |
required | |
num_gt |
number of GTs |
required |
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
|
forward(model_output, targets)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_output |
Union[list, Tuple[torch.Tensor, List]]
|
Union[list, Tuple[torch.Tensor, List]]: When list- output from all Yolo levels, each of shape [Batch x 1 x GridSizeY x GridSizeX x (4 + 1 + Num_classes)] And when tuple- the second item is the described list (first item is discarded) |
required |
targets |
torch.Tensor
|
torch.Tensor: Num_targets x (4 + 2)], values on dim 1 are: image id in a batch, class, box x y w h |
required |
Returns:
Type | Description |
---|---|
loss, all losses separately in a detached tensor |
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
get_assignments(image_idx, num_gt, total_num_anchors, gt_bboxes_per_image, gt_classes, bboxes_preds_per_image, expanded_strides, x_shifts, y_shifts, cls_preds, obj_preds, mode='gpu', ious_loss_cost_coeff=3.0, outside_boxes_and_center_cost_coeff=100000.0)
Match cells to ground truth: * at most 1 GT per cell * dynamic number of cells per GT
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outside_boxes_and_center_cost_coeff |
float: Cost coefficiant of cells the radius and bbox of gts in dynamic matching (default=100000). |
100000.0
|
|
ious_loss_cost_coeff |
float: Cost coefficiant for iou loss in dynamic matching (default=3). |
3.0
|
|
image_idx |
int: Image index in batch. |
required | |
num_gt |
int: Number of ground trunth targets in the image. |
required | |
total_num_anchors |
int: Total number of possible bboxes = sum of all grid cells. |
required | |
gt_bboxes_per_image |
torch.Tensor: Tensor of gt bboxes for the image, shape: (num_gt, 4). |
required | |
gt_classes |
torch.Tesnor: Tensor of the classes in the image, shape: (num_preds,4). |
required | |
bboxes_preds_per_image |
Tensor of the classes in the image, shape: (num_preds). |
required | |
expanded_strides |
torch.Tensor: Stride of the output grid the prediction is coming from, shape (1 x num_cells x 1). |
required | |
x_shifts |
torch.Tensor: X's in cell coordinates, shape (1,num_cells,1). |
required | |
y_shifts |
torch.Tensor: Y's in cell coordinates, shape (1,num_cells,1). |
required | |
cls_preds |
torch.Tensor: Class predictions in all cells, shape (batch_size, num_cells). |
required | |
obj_preds |
torch.Tensor: Objectness predictions in all cells, shape (batch_size, num_cells). |
required | |
mode |
str: One of ["gpu","cpu"], Controls the device the assignment operation should be taken place on (deafult="gpu") |
'gpu'
|
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|
get_in_boxes_info(gt_bboxes_per_image, expanded_strides, x_shifts, y_shifts, total_num_anchors, num_gt)
Create a mask for all cells, mask in only foreground: cells that have a center located: * withing a GT box; OR * within a fixed radius around a GT box (center sampling);
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_gt |
int: Number of ground trunth targets in the image. |
required | |
total_num_anchors |
int: Sum of all grid cells. |
required | |
gt_bboxes_per_image |
torch.Tensor: Tensor of gt bboxes for the image, shape: (num_gt, 4). |
required | |
expanded_strides |
torch.Tensor: Stride of the output grid the prediction is coming from, shape (1 x num_cells x 1). |
required | |
x_shifts |
torch.Tensor: X's in cell coordinates, shape (1,num_cells,1). |
required | |
y_shifts |
torch.Tensor: Y's in cell coordinates, shape (1,num_cells,1). |
required |
Returns:
Type | Description |
---|---|
|
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
|
get_l1_target(l1_target, gt, stride, x_shifts, y_shifts, eps=1e-08)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
l1_target |
tensor of zeros of shape [Num_cell_gt_pairs x 4] |
required | |
gt |
targets in coordinates [Num_cell_gt_pairs x (4 + 1 + num_classes)] |
required |
Returns:
Type | Description |
---|---|
targets in the format corresponding to logits |
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
394 395 396 397 398 399 400 401 402 403 404 405 |
|
prepare_predictions(predictions)
Convert raw outputs of the network into a format that merges outputs from all levels
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictions |
List[torch.Tensor]
|
output from all Yolo levels, each of shape [Batch x 1 x GridSizeY x GridSizeX x (4 + 1 + Num_classes)] |
required |
Returns:
Type | Description |
---|---|
Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]
|
5 tensors representing predictions: * x_shifts: shape [1 x * num_cells x 1], where num_cells = grid1X * grid1Y + grid2X * grid2Y + grid3X * grid3Y, x coordinate on the grid cell the prediction is coming from * y_shifts: shape [1 x num_cells x 1], y coordinate on the grid cell the prediction is coming from * expanded_strides: shape [1 x num_cells x 1], stride of the output grid the prediction is coming from * transformed_outputs: shape [batch_size x num_cells x (num_classes + 5)], predictions with boxes in real coordinates and logprobabilities * raw_outputs: shape [batch_size x num_cells x (num_classes + 5)], raw predictions with boxes and confidences as logits |
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
YoloXFastDetectionLoss
Bases: YoloXDetectionLoss
A completely new implementation of YOLOX loss. This is NOT an equivalent implementation to the regular yolox loss.
- Completely avoids using loops compared to the nested loops in the original implementation. As a result runs much faster (speedup depends on the type of GPUs, their count, the batch size, etc.).
- Tensors format is very different the original implementation. Tensors contain image ids, ground truth ids and anchor ids as values to support variable length data.
- There are differences in terms of the algorithm itself:
- When computing a dynamic k for a ground truth, in the original implementation they consider the sum of top 10 predictions sorted by ious among the initial foregrounds of any ground truth in the image, while in our implementation we consider only the initial foreground of that particular ground truth. To compensate for that difference we introduce the dynamic_ks_bias hyperparamter which makes the dynamic ks larger.
- When computing the k matched detections for a ground truth, in the original implementation they consider the initial foregrounds of any ground truth in the image as candidates, while in our implementation we consider only the initial foreground of that particular ground truth as candidates. We believe that this difference is minor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dynamic_ks_bias |
hyperparameter to compensate for the discrepancies between the regular loss and this loss. |
1.1
|
|
sync_num_fgs |
sync num of fgs. Can be used for DDP training. |
False
|
|
obj_loss_fix |
devide by total of num anchors instead num of matching fgs. Can be used for objectness loss. |
False
|
Source code in V3_4/src/super_gradients/training/losses/yolox_loss.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
|