yolo_loss

paddle.vision.ops. yolo_loss ( x, gt_box, gt_label, anchors, anchor_mask, class_num, ignore_thresh, downsample_ratio, gt_score=None, use_label_smooth=True, name=None, scale_x_y=1.0 ) [source]

This operator generates YOLOv3 loss based on given predict result and ground truth boxes.

The output of previous network is in shape [N, C, H, W], while H and W should be the same, H and W specify the grid size, each grid point predict given number bounding boxes, this given number, which following will be represented as S, is specified by the number of anchor clusters in each scale. In the second dimension(the channel dimension), C should be equal to S * (class_num + 5), class_num is the object category number of source dataset(such as 80 in coco dataset), so in the second(channel) dimension, apart from 4 box location coordinates x, y, w, h, also includes confidence score of the box and class one-hot key of each anchor box.

Assume the 4 location coordinates are \(t_x, t_y, t_w, t_h\), the box predictions should be as follows:

$$ b_x = \sigma(t_x) + c_x $$ $$ b_y = \sigma(t_y) + c_y $$ $$ b_w = p_w e^{t_w} $$ $$ b_h = p_h e^{t_h} $$

In the equation above, \(c_x, c_y\) is the left top corner of current grid and \(p_w, p_h\) is specified by anchors.

As for confidence score, it is the logistic regression value of IoU between anchor boxes and ground truth boxes, the score of the anchor box which has the max IoU should be 1, and if the anchor box has IoU bigger than ignore thresh, the confidence score loss of this anchor box will be ignored.

Therefore, the YOLOv3 loss consists of three major parts: box location loss, objectness loss and classification loss. The L1 loss is used for box coordinates (w, h), sigmoid cross entropy loss is used for box coordinates (x, y), objectness loss and classification loss.

Each groud truth box finds a best matching anchor box in all anchors. Prediction of this anchor box will incur all three parts of losses, and prediction of anchor boxes with no GT box matched will only incur objectness loss.

In order to trade off box coordinate losses between big boxes and small boxes, box coordinate losses will be mutiplied by scale weight, which is calculated as follows.

$$ weight_{box} = 2.0 - t_w * t_h $$

Final loss will be represented as follows.

$$ loss = (loss_{xy} + loss_{wh}) * weight_{box} + loss_{conf} + loss_{class} $$

While use_label_smooth is set to be True, the classification target will be smoothed when calculating classification loss, target of positive samples will be smoothed to \(1.0 - 1.0 / class\_num\) and target of negetive samples will be smoothed to \(1.0 / class\_num\).

While gt_score is given, which means the mixup score of ground truth boxes, all losses incured by a ground truth box will be multiplied by its mixup score.

Parameters
  • x (Tensor) – The input tensor of YOLOv3 loss operator, This is a 4-D tensor with shape of [N, C, H, W]. H and W should be same, and the second dimension(C) stores box locations, confidence score and classification one-hot keys of each anchor box. The data type is float32 or float64.

  • gt_box (Tensor) – groud truth boxes, should be in shape of [N, B, 4], in the third dimension, x, y, w, h should be stored. x,y is the center coordinate of boxes, w, h are the width and height, x, y, w, h should be divided by input image height to scale to [0, 1]. N is the batch number and B is the max box number in an image.The data type is float32 or float64.

  • gt_label (Tensor) – class id of ground truth boxes, should be in shape of [N, B].The data type is int32.

  • anchors (list|tuple) – The anchor width and height, it will be parsed pair by pair.

  • anchor_mask (list|tuple) – The mask index of anchors used in current YOLOv3 loss calculation.

  • class_num (int) – The number of classes.

  • ignore_thresh (float) – The ignore threshold to ignore confidence loss.

  • downsample_ratio (int) – The downsample ratio from network input to YOLOv3 loss input, so 32, 16, 8 should be set for the first, second, and thrid YOLOv3 loss operators.

  • gt_score (Tensor, optional) – mixup score of ground truth boxes, should be in shape of [N, B]. Default None.

  • use_label_smooth (bool, optional) – Whether to use label smooth. Default True.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name

  • scale_x_y (float, optional) – Scale the center point of decoded bounding box. Default 1.0.

Returns

A 1-D tensor with shape [N], the value of yolov3 loss

Return type

Tensor

Examples

>>> import paddle
>>> x = paddle.rand([2, 14, 8, 8]).astype('float32')
>>> gt_box = paddle.rand([2, 10, 4]).astype('float32')
>>> gt_label = paddle.rand([2, 10]).astype('int32')
>>> loss = paddle.vision.ops.yolo_loss(x,
...                                    gt_box=gt_box,
...                                    gt_label=gt_label,
...                                    anchors=[10, 13, 16, 30],
...                                    anchor_mask=[0, 1],
...                                    class_num=2,
...                                    ignore_thresh=0.7,
...                                    downsample_ratio=8,
...                                    use_label_smooth=True,
...                                    scale_x_y=1.)