# 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} System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/vision/ops.py:docstring of paddle.vision.ops.yolo_loss, line 59) Unexpected indentation. • loss_{conf} + loss_{class} System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/vision/ops.py:docstring of paddle.vision.ops.yolo_loss, line 60) Block quote ends without a blank line; unexpected unindent.$$

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.

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

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

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

• scale_x_y (float) – 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

Raises
• TypeError – Input x of yolov3_loss must be Tensor

• TypeError – Input gtbox of yolov3_loss must be Tensor

• TypeError – Input gtlabel of yolov3_loss must be Tensor

• TypeError – Input gtscore of yolov3_loss must be None or Tensor

• TypeError – Attr anchors of yolov3_loss must be list or tuple

• TypeError – Attr class_num of yolov3_loss must be an integer

• TypeError – Attr ignore_thresh of yolov3_loss must be a float number

• TypeError – Attr use_label_smooth of yolov3_loss must be a bool value

Examples

import paddle
import numpy as np

x = np.random.random([2, 14, 8, 8]).astype('float32')
gt_box = np.random.random([2, 10, 4]).astype('float32')
gt_label = np.random.random([2, 10]).astype('int32')