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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 |
|
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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/src/super_gradients/training/losses/kd_losses.py
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 |
|
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., LabelSmoothingCrossEntropyLoss |
required |
distillation_loss_fn |
_Loss
|
distillation loss. E.g., KLDivLoss |
KDklDivLoss()
|
distillation_loss_coeff |
float
|
0.5
|
Source code in latest/src/super_gradients/training/losses/kd_losses.py
22 23 24 25 26 27 28 29 30 31 32 |
|
KDklDivLoss
Bases: KLDivLoss
KL divergence wrapper for knowledge distillation
Source code in latest/src/super_gradients/training/losses/kd_losses.py
8 9 10 11 12 13 14 15 |
|
LabelSmoothingCrossEntropyLoss
Bases: nn.CrossEntropyLoss
CrossEntropyLoss - with ability to recieve distrbution as targets, and optional label smoothing
Source code in latest/src/super_gradients/training/losses/label_smoothing_cross_entropy_loss.py
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 |
|
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 latest/src/super_gradients/training/losses/label_smoothing_cross_entropy_loss.py
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 |
|
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 latest/src/super_gradients/training/losses/label_smoothing_cross_entropy_loss.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
MaskAttentionLoss
Bases: _Loss
Pixel mask attention loss. For semantic segmentation usages with 4D tensors.
Source code in latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 |
|
__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 latest/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 |
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 latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
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 latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
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 latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
PPYoloELoss
Bases: nn.Module
Source code in latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
__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)
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
|
static_assigner_epoch |
Whether to use static assigner or Task-Aligned assigner |
required | |
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
|
Source code in latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
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 |
---|---|
Mapping[str, Tensor]
|
Source code in latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
TaskAlignedAssigner
Bases: nn.Module
TOOD: Task-aligned One-stage Object Detection
Source code in latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
__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 latest/src/super_gradients/training/losses/ppyolo_loss.py
433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
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 |
Tensor
|
Tensor (float32): 1 means bbox, 0 means no bbox, shape(B, n, 1) |
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 latest/src/super_gradients/training/losses/ppyolo_loss.py
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 |
|
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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 kd_loss
i.e
ChannelWiseKnowledgeDistillationLoss
.
Source code in latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 latest/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 |
|
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 latest/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 |
|