You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

eval_metric.py 3.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import mmcv
  4. from mmcv import Config, DictAction
  5. from mmdet.datasets import build_dataset
  6. def parse_args():
  7. parser = argparse.ArgumentParser(description='Evaluate metric of the '
  8. 'results saved in pkl format')
  9. parser.add_argument('config', help='Config of the model')
  10. parser.add_argument('pkl_results', help='Results in pickle format')
  11. parser.add_argument(
  12. '--format-only',
  13. action='store_true',
  14. help='Format the output results without perform evaluation. It is'
  15. 'useful when you want to format the result to a specific format and '
  16. 'submit it to the test server')
  17. parser.add_argument(
  18. '--eval',
  19. type=str,
  20. nargs='+',
  21. help='Evaluation metrics, which depends on the dataset, e.g., "bbox",'
  22. ' "segm", "proposal" for COCO, and "mAP", "recall" for PASCAL VOC')
  23. parser.add_argument(
  24. '--cfg-options',
  25. nargs='+',
  26. action=DictAction,
  27. help='override some settings in the used config, the key-value pair '
  28. 'in xxx=yyy format will be merged into config file. If the value to '
  29. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  30. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  31. 'Note that the quotation marks are necessary and that no white space '
  32. 'is allowed.')
  33. parser.add_argument(
  34. '--eval-options',
  35. nargs='+',
  36. action=DictAction,
  37. help='custom options for evaluation, the key-value pair in xxx=yyy '
  38. 'format will be kwargs for dataset.evaluate() function')
  39. args = parser.parse_args()
  40. return args
  41. def main():
  42. args = parse_args()
  43. cfg = Config.fromfile(args.config)
  44. assert args.eval or args.format_only, (
  45. 'Please specify at least one operation (eval/format the results) with '
  46. 'the argument "--eval", "--format-only"')
  47. if args.eval and args.format_only:
  48. raise ValueError('--eval and --format_only cannot be both specified')
  49. if args.cfg_options is not None:
  50. cfg.merge_from_dict(args.cfg_options)
  51. # import modules from string list.
  52. if cfg.get('custom_imports', None):
  53. from mmcv.utils import import_modules_from_strings
  54. import_modules_from_strings(**cfg['custom_imports'])
  55. cfg.data.test.test_mode = True
  56. dataset = build_dataset(cfg.data.test)
  57. outputs = mmcv.load(args.pkl_results)
  58. kwargs = {} if args.eval_options is None else args.eval_options
  59. if args.format_only:
  60. dataset.format_results(outputs, **kwargs)
  61. if args.eval:
  62. eval_kwargs = cfg.get('evaluation', {}).copy()
  63. # hard-code way to remove EvalHook args
  64. for key in [
  65. 'interval', 'tmpdir', 'start', 'gpu_collect', 'save_best',
  66. 'rule'
  67. ]:
  68. eval_kwargs.pop(key, None)
  69. eval_kwargs.update(dict(metric=args.eval, **kwargs))
  70. print(dataset.evaluate(outputs, **eval_kwargs))
  71. if __name__ == '__main__':
  72. main()

No Description

Contributors (3)