super_gradients.training.losses package

Submodules

super_gradients.training.losses.all_losses module

super_gradients.training.losses.ddrnet_loss module

class super_gradients.training.losses.ddrnet_loss.DDRNetLoss(threshold: float = 0.7, ohem_percentage: float = 0.1, weights: list = [1.0, 0.4], ignore_label=255, num_pixels_exclude_ignored: bool = False)[source]

Bases: super_gradients.training.losses.ohem_ce_loss.OhemCELoss

forward(predictions_list: Union[list, tuple, torch.Tensor], targets: torch.Tensor)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str

super_gradients.training.losses.focal_loss module

class super_gradients.training.losses.focal_loss.FocalLoss(loss_fcn: torch.nn.modules.loss.BCEWithLogitsLoss, gamma=1.5, alpha=0.25)[source]

Bases: torch.nn.modules.loss._Loss

Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)

reduction: str
forward(pred, true)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

super_gradients.training.losses.label_smoothing_cross_entropy_loss module

super_gradients.training.losses.label_smoothing_cross_entropy_loss.onehot(indexes, N=None, ignore_index=None)[source]

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

super_gradients.training.losses.label_smoothing_cross_entropy_loss.cross_entropy(inputs, target, weight=None, ignore_index=- 100, reduction='mean', smooth_eps=None, smooth_dist=None, from_logits=True)[source]

cross entropy loss, with support for target distributions and label smoothing https://arxiv.org/abs/1512.00567

class super_gradients.training.losses.label_smoothing_cross_entropy_loss.LabelSmoothingCrossEntropyLoss(weight=None, ignore_index=- 100, reduction='mean', smooth_eps=None, smooth_dist=None, from_logits=True)[source]

Bases: torch.nn.modules.loss.CrossEntropyLoss

CrossEntropyLoss - with ability to recieve distrbution as targets, and optional label smoothing

forward(input, target, smooth_dist=None)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

ignore_index: int
label_smoothing: float

super_gradients.training.losses.ohem_ce_loss module

class super_gradients.training.losses.ohem_ce_loss.OhemLoss(threshold: float, mining_percent: float = 0.1, ignore_lb: int = - 100, num_pixels_exclude_ignored: bool = True, criteria: Optional[torch.nn.modules.loss._Loss] = None)[source]

Bases: torch.nn.modules.loss._Loss

OhemLoss - Online Hard Example Mining Cross Entropy Loss

forward(logits, labels)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str
class super_gradients.training.losses.ohem_ce_loss.OhemCELoss(threshold: float, mining_percent: float = 0.1, ignore_lb: int = - 100, num_pixels_exclude_ignored: bool = True)[source]

Bases: super_gradients.training.losses.ohem_ce_loss.OhemLoss

OhemLoss - Online Hard Example Mining Cross Entropy Loss

reduction: str
training: bool
class super_gradients.training.losses.ohem_ce_loss.OhemBCELoss(threshold: float, mining_percent: float = 0.1, ignore_lb: int = - 100, num_pixels_exclude_ignored: bool = True)[source]

Bases: super_gradients.training.losses.ohem_ce_loss.OhemLoss

OhemBCELoss - Online Hard Example Mining Binary Cross Entropy Loss

forward(logits, labels)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str
training: bool

super_gradients.training.losses.r_squared_loss module

class super_gradients.training.losses.r_squared_loss.RSquaredLoss(size_average=None, reduce=None, reduction: str = 'mean')[source]

Bases: torch.nn.modules.loss._Loss

forward(output, target)[source]

Computes the R-squared for the output and target values :param output: Tensor / Numpy / List

The prediction

Parameters

target – Tensor / Numpy / List The corresponding lables

reduction: str

super_gradients.training.losses.shelfnet_ohem_loss module

class super_gradients.training.losses.shelfnet_ohem_loss.ShelfNetOHEMLoss(threshold: float = 0.7, mining_percent: float = 0.0001, ignore_lb: int = 255)[source]

Bases: super_gradients.training.losses.ohem_ce_loss.OhemCELoss

forward(predictions_list: list, targets)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str
training: bool

super_gradients.training.losses.shelfnet_semantic_encoding_loss module

class super_gradients.training.losses.shelfnet_semantic_encoding_loss.ShelfNetSemanticEncodingLoss(se_weight=0.2, nclass=21, aux_weight=0.4, weight=None, ignore_index=- 1)[source]

Bases: torch.nn.modules.loss.CrossEntropyLoss

2D Cross Entropy Loss with Auxilary Loss

forward(logits, labels)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

ignore_index: int
label_smoothing: float

super_gradients.training.losses.ssd_loss module

class super_gradients.training.losses.ssd_loss.HardMiningCrossEntropyLoss(neg_pos_ratio: float)[source]

Bases: torch.nn.modules.loss._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)

