-
Notifications
You must be signed in to change notification settings - Fork 4
/
evaluate.py
131 lines (103 loc) · 4.23 KB
/
evaluate.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Author: @Sentient07
import os.path as osp
import sys
from pathlib import Path
import configargparse
import numpy as np
import torch
import yaml
from scipy.io import savemat
from torch import nn
from torch.utils.data import DataLoader
from tqdm import tqdm
import dataset
from model import IFMatchNet
from utils import *
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
def build_argparse():
p = configargparse.ArgumentParser()
p.add_argument('--config', required=True,
help='Evaluation configuration')
p.add_argument('--wo_normal_constraint', action='store_true',
help='Evaluating point cloud wo normals?')
p.add_argument('--deformsdf_loss', action='store_true',
help='Use deform SDF loss?')
p.add_argument('--rot_invariance', action='store_true',
help='Use rot_invariance?')
p.add_argument('--redo', action='store_true',
help='Use rot_invariance?')
return p.parse_args()
def evaluate(model, train_dataloader, file_name, model_dir,
wo_normal_constraint=False, deformsdf_loss=False,
**kwargs):
assert file_name is not None, "Must provide to save"
epochs = kwargs['epochs']
lr = kwargs['lr']
# Initialise LVs
embedding = model.latent_codes(
torch.zeros(1).long().cuda()).clone().detach()
embedding.requires_grad = True
optim = torch.optim.Adam(lr=lr, params=[embedding])
checkpoints_dir = osp.join(model_dir, 'Codes')
safe_make_dirs(checkpoints_dir)
with tqdm(total=len(train_dataloader) * epochs) as pbar:
net_losses = []
for _ in range(epochs):
for _, (model_input, gt) in enumerate(train_dataloader):
model_input = {key: value.cuda()
for key, value in model_input.items()}
gt = {key: value.cuda() for key, value in gt.items()}
losses = model.embedding(
embedding, model_input, gt,
wo_normal_constraint=wo_normal_constraint,
deformsdf_loss=deformsdf_loss)
net_loss = 0.
for _, loss in losses.items():
single_loss = loss.mean()
net_loss += single_loss
net_losses.append(net_loss.item())
optim.zero_grad()
net_loss.backward()
optim.step()
pbar.update(1)
embed_save = embedding.detach().squeeze()
torch.save(embed_save, osp.join(checkpoints_dir, '%s.pth' % file_name))
if __name__ == '__main__':
# load configs
args = build_argparse()
with open(osp.join(args.config), 'r') as stream:
meta_params = yaml.safe_load(stream)
if args.deformsdf_loss:
assert meta_params['mesh_dir'], 'Cannot use Deform Loss w/o mesh'
model = IFMatchNet(**meta_params)
model.load_state_dict(torch.load(meta_params['checkpoint_path']))
for param in model.hyper_net_s.parameters():
param.requires_grad = False
for param in model.hyper_net_d.parameters():
param.requires_grad = False
model.cuda()
# create save path
root_path = osp.join(
meta_params['logging_root'],
meta_params['experiment_name'])
safe_make_dirs(root_path)
with open(meta_params['eval_split'], 'r') as file:
all_names = file.read().split('\n')
# optimize latent code for each test subject
for file in all_names:
save_path = osp.join(root_path, file)
if osp.isfile(
osp.join(root_path, 'Codes', '%s.pth' % file)) and not args.redo:
continue
sdf_dataset = dataset.PointCloudMulti(
root_dir=[osp.join(
meta_params['point_cloud_path'],
file + '.mat')],
is_train=False, **meta_params)
dataloader = DataLoader(
sdf_dataset, shuffle=False, collate_fn=sdf_dataset.collate_fn,
batch_size=1, pin_memory=True, num_workers=0, drop_last=True)
evaluate(
model=model, train_dataloader=dataloader, model_dir=root_path,
file_name=file, wo_normal_constraint=args.wo_normal_constraint,
deformsdf_loss=args.deformsdf_loss, **meta_params)