-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
52 lines (40 loc) · 1.56 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
50
51
52
import yaml
import os
import glob
import torch
def load_model(args):
checkpoint_path = f'{os.path.abspath(os.path.dirname(__file__))}/checkpoints/{args.group}:{args.name}/*.ckpt'
print("::: Loading model from", checkpoint_path)
checkpoints = glob.glob(checkpoint_path)
try:
state_dict = torch.load(checkpoints[-1])
except Exception as e:
print("No checkpoints found: ", checkpoint_path)
raise e
return state_dict
def load_model_by_dir(name):
checkpoint_path = f'{os.path.abspath(os.path.dirname(__file__))}/{name}/*.ckpt'
print('::: Loading model from:', checkpoint_path)
checkpoints = glob.glob(checkpoint_path)
return torch.load(checkpoints[-1])
def load_model_by_name(name):
checkpoint_path = f'{os.path.abspath(os.path.dirname(__file__))}/checkpoints/{name}/*.ckpt'
checkpoints = glob.glob(checkpoint_path)
return torch.load(checkpoints[-1])
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