forward(pred_labels, target_labels)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str
class super_gradients.training.losses.ssd_loss.SSDLoss(dboxes: super_gradients.training.utils.ssd_utils.DefaultBoxes, alpha: float = 1.0, iou_thresh: float = 0.5, neg_pos_ratio: float = 3.0)[source]

Bases: torch.nn.modules.loss._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]

match_dboxes(targets)[source]

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

targets – a tensor containing the boxes for a single image; shape [num_boxes, 6] (image_id, label, x, y, w, h)

Returns

two tensors boxes - shape of dboxes [4, num_dboxes] (x,y,w,h) labels - sahpe [num_dboxes]

forward(predictions: Tuple, targets)[source]
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)

reduction: str

super_gradients.training.losses.yolo_v3_loss module

super_gradients.training.losses.yolo_v5_loss module

Module contents

class super_gradients.training.losses.FocalLoss(loss_fcn: torch.nn.modules.loss.BCEWithLogitsLoss, gamma=1.5, alpha=0.25)[source]

Bases: torch.nn.modules.loss._Loss

Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)

reduction: str
forward(pred, true)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class super_gradients.training.losses.LabelSmoothingCrossEntropyLoss(weight=None, ignore_index=- 100, reduction='mean', smooth_eps=None, smooth_dist=None, from_logits=True)[source]

Bases: torch.nn.modules.loss.CrossEntropyLoss

CrossEntropyLoss - with ability to recieve distrbution as targets, and optional label smoothing

forward(input, target, smooth_dist=None)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

ignore_index: int
label_smoothing: float
weight: Optional[Tensor]
reduction: str
training: bool
class super_gradients.training.losses.ShelfNetOHEMLoss(threshold: float = 0.7, mining_percent: float = 0.0001, ignore_lb: int = 255)[source]

Bases: super_gradients.training.losses.ohem_ce_loss.OhemCELoss

forward(predictions_list: list, targets)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str
training: bool
class super_gradients.training.losses.ShelfNetSemanticEncodingLoss(se_weight=0.2, nclass=21, aux_weight=0.4, weight=None, ignore_index=- 1)[source]

Bases: torch.nn.modules.loss.CrossEntropyLoss

2D Cross Entropy Loss with Auxilary Loss

forward(logits, labels)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

ignore_index: int
label_smoothing: float
weight: Optional[Tensor]
reduction: str
training: bool
class super_gradients.training.losses.YoloXDetectionLoss(strides: list, num_classes: int, use_l1: bool = False, center_sampling_radius: float = 2.5, iou_type='iou')[source]

Bases: torch.nn.modules.loss._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

strides

list: List of Yolo levels output grid sizes (i.e [8, 16, 32]).

num_classes

int: Number of classes.

use_l1

bool: Controls the L_l1 Coef as discussed above (default=False).

center_sampling_radius

float: Sampling radius used for center sampling when creating the fg mask (default=2.5).

iou_type

str: Iou loss type, one of [“iou”,”giou”] (deafult=”iou”).

forward(model_output: Union[list, Tuple[torch.Tensor, List]], targets: torch.Tensor)[source]
Parameters
  • model_output

    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)

  • targets – torch.Tensor: Num_targets x (4 + 2)], values on dim 1 are: image id in a batch, class, box x y w h

Returns

loss, all losses separately in a detached tensor

prepare_predictions(predictions: List[torch.Tensor])Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor][source]

Convert raw outputs of the network into a format that merges outputs from all levels :param predictions: output from all Yolo levels, each of shape

[Batch x 1 x GridSizeY x GridSizeX x (4 + 1 + Num_classes)]

Returns

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

get_l1_target(l1_target, gt, stride, x_shifts, y_shifts, eps=1e-08)[source]
Parameters
  • l1_target – tensor of zeros of shape [Num_cell_gt_pairs x 4]

  • gt – targets in coordinates [Num_cell_gt_pairs x (4 + 1 + num_classes)]

Returns

targets in the format corresponding to logits

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)[source]
Match cells to ground truth:
  • at most 1 GT per cell

  • dynamic number of cells per GT

Parameters
  • outside_boxes_and_center_cost_coeff – float: Cost coefficiant of cells the radius and bbox of gts in dynamic matching (default=100000).

  • ious_loss_cost_coeff – float: Cost coefficiant for iou loss in dynamic matching (default=3).

  • image_idx – int: Image index in batch.

  • num_gt – int: Number of ground trunth targets in the image.

  • total_num_anchors – int: Total number of possible bboxes = sum of all grid cells.

  • gt_bboxes_per_image – torch.Tensor: Tensor of gt bboxes for the image, shape: (num_gt, 4).

  • gt_classes – torch.Tesnor: Tensor of the classes in the image, shape: (num_preds,4).

  • bboxes_preds_per_image – Tensor of the classes in the image, shape: (num_preds).

  • expanded_strides – torch.Tensor: Stride of the output grid the prediction is coming from, shape (1 x num_cells x 1).

  • x_shifts – torch.Tensor: X’s in cell coordinates, shape (1,num_cells,1).

  • y_shifts – torch.Tensor: Y’s in cell coordinates, shape (1,num_cells,1).

  • cls_preds – torch.Tensor: Class predictions in all cells, shape (batch_size, num_cells).

  • obj_preds – torch.Tensor: Objectness predictions in all cells, shape (batch_size, num_cells).

  • mode – str: One of [“gpu”,”cpu”], Controls the device the assignment operation should be taken place on (deafult=”gpu”)

