def setUp(self):
        num_groundtruth_classes = 1
        matching_iou_threshold_high_iou = 0.5
        matching_iou_threshold_low_iou = 0.1
        nms_iou_threshold = 1.0
        nms_max_output_boxes = 10000
        self.eval_high_iou = per_image_evaluation.PerImageEvaluation(
            num_groundtruth_classes, matching_iou_threshold_high_iou,
            nms_iou_threshold, nms_max_output_boxes)

        self.eval_low_iou = per_image_evaluation.PerImageEvaluation(
            num_groundtruth_classes, matching_iou_threshold_low_iou,
            nms_iou_threshold, nms_max_output_boxes)

        self.detected_boxes = np.array(
            [[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]], dtype=float)
        self.detected_scores = np.array([0.6, 0.8, 0.5], dtype=float)
        detected_masks_0 = np.array([[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]],
                                    dtype=np.uint8)
        detected_masks_1 = np.array([[1, 0, 0, 0], [1, 1, 0, 0], [0, 0, 0, 0]],
                                    dtype=np.uint8)
        detected_masks_2 = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 0, 0]],
                                    dtype=np.uint8)
        self.detected_masks = np.stack(
            [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)
  def __init__(self,
               num_groundtruth_classes,
               matching_iou_threshold=0.5,
               nms_iou_threshold=1.0,
               nms_max_output_boxes=10000,
               use_weighted_mean_ap=False,
               label_id_offset=0,
               group_of_weight=0.0):
    if num_groundtruth_classes < 1:
      raise ValueError('Need at least 1 groundtruth class for evaluation.')

    self.per_image_eval = per_image_evaluation.PerImageEvaluation(
        num_groundtruth_classes=num_groundtruth_classes,
        matching_iou_threshold=matching_iou_threshold,
        nms_iou_threshold=nms_iou_threshold,
        nms_max_output_boxes=nms_max_output_boxes,
        group_of_weight=group_of_weight)
    self.group_of_weight = group_of_weight
    self.num_class = num_groundtruth_classes
    self.use_weighted_mean_ap = use_weighted_mean_ap
    self.label_id_offset = label_id_offset

    self.groundtruth_boxes = {}
    self.groundtruth_class_labels = {}
    self.groundtruth_masks = {}
    self.groundtruth_is_difficult_list = {}
    self.groundtruth_is_group_of_list = {}
    self.num_gt_instances_per_class = np.zeros(self.num_class, dtype=float)
    self.num_gt_imgs_per_class = np.zeros(self.num_class, dtype=int)

    self._initialize_detections()
 def test_tp_fp(self):
     num_groundtruth_classes = 3
     matching_iou_threshold = 0.5
     nms_iou_threshold = 1.0
     nms_max_output_boxes = 10000
     eval1 = per_image_evaluation.PerImageEvaluation(
         num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
         nms_max_output_boxes)
     detected_boxes = np.array(
         [[0, 0, 1, 1], [10, 10, 5, 5], [0, 0, 2, 2], [5, 10, 10, 5],
          [10, 5, 5, 10], [0, 0, 3, 3]],
         dtype=float)
     detected_scores = np.array([0.8, 0.1, 0.8, 0.9, 0.7, 0.8], dtype=float)
     detected_class_labels = np.array([0, 1, 1, 2, 0, 2], dtype=int)
     groundtruth_boxes = np.array([[0, 0, 1, 1], [0, 0, 3.5, 3.5]],
                                  dtype=float)
     groundtruth_class_labels = np.array([0, 2], dtype=int)
     groundtruth_groundtruth_is_difficult_list = np.zeros(2, dtype=float)
     groundtruth_groundtruth_is_group_of_list = np.array([False, False],
                                                         dtype=bool)
     scores, tp_fp_labels, _ = eval1.compute_object_detection_metrics(
         detected_boxes, detected_scores, detected_class_labels,
         groundtruth_boxes, groundtruth_class_labels,
         groundtruth_groundtruth_is_difficult_list,
         groundtruth_groundtruth_is_group_of_list)
     expected_scores = [np.array([0.8], dtype=float)] * 3
     expected_tp_fp_labels = [
         np.array([True]),
         np.array([False]),
         np.array([True])
     ]
     for i in range(len(expected_scores)):
         self.assertTrue(np.allclose(expected_scores[i], scores[i]))
         self.assertTrue(
             np.array_equal(expected_tp_fp_labels[i], tp_fp_labels[i]))
    def setUp(self):
        num_groundtruth_classes = 1
        matching_iou_threshold = 0.5
        nms_iou_threshold = 1.0
        nms_max_output_boxes = 10000
        self.group_of_weight = 0.5
        self.eval = per_image_evaluation.PerImageEvaluation(
            num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
            nms_max_output_boxes, self.group_of_weight)

        self.detected_boxes = np.array(
            [[0, 0, 1, 1], [0, 0, 2, 1], [0, 0, 3, 1]], dtype=float)
        self.detected_scores = np.array([0.8, 0.6, 0.5], dtype=float)
        detected_masks_0 = np.array([[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0, 0]],
                                    dtype=np.uint8)
        detected_masks_1 = np.array([[1, 0, 0, 0], [1, 1, 0, 0], [0, 0, 0, 0]],
                                    dtype=np.uint8)
        detected_masks_2 = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 0, 0]],
                                    dtype=np.uint8)
        self.detected_masks = np.stack(
            [detected_masks_0, detected_masks_1, detected_masks_2], axis=0)

        self.groundtruth_boxes = np.array(
            [[0, 0, 1, 1], [0, 0, 5, 5], [10, 10, 20, 20]], dtype=float)
        groundtruth_masks_0 = np.array(
            [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]], dtype=np.uint8)
        groundtruth_masks_1 = np.array(
            [[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]], dtype=np.uint8)
        groundtruth_masks_2 = np.array(
            [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]], dtype=np.uint8)
        self.groundtruth_masks = np.stack(
            [groundtruth_masks_0, groundtruth_masks_1, groundtruth_masks_2],
            axis=0)
    def test_compute_corloc_with_very_large_iou_threshold(self):
        num_groundtruth_classes = 3
        matching_iou_threshold = 0.9
        nms_iou_threshold = 1.0
        nms_max_output_boxes = 10000
        eval1 = per_image_evaluation.PerImageEvaluation(
            num_groundtruth_classes, matching_iou_threshold, nms_iou_threshold,
            nms_max_output_boxes)
        detected_boxes = np.array(
            [[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3], [0, 0, 5, 5]],
            dtype=float)
        detected_scores = np.array([0.9, 0.9, 0.1, 0.9], dtype=float)
        detected_class_labels = np.array([0, 1, 0, 2], dtype=int)
        groundtruth_boxes = np.array(
            [[0, 0, 1, 1], [0, 0, 3, 3], [0, 0, 6, 6]], dtype=float)
        groundtruth_class_labels = np.array([0, 0, 2], dtype=int)

        is_class_correctly_detected_in_image = eval1._compute_cor_loc(
            detected_boxes, detected_scores, detected_class_labels,
            groundtruth_boxes, groundtruth_class_labels)
        expected_result = np.array([1, 0, 0], dtype=int)
        self.assertTrue(
            np.array_equal(expected_result,
                           is_class_correctly_detected_in_image))