第1关:YOLO算法的损失函数
from keras import backend as K
def xy_loss(object_mask, raw_pred, box_loss_scale, raw_true_xy, mf):
"""
object_mask: 真实置信度
raw_true_xy: 真实xy
raw_pred: 预测结果,包含坐标以及置信度和分类结果([x, y, w, h, c, a1, a2, a3...])
box_loss_scale: box比例
"""
#********* Begin *********#
xy_loss = object_mask*box_loss_scale*K.binary_crossentropy(raw_true_xy,raw_pred[...,0:2],from_logits=True)
#********* End *********#
return K.sum(xy_loss) / mf
def wh_loss(object_mask, raw_pred, box_loss_scale, raw_true_wh, mf):
"""
object_mask: 真实置信度
raw_true_xy: 真实wh
raw_pred: 预测结果,包含坐标以及置信度和分类结果([x, y, w, h, c, a1, a2, a3...])
box_loss_scale: box比例
"""
#********* Begin *********#
wh_loss = object_mask*box_loss_scale*0.5*K.square(raw_true_wh - raw_pred[...,2:4])
#********* End *********#
return K.sum(wh_loss) / mf
def confidence_loss