train.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import logging
  4. import os
  5. import os.path as osp
  6. from mmengine.config import Config, DictAction
  7. from mmengine.logging import print_log
  8. from mmengine.runner import Runner
  9. from mmseg.registry import RUNNERS
  10. import model
  11. def parse_args():
  12. parser = argparse.ArgumentParser(description='Train a segmentor')
  13. parser.add_argument('config', help='train config file path')
  14. parser.add_argument('--work-dir', help='the dir to save logs and models')
  15. parser.add_argument(
  16. '--resume',
  17. action='store_true',
  18. default=False,
  19. help='resume from the latest checkpoint in the work_dir automatically')
  20. parser.add_argument(
  21. '--amp',
  22. action='store_true',
  23. default=False,
  24. help='enable automatic-mixed-precision training')
  25. parser.add_argument(
  26. '--cfg-options',
  27. nargs='+',
  28. action=DictAction,
  29. help='override some settings in the used config, the key-value pair '
  30. 'in xxx=yyy format will be merged into config file. If the value to '
  31. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  32. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  33. 'Note that the quotation marks are necessary and that no white space '
  34. 'is allowed.')
  35. parser.add_argument(
  36. '--launcher',
  37. choices=['none', 'pytorch', 'slurm', 'mpi'],
  38. default='none',
  39. help='job launcher')
  40. # When using PyTorch version >= 2.0.0, the `torch.distributed.launch`
  41. # will pass the `--local-rank` parameter to `tools/train.py` instead
  42. # of `--local_rank`.
  43. parser.add_argument('--local_rank', '--local-rank', type=int, default=0)
  44. args = parser.parse_args()
  45. if 'LOCAL_RANK' not in os.environ:
  46. os.environ['LOCAL_RANK'] = str(args.local_rank)
  47. return args
  48. def main():
  49. args = parse_args()
  50. # load config
  51. cfg = Config.fromfile(args.config)
  52. cfg.launcher = args.launcher
  53. if args.cfg_options is not None:
  54. cfg.merge_from_dict(args.cfg_options)
  55. # work_dir is determined in this priority: CLI > segment in file > filename
  56. if args.work_dir is not None:
  57. # update configs according to CLI args if args.work_dir is not None
  58. cfg.work_dir = args.work_dir
  59. elif cfg.get('work_dir', None) is None:
  60. # use config filename as default work_dir if cfg.work_dir is None
  61. cfg.work_dir = osp.join('./work_dirs',
  62. osp.splitext(osp.basename(args.config))[0])
  63. # enable automatic-mixed-precision training
  64. if args.amp is True:
  65. optim_wrapper = cfg.optim_wrapper.type
  66. if optim_wrapper == 'AmpOptimWrapper':
  67. print_log(
  68. 'AMP training is already enabled in your config.',
  69. logger='current',
  70. level=logging.WARNING)
  71. else:
  72. assert optim_wrapper == 'OptimWrapper', (
  73. '`--amp` is only supported when the optimizer wrapper type is '
  74. f'`OptimWrapper` but got {optim_wrapper}.')
  75. cfg.optim_wrapper.type = 'AmpOptimWrapper'
  76. cfg.optim_wrapper.loss_scale = 'dynamic'
  77. # resume training
  78. cfg.resume = args.resume
  79. # build the runner from config
  80. if 'runner_type' not in cfg:
  81. # build the default runner
  82. runner = Runner.from_cfg(cfg)
  83. else:
  84. # build customized runner from the registry
  85. # if 'runner_type' is set in the cfg
  86. runner = RUNNERS.build(cfg)
  87. # start training
  88. runner.train()
  89. if __name__ == '__main__':
  90. main()