|
| 1 | +import os.path as osp |
| 2 | +import tempfile |
| 3 | +from copy import deepcopy |
| 4 | + |
| 5 | +import pytest |
| 6 | +from mmcv.utils import Config |
| 7 | + |
| 8 | +from mmdet.utils import replace_cfg_vals |
| 9 | + |
| 10 | + |
| 11 | +def test_replace_cfg_vals(): |
| 12 | + temp_file = tempfile.NamedTemporaryFile() |
| 13 | + cfg_path = f'{temp_file.name}.py' |
| 14 | + with open(cfg_path, 'w') as f: |
| 15 | + f.write('configs') |
| 16 | + |
| 17 | + ori_cfg_dict = dict() |
| 18 | + ori_cfg_dict['cfg_name'] = osp.basename(temp_file.name) |
| 19 | + ori_cfg_dict['work_dir'] = 'work_dirs/${cfg_name}/${percent}/${fold}' |
| 20 | + ori_cfg_dict['percent'] = 5 |
| 21 | + ori_cfg_dict['fold'] = 1 |
| 22 | + ori_cfg_dict['model_wrapper'] = dict( |
| 23 | + type='SoftTeacher', detector='${model}') |
| 24 | + ori_cfg_dict['model'] = dict( |
| 25 | + type='FasterRCNN', |
| 26 | + backbone=dict(type='ResNet'), |
| 27 | + neck=dict(type='FPN'), |
| 28 | + rpn_head=dict(type='RPNHead'), |
| 29 | + roi_head=dict(type='StandardRoIHead'), |
| 30 | + train_cfg=dict( |
| 31 | + rpn=dict( |
| 32 | + assigner=dict(type='MaxIoUAssigner'), |
| 33 | + sampler=dict(type='RandomSampler'), |
| 34 | + ), |
| 35 | + rpn_proposal=dict(nms=dict(type='nms', iou_threshold=0.7)), |
| 36 | + rcnn=dict( |
| 37 | + assigner=dict(type='MaxIoUAssigner'), |
| 38 | + sampler=dict(type='RandomSampler'), |
| 39 | + ), |
| 40 | + ), |
| 41 | + test_cfg=dict( |
| 42 | + rpn=dict(nms=dict(type='nms', iou_threshold=0.7)), |
| 43 | + rcnn=dict(nms=dict(type='nms', iou_threshold=0.5)), |
| 44 | + ), |
| 45 | + ) |
| 46 | + ori_cfg_dict['iou_threshold'] = dict( |
| 47 | + rpn_proposal_nms='${model.train_cfg.rpn_proposal.nms.iou_threshold}', |
| 48 | + test_rpn_nms='${model.test_cfg.rpn.nms.iou_threshold}', |
| 49 | + test_rcnn_nms='${model.test_cfg.rcnn.nms.iou_threshold}', |
| 50 | + ) |
| 51 | + |
| 52 | + ori_cfg_dict['str'] = 'Hello, world!' |
| 53 | + ori_cfg_dict['dict'] = {'Hello': 'world!'} |
| 54 | + ori_cfg_dict['list'] = [ |
| 55 | + 'Hello, world!', |
| 56 | + ] |
| 57 | + ori_cfg_dict['tuple'] = ('Hello, world!', ) |
| 58 | + ori_cfg_dict['test_str'] = 'xxx${str}xxx' |
| 59 | + |
| 60 | + ori_cfg = Config(ori_cfg_dict, filename=cfg_path) |
| 61 | + updated_cfg = replace_cfg_vals(deepcopy(ori_cfg)) |
| 62 | + |
| 63 | + assert updated_cfg.work_dir \ |
| 64 | + == f'work_dirs/{osp.basename(temp_file.name)}/5/1' |
| 65 | + assert updated_cfg.model.detector == ori_cfg.model |
| 66 | + assert updated_cfg.iou_threshold.rpn_proposal_nms \ |
| 67 | + == ori_cfg.model.train_cfg.rpn_proposal.nms.iou_threshold |
| 68 | + assert updated_cfg.test_str == 'xxxHello, world!xxx' |
| 69 | + ori_cfg_dict['test_dict'] = 'xxx${dict}xxx' |
| 70 | + ori_cfg_dict['test_list'] = 'xxx${list}xxx' |
| 71 | + ori_cfg_dict['test_tuple'] = 'xxx${tuple}xxx' |
| 72 | + with pytest.raises(AssertionError): |
| 73 | + cfg = deepcopy(ori_cfg) |
| 74 | + cfg['test_dict'] = 'xxx${dict}xxx' |
| 75 | + updated_cfg = replace_cfg_vals(cfg) |
| 76 | + with pytest.raises(AssertionError): |
| 77 | + cfg = deepcopy(ori_cfg) |
| 78 | + cfg['test_list'] = 'xxx${list}xxx' |
| 79 | + updated_cfg = replace_cfg_vals(cfg) |
| 80 | + with pytest.raises(AssertionError): |
| 81 | + cfg = deepcopy(ori_cfg) |
| 82 | + cfg['test_tuple'] = 'xxx${tuple}xxx' |
| 83 | + updated_cfg = replace_cfg_vals(cfg) |
0 commit comments