-
Notifications
You must be signed in to change notification settings - Fork 8
/
evaluate_bid_pointconv.py
180 lines (145 loc) · 5.69 KB
/
evaluate_bid_pointconv.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse
import sys
import os
import torch, numpy as np, glob, math, torch.utils.data, scipy.ndimage, multiprocessing as mp
import torch.nn.functional as F
import time
import torch.nn as nn
import pickle
import datetime
import logging
from tqdm import tqdm
from models_bid_pointconv import PointConvBidirection
from models_bid_pointconv import multiScaleLoss
from pathlib import Path
from collections import defaultdict
import transforms
import datasets
import cmd_args
from main_utils import *
from utils import geometry
from evaluation_utils import evaluate_2d, evaluate_3d
def main():
#import ipdb; ipdb.set_trace()
if 'NUMBA_DISABLE_JIT' in os.environ:
del os.environ['NUMBA_DISABLE_JIT']
global args
args = cmd_args.parse_args_from_yaml(sys.argv[1])
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu if args.multi_gpu is None else '0,1,2,3'
'''CREATE DIR'''
experiment_dir = Path('./Evaluate_experiment/')
experiment_dir.mkdir(exist_ok=True)
file_dir = Path(str(experiment_dir) + '/%sFlyingthings3d-'%args.model_name + str(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')))
file_dir.mkdir(exist_ok=True)
checkpoints_dir = file_dir.joinpath('checkpoints/')
checkpoints_dir.mkdir(exist_ok=True)
log_dir = file_dir.joinpath('logs/')
log_dir.mkdir(exist_ok=True)
os.system('cp %s %s' % ('models.py', log_dir))
os.system('cp %s %s' % ('pointconv_util.py', log_dir))
os.system('cp %s %s' % ('evaluate.py', log_dir))
os.system('cp %s %s' % ('config_evaluate.yaml', log_dir))
'''LOG'''
logger = logging.getLogger(args.model_name)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler = logging.FileHandler(str(log_dir) + 'train_%s_sceneflow.txt'%args.model_name)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.info('----------------------------------------TRAINING----------------------------------')
logger.info('PARAMETER ...')
logger.info(args)
blue = lambda x: '\033[94m' + x + '\033[0m'
model = PointConvBidirection()
val_dataset = datasets.__dict__[args.dataset](
train=False,
transform=transforms.ProcessData(args.data_process,
args.num_points,
args.allow_less_points),
num_points=args.num_points,
data_root = args.data_root
)
logger.info('val_dataset: ' + str(val_dataset))
val_loader = torch.utils.data.DataLoader(
val_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True,
worker_init_fn=lambda x: np.random.seed((torch.initial_seed()) % (2 ** 32))
)
#load pretrained model
pretrain = args.ckpt_dir + args.pretrain
model.load_state_dict(torch.load(pretrain))
print('load model %s'%pretrain)
logger.info('load model %s'%pretrain)
model.cuda()
epe3ds = AverageMeter()
acc3d_stricts = AverageMeter()
acc3d_relaxs = AverageMeter()
outliers = AverageMeter()
# 2D
epe2ds = AverageMeter()
acc2ds = AverageMeter()
total_loss = 0
total_seen = 0
total_epe = 0
metrics = defaultdict(lambda:list())
for i, data in tqdm(enumerate(val_loader, 0), total=len(val_loader), smoothing=0.9):
pos1, pos2, norm1, norm2, flow, path = data
#move to cuda
pos1 = pos1.cuda()
pos2 = pos2.cuda()
norm1 = norm1.cuda()
norm2 = norm2.cuda()
flow = flow.cuda()
model = model.eval()
with torch.no_grad():
pred_flows, fps_pc1_idxs, _, _, _ = model(pos1, pos2, norm1, norm2)
loss = multiScaleLoss(pred_flows, flow, fps_pc1_idxs)
full_flow = pred_flows[0].permute(0, 2, 1)
epe3d = torch.norm(full_flow - flow, dim = 2).mean()
total_loss += loss.cpu().data * args.batch_size
total_epe += epe3d.cpu().data * args.batch_size
total_seen += args.batch_size
pc1_np = pos1.cpu().numpy()
pc2_np = pos2.cpu().numpy()
sf_np = flow.cpu().numpy()
pred_sf = full_flow.cpu().numpy()
EPE3D, acc3d_strict, acc3d_relax, outlier = evaluate_3d(pred_sf, sf_np)
epe3ds.update(EPE3D)
acc3d_stricts.update(acc3d_strict)
acc3d_relaxs.update(acc3d_relax)
outliers.update(outlier)
# 2D evaluation metrics
flow_pred, flow_gt = geometry.get_batch_2d_flow(pc1_np,
pc1_np+sf_np,
pc1_np+pred_sf,
path)
EPE2D, acc2d = evaluate_2d(flow_pred, flow_gt)
epe2ds.update(EPE2D)
acc2ds.update(acc2d)
mean_loss = total_loss / total_seen
mean_epe = total_epe / total_seen
str_out = '%s mean loss: %f mean epe: %f'%(blue('Evaluate'), mean_loss, mean_epe)
print(str_out)
logger.info(str_out)
res_str = (' * EPE3D {epe3d_.avg:.4f}\t'
'ACC3DS {acc3d_s.avg:.4f}\t'
'ACC3DR {acc3d_r.avg:.4f}\t'
'Outliers3D {outlier_.avg:.4f}\t'
'EPE2D {epe2d_.avg:.4f}\t'
'ACC2D {acc2d_.avg:.4f}'
.format(
epe3d_=epe3ds,
acc3d_s=acc3d_stricts,
acc3d_r=acc3d_relaxs,
outlier_=outliers,
epe2d_=epe2ds,
acc2d_=acc2ds
))
print(res_str)
logger.info(res_str)
if __name__ == '__main__':
main()