File size: 1,085 Bytes
c18a21e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

import yaml
from omegaconf import OmegaConf, DictConfig

def load_base_cfg():
    with open('configs/base.yml', 'r') as fp:
        cfg = yaml.load(fp, Loader=yaml.SafeLoader)
    return cfg

def load_cfg(cfg_file):
    cfg = load_base_cfg()
    with open(cfg_file, 'r') as fp:
        exp_cfg = yaml.load(fp, Loader=yaml.SafeLoader)

    cfg['model'].update(exp_cfg.get('model', {}))
    cfg['data'].update(exp_cfg.get('data', {}))
    dataset = cfg['data'].get('dataset')
    return cfg

def convert_types(config):
    """Convert `'None'` (str) --> `None` (None). Only supports top-level"""
    for k, v in config.items():
        if isinstance(v, DictConfig):
            setattr(config, k, convert_types(v))

        # TODO convert types in ListConfig, right now they are ignored
        # if isinstance(v, ListConfig):
        #     new_v = ListConfig()

        if v in ["None", "none"]:
            setattr(config, k, None)
    return config

def setup_config(config_path):
    yaml_config = OmegaConf.load(config_path)
    config = convert_types(yaml_config)
    return config