get_in_boxes_info(gt_bboxes_per_image, expanded_strides, x_shifts, y_shifts, total_num_anchors, num_gt)[source]
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
  • num_gt – int: Number of ground trunth targets in the image.

  • total_num_anchors – int: Sum of all grid cells.

  • gt_bboxes_per_image – torch.Tensor: Tensor of gt bboxes for the image, shape: (num_gt, 4).

  • expanded_strides – torch.Tensor: Stride of the output grid the prediction is coming from, shape (1 x num_cells x 1).

  • x_shifts – torch.Tensor: X’s in cell coordinates, shape (1,num_cells,1).

  • y_shifts – torch.Tensor: Y’s in cell coordinates, shape (1,num_cells,1).

:return is_in_boxes_anchor, is_in_boxes_and_center
where:
  • is_in_boxes_anchor masks the cells that their cell center is inside a gt bbox and within

    self.center_sampling_radius cells away, without reduction (i.e shape=(num_gts, num_fgs))

  • is_in_boxes_and_center masks the cells that their center is either inside a gt bbox or within

    self.center_sampling_radius cells away, shape (num_fgs)

dynamic_k_matching(cost, pair_wise_ious, gt_classes, num_gt, fg_mask)[source]
Parameters
  • cost – pairwise cost, [num_FGs x num_GTs]

  • pair_wise_ious – pairwise IoUs, [num_FGs x num_GTs]

  • gt_classes – class of each GT

  • num_gt – number of GTs

:return num_fg, (number of foregrounds)

gt_matched_classes, (the classes that have been matched with fgs) pred_ious_this_matching matched_gt_inds

reduction: str
class super_gradients.training.losses.RSquaredLoss(size_average=None, reduce=None, reduction: str = 'mean')[source]

Bases: torch.nn.modules.loss._Loss

forward(output, target)[source]

Computes the R-squared for the output and target values :param output: Tensor / Numpy / List

The prediction

Parameters

target – Tensor / Numpy / List The corresponding lables

reduction: str
training: bool
class super_gradients.training.losses.SSDLoss(dboxes: super_gradients.training.utils.ssd_utils.DefaultBoxes, alpha: float = 1.0, iou_thresh: float = 0.5, neg_pos_ratio: float = 3.0)[source]

Bases: torch.nn.modules.loss._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]

match_dboxes(targets)[source]

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

targets – a tensor containing the boxes for a single image; shape [num_boxes, 6] (image_id, label, x, y, w, h)

Returns

two tensors boxes - shape of dboxes [4, num_dboxes] (x,y,w,h) labels - sahpe [num_dboxes]

forward(predictions: Tuple, targets)[source]
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)

reduction: str
training: bool
class super_gradients.training.losses.BCEDiceLoss(loss_weights=[0.5, 0.5], logits=True)[source]

Bases: torch.nn.modules.module.Module

Binary Cross Entropy + Dice Loss

Weighted average of BCE and Dice loss

loss_weights

list of size 2 s.t loss_weights[0], loss_weights[1] are the weights for BCE, Dice

respectively.
forward(input: torch.Tensor, target: torch.Tensor)torch.Tensor[source]

@param input: Network’s raw output shaped (N,1,H,W) @param target: Ground truth shaped (N,H,W)

training: bool
class super_gradients.training.losses.KDLogitsLoss(task_loss_fn: torch.nn.modules.loss._Loss, distillation_loss_fn: torch.nn.modules.loss._Loss = KDklDivLoss(), distillation_loss_coeff: float = 0.5)[source]

Bases: torch.nn.modules.loss._Loss

Knowledge distillation loss, wraps the task loss and distillation loss

forward(kd_module_output, target)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

reduction: str
class super_gradients.training.losses.DiceCEEdgeLoss(num_classes: int, num_aux_heads: int = 2, num_detail_heads: int = 1, weights: Union[tuple, list] = (1, 1, 1, 1), dice_ce_weights: Union[tuple, list] = (1, 1), ignore_index: int = - 100, edge_kernel: int = 3, ce_edge_weights: Union[tuple, list] = (0.5, 0.5))[source]

Bases: torch.nn.modules.loss._Loss

forward(preds: Tuple[torch.Tensor], target: torch.Tensor)[source]
Parameters

preds – 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]

reduction: str