forked from xuekt98/BBDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
46 lines (36 loc) · 1.41 KB
/
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
import argparse
import importlib
import omegaconf.dictconfig
from Register import Registers
from runners.DiffusionBasedModelRunners.BBDMRunner import BBDMRunner
from runners.DiffusionBasedModelRunners.BBDMRunner_Augmented import BBDMRunner_Augmented
def dict2namespace(config):
namespace = argparse.Namespace()
for key, value in config.items():
if isinstance(value, dict) or isinstance(value, omegaconf.dictconfig.DictConfig):
new_value = dict2namespace(value)
else:
new_value = value
setattr(namespace, key, new_value)
return namespace
def namespace2dict(config):
conf_dict = {}
for key, value in vars(config).items():
if isinstance(value, argparse.Namespace):
conf_dict[key] = namespace2dict(value)
else:
conf_dict[key] = value
return conf_dict
def get_obj_from_str(string, reload=False):
module, cls = string.rsplit(".", 1)
if reload:
module_imp = importlib.import_module(module)
importlib.reload(module_imp)
return getattr(importlib.import_module(module, package=None), cls)
def instantiate_from_config(config):
if not "target" in config:
raise KeyError("Expected key `target` to instantiate.")
return get_obj_from_str(config["target"])(**config.get("params", dict()))
def get_runner(runner_name, config):
runner = Registers.runners[runner_name](config)
return runner