-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
49 lines (35 loc) · 1.25 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
47
48
49
import yaml
import os
import glob
import torch
def extend_config(cfg_path, child=None):
with open(cfg_path, "rt") as f:
parent_cfg = yaml.load(f, Loader=yaml.FullLoader)
if child is not None:
parent_cfg.update(child)
if "$extends$" in parent_cfg:
path = parent_cfg["$extends$"]
del parent_cfg["$extends$"]
parent_cfg = extend_config(child=parent_cfg, cfg_path=path)
return parent_cfg
def load_args(args):
cfg = extend_config(cfg_path=f"{args.config_file}", child=None)
for key, value in cfg.items():
if key in args and args.__dict__[key] is not None:
continue
args.__dict__[key] = value
return args, cfg
def load_model(args):
checkpoint_path = f"{os.path.abspath(os.path.dirname(__file__))}/checkpoints/{args.group}:{args.name}/*.ckpt"
print("::: Searching for model at", checkpoint_path)
checkpoints = glob.glob(checkpoint_path)
if len(checkpoints) > 1:
print("::: Multiple checkpoints found !!")
exit(-1)
try:
state_dict = torch.load(checkpoints[-1])
print("::: Found model at:", checkpoints[-1])
except Exception as e:
print("No checkpoints found: ", checkpoint_path)
raise e
return state_dict