print_config.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os
  4. from mmengine import Config, DictAction
  5. from mmdet.utils import replace_cfg_vals, update_data_root
  6. def parse_args():
  7. parser = argparse.ArgumentParser(description='Print the whole config')
  8. parser.add_argument('config', help='config file path')
  9. parser.add_argument(
  10. '--save-path',
  11. default=None,
  12. help='save path of whole config, suffixed with .py, .json or .yml')
  13. parser.add_argument(
  14. '--cfg-options',
  15. nargs='+',
  16. action=DictAction,
  17. help='override some settings in the used config, the key-value pair '
  18. 'in xxx=yyy format will be merged into config file. If the value to '
  19. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  20. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  21. 'Note that the quotation marks are necessary and that no white space '
  22. 'is allowed.')
  23. args = parser.parse_args()
  24. return args
  25. def main():
  26. args = parse_args()
  27. cfg = Config.fromfile(args.config)
  28. # replace the ${key} with the value of cfg.key
  29. cfg = replace_cfg_vals(cfg)
  30. # update data root according to MMDET_DATASETS
  31. update_data_root(cfg)
  32. if args.cfg_options is not None:
  33. cfg.merge_from_dict(args.cfg_options)
  34. print(f'Config:\n{cfg.pretty_text}')
  35. if args.save_path is not None:
  36. save_path = args.save_path
  37. suffix = os.path.splitext(save_path)[-1]
  38. assert suffix in ['.py', '.json', '.yml']
  39. if not os.path.exists(os.path.split(save_path)[0]):
  40. os.makedirs(os.path.split(save_path)[0])
  41. cfg.dump(save_path)
  42. print(f'Config saving at {save_path}')
  43. if __name__ == '__main__':
  44. main()