a b/ViTPose/tests/test_config.py
1
# Copyright (c) OpenMMLab. All rights reserved.
2
from os.path import dirname, exists, join, relpath
3
4
import torch
5
from mmcv.runner import build_optimizer
6
7
8
def _get_config_directory():
9
    """Find the predefined detector config directory."""
10
    try:
11
        # Assume we are running in the source mmdetection repo
12
        repo_dpath = dirname(dirname(__file__))
13
    except NameError:
14
        # For IPython development when this __file__ is not defined
15
        import mmpose
16
        repo_dpath = dirname(dirname(mmpose.__file__))
17
    config_dpath = join(repo_dpath, 'configs')
18
    if not exists(config_dpath):
19
        raise Exception('Cannot find config path')
20
    return config_dpath
21
22
23
def test_config_build_detector():
24
    """Test that all detection models defined in the configs can be
25
    initialized."""
26
    from mmcv import Config
27
28
    from mmpose.models import build_posenet
29
30
    config_dpath = _get_config_directory()
31
    print(f'Found config_dpath = {config_dpath}')
32
33
    import glob
34
    config_fpaths = list(glob.glob(join(config_dpath, '**', '*.py')))
35
    config_fpaths = [p for p in config_fpaths if p.find('_base_') == -1]
36
    config_names = [relpath(p, config_dpath) for p in config_fpaths]
37
38
    print(f'Using {len(config_names)} config files')
39
40
    for config_fname in config_names:
41
        config_fpath = join(config_dpath, config_fname)
42
        config_mod = Config.fromfile(config_fpath)
43
44
        print(f'Building detector, config_fpath = {config_fpath}')
45
46
        # Remove pretrained keys to allow for testing in an offline environment
47
        if 'pretrained' in config_mod.model:
48
            config_mod.model['pretrained'] = None
49
50
        detector = build_posenet(config_mod.model)
51
        assert detector is not None
52
53
        optimizer = build_optimizer(detector, config_mod.optimizer)
54
        assert isinstance(optimizer, torch.optim.Optimizer)