Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] add config new feature #105

Merged
merged 56 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
8f2e7bc
update
HAOCHENYE Mar 6, 2022
af597a1
update
HAOCHENYE Mar 6, 2022
1ee2c59
update
HAOCHENYE Mar 6, 2022
db8df66
add config new feature
HAOCHENYE Mar 7, 2022
22a042a
update
HAOCHENYE Mar 7, 2022
fe61253
update
HAOCHENYE Mar 7, 2022
a9ad44d
remove unused file
HAOCHENYE Mar 7, 2022
d91aa0d
Update pretrained
HAOCHENYE Mar 7, 2022
89e15de
update
HAOCHENYE Mar 7, 2022
837128b
Fix as commetn
HAOCHENYE Mar 9, 2022
43b6eac
update
HAOCHENYE Mar 9, 2022
45c77c8
update
HAOCHENYE Mar 10, 2022
752ea85
Add get_config and get_model test
HAOCHENYE Mar 10, 2022
23b3720
update
HAOCHENYE Mar 10, 2022
d38004c
resolve conflict in mmengine.__init__.py
HAOCHENYE Mar 14, 2022
cb024df
clean code and add comment to config.py
HAOCHENYE Mar 14, 2022
c6d7ddf
fix cycle import
HAOCHENYE Mar 14, 2022
4591f41
fix as comment
HAOCHENYE Jun 10, 2022
69e41a0
temp save
HAOCHENYE Jun 20, 2022
69086c1
tmp
HAOCHENYE Jun 20, 2022
ab9d516
tmp
HAOCHENYE Jun 20, 2022
c754b4c
add unit test
HAOCHENYE Jun 21, 2022
23c4e93
support modify base variable and add unit test
HAOCHENYE Jun 21, 2022
14cfb57
refine build and docstring
HAOCHENYE Jun 21, 2022
f8940c7
move switch scope to build_from_cfg
HAOCHENYE Jun 22, 2022
33d9853
add docstring
HAOCHENYE Jun 22, 2022
da4e1ef
add build model from cfg
HAOCHENYE Jun 22, 2022
2ed530b
fix root registry
HAOCHENYE Jun 22, 2022
20b550d
Merge remote-tracking branch 'origin/main' into HAOCHENYE/config_new_…
HAOCHENYE Jun 22, 2022
313124c
fix docstring
HAOCHENYE Jun 22, 2022
34a7b14
fix remove scope bug
HAOCHENYE Jun 23, 2022
aa3d085
Merge remote-tracking branch 'origin/HAOCHENYE/config_new_feature' in…
HAOCHENYE Jun 23, 2022
b60ef05
fix bug in get model
HAOCHENYE Jun 23, 2022
a33bbae
fix get model bug
HAOCHENYE Jun 24, 2022
fa74795
Merge remote-tracking branch 'origin/HAOCHENYE/config_new_feature' in…
HAOCHENYE Jun 24, 2022
960feb7
add scope to list cfg
HAOCHENYE Jun 27, 2022
5f7a0f5
support get attribute of base variable in config
HAOCHENYE Jun 29, 2022
4afed70
add docstring to _dict_to_config_dict
HAOCHENYE Jun 29, 2022
5b8a2d8
minor refine
HAOCHENYE Jun 29, 2022
2114c25
fix lint
HAOCHENYE Jun 29, 2022
bc53cc8
update registry code
HAOCHENYE Jun 30, 2022
1db616b
add scope when access
HAOCHENYE Jul 4, 2022
aeaa4af
Merge branch 'main' into HAOCHENYE/config_new_feature
HAOCHENYE Jul 4, 2022
c07c0cd
remove unnecessary scope in local base config
HAOCHENYE Jul 4, 2022
b3ea3e3
upload test file
HAOCHENYE Jul 4, 2022
672f95d
fix circle import
HAOCHENYE Jul 4, 2022
2f86cbb
refine ut
HAOCHENYE Jul 6, 2022
912ed85
Merge branch 'main' into HAOCHENYE/config_new_feature
HAOCHENYE Jul 14, 2022
9d4e4c3
fix nested get external config bug
HAOCHENYE Jul 14, 2022
cf75c4e
add comments
HAOCHENYE Jul 14, 2022
aef5dbe
Merge branch 'main' into HAOCHENYE/config_new_feature
HAOCHENYE Jul 21, 2022
2cfb3fa
remove check install in switch_scope_and_registry
HAOCHENYE Jul 27, 2022
4815a55
Merge branch 'main' into HAOCHENYE/config_new_feature
HAOCHENYE Aug 4, 2022
620a36d
only pick first weight
HAOCHENYE Aug 5, 2022
9c20498
Merge branch 'main' into HAOCHENYE/config_new_feature
HAOCHENYE Aug 8, 2022
048c138
fix comment
HAOCHENYE Aug 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update pretrained
  • Loading branch information
