def testSingleImageGroundtruthExport(self): masks = np.array([[[ 1, 1, ], [1, 1]], [[0, 0], [0, 1]], [[0, 0], [0, 0]]], dtype=np.uint8) boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, 1, 1]], dtype=np.float32) coco_boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, .5, .5]], dtype=np.float32) classes = np.array([1, 2, 3], dtype=np.int32) is_crowd = np.array([0, 1, 0], dtype=np.int32) next_annotation_id = 1 expected_counts = ['04', '31', '4'] # Tests exporting without passing in is_crowd (for backward compatibility). coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), next_annotation_id=next_annotation_id, groundtruth_boxes=boxes, groundtruth_classes=classes, groundtruth_masks=masks) for i, annotation in enumerate(coco_annotations): self.assertEqual(annotation['segmentation']['counts'], expected_counts[i]) self.assertTrue( np.all( np.equal(mask.decode(annotation['segmentation']), masks[i]))) self.assertTrue( np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) self.assertEqual(annotation['image_id'], 'first_image') self.assertEqual(annotation['category_id'], classes[i]) self.assertEqual(annotation['id'], i + next_annotation_id) # Tests exporting with is_crowd. coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), next_annotation_id=next_annotation_id, groundtruth_boxes=boxes, groundtruth_classes=classes, groundtruth_masks=masks, groundtruth_is_crowd=is_crowd) for i, annotation in enumerate(coco_annotations): self.assertEqual(annotation['segmentation']['counts'], expected_counts[i]) self.assertTrue( np.all( np.equal(mask.decode(annotation['segmentation']), masks[i]))) self.assertTrue( np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) self.assertEqual(annotation['image_id'], 'first_image') self.assertEqual(annotation['category_id'], classes[i]) self.assertEqual(annotation['iscrowd'], is_crowd[i]) self.assertEqual(annotation['id'], i + next_annotation_id)
def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Adds groundtruth for a single image to be used for evaluation. If the image has already been added, a warning is logged, and groundtruth is ignored. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A dictionary containing - InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. """ if image_id in self._image_ids: tf.logging.warning( 'Ignoring ground truth with image id %s since it was ' 'previously added', image_id) return self._groundtruth_list.extend( coco_tools.ExportSingleImageGroundtruthToCoco( image_id=image_id, next_annotation_id=self._annotation_id, category_id_set=self._category_id_set, groundtruth_boxes=groundtruth_dict[ standard_fields.InputDataFields.groundtruth_boxes], groundtruth_classes=groundtruth_dict[ standard_fields.InputDataFields.groundtruth_classes])) self._annotation_id += groundtruth_dict[ standard_fields.InputDataFields.groundtruth_boxes].shape[0] self._image_ids[image_id] = False
def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Adds groundtruth for a single image to be used for evaluation. If the image has already been added, a warning is logged, and groundtruth is ignored. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A dictionary containing - InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. InputDataFields.groundtruth_instance_masks: uint8 numpy array of shape [num_boxes, image_height, image_width] containing groundtruth masks corresponding to the boxes. The elements of the array must be in {0, 1}. """ if image_id in self._image_id_to_mask_shape_map: tf.compat.v1.logging.warning( 'Ignoring ground truth with image id %s since it was ' 'previously added', image_id) return groundtruth_instance_masks = groundtruth_dict[ standard_fields.InputDataFields.groundtruth_instance_masks] _check_mask_type_and_value( standard_fields.InputDataFields.groundtruth_instance_masks, groundtruth_instance_masks) self._groundtruth_list.extend( coco_tools.ExportSingleImageGroundtruthToCoco( image_id=image_id, next_annotation_id=self._annotation_id, category_id_set=self._category_id_set, groundtruth_boxes=groundtruth_dict[ standard_fields.InputDataFields.groundtruth_boxes], groundtruth_classes=groundtruth_dict[ standard_fields.InputDataFields.groundtruth_classes], groundtruth_masks=groundtruth_instance_masks)) self._annotation_id += groundtruth_dict[ standard_fields.InputDataFields.groundtruth_boxes].shape[0] self._image_id_to_mask_shape_map[image_id] = groundtruth_dict[ standard_fields.InputDataFields.groundtruth_instance_masks].shape
def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): """Add groundtruth results of all frames to the eval pipeline. This method overrides the function defined in the base class. Args: image_id: A unique string/integer identifier for the image. groundtruth_dict: A list of dictionary containing - InputDataFields.groundtruth_boxes: float32 numpy array of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. InputDataFields.groundtruth_classes: integer numpy array of shape [num_boxes] containing 1-indexed groundtruth classes for the boxes. InputDataFields.groundtruth_is_crowd (optional): integer numpy array of shape [num_boxes] containing iscrowd flag for groundtruth boxes. """ for idx, gt in enumerate(groundtruth_dict): if not gt: continue image_frame_id = '{}_{}'.format(image_id, idx) if image_frame_id in self._image_ids: tf.logging.warning( 'Ignoring ground truth with image id %s since it was ' 'previously added', image_frame_id) continue self._groundtruth_list.extend( coco_tools.ExportSingleImageGroundtruthToCoco( image_id=image_frame_id, next_annotation_id=self._annotation_id, category_id_set=self._category_id_set, groundtruth_boxes=gt[ standard_fields.InputDataFields.groundtruth_boxes], groundtruth_classes=gt[ standard_fields.InputDataFields.groundtruth_classes])) self._annotation_id += ( gt[standard_fields.InputDataFields.groundtruth_boxes].shape[0]) # Boolean to indicate whether a detection has been added for this image. self._image_ids[image_frame_id] = False
def testSingleImageGroundtruthExportWithKeypoints(self): boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, 1, 1]], dtype=np.float32) coco_boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, .5, .5]], dtype=np.float32) keypoints = np.array([[[0, 0], [0.25, 0.25], [0.75, 0.75]], [[0, 0], [0.125, 0.125], [0.375, 0.375]], [[0.5, 0.5], [0.75, 0.75], [1.0, 1.0]]], dtype=np.float32) visibilities = np.array([[2, 2, 2], [2, 2, 0], [2, 0, 0]], dtype=np.int32) areas = np.array([15., 16., 17.]) classes = np.array([1, 2, 3], dtype=np.int32) is_crowd = np.array([0, 1, 0], dtype=np.int32) next_annotation_id = 1 # Tests exporting without passing in is_crowd (for backward compatibility). coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), next_annotation_id=next_annotation_id, groundtruth_boxes=boxes, groundtruth_classes=classes, groundtruth_keypoints=keypoints, groundtruth_keypoint_visibilities=visibilities, groundtruth_area=areas) for i, annotation in enumerate(coco_annotations): self.assertTrue( np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) self.assertEqual(annotation['image_id'], 'first_image') self.assertEqual(annotation['category_id'], classes[i]) self.assertEqual(annotation['id'], i + next_annotation_id) self.assertEqual(annotation['num_keypoints'], 3 - i) self.assertEqual(annotation['area'], 15.0 + i) self.assertTrue( np.all( np.isclose(annotation['keypoints'][0::3], keypoints[i, :, 1]))) self.assertTrue( np.all( np.isclose(annotation['keypoints'][1::3], keypoints[i, :, 0]))) self.assertTrue( np.all(np.equal(annotation['keypoints'][2::3], visibilities[i]))) # Tests exporting with is_crowd. coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), next_annotation_id=next_annotation_id, groundtruth_boxes=boxes, groundtruth_classes=classes, groundtruth_keypoints=keypoints, groundtruth_keypoint_visibilities=visibilities, groundtruth_is_crowd=is_crowd) for i, annotation in enumerate(coco_annotations): self.assertTrue( np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) self.assertEqual(annotation['image_id'], 'first_image') self.assertEqual(annotation['category_id'], classes[i]) self.assertEqual(annotation['iscrowd'], is_crowd[i]) self.assertEqual(annotation['id'], i + next_annotation_id) self.assertEqual(annotation['num_keypoints'], 3 - i) self.assertTrue( np.all( np.isclose(annotation['keypoints'][0::3], keypoints[i, :, 1]))) self.assertTrue( np.all( np.isclose(annotation['keypoints'][1::3], keypoints[i, :, 0]))) self.assertTrue( np.all(np.equal(annotation['keypoints'][2::3], visibilities[i]))) # Testing the area values are derived from the bounding boxes. if i == 0: self.assertAlmostEqual(annotation['area'], 1.0) else: self.assertAlmostEqual(annotation['area'], 0.25)