-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_util.py
109 lines (83 loc) · 3.6 KB
/
main_util.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
import numpy as np
import os
def project_3d_to_2d(pc, f=-1050., cx=479.5, cy=269.5, constx=0, consty=0, constz=0):
x = (pc[..., 0] * f + cx * pc[..., 2] + constx) / (pc[..., 2] + constz)
y = (pc[..., 1] * f * -1.0 + cy * pc[..., 2] + consty) / (pc[..., 2] + constz)
return x, y
def get_2d_flow(pc1, pc2, predicted_pc2, paths=None):
if paths == None:
px1, py1 = project_3d_to_2d(pc1)
px2, py2 = project_3d_to_2d(predicted_pc2)
px2_gt, py2_gt = project_3d_to_2d(pc2)
else:
focallengths = []
cxs = []
cys = []
constx = []
consty = []
constz = []
for path in paths:
fname = os.path.split(path)[-1]
calib_path = os.path.join(
os.path.dirname(__file__),
'utils',
'calib_cam_to_cam',
fname + '.txt')
with open(calib_path) as fd:
lines = fd.readlines()
P_rect_left = \
np.array([float(item) for item in
[line for line in lines if line.startswith('P_rect_02')][0].split()[1:]],
dtype=np.float32).reshape(3, 4)
focallengths.append(-P_rect_left[0, 0])
cxs.append(P_rect_left[0, 2])
cys.append(P_rect_left[1, 2])
constx.append(P_rect_left[0, 3])
consty.append(P_rect_left[1, 3])
constz.append(P_rect_left[2, 3])
focallengths = np.array(focallengths)[:, None, None]
cxs = np.array(cxs)[:, None, None]
cys = np.array(cys)[:, None, None]
constx = np.array(constx)[:, None, None]
consty = np.array(consty)[:, None, None]
constz = np.array(constz)[:, None, None]
px1, py1 = project_3d_to_2d(pc1, f=focallengths, cx=cxs, cy=cys,
constx=constx, consty=consty, constz=constz)
px2, py2 = project_3d_to_2d(predicted_pc2, f=focallengths, cx=cxs, cy=cys,
constx=constx, consty=consty, constz=constz)
px2_gt, py2_gt = project_3d_to_2d(pc2, f=focallengths, cx=cxs, cy=cys,
constx=constx, consty=consty, constz=constz)
flow_x = px2 - px1
flow_y = py2 - py1
flow_x_gt = px2_gt - px1
flow_y_gt = py2_gt - py1
flow_pred = np.concatenate((flow_x[..., None], flow_y[..., None]), axis=-1)
flow_gt = np.concatenate((flow_x_gt[..., None], flow_y_gt[..., None]), axis=-1)
return flow_pred, flow_gt
def scene_flow_EPE_np(pred, labels):
error = np.sqrt(np.sum((pred - labels)**2, 2) + 1e-20)
num = pred.shape[1]
gtflow_len = np.sqrt(np.sum(labels*labels, 2) + 1e-20) # B,N
acc1 = np.sum(np.logical_or((error <= 0.05), (error/gtflow_len <= 0.05)), axis=1)
acc2 = np.sum(np.logical_or((error <= 0.1), (error/gtflow_len <= 0.1)), axis=1)
outlier = np.sum(np.logical_or((error > 0.3), (error / gtflow_len > 0.1)), axis=1)
acc1 = acc1/num
acc1 = np.mean(acc1)
acc2 = acc2/num
acc2 = np.mean(acc2)
outlier = outlier/num
outlier = np.mean(outlier)
EPE = np.sum(error, 1) / num
EPE = np.mean(EPE)
return EPE, acc1, acc2, outlier
def evaluate_2d(flow_pred, flow_gt):
"""
flow_pred: (N, 2)
flow_gt: (N, 2)
"""
epe2d = np.linalg.norm(flow_gt - flow_pred, axis=-1)
epe2d_mean = epe2d.mean()
flow_gt_norm = np.linalg.norm(flow_gt, axis=-1)
relative_err = epe2d / (flow_gt_norm + 1e-5)
acc2d = (np.logical_or(epe2d < 3., relative_err < 0.05)).astype(np.float).mean()
return epe2d_mean, acc2d