def setUp(self):
        num_groundtruth_classes = 1
        matching_iou_threshold1 = 0.5
        matching_iou_threshold2 = 0.1

        self.eval1 = per_image_evaluation_rbox.PerImageEvaluationRbox(
            num_groundtruth_classes, matching_iou_threshold1)

        self.eval2 = per_image_evaluation_rbox.PerImageEvaluationRbox(
            num_groundtruth_classes, matching_iou_threshold2)

        self.detected_boxes = np.array(
            [[0, 0, 1, 1, 0.0], [0, 0, 2, 2, 0.0], [0, 0, 3, 3, 0.0]],
            dtype=np.float32)
        self.detected_scores = np.array([0.6, 0.8, 0.5], dtype=np.float32)
 def test_tp_fp(self):
     num_groundtruth_classes = 3
     matching_iou_threshold = 0.5
     eval1 = per_image_evaluation_rbox.PerImageEvaluationRbox(
         num_groundtruth_classes, matching_iou_threshold)
     detected_boxes = np.array(
         [[0, 0, 1, 1, 0.0], [0, 0, 2, 2, 0.0], [0, 0, 3, 3, 0.0]],
         dtype=np.float32)
     detected_scores = np.array([0.8, 0.8, 0.8], dtype=np.float32)
     detected_class_labels = np.array([0, 1, 2], dtype=int)
     groundtruth_boxes = np.array(
         [[0, 0, 1, 1, 0.0], [0, 0, 3.5, 3.5, 0.0]], dtype=np.float32)
     groundtruth_class_labels = np.array([0, 2], dtype=int)
     groundtruth_groundtruth_is_difficult_list = np.zeros(2,
                                                          dtype=np.float32)
     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)
     expected_scores = [np.array([0.8], dtype=np.float32)] * 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 test_compute_corloc_with_normal_iou_threshold(self):
        num_groundtruth_classes = 3
        matching_iou_threshold = 0.5
        eval1 = per_image_evaluation_rbox.PerImageEvaluationRbox(
            num_groundtruth_classes, matching_iou_threshold)
        detected_boxes = np.array([[0, 0, 1, 1, 0], [0, 0, 2, 2, 0],
                                   [0, 0, 3, 3, 0], [0, 0, 5, 5, 0]],
                                  dtype=np.float32)
        detected_scores = np.array([0.9, 0.9, 0.1, 0.9], dtype=np.float32)
        detected_class_labels = np.array([0, 1, 0, 2], dtype=int)
        groundtruth_boxes = np.array(
            [[0, 0, 1, 1, 0], [0, 0, 3, 3, 0], [0, 0, 6, 6, 0]],
            dtype=np.float32)
        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, 1], dtype=int)
        self.assertTrue(
            np.array_equal(expected_result,
                           is_class_correctly_detected_in_image))
    def __init__(self,
                 num_groundtruth_classes,
                 matching_iou_threshold=0.5,
                 nms_iou_threshold=1.0,
                 nms_max_output_boxes=10000,
                 box_type='box',
                 is_save_detection_results=False):
        self.box_type = box_type
        if box_type == 'rbox':
            self.per_image_eval = per_image_evaluation_rbox.PerImageEvaluationRbox(
                num_groundtruth_classes, matching_iou_threshold,
                is_save_detection_results)
        else:  # box_type == 'box'
            self.per_image_eval = per_image_evaluation.PerImageEvaluation(
                num_groundtruth_classes, matching_iou_threshold,
                nms_iou_threshold, nms_max_output_boxes)
        self.num_class = num_groundtruth_classes

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

        self.detection_keys = set()
        self.scores_per_class = [[] for _ in range(self.num_class)]
        self.tp_fp_labels_per_class = [[] for _ in range(self.num_class)]
        self.num_images_correctly_detected_per_class = np.zeros(self.num_class)
        self.average_precision_per_class = np.empty(self.num_class,
                                                    dtype=float)
        self.average_precision_per_class.fill(np.nan)
        self.precisions_per_class = []
        self.recalls_per_class = []
        self.corloc_per_class = np.ones(self.num_class, dtype=float)
        self.is_save_detection_results = is_save_detection_results
        self.detection_results = [] if is_save_detection_results else None