def get_intersect_area(true_xy,true_wh, pred_xy,pred_wh): ''' == INPUT == true_xy,pred_xy, true_wh and pred_wh must have the same shape length p1 : pred_mins = (px1,py1) p2 : pred_maxs = (px2,py2) t1 : true_mins = (tx1,ty1) t2 : true_maxs = (tx2,ty2) p1______________________ | t1___________ | | | | | |_______|___________|__|p2 | |rmax |___________| t2 intersect_mins : rmin = t1 = (tx1,ty1) intersect_maxs : rmax = (rmaxx,rmaxy) intersect_wh : (rmaxx - tx1, rmaxy - ty1) ''' true_wh_half = true_wh / 2. true_mins = true_xy - true_wh_half true_maxes = true_xy + true_wh_half pred_wh_half = pred_wh / 2. pred_mins = pred_xy - pred_wh_half pred_maxes = pred_xy + pred_wh_half intersect_mins = tf.maximum(pred_mins, true_mins) intersect_maxes = tf.minimum(pred_maxes, true_maxes) intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] true_areas = true_wh[..., 0] * true_wh[..., 1] pred_areas = pred_wh[..., 0] * pred_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = tf.truediv(intersect_areas, union_areas) return(iou_scores) def calc_IOU_pred_true_assigned(true_box_conf, true_box_xy, true_box_wh, pred_box_xy, pred_box_wh): ''' == input == true_box_conf : tensor of shape (N batch, N grid h, N grid w, N anchor ) true_box_xy : tensor of shape (N batch, N grid h, N grid w, N anchor , 2) true_box_wh : tensor of shape (N batch, N grid h, N grid w, N anchor , 2) pred_box_xy : tensor of shape (N batch, N grid h, N grid w, N anchor , 2) pred_box_wh : tensor of shape (N batch, N grid h, N grid w, N anchor , 2) == output == true_box_conf : tensor of shape (N batch, N grid h, N grid w, N anchor) true_box_conf value depends on the predicted values true_box_conf = IOU_{true,pred} if objecte exist in this anchor else 0 ''' iou_scores = get_intersect_area(true_box_xy,true_box_wh, pred_box_xy,pred_box_wh) true_box_conf_IOU = iou_scores * true_box_conf return(true_box_conf_IOU)