Reference for ultralytics/models/utils/loss.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/utils/loss.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.utils.loss.DETRLoss
DETRLoss(nc=80, loss_gain=None, aux_loss=True, use_fl=True, use_vfl=False, use_uni_match=False, uni_match_ind=0)
Bases: Module
DETR (DEtection TRansformer) Loss class. This class calculates and returns the different loss components for the DETR object detection model. It computes classification loss, bounding box loss, GIoU loss, and optionally auxiliary losses.
Attributes:
Name | Type | Description |
---|---|---|
nc |
int
|
The number of classes. |
loss_gain |
dict
|
Coefficients for different loss components. |
aux_loss |
bool
|
Whether to compute auxiliary losses. |
use_fl |
bool
|
Use FocalLoss or not. |
use_vfl |
bool
|
Use VarifocalLoss or not. |
use_uni_match |
bool
|
Whether to use a fixed layer to assign labels for the auxiliary branch. |
uni_match_ind |
int
|
The fixed indices of a layer to use if |
matcher |
HungarianMatcher
|
Object to compute matching cost and indices. |
fl |
FocalLoss or None
|
Focal Loss object if |
vfl |
VarifocalLoss or None
|
Varifocal Loss object if |
device |
device
|
Device on which tensors are stored. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nc |
int
|
The number of classes. |
80
|
loss_gain |
dict
|
The coefficient of loss. |
None
|
aux_loss |
bool
|
If 'aux_loss = True', loss at each decoder layer are to be used. |
True
|
use_vfl |
bool
|
Use VarifocalLoss or not. |
False
|
use_uni_match |
bool
|
Whether to use a fixed layer to assign labels for auxiliary branch. |
False
|
uni_match_ind |
int
|
The fixed indices of a layer. |
0
|
Source code in ultralytics/models/utils/loss.py
forward
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_bboxes |
Tensor
|
[l, b, query, 4] |
required |
pred_scores |
Tensor
|
[l, b, query, num_classes] |
required |
batch |
dict
|
A dict includes: gt_cls (torch.Tensor) with shape [num_gts, ], gt_bboxes (torch.Tensor): [num_gts, 4], gt_groups (List(int)): a list of batch size length includes the number of gts of each image. |
required |
postfix |
str
|
postfix of loss name. |
''
|
Source code in ultralytics/models/utils/loss.py
ultralytics.models.utils.loss.RTDETRDetectionLoss
RTDETRDetectionLoss(nc=80, loss_gain=None, aux_loss=True, use_fl=True, use_vfl=False, use_uni_match=False, uni_match_ind=0)
Bases: DETRLoss
Real-Time DeepTracker (RT-DETR) Detection Loss class that extends the DETRLoss.
This class computes the detection loss for the RT-DETR model, which includes the standard detection loss as well as an additional denoising training loss when provided with denoising metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nc |
int
|
The number of classes. |
80
|
loss_gain |
dict
|
The coefficient of loss. |
None
|
aux_loss |
bool
|
If 'aux_loss = True', loss at each decoder layer are to be used. |
True
|
use_vfl |
bool
|
Use VarifocalLoss or not. |
False
|
use_uni_match |
bool
|
Whether to use a fixed layer to assign labels for auxiliary branch. |
False
|
uni_match_ind |
int
|
The fixed indices of a layer. |
0
|
Source code in ultralytics/models/utils/loss.py
forward
Forward pass to compute the detection loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds |
tuple
|
Predicted bounding boxes and scores. |
required |
batch |
dict
|
Batch data containing ground truth information. |
required |
dn_bboxes |
Tensor
|
Denoising bounding boxes. Default is None. |
None
|
dn_scores |
Tensor
|
Denoising scores. Default is None. |
None
|
dn_meta |
dict
|
Metadata for denoising. Default is None. |
None
|
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing the total loss and, if applicable, the denoising loss. |
Source code in ultralytics/models/utils/loss.py
get_dn_match_indices
staticmethod
Get the match indices for denoising.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dn_pos_idx |
List[Tensor]
|
List of tensors containing positive indices for denoising. |
required |
dn_num_group |
int
|
Number of denoising groups. |
required |
gt_groups |
List[int]
|
List of integers representing the number of ground truths for each image. |
required |
Returns:
Type | Description |
---|---|
List[tuple]
|
List of tuples containing matched indices for denoising. |