def testSingleImageDetectionMaskExport(self): masks = np.array([[[ 1, 1, ], [1, 1]], [[0, 0], [0, 1]], [[0, 0], [0, 0]]], dtype=np.uint8) classes = np.array([1, 2, 3], dtype=np.int32) scores = np.array([0.8, 0.2, 0.7], dtype=np.float32) coco_annotations = coco_tools.ExportSingleImageDetectionMasksToCoco( image_id='first_image', category_id_set=set([1, 2, 3]), detection_classes=classes, detection_scores=scores, detection_masks=masks) expected_counts = ['04', '31', '4'] for i, mask_annotation in enumerate(coco_annotations): self.assertEqual(mask_annotation['segmentation']['counts'], expected_counts[i]) self.assertTrue( np.all( np.equal(mask.decode(mask_annotation['segmentation']), masks[i]))) self.assertEqual(mask_annotation['image_id'], 'first_image') self.assertEqual(mask_annotation['category_id'], classes[i]) self.assertAlmostEqual(mask_annotation['score'], scores[i])
def add_single_detected_image_info(self, image_id, detections_dict): """Adds detections for a single image to be used for evaluation. If a detection has already been added for this image id, a warning is logged, and the detection is skipped. Args: image_id: A unique string/integer identifier for the image. detections_dict: A dictionary containing - DetectionResultFields.detection_scores: float32 numpy array of shape [num_boxes] containing detection scores for the boxes. DetectionResultFields.detection_classes: integer numpy array of shape [num_boxes] containing 1-indexed detection classes for the boxes. DetectionResultFields.detection_masks: optional uint8 numpy array of shape [num_boxes, image_height, image_width] containing instance masks corresponding to the boxes. The elements of the array must be in {0, 1}. Raises: ValueError: If groundtruth for the image_id is not available or if spatial shapes of groundtruth_instance_masks and detection_masks are incompatible. """ if image_id not in self._image_id_to_mask_shape_map: raise ValueError('Missing groundtruth for image id: {}'.format(image_id)) if image_id in self._image_ids_with_detections: tf.logging.warning('Ignoring detection with image id %s since it was ' 'previously added', image_id) return groundtruth_masks_shape = self._image_id_to_mask_shape_map[image_id] detection_masks = detections_dict[standard_fields.DetectionResultFields. detection_masks] if groundtruth_masks_shape[1:] != detection_masks.shape[1:]: raise ValueError('Spatial shape of groundtruth masks and detection masks ' 'are incompatible: {} vs {}'.format( groundtruth_masks_shape, detection_masks.shape)) _check_mask_type_and_value(standard_fields.DetectionResultFields. detection_masks, detection_masks) self._detection_masks_list.extend( coco_tools.ExportSingleImageDetectionMasksToCoco( image_id=image_id, category_id_set=self._category_id_set, detection_masks=detection_masks, detection_scores=detections_dict[standard_fields. DetectionResultFields. detection_scores], detection_classes=detections_dict[standard_fields. DetectionResultFields. detection_classes])) self._image_ids_with_detections.update([image_id])