Class DETRSetLoss<T>
- Namespace
- AiDotNet.ComputerVision.Detection.Losses
- Assembly
- AiDotNet.dll
DETR Set Prediction Loss with Hungarian Matching for end-to-end object detection.
public class DETRSetLoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
DETRSetLoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Unlike traditional detectors that use anchors and NMS, DETR treats detection as a set prediction problem. It uses Hungarian matching to find the optimal assignment between predicted and ground truth boxes, then computes loss on the matched pairs.
The loss has three components: - Classification loss: Cross-entropy for class predictions - Box loss: L1 loss for box coordinates - GIoU loss: For better box regression
Reference: Carion et al., "End-to-End Object Detection with Transformers", ECCV 2020
Constructors
DETRSetLoss(int, double, double, double)
Creates a new DETR set loss instance.
public DETRSetLoss(int numClasses = 91, double classWeight = 1, double boxL1Weight = 5, double boxGIoUWeight = 2)
Parameters
numClassesintNumber of object classes (including no-object class).
classWeightdoubleWeight for classification loss.
boxL1WeightdoubleWeight for L1 box loss.
boxGIoUWeightdoubleWeight for GIoU box loss.
Methods
CalculateDerivative(Tensor<T>, Tensor<T>)
Calculates the gradient of the DETR loss.
public Tensor<T> CalculateDerivative(Tensor<T> predicted, Tensor<T> targets)
Parameters
predictedTensor<T>targetsTensor<T>
Returns
- Tensor<T>
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the gradient of DETR loss with respect to predicted values.
public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>actualVector<T>
Returns
- Vector<T>
CalculateLoss(Tensor<T>, Tensor<T>)
Calculates the DETR set loss.
public T CalculateLoss(Tensor<T> predicted, Tensor<T> targets)
Parameters
predictedTensor<T>Predicted tensor containing class logits and boxes.
targetsTensor<T>Target tensor containing ground truth.
Returns
- T
Combined loss value.
Remarks
Expected shapes:
- predicted: [batch, num_queries, num_classes + 4] (logits + boxes)
- targets: [batch, max_objects, 1 + 4] (class + boxes, padded)
CalculateLoss(Vector<T>, Vector<T>)
Calculates the DETR loss using flattened vectors (simplified for interface compatibility).
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>actualVector<T>
Returns
- T
Remarks
For DETR, the tensor-based CalculateLoss overload is preferred as it preserves the structured format needed for Hungarian matching. This vector-based method provides a basic L1 loss between predicted and actual values.