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: int = 80,
loss_gain: Optional[Dict[str, float]] = None,
aux_loss: bool = True,
use_fl: bool = True,
use_vfl: bool = False,
use_uni_match: bool = False,
uni_match_ind: int = 0,
gamma: float = 1.5,
alpha: float = 0.25,
)
Bases: Module
DETR (DEtection TRansformer) Loss class for calculating various loss components.
This class computes classification loss, bounding box loss, GIoU loss, and optionally auxiliary losses for the DETR object detection model.
Attributes:
Name | Type | Description |
---|---|---|
nc |
int
|
Number of classes. |
loss_gain |
Dict[str, float]
|
Coefficients for different loss components. |
aux_loss |
bool
|
Whether to compute auxiliary losses. |
use_fl |
bool
|
Whether to use FocalLoss. |
use_vfl |
bool
|
Whether to use VarifocalLoss. |
use_uni_match |
bool
|
Whether to use a fixed layer for auxiliary branch label assignment. |
uni_match_ind |
int
|
Index of fixed layer to use if use_uni_match is True. |
matcher |
HungarianMatcher
|
Object to compute matching cost and indices. |
fl |
FocalLoss | None
|
Focal Loss object if use_fl is True, otherwise None. |
vfl |
VarifocalLoss | None
|
Varifocal Loss object if use_vfl is True, otherwise None. |
device |
device
|
Device on which tensors are stored. |
Uses default loss_gain if not provided. Initializes HungarianMatcher with preset cost gains. Supports auxiliary losses and various loss types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nc
|
int
|
Number of classes. |
80
|
loss_gain
|
Dict[str, float]
|
Coefficients for different loss components. |
None
|
aux_loss
|
bool
|
Whether to use auxiliary losses from each decoder layer. |
True
|
use_fl
|
bool
|
Whether to use FocalLoss. |
True
|
use_vfl
|
bool
|
Whether to use VarifocalLoss. |
False
|
use_uni_match
|
bool
|
Whether to use fixed layer for auxiliary branch label assignment. |
False
|
uni_match_ind
|
int
|
Index of fixed layer for uni_match. |
0
|
gamma
|
float
|
The focusing parameter that controls how much the loss focuses on hard-to-classify examples. |
1.5
|
alpha
|
float
|
The balancing factor used to address class imbalance. |
0.25
|
Source code in ultralytics/models/utils/loss.py
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 |
|
forward
forward(
pred_bboxes: Tensor,
pred_scores: Tensor,
batch: Dict[str, Any],
postfix: str = "",
**kwargs: Any
) -> Dict[str, torch.Tensor]
Calculate loss for predicted bounding boxes and scores.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred_bboxes
|
Tensor
|
Predicted bounding boxes, shape (L, B, N, 4). |
required |
pred_scores
|
Tensor
|
Predicted class scores, shape (L, B, N, C). |
required |
batch
|
Dict[str, Any]
|
Batch information containing cls, bboxes, and gt_groups. |
required |
postfix
|
str
|
Postfix for loss names. |
''
|
**kwargs
|
Any
|
Additional arguments, may include 'match_indices'. |
{}
|
Returns:
Type | Description |
---|---|
Dict[str, Tensor]
|
Computed losses, including main and auxiliary (if enabled). |
Notes
Uses last elements of pred_bboxes and pred_scores for main loss, and the rest for auxiliary losses if self.aux_loss is True.
Source code in ultralytics/models/utils/loss.py
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 |
|
ultralytics.models.utils.loss.RTDETRDetectionLoss
RTDETRDetectionLoss(
nc: int = 80,
loss_gain: Optional[Dict[str, float]] = None,
aux_loss: bool = True,
use_fl: bool = True,
use_vfl: bool = False,
use_uni_match: bool = False,
uni_match_ind: int = 0,
gamma: float = 1.5,
alpha: float = 0.25,
)
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.
Source code in ultralytics/models/utils/loss.py
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 |
|
forward
forward(
preds: Tuple[Tensor, Tensor],
batch: Dict[str, Any],
dn_bboxes: Optional[Tensor] = None,
dn_scores: Optional[Tensor] = None,
dn_meta: Optional[Dict[str, Any]] = None,
) -> Dict[str, torch.Tensor]
Forward pass to compute detection loss with optional denoising loss.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
preds
|
Tuple[Tensor, Tensor]
|
Tuple containing predicted bounding boxes and scores. |
required |
batch
|
Dict[str, Any]
|
Batch data containing ground truth information. |
required |
dn_bboxes
|
Tensor
|
Denoising bounding boxes. |
None
|
dn_scores
|
Tensor
|
Denoising scores. |
None
|
dn_meta
|
Dict[str, Any]
|
Metadata for denoising. |
None
|
Returns:
Type | Description |
---|---|
Dict[str, Tensor]
|
Dictionary containing total loss and denoising loss if applicable. |
Source code in ultralytics/models/utils/loss.py
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 |
|
get_dn_match_indices
staticmethod
get_dn_match_indices(
dn_pos_idx: List[Tensor], dn_num_group: int, gt_groups: List[int]
) -> List[Tuple[torch.Tensor, torch.Tensor]]
Get 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 number of ground truths per image. |
required |
Returns:
Type | Description |
---|---|
List[Tuple[Tensor, Tensor]]
|
List of tuples containing matched indices for denoising. |
Source code in ultralytics/models/utils/loss.py
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 |
|