Table of Contents

Class CIoULoss<T>

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

Complete Intersection over Union (CIoU) loss for bounding box regression.

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

Type Parameters

T

The numeric type used for calculations.

Inheritance
CIoULoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: CIoU loss extends DIoU by also considering aspect ratio. This provides the most accurate bounding box regression and is used in modern YOLO versions (v5, v7, v8, etc.).

CIoU = IoU - d²/c² - αv, where: - d is the center distance - c is the enclosing diagonal - v measures aspect ratio consistency - α is a balancing factor CIoU Loss = 1 - CIoU

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

Constructors

CIoULoss()

Creates a new CIoU loss instance.

public CIoULoss()

Methods

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

Calculates the gradient of CIoU 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 CIoU 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 CIoU loss value.

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

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

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

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

CIoU loss value (1 - CIoU).