config_utils.py 8.5 KB

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