Created
July 7, 2020 01:41
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def custom_loss(y_true, y_pred): | |
''' | |
y_true : (N batch, N grid h, N grid w, N anchor, 4 + 1 + N classes) | |
y_true[irow, i_gridh, i_gridw, i_anchor, :4] = center_x, center_y, w, h | |
center_x : The x coordinate center of the bounding box. | |
Rescaled to range between 0 and N gird w (e.g., ranging between [0,13) | |
center_y : The y coordinate center of the bounding box. | |
Rescaled to range between 0 and N gird h (e.g., ranging between [0,13) | |
w : The width of the bounding box. | |
Rescaled to range between 0 and N gird w (e.g., ranging between [0,13) | |
h : The height of the bounding box. | |
Rescaled to range between 0 and N gird h (e.g., ranging between [0,13) | |
y_true[irow, i_gridh, i_gridw, i_anchor, 4] = ground truth confidence | |
ground truth confidence is 1 if object exists in this (anchor box, gird cell) pair | |
y_true[irow, i_gridh, i_gridw, i_anchor, 5 + iclass] = 1 if the object is in category else 0 | |
''' | |
total_recall = tf.Variable(0.) | |
# Step 1: Adjust prediction output | |
cell_grid = get_cell_grid(GRID_W,GRID_H,BATCH_SIZE,BOX) | |
pred_box_xy, pred_box_wh, pred_box_conf, pred_box_class = adjust_scale_prediction(y_pred,cell_grid,ANCHORS) | |
# Step 2: Extract ground truth output | |
true_box_xy, true_box_wh, true_box_conf, true_box_class = extract_ground_truth(y_true) | |
# Step 3: Calculate loss for the bounding box parameters | |
loss_xywh, coord_mask = calc_loss_xywh(true_box_conf,LAMBDA_COORD, | |
true_box_xy, pred_box_xy,true_box_wh,pred_box_wh) | |
# Step 4: Calculate loss for the class probabilities | |
loss_class = calc_loss_class(true_box_conf,LAMBDA_CLASS, | |
true_box_class,pred_box_class) | |
# Step 5: For each (grid cell, anchor) pair, | |
# calculate the IoU between predicted and ground truth bounding box | |
true_box_conf_IOU = calc_IOU_pred_true_assigned(true_box_conf, | |
true_box_xy, true_box_wh, | |
pred_box_xy, pred_box_wh) | |
# Step 6: For each predicted bounded box from (grid cell, anchor box), | |
# calculate the best IOU, regardless of the ground truth anchor box that each object gets assigned. | |
best_ious = calc_IOU_pred_true_best(pred_box_xy,pred_box_wh,true_boxes) | |
# Step 7: For each grid cell, calculate the L_{i,j}^{noobj} | |
conf_mask = get_conf_mask(best_ious, true_box_conf, true_box_conf_IOU,LAMBDA_NO_OBJECT, LAMBDA_OBJECT) | |
# Step 8: Calculate loss for the confidence | |
loss_conf = calc_loss_conf(conf_mask,true_box_conf_IOU, pred_box_conf) | |
loss = loss_xywh + loss_conf + loss_class | |
return loss | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment