Skip to content

[Enhance] Add support for sequence input in --search-args option #195

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

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 11 additions & 5 deletions mim/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from collections import defaultdict
from email.parser import FeedParser
from pkg_resources import get_distribution, parse_version
from typing import Any, List, Optional, Tuple, Union
from typing import Any, List, Optional, Sequence, Tuple, Union

import click
import requests
Expand Down Expand Up @@ -509,8 +509,11 @@ def get_config(cfg, name):
name = name.split('.')
suffix = ''
for item in name:
assert item in cfg, f'attribute {item} not cfg{suffix}'
cfg = cfg[item]
if isinstance(cfg, Sequence) and not isinstance(cfg, str):
cfg = cfg[int(item)]
else:
assert item in cfg, f'attribute {item} not cfg{suffix}'
cfg = cfg[item]
suffix += f'.{item}'
return cfg

Expand All @@ -524,8 +527,11 @@ def set_config(cfg, name, value):
name = name.split('.')
suffix = ''
for item in name[:-1]:
assert item in cfg, f'attribute {item} not cfg{suffix}'
cfg = cfg[item]
if isinstance(cfg, Sequence) and not isinstance(cfg, str):
cfg = cfg[int(item)]
else:
assert item in cfg, f'attribute {item} not cfg{suffix}'
cfg = cfg[item]
suffix += f'.{item}'

assert name[-1] in cfg, f'attribute {item} not cfg{suffix}'
Expand Down
6 changes: 5 additions & 1 deletion tests/data/lenet5_mnist_2.0.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
dataset_type = 'MNIST'
data_preprocessor = dict(mean=[33.46], std=[78.87])

pipeline = [dict(type='Resize', scale=32), dict(type='PackClsInputs')]
pipeline = [
dict(type='Resize', scale=32),
dict(type='Pad', size=(32, 32)),
dict(type='PackClsInputs')
]

common_data_cfg = dict(
type=dataset_type, data_prefix='data/mnist', pipeline=pipeline)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_gridsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def test_gridsearch(gpus, tmp_path):
f'--work-dir={tmp_path}', '--search-args'
]

args5 = [
'mmcls', 'tests/data/lenet5_mnist_2.0.py', f'--gpus={gpus}',
f'--work-dir={tmp_path}', '--search-args',
'--train_dataloader.dataset.pipeline.0.scale 16 32'
]

result = runner.invoke(gridsearch, args1)
assert result.exit_code == 0

Expand All @@ -69,6 +75,9 @@ def test_gridsearch(gpus, tmp_path):
result = runner.invoke(gridsearch, args4)
assert result.exit_code != 0

result = runner.invoke(gridsearch, args5)
assert result.exit_code == 0


def teardown_module():
runner = CliRunner()
Expand Down