Table of Contents

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

T

The 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

predicted Vector<T>
actual Vector<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

predicted Tensor<T>

Predicted boxes tensor [batch, num_boxes, 4] in XYXY format.

targets Tensor<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

predicted Vector<T>

Flattened predicted boxes [x1,y1,x2,y2, x1,y1,x2,y2, ...]

actual Vector<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

predicted BoundingBox<T>

Predicted bounding box.

target BoundingBox<T>

Target bounding box.

Returns

double

GIoU loss value (1 - GIoU).