responsible_bbox_confidence = bbox1_pred_confidence if iou_bbox1 > iou_bbox2 else bbox2_pred_confidence # ④
non_responsible_bbox_confidence = bbox2_pred_confidence if iou_bbox1 > iou_bbox2 else bbox1_pred_confidence # ④
obj_exist = 1.0 - tf.cast(tf.reduce_all(tf.equal(bbox_true, 0.0)),
tf.float32)
localization_err = tf.reduce_sum(tf.square(bbox_true - responsible_bbox)) * obj_exist # ⑤
confidence_err_obj = tf.square(responsible_bbox_confidence - bbox_true_confidence) * obj_exist # ⑤
confidence_err_noobj = 0.5 * tf.square(non_responsible_bbox_confidence) * (1.0 - obj_exist) # ⑤
classification_err = tf.reduce_sum(tf.square(class_true - class_pred)) * obj_exist # ⑤
cell_loss = 5.0 * localization_err + confidence_err_obj + confidence_err_noobj + classification_err # ⑥
cell_losses.append(cell_loss)
batch_loss += tf.reduce_sum(cell_losses) # ⑦
batch_loss /= tf.cast(tf.shape(y_true)[0], tf.float32) # ⑧
return batch_loss