|
|
import argparse |
|
|
from pathlib import Path |
|
|
|
|
|
import pytest |
|
|
|
|
|
from anti_kd_backdoor.config import Config, DictAction |
|
|
|
|
|
|
|
|
class TestConfig: |
|
|
"""Modify from mmengine""" |
|
|
|
|
|
data_path: Path = Path(__file__).parent.parent / 'data' |
|
|
config_path: Path = data_path / 'config' |
|
|
|
|
|
def test_fromfile(self) -> None: |
|
|
config_path = self.config_path / 'cifar10_resnet18.py' |
|
|
config = Config.fromfile(config_path) |
|
|
|
|
|
assert config.dataset == 'CIFAR10' |
|
|
assert config.network.type == 'resnet18' |
|
|
assert config.test_dataloader.batch_size == 64 |
|
|
assert len(config.train_dataloader.dataset.transform) == 4 |
|
|
assert len(config.test_dataloader.dataset.transform) == 2 |
|
|
|
|
|
with pytest.raises(ValueError): |
|
|
_ = Config.fromfile(self.config_path / 'error.txt') |
|
|
|
|
|
def test_magic_method(self) -> None: |
|
|
cfg_dict = dict(item1=[1, 2], |
|
|
item2=dict(a=0), |
|
|
item3=True, |
|
|
item4='test') |
|
|
cfg_file = self.config_path / 'simple_config.py' |
|
|
cfg = Config.fromfile(cfg_file) |
|
|
|
|
|
assert len(cfg) == 4 |
|
|
|
|
|
assert set(cfg.keys()) == set(cfg_dict.keys()) |
|
|
assert set(cfg._cfg_dict.keys()) == set(cfg_dict.keys()) |
|
|
|
|
|
for value in cfg.values(): |
|
|
assert value in cfg_dict.values() |
|
|
|
|
|
for name, value in cfg.items(): |
|
|
assert name in cfg_dict |
|
|
assert value in cfg_dict.values() |
|
|
|
|
|
assert cfg.item1 == cfg_dict['item1'] |
|
|
assert cfg.item2 == cfg_dict['item2'] |
|
|
assert cfg.item2.a == 0 |
|
|
assert cfg.item3 == cfg_dict['item3'] |
|
|
assert cfg.item4 == cfg_dict['item4'] |
|
|
|
|
|
with pytest.raises(AttributeError): |
|
|
cfg.not_exist |
|
|
|
|
|
for name in ['item1', 'item2', 'item3', 'item4']: |
|
|
assert name in cfg |
|
|
assert cfg[name] == cfg_dict[name] |
|
|
assert cfg.get(name) == cfg_dict[name] |
|
|
assert cfg.get('not_exist') is None |
|
|
assert cfg.get('not_exist', 0) == 0 |
|
|
|
|
|
with pytest.raises(KeyError): |
|
|
cfg['not_exist'] |
|
|
assert 'item1' in cfg |
|
|
assert 'not_exist' not in cfg |
|
|
|
|
|
cfg.update(dict(item1=0)) |
|
|
assert cfg.item1 == 0 |
|
|
cfg.update(dict(item2=dict(a=1))) |
|
|
assert cfg.item2.a == 1 |
|
|
|
|
|
cfg = Config() |
|
|
cfg.item1 = [1, 2] |
|
|
cfg.item2 = {'a': 0} |
|
|
cfg['item5'] = {'a': {'b': None}} |
|
|
assert cfg._cfg_dict['item1'] == [1, 2] |
|
|
assert cfg.item1 == [1, 2] |
|
|
assert cfg._cfg_dict['item2'] == {'a': 0} |
|
|
assert cfg.item2.a == 0 |
|
|
assert cfg._cfg_dict['item5'] == {'a': {'b': None}} |
|
|
assert cfg.item5.a.b is None |
|
|
|
|
|
def test_dict_action(self): |
|
|
parser = argparse.ArgumentParser(description='Train a detector') |
|
|
parser.add_argument('--options', |
|
|
nargs='+', |
|
|
action=DictAction, |
|
|
help='custom options') |
|
|
|
|
|
args = parser.parse_args( |
|
|
['--options', 'item2.a=a,b', 'item2.b=[(a,b), [1,2], false]']) |
|
|
out_dict = { |
|
|
'item2.a': ['a', 'b'], |
|
|
'item2.b': [('a', 'b'), [1, 2], False] |
|
|
} |
|
|
assert args.options == out_dict |
|
|
|
|
|
args = parser.parse_args(['--options', 'item2.a=[[1]]']) |
|
|
out_dict = {'item2.a': [[1]]} |
|
|
assert args.options == out_dict |
|
|
|
|
|
with pytest.raises(AssertionError): |
|
|
parser.parse_args(['--options', 'item2.a=[(a,b), [1,2], false']) |
|
|
|
|
|
args = parser.parse_args([ |
|
|
'--options', 'item2.a=1', 'item2.b=0.1', 'item2.c=x', 'item3=false' |
|
|
]) |
|
|
out_dict = { |
|
|
'item2.a': 1, |
|
|
'item2.b': 0.1, |
|
|
'item2.c': 'x', |
|
|
'item3': False |
|
|
} |
|
|
assert args.options == out_dict |
|
|
|