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

Support environment variables in config #1565

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

innerlee
Copy link
Contributor

@innerlee innerlee commented Dec 7, 2021

Motivation

Use environment variable to dynamically change parts of the config.
Kind of a template functionality.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

no breaking change.

Use cases (Optional)

If you want to test many datasets on a checkpoint, and the only difference is the dataset path, then you can use environment variable as a placeholder. No need to create many configs anymore.

Before:

cfg1.py

...
data_root = 'data/dataset1'
...

cfg2.py

...
data_root = 'data/dataset2'
...

After:

cfg.py

...
data_root = '{{ _env_.DATA_ROOT }}'
...

Checklist

Tests and docs will be added later. Lets see the general reaction first.

Before PR:

  • I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects, like MMDet or MMCls.
  • CLA has been signed and all committers have signed the CLA in this PR.

@zhouzaida
Copy link
Collaborator

Hi @innerlee , you can merge the upstream master to your branch to fix the error of building documentation

@zhouzaida
Copy link
Collaborator

https://mmdetection.readthedocs.io/en/latest/1_exist_data_model.html
image

Can we use the merge_from_dict to implement the same behavior like --cfg-options checkpoint_config.max_keep_ckpts=1

def merge_from_dict(self, options, allow_list_keys=True):

@innerlee
Copy link
Contributor Author

innerlee commented Dec 9, 2021

  • For simple replacement, yes. But the --cfg-options thing is verbose and hard to use
  • For complex cfgs such as multiple references, the --cfg-options is not powerful enough. Try this example:

image

@zhouzaida
Copy link
Collaborator

  • For simple replacement, yes. But the --cfg-options thing is verbose and hard to use
  • For complex cfgs such as multiple references, the --cfg-options is not powerful enough. Try this example:

image

Got it. Great feature.

@zhouzaida
Copy link
Collaborator

We also need to update the usage of Config (https://mmcv.readthedocs.io/en/latest/understand_mmcv/config.html) and add some unit tests for the feature.

@@ -176,7 +196,9 @@ def _substitute_base_vars(cfg, base_var_dict, base_cfg):
return cfg

@staticmethod
def _file2dict(filename, use_predefined_variables=True):
def _file2dict(filename,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add argument use_environment_variables for fromfile function, as it call _file2dict

@zhouzaida
Copy link
Collaborator

Hi @innerlee , is there any progress?

@innerlee
Copy link
Contributor Author

Sorry being busy. Will complete it when I got time

@innerlee innerlee force-pushed the zz/env branch 2 times, most recently from a09bcdc to 9663996 Compare January 26, 2022 13:11
assert cfg.item88 == 'OK'

for file in ['tt.py', 'tt.json', 'tt.yaml']:
with pytest.raises(Exception):
Copy link
Collaborator

@zhouzaida zhouzaida Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with pytest.raises(Exception):
with pytest.raises(ValueError, match='...'):

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the match argument here will help us know why it should raise an exception.

@zhouzaida
Copy link
Collaborator

Hi, is any progess?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants