config_utils.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env python
  2. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import os.path as osp
  17. from collections.abc import Mapping
  18. import yaml
  19. def _chain_maps(*maps):
  20. chained = dict()
  21. keys = set().union(*maps)
  22. for key in keys:
  23. vals = [m[key] for m in maps if key in m]
  24. if isinstance(vals[0], Mapping):
  25. chained[key] = _chain_maps(*vals)
  26. else:
  27. chained[key] = vals[0]
  28. return chained
  29. def read_config(config_path):
  30. with open(config_path, 'r', encoding='utf-8') as f:
  31. cfg = yaml.safe_load(f)
  32. return cfg or {}
  33. def parse_configs(cfg_path, inherit=True):
  34. if inherit:
  35. cfgs = []
  36. cfgs.append(read_config(cfg_path))
  37. while cfgs[-1].get('_base_'):
  38. base_path = cfgs[-1].pop('_base_')
  39. curr_dir = osp.dirname(cfg_path)
  40. cfgs.append(
  41. read_config(osp.normpath(osp.join(curr_dir, base_path))))
  42. return _chain_maps(*cfgs)
  43. else:
  44. return read_config(cfg_path)
  45. def _cfg2args(cfg, parser, prefix=''):
  46. node_keys = set()
  47. for k, v in cfg.items():
  48. opt = prefix + k
  49. if isinstance(v, list):
  50. if len(v) == 0:
  51. parser.add_argument(
  52. '--' + opt, type=object, nargs='*', default=v)
  53. else:
  54. # Only apply to homogeneous lists
  55. if isinstance(v[0], CfgNode):
  56. node_keys.add(opt)
  57. parser.add_argument(
  58. '--' + opt, type=type(v[0]), nargs='*', default=v)
  59. elif isinstance(v, dict):
  60. # Recursively parse a dict
  61. _, new_node_keys = _cfg2args(v, parser, opt + '.')
  62. node_keys.update(new_node_keys)
  63. elif isinstance(v, CfgNode):
  64. node_keys.add(opt)
  65. _, new_node_keys = _cfg2args(v.to_dict(), parser, opt + '.')
  66. node_keys.update(new_node_keys)
  67. elif isinstance(v, bool):
  68. parser.add_argument('--' + opt, action='store_true', default=v)
  69. else:
  70. parser.add_argument('--' + opt, type=type(v), default=v)
  71. return parser, node_keys
  72. def _args2cfg(cfg, args, node_keys):
  73. args = vars(args)
  74. for k, v in args.items():
  75. pos = k.find('.')
  76. if pos != -1:
  77. # Iteratively parse a dict
  78. dict_ = cfg
  79. while pos != -1:
  80. dict_.setdefault(k[:pos], {})
  81. dict_ = dict_[k[:pos]]
  82. k = k[pos + 1:]
  83. pos = k.find('.')
  84. dict_[k] = v
  85. else:
  86. cfg[k] = v
  87. for k in node_keys:
  88. pos = k.find('.')
  89. if pos != -1:
  90. # Iteratively parse a dict
  91. dict_ = cfg
  92. while pos != -1:
  93. dict_.setdefault(k[:pos], {})
  94. dict_ = dict_[k[:pos]]
  95. k = k[pos + 1:]
  96. pos = k.find('.')
  97. v = dict_[k]
  98. dict_[k] = [CfgNode(v_) for v_ in v] if isinstance(
  99. v, list) else CfgNode(v)
  100. else:
  101. v = cfg[k]
  102. cfg[k] = [CfgNode(v_) for v_ in v] if isinstance(
  103. v, list) else CfgNode(v)
  104. return cfg
  105. def parse_args(*args, **kwargs):
  106. cfg_parser = argparse.ArgumentParser(add_help=False)
  107. cfg_parser.add_argument('--config', type=str, default='')
  108. cfg_parser.add_argument('--inherit_off', action='store_true')
  109. cfg_args = cfg_parser.parse_known_args()[0]
  110. cfg_path = cfg_args.config
  111. inherit_on = not cfg_args.inherit_off
  112. # Main parser
  113. parser = argparse.ArgumentParser(
  114. conflict_handler='resolve', parents=[cfg_parser])
  115. # Global settings
  116. parser.add_argument('cmd', choices=['train', 'eval'])
  117. parser.add_argument('task', choices=['cd', 'clas', 'det', 'res', 'seg'])
  118. parser.add_argument('--seed', type=int, default=None)
  119. # Data
  120. parser.add_argument('--datasets', type=dict, default={})
  121. parser.add_argument('--transforms', type=dict, default={})
  122. parser.add_argument('--download_on', action='store_true')
  123. parser.add_argument('--download_url', type=str, default='')
  124. parser.add_argument('--download_path', type=str, default='./')
  125. # Optimizer
  126. parser.add_argument('--optimizer', type=dict, default={})
  127. # Training related
  128. parser.add_argument('--num_epochs', type=int, default=100)
  129. parser.add_argument('--train_batch_size', type=int, default=8)
  130. parser.add_argument('--save_interval_epochs', type=int, default=1)
  131. parser.add_argument('--log_interval_steps', type=int, default=1)
  132. parser.add_argument('--save_dir', default='../exp/')
  133. parser.add_argument('--learning_rate', type=float, default=0.01)
  134. parser.add_argument('--early_stop', action='store_true')
  135. parser.add_argument('--early_stop_patience', type=int, default=5)
  136. parser.add_argument('--use_vdl', action='store_true')
  137. parser.add_argument('--resume_checkpoint', type=str)
  138. parser.add_argument('--train', type=dict, default={})
  139. # Loss
  140. parser.add_argument('--losses', type=dict, nargs='+', default={})
  141. # Model
  142. parser.add_argument('--model', type=dict, default={})
  143. if osp.exists(cfg_path):
  144. cfg = parse_configs(cfg_path, inherit_on)
  145. parser, node_keys = _cfg2args(cfg, parser, '')
  146. node_keys = sorted(node_keys, reverse=True)
  147. args = parser.parse_args(*args, **kwargs)
  148. return _args2cfg(dict(), args, node_keys)
  149. elif cfg_path != '':
  150. raise FileNotFoundError
  151. else:
  152. args = parser.parse_args()
  153. return _args2cfg(dict(), args, set())
  154. class _CfgNodeMeta(yaml.YAMLObjectMetaclass):
  155. def __call__(cls, obj):
  156. if isinstance(obj, CfgNode):
  157. return obj
  158. return super(_CfgNodeMeta, cls).__call__(obj)
  159. class CfgNode(yaml.YAMLObject, metaclass=_CfgNodeMeta):
  160. yaml_tag = u'!Node'
  161. yaml_loader = yaml.SafeLoader
  162. # By default use a lexical scope
  163. ctx = globals()
  164. def __init__(self, dict_):
  165. super().__init__()
  166. self.type = dict_['type']
  167. self.args = dict_.get('args', [])
  168. self.module = dict_.get('module', '')
  169. @classmethod
  170. def set_context(cls, ctx):
  171. # TODO: Implement dynamic scope with inspect.stack()
  172. old_ctx = cls.ctx
  173. cls.ctx = ctx
  174. return old_ctx
  175. def build_object(self, mod=None):
  176. if mod is None:
  177. mod = self._get_module(self.module)
  178. cls = getattr(mod, self.type)
  179. if isinstance(self.args, list):
  180. args = build_objects(self.args)
  181. obj = cls(*args)
  182. elif isinstance(self.args, dict):
  183. args = build_objects(self.args)
  184. obj = cls(**args)
  185. else:
  186. raise NotImplementedError
  187. return obj
  188. def _get_module(self, s):
  189. mod = None
  190. while s:
  191. idx = s.find('.')
  192. if idx == -1:
  193. next_ = s
  194. s = ''
  195. else:
  196. next_ = s[:idx]
  197. s = s[idx + 1:]
  198. if mod is None:
  199. mod = self.ctx[next_]
  200. else:
  201. mod = getattr(mod, next_)
  202. return mod
  203. @staticmethod
  204. def build_objects(cfg, mod=None):
  205. if isinstance(cfg, list):
  206. return [CfgNode.build_objects(c, mod=mod) for c in cfg]
  207. elif isinstance(cfg, CfgNode):
  208. return cfg.build_object(mod=mod)
  209. elif isinstance(cfg, dict):
  210. return {
  211. k: CfgNode.build_objects(
  212. v, mod=mod)
  213. for k, v in cfg.items()
  214. }
  215. else:
  216. return cfg
  217. def __repr__(self):
  218. return f"(type={self.type}, args={self.args}, module={self.module or ' '})"
  219. @classmethod
  220. def from_yaml(cls, loader, node):
  221. map_ = loader.construct_mapping(node)
  222. return cls(map_)
  223. def items(self):
  224. yield from [('type', self.type), ('args', self.args), ('module',
  225. self.module)]
  226. def to_dict(self):
  227. return dict(self.items())
  228. def build_objects(cfg, mod=None):
  229. return CfgNode.build_objects(cfg, mod=mod)