-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml_utils.py
96 lines (73 loc) · 2.43 KB
/
yaml_utils.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Copied from https://github.com/pfnet-research/sngan_projection/blob/master/source/yaml_utils.py
import argparse
import os
import shutil
import sys
import time
import yaml
class Config(object):
def __init__(self, config_dict):
self.config = config_dict
def __getattr__(self, key):
if key in self.config:
return self.config[key]
else:
raise AttributeError(key)
def __getitem__(self, key):
return self.config[key]
def __repr__(self):
return yaml.dump(self.config, default_flow_style=False)
def load_dataset(config, test=False):
dataset = load_module(config.dataset['dataset_func'],
config.dataset['dataset_name'])
args = config.dataset['args']
args['test'] = test
return dataset(**args)
def load_module(func, name):
mod_name = os.path.splitext(os.path.basename(func))[0]
mod_path = os.path.dirname(func)
sys.path.insert(0, mod_path)
return getattr(__import__(mod_name), name)
def load_model(model_func, model_name, args=None):
model = load_module(model_func, model_name)
if args:
return model(**args)
return model()
def load_optimizer(opt_name, args=None):
opt_module = __import__('chainer.optimizers', fromlist=[opt_name])
opt_class = getattr(opt_module, opt_name)
if args:
return opt_class(**args)
return opt_class()
def load_updater_class(config):
func = config.updater['func']
name = config.updater['name']
if func.endswith('.py'):
updater = load_module(func, name)
else:
updater = getattr(__import__(func, fromlist=[name]), name)
return updater
def load_preupdater_class(config):
func = config.preupdater['func']
name = config.preupdater['name']
if func.endswith('.py'):
preupdater = load_module(func, name)
else:
preupdater = getattr(__import__(func, fromlist=[name]), name)
return preupdater
def load_method(method_config):
func = method_config['func']
name = method_config['name']
if func.endswith('.py'):
method = load_module(func, name)
else:
method = getattr(__import__(func, fromlist=[name]), name)
return method
def load_method_concrete(func, name):
if func.endswith('.py'):
method = load_module(func, name)
else:
method = getattr(__import__(func, fromlist=[name]), name)
return method