def custom_loss(y_true, y_pred):
# Get prediction
pred_box_xy = tf.sigmoid(y_pred[..., :2])
pred_box_wh = y_pred[..., 2:4]
pred_box_conf = tf.sigmoid(y_pred[..., 4])
# Get ground truth
true_box_xy = y_true[..., :2]
true_box_wh = y_true[..., 2:4]
true_box_conf = y_true[..., 4]
# Determine the mask: simply the position of the ground truth boxes (the predictors)
true_mask = tf.expand_dims(y_true[..., 4], axis=-1)
# Calculate the loss. A scale can be associated with each loss, indicating how important
# the loss is. The bigger the scale, more important the loss is.
loss_xy = tf.reduce_sum(tf.square(true_box_xy - pred_box_xy) * true_mask) * 1.0
loss_wh = tf.reduce_sum(tf.square(true_box_wh - pred_box_wh) * true_mask) * 1.0
loss_conf = tf.reduce_sum(tf.square(true_box_conf - pred_box_conf)) * 1.2
loss = loss_xy + loss_wh + loss_conf
return loss
评论列表
文章目录