py123d_detection.torch.models.common.loss

The set-prediction loss both detectors train against.

A detection head emits a fixed number of queries, 900 in the shipped configs, and a frame holds a handful of objects. HungarianMatcher decides which query is responsible for which box, by solving the assignment that minimises a cost of focal classification plus L1 on the normalised box. SetCriterion then scores that assignment: focal loss over all queries, with the unmatched ones as background, and L1 over the matched ones only.

The matching is what makes the loss order-invariant, and also what makes it easy to get subtly wrong. A matcher that pairs the wrong query with the wrong box still produces a loss that falls, just to a worse place, so tests/test_loss.py pins the matching itself rather than only the number that comes out.

Every decoder layer is supervised, not just the last. The intermediate losses are keyed d0., d1., … and the final one carries no prefix, which is the convention the PETR and BEVFormer configs use.

Under DDP the box count the loss normalises by is all-reduced across ranks, so each rank divides by the same number and the gradients agree. That reduction has to happen before any per-dataset split of the batch, because a split that differs between ranks would leave them reducing over a different number of groups and the job would hang instead of failing.

Classes

HungarianMatcher([cost_class, cost_bbox, ...])

SetCriterion(*args, **kwargs)

class py123d_detection.torch.models.common.loss.HungarianMatcher(cost_class=2.0, cost_bbox=0.25, pc_range=None, debug_nan_checks=False)[source]

Bases: Module

forward(outputs, targets, precomputed_norm_bboxes=None)
class py123d_detection.torch.models.common.loss.SetCriterion(*args: Any, **kwargs: Any)[source]

Bases: Module

get_layer_loss(cls_scores, bbox_preds, targets, indices, num_boxes, precomputed_norm_bboxes=None)[source]
forward(outputs, targets, sync=True, num_boxes_synced=None)[source]