def _build_coco_metrics(self): """Build COCO metrics evaluator.""" if (not self._task_config.model.include_mask ) or self._task_config.annotation_file: self.coco_metric = coco_evaluator.COCOEvaluator( annotation_file=self._task_config.annotation_file, include_mask=self._task_config.model.include_mask, per_category_metrics=self._task_config.per_category_metrics) else: # Builds COCO-style annotation file if include_mask is True, and # annotation_file isn't provided. annotation_path = os.path.join(self._logging_dir, 'annotation.json') if tf.io.gfile.exists(annotation_path): logging.info( 'annotation.json file exists, skipping creating the annotation' ' file.') else: if self._task_config.validation_data.num_examples <= 0: logging.info('validation_data.num_examples needs to be > 0') if not self._task_config.validation_data.input_path: logging.info('Can not create annotation file for tfds.') logging.info( 'Creating coco-style annotation file: %s', annotation_path) coco_utils.scan_and_generator_annotation_file( self._task_config.validation_data.input_path, self._task_config.validation_data.file_type, self._task_config.validation_data.num_examples, self.task_config.model.include_mask, annotation_path, regenerate_source_id=self._task_config.validation_data.decoder .simple_decoder.regenerate_source_id) self.coco_metric = coco_evaluator.COCOEvaluator( annotation_file=annotation_path, include_mask=self._task_config.model.include_mask, per_category_metrics=self._task_config.per_category_metrics)
def build_metrics(self, training: bool = True): """Build detection metrics.""" metrics = [] if training: metric_names = [ 'total_loss', 'rpn_score_loss', 'rpn_box_loss', 'frcnn_cls_loss', 'frcnn_box_loss', 'mask_loss', 'model_loss' ] for name in metric_names: metrics.append(tf.keras.metrics.Mean(name, dtype=tf.float32)) else: if (not self._task_config.model.include_mask ) or self._task_config.annotation_file: self.coco_metric = coco_evaluator.COCOEvaluator( annotation_file=self._task_config.annotation_file, include_mask=self._task_config.model.include_mask, per_category_metrics=self._task_config.per_category_metrics) else: # Builds COCO-style annotation file if include_mask is True, and # annotation_file isn't provided. annotation_path = os.path.join(self._logging_dir, 'annotation.json') if tf.io.gfile.exists(annotation_path): logging.info( 'annotation.json file exists, skipping creating the annotation' ' file.') else: if self._task_config.validation_data.num_examples <= 0: logging.info('validation_data.num_examples needs to be > 0') if not self._task_config.validation_data.input_path: logging.info('Can not create annotation file for tfds.') logging.info( 'Creating coco-style annotation file: %s', annotation_path) coco_utils.scan_and_generator_annotation_file( self._task_config.validation_data.input_path, self._task_config.validation_data.file_type, self._task_config.validation_data.num_examples, self.task_config.model.include_mask, annotation_path) self.coco_metric = coco_evaluator.COCOEvaluator( annotation_file=annotation_path, include_mask=self._task_config.model.include_mask, per_category_metrics=self._task_config.per_category_metrics) return metrics
def test_scan_and_generator_annotation_file(self): num_samples = 10 example = tfexample_utils.create_detection_test_example( image_height=512, image_width=512, image_channel=3, num_instances=10) tf_examples = [example] * num_samples data_file = os.path.join(self.create_tempdir(), 'test.tfrecord') tfexample_utils.dump_to_tfrecord(record_file=data_file, tf_examples=tf_examples) annotation_file = os.path.join(self.create_tempdir(), 'annotation.json') coco_utils.scan_and_generator_annotation_file( file_pattern=data_file, file_type='tfrecord', num_samples=num_samples, include_mask=True, annotation_file=annotation_file) self.assertTrue( tf.io.gfile.exists(annotation_file), msg='Annotation file {annotation_file} does not exists.')