Class GIoULoss<T>
- Namespace
- AiDotNet.ComputerVision.Detection.Losses
- Assembly
- AiDotNet.dll
Generalized Intersection over Union (GIoU) loss for bounding box regression.
public class GIoULoss<T> : LossFunctionBase<T>, ILossFunction<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
GIoULoss<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: GIoU loss improves upon standard IoU loss by providing gradients even when boxes don't overlap. This helps the model learn to move boxes towards their targets even when they start far apart.
GIoU = IoU - (|C - U|) / |C|, where C is the smallest enclosing box and U is the union. GIoU Loss = 1 - GIoU
Reference: Rezatofighi et al., "Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression", CVPR 2019
Constructors
GIoULoss()
Creates a new GIoU loss instance.
public GIoULoss()
Methods
CalculateDerivative(Vector<T>, Vector<T>)
Calculates the gradient of GIoU loss with respect to predicted boxes.
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 GIoU loss for tensor inputs.
public T CalculateLoss(Tensor<T> predicted, Tensor<T> targets)
Parameters
predictedTensor<T>Predicted boxes tensor [batch, num_boxes, 4] in XYXY format.
targetsTensor<T>Target boxes tensor [batch, num_boxes, 4] in XYXY format.
Returns
- T
Mean GIoU loss value.
CalculateLoss(Vector<T>, Vector<T>)
Calculates the GIoU loss between predicted and target bounding box vectors.
public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)
Parameters
predictedVector<T>Flattened predicted boxes [x1,y1,x2,y2, x1,y1,x2,y2, ...]
actualVector<T>Flattened target boxes [x1,y1,x2,y2, x1,y1,x2,y2, ...]
Returns
- T
Mean GIoU loss value.
CalculateLossForBox(BoundingBox<T>, BoundingBox<T>)
Calculates GIoU loss for a pair of bounding boxes.
public double CalculateLossForBox(BoundingBox<T> predicted, BoundingBox<T> target)
Parameters
predictedBoundingBox<T>Predicted bounding box.
targetBoundingBox<T>Target bounding box.
Returns
- double
GIoU loss value (1 - GIoU).