Reference for ultralytics/utils/loss.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/loss.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.loss.VarifocalLoss
Bases: Module
Varifocal loss by Zhang et al.
https://arxiv.org/abs/2008.13367.
Source code in ultralytics/utils/loss.py
forward
staticmethod
Computes varfocal loss.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.FocalLoss
Bases: Module
Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5).
Source code in ultralytics/utils/loss.py
forward
staticmethod
Calculates and updates confusion matrix for object detection/classification tasks.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.DFLoss
Bases: Module
Criterion class for computing DFL losses during training.
Source code in ultralytics/utils/loss.py
__call__
Return sum of left and right DFL losses.
Distribution Focal Loss (DFL) proposed in Generalized Focal Loss https://ieeexplore.ieee.org/document/9792391
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.BboxLoss
Bases: Module
Criterion class for computing training losses during training.
Source code in ultralytics/utils/loss.py
forward
forward(
pred_dist,
pred_bboxes,
anchor_points,
target_bboxes,
target_scores,
target_scores_sum,
fg_mask,
)
IoU loss.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.RotatedBboxLoss
Bases: BboxLoss
Criterion class for computing training losses during training.
Source code in ultralytics/utils/loss.py
forward
forward(
pred_dist,
pred_bboxes,
anchor_points,
target_bboxes,
target_scores,
target_scores_sum,
fg_mask,
)
IoU loss.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.KeypointLoss
Bases: Module
Criterion class for computing training losses.
Source code in ultralytics/utils/loss.py
forward
Calculates keypoint loss factor and Euclidean distance loss for predicted and actual keypoints.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.v8DetectionLoss
Criterion class for computing training losses.
Source code in ultralytics/utils/loss.py
__call__
Calculate the sum of the loss for box, cls and dfl multiplied by batch size.
Source code in ultralytics/utils/loss.py
bbox_decode
Decode predicted object bounding box coordinates from anchor points and distribution.
Source code in ultralytics/utils/loss.py
preprocess
Preprocesses the target counts and matches with the input batch size to output a tensor.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.v8SegmentationLoss
Bases: v8DetectionLoss
Criterion class for computing training losses.
Source code in ultralytics/utils/loss.py
__call__
Calculate and return the loss for the YOLO model.
Source code in ultralytics/utils/loss.py
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 |
|
calculate_segmentation_loss
calculate_segmentation_loss(
fg_mask: torch.Tensor,
masks: torch.Tensor,
target_gt_idx: torch.Tensor,
target_bboxes: torch.Tensor,
batch_idx: torch.Tensor,
proto: torch.Tensor,
pred_masks: torch.Tensor,
imgsz: torch.Tensor,
overlap: bool,
) -> torch.Tensor
Calculate the loss for instance segmentation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fg_mask
|
Tensor
|
A binary tensor of shape (BS, N_anchors) indicating which anchors are positive. |
required |
masks
|
Tensor
|
Ground truth masks of shape (BS, H, W) if |
required |
target_gt_idx
|
Tensor
|
Indexes of ground truth objects for each anchor of shape (BS, N_anchors). |
required |
target_bboxes
|
Tensor
|
Ground truth bounding boxes for each anchor of shape (BS, N_anchors, 4). |
required |
batch_idx
|
Tensor
|
Batch indices of shape (N_labels_in_batch, 1). |
required |
proto
|
Tensor
|
Prototype masks of shape (BS, 32, H, W). |
required |
pred_masks
|
Tensor
|
Predicted masks for each anchor of shape (BS, N_anchors, 32). |
required |
imgsz
|
Tensor
|
Size of the input image as a tensor of shape (2), i.e., (H, W). |
required |
overlap
|
bool
|
Whether the masks in |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The calculated loss for instance segmentation. |
Notes
The batch loss can be computed for improved speed at higher memory usage. For example, pred_mask can be computed as follows: pred_mask = torch.einsum('in,nhw->ihw', pred, proto) # (i, 32) @ (32, 160, 160) -> (i, 160, 160)
Source code in ultralytics/utils/loss.py
single_mask_loss
staticmethod
single_mask_loss(
gt_mask: torch.Tensor,
pred: torch.Tensor,
proto: torch.Tensor,
xyxy: torch.Tensor,
area: torch.Tensor,
) -> torch.Tensor
Compute the instance segmentation loss for a single image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gt_mask
|
Tensor
|
Ground truth mask of shape (n, H, W), where n is the number of objects. |
required |
pred
|
Tensor
|
Predicted mask coefficients of shape (n, 32). |
required |
proto
|
Tensor
|
Prototype masks of shape (32, H, W). |
required |
xyxy
|
Tensor
|
Ground truth bounding boxes in xyxy format, normalized to [0, 1], of shape (n, 4). |
required |
area
|
Tensor
|
Area of each ground truth bounding box of shape (n,). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The calculated mask loss for a single image. |
Notes
The function uses the equation pred_mask = torch.einsum('in,nhw->ihw', pred, proto) to produce the predicted masks from the prototype masks and predicted mask coefficients.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.v8PoseLoss
Bases: v8DetectionLoss
Criterion class for computing training losses.
Source code in ultralytics/utils/loss.py
__call__
Calculate the total loss and detach it.
Source code in ultralytics/utils/loss.py
calculate_keypoints_loss
calculate_keypoints_loss(
masks,
target_gt_idx,
keypoints,
batch_idx,
stride_tensor,
target_bboxes,
pred_kpts,
)
Calculate the keypoints loss for the model.
This function calculates the keypoints loss and keypoints object loss for a given batch. The keypoints loss is based on the difference between the predicted keypoints and ground truth keypoints. The keypoints object loss is a binary classification loss that classifies whether a keypoint is present or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks
|
Tensor
|
Binary mask tensor indicating object presence, shape (BS, N_anchors). |
required |
target_gt_idx
|
Tensor
|
Index tensor mapping anchors to ground truth objects, shape (BS, N_anchors). |
required |
keypoints
|
Tensor
|
Ground truth keypoints, shape (N_kpts_in_batch, N_kpts_per_object, kpts_dim). |
required |
batch_idx
|
Tensor
|
Batch index tensor for keypoints, shape (N_kpts_in_batch, 1). |
required |
stride_tensor
|
Tensor
|
Stride tensor for anchors, shape (N_anchors, 1). |
required |
target_bboxes
|
Tensor
|
Ground truth boxes in (x1, y1, x2, y2) format, shape (BS, N_anchors, 4). |
required |
pred_kpts
|
Tensor
|
Predicted keypoints, shape (BS, N_anchors, N_kpts_per_object, kpts_dim). |
required |
Returns:
Name | Type | Description |
---|---|---|
kpts_loss |
Tensor
|
The keypoints loss. |
kpts_obj_loss |
Tensor
|
The keypoints object loss. |
Source code in ultralytics/utils/loss.py
kpts_decode
staticmethod
Decodes predicted keypoints to image coordinates.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.v8ClassificationLoss
Criterion class for computing training losses.
__call__
Compute the classification loss between predictions and true labels.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.v8OBBLoss
Bases: v8DetectionLoss
Calculates losses for object detection, classification, and box distribution in rotated YOLO models.
Source code in ultralytics/utils/loss.py
__call__
Calculate and return the loss for the YOLO model.
Source code in ultralytics/utils/loss.py
bbox_decode
Decode predicted object bounding box coordinates from anchor points and distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
anchor_points
|
Tensor
|
Anchor points, (h*w, 2). |
required |
pred_dist
|
Tensor
|
Predicted rotated distance, (bs, h*w, 4). |
required |
pred_angle
|
Tensor
|
Predicted angle, (bs, h*w, 1). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Predicted rotated bounding boxes with angles, (bs, h*w, 5). |
Source code in ultralytics/utils/loss.py
preprocess
Preprocesses the target counts and matches with the input batch size to output a tensor.
Source code in ultralytics/utils/loss.py
ultralytics.utils.loss.E2EDetectLoss
Criterion class for computing training losses.
Source code in ultralytics/utils/loss.py
__call__
Calculate the sum of the loss for box, cls and dfl multiplied by batch size.