# yolov3_loss¶

paddle.fluid.layers.detection. yolov3_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 GTScore 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.

Args:

x (Variable): 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) storesbox locations, confidence score and classification one-hotkeys of each anchor boxThe data type is float32 or float64. gt_box (Variable): groud truth boxes, should be in shape of [N, B, 4],

Unexpected indentation.

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.

Block quote ends without a blank line; unexpected unindent.

gt_label (Variable): class id of ground truth boxes, should be in shape

of [N, B].The data type is int32.

Definition list ends without a blank line; unexpected unindent.

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 to predict 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

Unexpected indentation.

Block quote ends without a blank line; unexpected unindent.

gt_score (Variable): mixup score of ground truth boxes, should be in shape

of [N, B]. Default None.

Definition list ends without a blank line; unexpected unindent.

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:

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

Raises:

TypeError: Input x of yolov3_loss must be Variable TypeError: Input gtbox of yolov3_loss must be Variable TypeError: Input gtlabel of yolov3_loss must be Variable TypeError: Input gtscore of yolov3_loss must be None or Variable 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.fluid as fluid