Skip to content

Commit

Permalink
Decrease a time for making a workspace (#2223)
Browse files Browse the repository at this point in the history
* do not use pretty text

* align with pre commit

* add change log

* update comment
  • Loading branch information
eunwoosh authored Jun 8, 2023
1 parent 09a5747 commit a2a7999
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<https://github.com/openvinotoolkit/training_extensions/pull/2205>)
- Decrease a time for making a workspace (<https://github.com/openvinotoolkit/training_extensions/pull/2223>)

### Bug fixes

Expand Down
91 changes: 90 additions & 1 deletion otx/algorithms/common/adapters/mmcv/utils/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit a2a7999

Please sign in to comment.