benchmark_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import logging
  3. import os
  4. import os.path as osp
  5. from argparse import ArgumentParser
  6. from mmengine.config import Config, DictAction
  7. from mmengine.logging import MMLogger
  8. from mmengine.registry import RUNNERS
  9. from mmengine.runner import Runner
  10. from mmdet.testing import replace_to_ceph
  11. from mmdet.utils import register_all_modules, replace_cfg_vals
  12. def parse_args():
  13. parser = ArgumentParser()
  14. parser.add_argument('config', help='test config file path')
  15. parser.add_argument('checkpoint_root', help='Checkpoint file root path')
  16. parser.add_argument('--work-dir', help='the dir to save logs')
  17. parser.add_argument('--ceph', action='store_true')
  18. parser.add_argument(
  19. '--cfg-options',
  20. nargs='+',
  21. action=DictAction,
  22. help='override some settings in the used config, the key-value pair '
  23. 'in xxx=yyy format will be merged into config file. If the value to '
  24. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  25. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  26. 'Note that the quotation marks are necessary and that no white space '
  27. 'is allowed.')
  28. parser.add_argument(
  29. '--launcher',
  30. choices=['none', 'pytorch', 'slurm', 'mpi'],
  31. default='none',
  32. help='job launcher')
  33. parser.add_argument('--local_rank', type=int, default=0)
  34. args = parser.parse_args()
  35. if 'LOCAL_RANK' not in os.environ:
  36. os.environ['LOCAL_RANK'] = str(args.local_rank)
  37. args = parser.parse_args()
  38. return args
  39. # TODO: Need to refactor test.py so that it can be reused.
  40. def fast_test_model(config_name, checkpoint, args, logger=None):
  41. cfg = Config.fromfile(config_name)
  42. cfg = replace_cfg_vals(cfg)
  43. cfg.launcher = args.launcher
  44. if args.cfg_options is not None:
  45. cfg.merge_from_dict(args.cfg_options)
  46. # work_dir is determined in this priority: CLI > segment in file > filename
  47. if args.work_dir is not None:
  48. # update configs according to CLI args if args.work_dir is not None
  49. cfg.work_dir = osp.join(args.work_dir,
  50. osp.splitext(osp.basename(config_name))[0])
  51. elif cfg.get('work_dir', None) is None:
  52. # use config filename as default work_dir if cfg.work_dir is None
  53. cfg.work_dir = osp.join('./work_dirs',
  54. osp.splitext(osp.basename(config_name))[0])
  55. if args.ceph:
  56. replace_to_ceph(cfg)
  57. cfg.load_from = checkpoint
  58. # TODO: temporary plan
  59. if 'visualizer' in cfg:
  60. if 'name' in cfg.visualizer:
  61. del cfg.visualizer.name
  62. # build the runner from config
  63. if 'runner_type' not in cfg:
  64. # build the default runner
  65. runner = Runner.from_cfg(cfg)
  66. else:
  67. # build customized runner from the registry
  68. # if 'runner_type' is set in the cfg
  69. runner = RUNNERS.build(cfg)
  70. runner.test()
  71. # Sample test whether the inference code is correct
  72. def main(args):
  73. # register all modules in mmdet into the registries
  74. register_all_modules(init_default_scope=False)
  75. config = Config.fromfile(args.config)
  76. # test all model
  77. logger = MMLogger.get_instance(
  78. name='MMLogger',
  79. log_file='benchmark_test.log',
  80. log_level=logging.ERROR)
  81. for model_key in config:
  82. model_infos = config[model_key]
  83. if not isinstance(model_infos, list):
  84. model_infos = [model_infos]
  85. for model_info in model_infos:
  86. print('processing: ', model_info['config'], flush=True)
  87. config_name = model_info['config'].strip()
  88. checkpoint = osp.join(args.checkpoint_root,
  89. model_info['checkpoint'].strip())
  90. try:
  91. fast_test_model(config_name, checkpoint, args, logger)
  92. except Exception as e:
  93. logger.error(f'{config_name} " : {repr(e)}')
  94. if __name__ == '__main__':
  95. args = parse_args()
  96. main(args)