손실 함수 정의
다음 코드는 앞에서 학습 과정 파트에서 나온 손실 함수 수식을 구현한 것입니다.
def yolo_multitask_loss(y_true, y_pred):
batch_loss = 0
for true_vals, pred_vals in zip(y_true, y_pred):
true_vals = tf.reshape(true_vals, [49, 25]) # ①
pred_vals = tf.reshape(pred_vals, [49, 30]) # ①
cell_losses = []
for true_cell, pred_cell in zip(true_vals, pred_vals):
bbox1_pred, bbox1_pred_confidence, bbox2_pred, bbox2_pred_confidence, class_pred = \
pred_cell[:4], pred_cell[4], pred_cell[5:9], pred_cell[9], pred_cell[10:]
bbox_true, bbox_true_confidence, class_true = \
true_cell[:4], true_cell[4], true_cell[5:] # ②
def calculate_iou(bbox_pred, bbox_true): # ③
pred_area = bbox_pred[2] * bbox_pred[3]
true_area = bbox_true[2] * bbox_true[3]