print_config.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import warnings
  4. from mmengine import Config, DictAction
  5. from mmseg.apis import init_model
  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. '--graph', action='store_true', help='print the models graph')
  11. parser.add_argument(
  12. '--options',
  13. nargs='+',
  14. action=DictAction,
  15. help="--options is deprecated in favor of --cfg_options' and it will "
  16. 'not be supported in version v0.22.0. Override some settings in the '
  17. 'used config, the key-value pair in xxx=yyy format will be merged '
  18. 'into config file. If the value to be overwritten is a list, it '
  19. 'should be like key="[a,b]" or key=a,b It also allows nested '
  20. 'list/tuple values, e.g. key="[(a,b),(c,d)]" Note that the quotation '
  21. 'marks are necessary and that no white space is allowed.')
  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. args = parser.parse_args()
  33. if args.options and args.cfg_options:
  34. raise ValueError(
  35. '--options and --cfg-options cannot be both '
  36. 'specified, --options is deprecated in favor of --cfg-options. '
  37. '--options will not be supported in version v0.22.0.')
  38. if args.options:
  39. warnings.warn('--options is deprecated in favor of --cfg-options, '
  40. '--options will not be supported in version v0.22.0.')
  41. args.cfg_options = args.options
  42. return args
  43. def main():
  44. args = parse_args()
  45. cfg = Config.fromfile(args.config)
  46. if args.cfg_options is not None:
  47. cfg.merge_from_dict(args.cfg_options)
  48. print(f'Config:\n{cfg.pretty_text}')
  49. # dump config
  50. cfg.dump('example.py')
  51. # dump models graph
  52. if args.graph:
  53. model = init_model(args.config, device='cpu')
  54. print(f'Model graph:\n{str(model)}')
  55. with open('example-graph.txt', 'w') as f:
  56. f.writelines(str(model))
  57. if __name__ == '__main__':
  58. main()