-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
39 lines (33 loc) · 1.32 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
import torch
def extract_model_state_dict(ckpt_path, model_name='model', prefixes_to_ignore=[]):
checkpoint = torch.load(ckpt_path, map_location='cpu')
checkpoint_ = {}
if 'state_dict' in checkpoint: # if it's a pytorch-lightning checkpoint
checkpoint = checkpoint['state_dict']
for k, v in checkpoint.items():
if not k.startswith(model_name):
continue
k = k[len(model_name)+1:]
for prefix in prefixes_to_ignore:
if k.startswith(prefix):
break
else:
checkpoint_[k] = v
return checkpoint_
def load_ckpt(model, ckpt_path, model_name='model', prefixes_to_ignore=[]):
if not ckpt_path: return
model_dict = model.state_dict()
checkpoint_ = extract_model_state_dict(ckpt_path, model_name, prefixes_to_ignore)
model_dict.update(checkpoint_)
model.load_state_dict(model_dict)
def slim_ckpt(ckpt_path, save_poses=False):
ckpt = torch.load(ckpt_path, map_location='cpu')
# pop unused parameters
keys_to_pop = ['directions', 'model.density_grid', 'model.grid_coords']
if not save_poses: keys_to_pop += ['poses']
for k in ckpt['state_dict']:
if k.startswith('val_lpips'):
keys_to_pop += [k]
for k in keys_to_pop:
ckpt['state_dict'].pop(k, None)
return ckpt['state_dict']