HAOCHENYE committed Mar 10, 2022
commit d91aa0d939eaf15b3ccf67ae3d4acfcc2f77da52
9 changes: 5 additions & 4 deletions mmengine/config/collect_meta.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os.path as osp
import re
from typing import Tuple

from mmengine.utils import check_file_exist
from mmengine.fileio import load
import re
from mmengine.utils import check_file_exist


def _get_cfg_meta(package_path: str, cfg_dir: str, cfg_file: str) -> dict:
Expand Down Expand Up @@ -40,7 +41,7 @@ def _get_cfg_meta(package_path: str, cfg_dir: str, cfg_file: str) -> dict:


def _get_external_cfg_path(package_path: str, cfg_dir: str, cfg_file: str):
""" Get relative config path from 'metafile.yml' of external package.
"""Get relative config path from 'metafile.yml' of external package.

Args:
package_path (str): Path of external package.
Expand Down Expand Up @@ -79,7 +80,7 @@ def _parse_external_cfg_path(rel_cfg_path: str) -> Tuple[str, str]:
Returns:
Tuple(str, str): Package name and relative config path.
"""
if re.match(r"\w*::\w*/\w*", rel_cfg_path) is None:
if re.match(r'\w*::\w*/\w*', rel_cfg_path) is None:
raise ValueError('`_parse_external_cfg_path` is used for parse '
'external package, please specify the package name '
'and relative config path, just like '
Expand Down
12 changes: 6 additions & 6 deletions mmengine/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
from yapf.yapflib.yapf_api import FormatCode

from mmengine.fileio import dump, load
from mmengine.utils import (check_file_exist, import_modules_from_strings,
check_install_package, get_installed_path)
from .collect_meta import (_parse_external_cfg_path, _parse_rel_cfg_path,
_get_external_cfg_path, _get_external_cfg_base_path)
from mmengine.utils import (check_file_exist, check_install_package,
get_installed_path, import_modules_from_strings)
from .collect_meta import (_get_external_cfg_base_path, _get_external_cfg_path,
_parse_external_cfg_path, _parse_rel_cfg_path)

BASE_KEY = '_base_'
DELETE_KEY = '_delete_'
Expand Down Expand Up @@ -430,8 +430,8 @@ def _file2dict(filename: str,

cfg_dict_list = list()
cfg_text_list = list()
for f in base_filename:
cfg_path = Config._get_cfg_path(f, filename)
for base_file in base_filename:
cfg_path = Config._get_cfg_path(base_file, filename)
_cfg_dict, _cfg_text = Config._file2dict(cfg_path)
cfg_dict_list.append(_cfg_dict)
cfg_text_list.append(_cfg_text)
Expand Down
41 changes: 28 additions & 13 deletions mmengine/config/download.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import os.path as osp
# Copyright (c) OpenMMLab. All rights reserved.
import importlib
import os.path as osp

import torch.nn as nn

from mmengine.runner import load_checkpoint
from mmengine.utils import check_install_package, get_installed_path
from .collect_meta import (_get_cfg_meta, _get_external_cfg_base_path,
_parse_external_cfg_path, _parse_rel_cfg_path)
from .config import Config
from mmengine.utils import get_installed_path, check_install_package
from .collect_meta import (_parse_external_cfg_path, _parse_rel_cfg_path,
_get_cfg_meta, _get_external_cfg_base_path)


def get_config(rel_cfg_path: str, suffix='.py') -> Config:
def get_config(rel_cfg_path: str, suffix='.py', pretrained: bool = False)\
-> Config:
"""Get config from external package.

Args:
Expand All @@ -18,6 +21,8 @@ def get_config(rel_cfg_path: str, suffix='.py') -> Config:
suffix (str): Suffix of ``rel_cfg_path``. If rel_cfg_path is a base
cfg, the `suffix` will be used to get the absolute config path.
Defaults to '.py'.
pretrained (bool): Whether to save pretrained model path. Defaults to
False.

