Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

There is no uniform method of calculating IOUs. #1095

Open
odoku opened this issue Jun 16, 2020 · 0 comments
Open

There is no uniform method of calculating IOUs. #1095

odoku opened this issue Jun 16, 2020 · 0 comments

Comments

@odoku
Copy link
Contributor

odoku commented Jun 16, 2020

The IOU calculation method used in Object Detection, etc. is not used in Blueoil. It does not seem to be consistent.
This could produce differences in learning and inference results and needs to be improved.

We need to consider the following points in the calculation.

  • INCLUSIVE

    • def _calc_overlap(gt_boxes, pred_box):
      """Calcurate overlap
      Args:
      gt_boxes: ground truth boxes in the image. shape is [num_boxes, 5(x, y, w, h, class)]
      pred_box: a predict box in the image. shape is [6(x, y, w, h, class, prob)]
      Return:
      """
      assert gt_boxes.size != 0, "Cannot clculate if ground truth boxes is zero"
      # compute overlaps
      gt_boxes_xmin = gt_boxes[:, 0]
      gt_boxes_ymin = gt_boxes[:, 1]
      gt_boxes_xmax = gt_boxes[:, 0] + gt_boxes[:, 2]
      gt_boxes_ymax = gt_boxes[:, 1] + gt_boxes[:, 3]
      pred_box_xmin = pred_box[0]
      pred_box_ymin = pred_box[1]
      pred_box_xmax = pred_box[0] + pred_box[2]
      pred_box_ymax = pred_box[1] + pred_box[3]
      # If border pixels are supposed to expand the bounding boxes each 0.5 pixel,
      # we have to add 1 pixel to any difference `xmax - xmin` or `ymax - ymin`.
      d = 1.
      # intersection
      inter_xmin = np.maximum(gt_boxes_xmin, pred_box_xmin)
      inter_ymin = np.maximum(gt_boxes_ymin, pred_box_ymin)
      inter_xmax = np.minimum(gt_boxes_xmax, pred_box_xmax)
      inter_ymax = np.minimum(gt_boxes_ymax, pred_box_ymax)
      inter_w = np.maximum(inter_xmax - inter_xmin + d, 0.)
      inter_h = np.maximum(inter_ymax - inter_ymin + d, 0.)
      inters = inter_w * inter_h
      # union
      union = (pred_box_xmax - pred_box_xmin + d) * (pred_box_ymax - pred_box_ymin + d) \
      + (gt_boxes_xmax - gt_boxes_xmin + d) * (gt_boxes_ymax - gt_boxes_ymin + d) \
      - inters
      return inters / union
      .
  • exclusive

    • def iou(boxes, box):
      """Calculate overlap
      Args:
      boxes: boxes in the image. shape is [num_boxes, 4 or more(x, y, w, h, ...)]
      box: a single box in the image. shape is [4 or more (x, y, w, h, ...)]
      Returns:
      iou: shape is [num_boxes]
      """
      if boxes.size == 0:
      raise ValueError("Cannot calculate if ground truth boxes is zero")
      # format boxes (left, top, right, bottom)
      boxes = np.stack([
      boxes[:, 0],
      boxes[:, 1],
      boxes[:, 0] + boxes[:, 2],
      boxes[:, 1] + boxes[:, 3],
      ], axis=1)
      # format box (left, top, right, bottom)
      box = np.array([
      box[0],
      box[1],
      box[0] + box[2],
      box[1] + box[3],
      ])
      # calculate the left up point
      left_top = np.maximum(boxes[:, 0:2], box[0:2])
      # calculate the right bottom point
      right_bottom = np.minimum(boxes[:, 2:], box[2:])
      horizon_vertical = np.maximum(right_bottom - left_top, 0)
      intersection = horizon_vertical[:, 0] * horizon_vertical[:, 1]
      areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
      area = (box[2] - box[0]) * (box[3] - box[1])
      epsilon = 1e-10
      union = area + areas - intersection
      return intersection / (union + epsilon)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant