From effbad5db4f1a046dd63cdf14ae49412ba4a836f Mon Sep 17 00:00:00 2001 From: shangliang Xu Date: Wed, 14 Apr 2021 14:33:00 +0800 Subject: [PATCH] add save only in eval (#2608) --- dygraph/ppdet/engine/trainer.py | 6 +++++- dygraph/ppdet/metrics/metrics.py | 37 +++++++++++++++++++++----------- dygraph/tools/eval.py | 7 ++++++ dygraph/tools/train.py | 6 ++++++ 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/dygraph/ppdet/engine/trainer.py b/dygraph/ppdet/engine/trainer.py index 25a5babc4..8377a25d1 100644 --- a/dygraph/ppdet/engine/trainer.py +++ b/dygraph/ppdet/engine/trainer.py @@ -116,9 +116,13 @@ class Trainer(object): if self.cfg.metric == 'COCO': # TODO: bias should be unified bias = self.cfg['bias'] if 'bias' in self.cfg else 0 + save_prediction_only = self.cfg['save_prediction_only'] \ + if 'save_prediction_only' in self.cfg else False self._metrics = [ COCOMetric( - anno_file=self.dataset.get_anno(), bias=bias) + anno_file=self.dataset.get_anno(), + bias=bias, + save_prediction_only=save_prediction_only) ] elif self.cfg.metric == 'VOC': self._metrics = [ diff --git a/dygraph/ppdet/metrics/metrics.py b/dygraph/ppdet/metrics/metrics.py index bed5ae18a..9f801a135 100644 --- a/dygraph/ppdet/metrics/metrics.py +++ b/dygraph/ppdet/metrics/metrics.py @@ -65,6 +65,7 @@ class COCOMetric(Metric): self.clsid2catid, self.catid2name = get_categories('COCO', anno_file) # TODO: bias should be unified self.bias = kwargs.get('bias', 0) + self.save_prediction_only = kwargs.get('save_prediction_only', False) self.reset() def reset(self): @@ -97,30 +98,42 @@ class COCOMetric(Metric): json.dump(self.results['bbox'], f) logger.info('The bbox result is saved to bbox.json.') - bbox_stats = cocoapi_eval( - 'bbox.json', 'bbox', anno_file=self.anno_file) - self.eval_results['bbox'] = bbox_stats - sys.stdout.flush() + if self.save_prediction_only: + logger.info('The bbox result is saved to bbox.json and do not ' + 'evaluate the mAP.') + else: + bbox_stats = cocoapi_eval( + 'bbox.json', 'bbox', anno_file=self.anno_file) + self.eval_results['bbox'] = bbox_stats + sys.stdout.flush() if len(self.results['mask']) > 0: with open("mask.json", 'w') as f: json.dump(self.results['mask'], f) logger.info('The mask result is saved to mask.json.') - seg_stats = cocoapi_eval( - 'mask.json', 'segm', anno_file=self.anno_file) - self.eval_results['mask'] = seg_stats - sys.stdout.flush() + if self.save_prediction_only: + logger.info('The mask result is saved to mask.json and do not ' + 'evaluate the mAP.') + else: + seg_stats = cocoapi_eval( + 'mask.json', 'segm', anno_file=self.anno_file) + self.eval_results['mask'] = seg_stats + sys.stdout.flush() if len(self.results['segm']) > 0: with open("segm.json", 'w') as f: json.dump(self.results['segm'], f) logger.info('The segm result is saved to segm.json.') - seg_stats = cocoapi_eval( - 'segm.json', 'segm', anno_file=self.anno_file) - self.eval_results['mask'] = seg_stats - sys.stdout.flush() + if self.save_prediction_only: + logger.info('The segm result is saved to segm.json and do not ' + 'evaluate the mAP.') + else: + seg_stats = cocoapi_eval( + 'segm.json', 'segm', anno_file=self.anno_file) + self.eval_results['mask'] = seg_stats + sys.stdout.flush() def log(self): pass diff --git a/dygraph/tools/eval.py b/dygraph/tools/eval.py index 690cc5501..2536f5d09 100755 --- a/dygraph/tools/eval.py +++ b/dygraph/tools/eval.py @@ -64,6 +64,12 @@ def parse_args(): action="store_true", help="whether add bias or not while getting w and h") + parser.add_argument( + '--save_prediction_only', + action='store_true', + default=False, + help='Whether to save the evaluation results only') + args = parser.parse_args() return args @@ -88,6 +94,7 @@ def main(): cfg = load_config(FLAGS.config) # TODO: bias should be unified cfg['bias'] = 1 if FLAGS.bias else 0 + cfg['save_prediction_only'] = FLAGS.save_prediction_only merge_config(FLAGS.opt) if FLAGS.slim_config: slim_cfg = load_config(FLAGS.slim_config) diff --git a/dygraph/tools/train.py b/dygraph/tools/train.py index b621991a6..59351028f 100755 --- a/dygraph/tools/train.py +++ b/dygraph/tools/train.py @@ -65,6 +65,11 @@ def parse_args(): default=False, help="If set True, enable continuous evaluation job." "This flag is only used for internal test.") + parser.add_argument( + '--save_prediction_only', + action='store_true', + default=False, + help='Whether to save the evaluation results only') args = parser.parse_args() return args @@ -91,6 +96,7 @@ def main(): FLAGS = parse_args() cfg = load_config(FLAGS.config) + cfg['save_prediction_only'] = FLAGS.save_prediction_only merge_config(FLAGS.opt) if FLAGS.slim_config: slim_cfg = load_config(FLAGS.slim_config) -- GitLab