Table of Contents

Class DIoULoss<T>

Namespace
AiDotNet.ComputerVision.Detection.Losses
Assembly
AiDotNet.dll

Distance Intersection over Union (DIoU) loss for bounding box regression.

public class DIoULoss<T> : LossFunctionBase<T>, ILossFunction<T>

Type Parameters

T

The numeric type used for calculations.

Inheritance
DIoULoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: DIoU loss adds a center distance penalty to GIoU loss. This helps the model converge faster by explicitly minimizing the distance between predicted and target box centers.

DIoU = IoU - d²/c², where d is the center distance and c is the enclosing diagonal. DIoU Loss = 1 - DIoU

Reference: Zheng et al., "Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression", AAAI 2020

Constructors

DIoULoss()

Creates a new DIoU loss instance.

public DIoULoss()

Methods

CalculateDerivative(Vector<T>, Vector<T>)

Calculates the gradient of DIoU 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 the DIoU loss between predicted and target bounding boxes.

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 DIoU loss value.

CalculateLoss(Vector<T>, Vector<T>)

Calculates the DIoU 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 DIoU loss value.

CalculateLossForBox(BoundingBox<T>, BoundingBox<T>)

Calculates DIoU 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

DIoU loss value (1 - DIoU).