From a2a799977d77aa35c802af657c2ee989671786d8 Mon Sep 17 00:00:00 2001 From: Eunwoo Shin Date: Thu, 8 Jun 2023 13:36:59 +0900 Subject: [PATCH] Decrease a time for making a workspace (#2223) * do not use pretty text * align with pre commit * add change log * update comment --- CHANGELOG.md | 1 + .../adapters/mmcv/utils/config_utils.py | 91 ++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 372971ab72f..af891dd1dc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. ### Enhancements - Introduce channel_last parameter to improve the performance () +- Decrease a time for making a workspace () ### Bug fixes diff --git a/otx/algorithms/common/adapters/mmcv/utils/config_utils.py b/otx/algorithms/common/adapters/mmcv/utils/config_utils.py index 2675ce74583..8c366edd655 100644 --- a/otx/algorithms/common/adapters/mmcv/utils/config_utils.py +++ b/otx/algorithms/common/adapters/mmcv/utils/config_utils.py @@ -161,7 +161,96 @@ def fromfile(filename, use_predefined_variables=True, import_custom_modules=True cfg_dict, cfg_text = MPAConfig._file2dict(filename, use_predefined_variables) if import_custom_modules and cfg_dict.get("custom_imports", None): import_modules_from_strings(**cfg_dict["custom_imports"]) - return Config(cfg_dict, cfg_text=cfg_text, filename=filename) + return MPAConfig(cfg_dict, cfg_text=cfg_text, filename=filename) + + @property + def pretty_text(self): + """Make python file human-readable. + + It's almost same as mmcv.Config's code but code to reformat using yapf is removed to reduce time. + """ + + indent = 4 + + def _indent(s_, num_spaces): + s = s_.split("\n") + if len(s) == 1: + return s_ + first = s.pop(0) + s = [(num_spaces * " ") + line for line in s] + s = "\n".join(s) + s = first + "\n" + s + return s + + def _format_basic_types(k, v, use_mapping=False): + if isinstance(v, str): + v_str = f"'{v}'" + else: + v_str = str(v) + + if use_mapping: + k_str = f"'{k}'" if isinstance(k, str) else str(k) + attr_str = f"{k_str}: {v_str}" + else: + attr_str = f"{str(k)}={v_str}" + attr_str = _indent(attr_str, indent) + + return attr_str + + def _format_list(k, v, use_mapping=False): + # check if all items in the list are dict + if all(isinstance(_, dict) for _ in v): + v_str = "[\n" + v_str += "\n".join(f"dict({_indent(_format_dict(v_), indent)})," for v_ in v).rstrip(",") + if use_mapping: + k_str = f"'{k}'" if isinstance(k, str) else str(k) + attr_str = f"{k_str}: {v_str}" + else: + attr_str = f"{str(k)}={v_str}" + attr_str = _indent(attr_str, indent) + "]" + else: + attr_str = _format_basic_types(k, v, use_mapping) + return attr_str + + def _contain_invalid_identifier(dict_str): + contain_invalid_identifier = False + for key_name in dict_str: + contain_invalid_identifier |= not str(key_name).isidentifier() + return contain_invalid_identifier + + def _format_dict(input_dict, outest_level=False): + r = "" + s = [] + + use_mapping = _contain_invalid_identifier(input_dict) + if use_mapping: + r += "{" + for idx, (k, v) in enumerate(input_dict.items()): + is_last = idx >= len(input_dict) - 1 + end = "" if outest_level or is_last else "," + if isinstance(v, dict): + v_str = "\n" + _format_dict(v) + if use_mapping: + k_str = f"'{k}'" if isinstance(k, str) else str(k) + attr_str = f"{k_str}: dict({v_str}" + else: + attr_str = f"{str(k)}=dict({v_str}" + attr_str = _indent(attr_str, indent) + ")" + end + elif isinstance(v, list): + attr_str = _format_list(k, v, use_mapping) + end + else: + attr_str = _format_basic_types(k, v, use_mapping) + end + + s.append(attr_str) + r += "\n".join(s) + if use_mapping: + r += "}" + return r + + cfg_dict = self._cfg_dict.to_dict() + text = _format_dict(cfg_dict, outest_level=True) + + return text def copy_config(cfg):