test_tracking.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os
  4. import os.path as osp
  5. from mmengine.config import Config, DictAction
  6. from mmengine.model import is_model_wrapper
  7. from mmengine.registry import RUNNERS
  8. from mmengine.runner import Runner
  9. from mmengine.runner.checkpoint import load_checkpoint
  10. from mmdet.utils import register_all_modules
  11. # TODO: support fuse_conv_bn, visualization, and format_only
  12. def parse_args():
  13. parser = argparse.ArgumentParser(
  14. description='MMTrack test (and eval) a model')
  15. parser.add_argument('config', help='test config file path')
  16. parser.add_argument('--checkpoint', help='checkpoint file')
  17. parser.add_argument('--detector', help='detection checkpoint file')
  18. parser.add_argument('--reid', help='reid checkpoint file')
  19. parser.add_argument(
  20. '--work-dir',
  21. help='the directory to save the file containing evaluation metrics')
  22. parser.add_argument(
  23. '--cfg-options',
  24. nargs='+',
  25. action=DictAction,
  26. help='override some settings in the used config, the key-value pair '
  27. 'in xxx=yyy format will be merged into config file. If the value to '
  28. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  29. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  30. 'Note that the quotation marks are necessary and that no white space '
  31. 'is allowed.')
  32. parser.add_argument(
  33. '--launcher',
  34. choices=['none', 'pytorch', 'slurm', 'mpi'],
  35. default='none',
  36. help='job launcher')
  37. parser.add_argument('--local-rank', type=int, default=0)
  38. args = parser.parse_args()
  39. if 'LOCAL_RANK' not in os.environ:
  40. os.environ['LOCAL_RANK'] = str(args.local_rank)
  41. return args
  42. def main():
  43. args = parse_args()
  44. # register all modules in mmtrack into the registries
  45. # do not init the default scope here because it will be init in the runner
  46. register_all_modules(init_default_scope=False)
  47. # load config
  48. cfg = Config.fromfile(args.config)
  49. cfg.launcher = args.launcher
  50. if args.cfg_options is not None:
  51. cfg.merge_from_dict(args.cfg_options)
  52. # work_dir is determined in this priority: CLI > segment in file > filename
  53. if args.work_dir is not None:
  54. # update configs according to CLI args if args.work_dir is not None
  55. cfg.work_dir = args.work_dir
  56. elif cfg.get('work_dir', None) is None:
  57. # use config filename as default work_dir if cfg.work_dir is None
  58. cfg.work_dir = osp.join('./work_dirs',
  59. osp.splitext(osp.basename(args.config))[0])
  60. cfg.load_from = args.checkpoint
  61. # build the runner from config
  62. if 'runner_type' not in cfg:
  63. # build the default runner
  64. runner = Runner.from_cfg(cfg)
  65. else:
  66. # build customized runner from the registry
  67. # if 'runner_type' is set in the cfg
  68. runner = RUNNERS.build(cfg)
  69. if is_model_wrapper(runner.model):
  70. model = runner.model.module
  71. else:
  72. model = runner.model
  73. if args.detector:
  74. assert not (args.checkpoint and args.detector), \
  75. 'Error: checkpoint and detector checkpoint cannot both exist'
  76. load_checkpoint(model.detector, args.detector)
  77. if args.reid:
  78. assert not (args.checkpoint and args.reid), \
  79. 'Error: checkpoint and reid checkpoint cannot both exist'
  80. load_checkpoint(model.reid, args.reid)
  81. # start testing
  82. runner.test()
  83. if __name__ == '__main__':
  84. main()