Returns:
Config: A `Config` parsed from external package.
Expand All @@ -38,15 +43,19 @@ def get_config(rel_cfg_path: str, suffix='.py') -> Config:
cfg_meta = _get_cfg_meta(package_path, rel_cfg_dir, rel_cfg_file)
cfg_path = osp.join(package_path, cfg_meta['Config'])
cfg = Config.fromfile(cfg_path)
assert 'Weights' in cfg_meta, 'Cannot find `Weights` in ' \
'cfg_file.metafile.yml, please check the ' \
'metafile'
cfg.model_path = cfg_meta['Weights']
if pretrained:
assert 'Weights' in cfg_meta, 'Cannot find `Weights` in ' \
'cfg_file.metafile.yml, please check ' \
'the metafile'
cfg.model_path = cfg_meta['Weights']
return cfg


def get_model(rel_cfg_path: str, suffix='.py',
build_func_name: str = 'build_model', **kwargs) -> nn.Module:
def get_model(rel_cfg_path: str,
suffix='.py',
build_func_name: str = 'build_model',
pretrained=False,
**kwargs) -> nn.Module:
"""Get built model from external package.

Args:
Expand All @@ -55,13 +64,14 @@ def get_model(rel_cfg_path: str, suffix='.py',
suffix (str): Suffix of ``rel_cfg_path``. If rel_cfg_path is a base
cfg, the `suffix` will be used to get the absolute config path.
Defaults to '.py'.
build_func_name: Name of model build function. Defaults to
build_func_name (str): Name of model build function. Defaults to
'build_model'
pretrained (bool): Whether to load pretrained model. Defaults to False.

Returns:
nn.Module: Built model.
"""
cfg = get_config(rel_cfg_path, suffix)
cfg = get_config(rel_cfg_path, suffix, pretrained)
package = rel_cfg_path.split('::')[0]

try:
Expand All @@ -74,4 +84,9 @@ def get_model(rel_cfg_path: str, suffix='.py',
raise RuntimeError(f'`{build_func_name}` is not defined in '
f'`{package}.models`')
model = build_func(cfg.model, **kwargs)
if pretrained:
assert hasattr(cfg, 'model_path'), 'Cannot find pretrained model. ' \
f'Please ensure {rel_cfg_path} ' \
'is not a base config.'
model = load_checkpoint(model, cfg.model_path)
return model
4 changes: 2 additions & 2 deletions mmengine/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
requires_executable, requires_package, slice_list,
to_1tuple, to_2tuple, to_3tuple, to_4tuple, to_ntuple,
tuple_cast)
from .package_utils import (call_command, check_install_package,
get_installed_path, is_installed)
from .parrots_wrapper import TORCH_VERSION
from .path import (check_file_exist, fopen, is_filepath, mkdir_or_exist,
scandir, symlink)
from .version_utils import digit_version, get_git_hash
from .package_utils import (is_installed, call_command, get_installed_path,
check_install_package)

__all__ = [
'is_str', 'iter_cast', 'list_cast', 'tuple_cast', 'is_seq_of',
Expand Down
14 changes: 8 additions & 6 deletions mmengine/utils/package_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from pkg_resources import get_distribution
import pkg_resources
import subprocess
import os.path as osp
# Copyright (c) OpenMMLab. All rights reserved.
import importlib
import os.path as osp
import subprocess

import pkg_resources
from pkg_resources import get_distribution


def is_installed(package: str) -> bool:
Expand Down Expand Up @@ -61,9 +63,9 @@ def call_command(cmd: list) -> None:
try:
subprocess.check_call(cmd)
except Exception as e:
raise highlighted_error(e) # type: ignore
raise e # type: ignore


def check_install_package(package: str):
if not is_installed(package):
call_command(['python', '-m', 'pip', 'install', package])
call_command(['python', '-m', 'pip', 'install', package])
8 changes: 2 additions & 6 deletions tests/test_config/test_collect_meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os.path

import pytest

from mmengine.config.collect_meta import (_get_external_cfg_base_path,
Expand Down Expand Up @@ -49,9 +51,3 @@ def test_parse_rel_cfg_path():
assert rel_cfg_file == 'cfg_file'
with pytest.raises(AssertionError):
_parse_rel_cfg_path('error/cfg_dir/cfg_file')