return loss
从上面的代码中我们可以看出,YoloV3的Loss有4部分组成。
-
中心点 x、y 的调整参数 ——二分类的交叉熵(keras :binary_cross_entropy,pytorch:BCELoss)
-
anchor的宽高w、h的调整参数 —— 均方损失函数:MSELoss
-
置信度Conf —— BCELoss
-
类别预测Cls —— BCELoss
计算loss,实际上是y_pre和y_true之间的对比:
y_pre就是一幅图像经过网络之后的3个输出,y_true就是一个真实图像中,将它的真实框的位置以及框内物体的种类,转化成yolo3网络输出后的格式的值,所以y_pre和y_true内容的shape都是
(batch_size,13,13,3,85),(batch_size,26,26,3,85),(batch_size,52,52,3,85)。
y_pre在上面的网络中已经做了说明,下面说一说如何生成y_true,在yolo3中,其使用了一个专门的函数用于处理读取进来的图片的框的真实情况。
def preprocess_true_boxes(true_boxes, input_shape, anchors, num_classes):
‘’'Preprocess true boxes to training input format
Parameters
true_boxes: array, shape=(m, T, 5)
Absolute x_min, y_min, x_max, y_max, class_id relative to input_shape.
input_shape: array-like, hw, multiples of 32
anchors: array, shape=(N, 2), wh
num_classes: integer
Returns
y_true: list of array, shape like yolo_outputs, xywh are reletive value
‘’’
assert (true_boxes[…, 4]<num_classes).all(), ‘class id must be less than num_classes’
num_layers = len(anchors)//3 # default setting
anchor_mask = [[6,7,8], [3,4,5], [0,1,2]] if num_layers==3 else [[3,4,5], [1,2,3]]
true_boxes = np.array(true_boxes, dtype=‘float32’)
input_shape = np.array(input_shape, dtype=‘int32’)<