TaskAlignedDetectionLoss<T>

Models & Types · AiDotNet.ComputerVision.Detection.Losses

Task-aligned assignment with BCE, CIoU and distribution focal losses for anchor-free YOLO heads.

For Beginners

This is the training objective of YOLOv8-style detectors. It picks the grid cells best placed to detect each object, teaches their class scores to reflect how well they detect it, and pulls their predicted box edges toward the real ones.

How It Works

Assignment (Feng et al. 2021, TOOD): an anchor point is a candidate for an object when it lies inside the object’s box. Candidates are ranked by t = s^alpha * IoU^beta, where s is the predicted score of the object’s class and IoU is between the anchor’s predicted box and the object. The top-k candidates become positives; an anchor selected by several objects keeps the one with the highest IoU. Each positive’s classification target is t normalized so that, per object, the largest target equals the largest IoU among that object’s positives. Assignment uses detached predictions.

Losses: BCE of every class logit against those targets; CIoU of each positive’s decoded box; and the distribution focal loss of Li et al. (2020), DFL = -((y_{i+1} - y) log S_i + (y - y_i) log S_{i+1}), averaged over the four box sides. Box and DFL terms are weighted per positive by its target. All three sums are divided by the total target mass (at least 1) and scaled by the configured gains. Boxes and DFL distances are measured in units of each level’s stride, as the head predicts them.

Constructors

Constructor Summary
TaskAlignedDetectionLoss(int numClasses, int regMax, TaskAlignedLossOptions options) Creates the objective for a head with the given class count and distribution bins.

Properties

Property Summary
int OneToOneTopK { get; } Anchors selected per object by a one-to-one head.
int TopK { get; } Anchors selected per object by the one-to-many assignment.

Methods

Method Summary
T CalculateLoss(IReadOnlyList<Tensor<T>> classLevels, IReadOnlyList<Tensor<T>> distributionLevels, IReadOnlyList<int> strides, int imageHeight, int imageWidth, DetectionTrainingBatch<T> targets, int topK) Evaluates the objective without recording gradients.
Tensor<T> ComputeTapeLoss(IReadOnlyList<Tensor<T>> classLevels, IReadOnlyList<Tensor<T>> distributionLevels, IReadOnlyList<int> strides, int imageHeight, int imageWidth, DetectionTrainingBatch<T> targets, int topK) Builds the differentiable objective over the live head outputs.