-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.py
153 lines (110 loc) · 4.46 KB
/
test.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
import torch
import torch.nn.functional as F
import numpy as np
import cv2
import ngdsac
import time
import argparse
import math
from dataset import CamLocDataset
from network import Network
parser = argparse.ArgumentParser(
description='Test a trained network on a specific scene.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('scene', help='name of a scene in the dataset folder')
parser.add_argument('network', help='file name of a network trained for the scene')
parser.add_argument('--hypotheses', '-hyps', type=int, default=256,
help='number of hypotheses, i.e. number of RANSAC iterations')
parser.add_argument('--threshold', '-t', type=float, default=10,
help='inlier threshold in pixels')
parser.add_argument('--inlieralpha', '-ia', type=float, default=100,
help='alpha parameter of the soft inlier count; controls the softness of the hypotheses score distribution; lower means softer')
parser.add_argument('--inlierbeta', '-ib', type=float, default=0.5,
help='beta parameter of the soft inlier count; controls the softness of the sigmoid; lower means softer')
parser.add_argument('--maxreprojection', '-maxr', type=float, default=100,
help='maximum reprojection error; reprojection error is clamped to this value for stability')
parser.add_argument('--session', '-sid', default='',
help='custom session name appended to output files, useful to separate different runs of a script')
parser.add_argument('--uniform', '-u', action='store_true',
help='disable neural-guidance and sample data points uniformely; corresponds to a DSAC model')
opt = parser.parse_args()
# setup dataset
testset = CamLocDataset("./dataset/" + opt.scene + "/test", training=False)
testset_loader = torch.utils.data.DataLoader(testset, shuffle=False, num_workers=6)
# load network
network = Network(torch.zeros((3)))
network.load_state_dict(torch.load(opt.network))
network = network.cuda()
network.eval()
test_log = open('test_%s_%s.txt' % (opt.scene, opt.session), 'w', 1)
pose_log = open('poses_%s_%s.txt' % (opt.scene, opt.session), 'w', 1)
print('Test images found: ', len(testset))
# keep track of rotation and translation errors for calculation of the mean error
rErrs = []
tErrs = []
avg_time = 0
with torch.no_grad():
for image, gt_pose, init, focal_length, file in testset_loader:
focal_length = float(focal_length[0])
file = file[0].split('/')[-1] # remove path from file name
gt_pose = gt_pose[0]
image = image.cuda()
start_time = time.time()
# predict scene coordinates and neural guidance
scene_coordinates, log_ng = network(image)
scene_coordinates = scene_coordinates.cpu()
neural_guidance = torch.exp(log_ng).cpu()
if opt.uniform:
# overwrite neural guidance with uniform probabilities
neural_guidance.fill_(1 / (log_ng.shape[2]*log_ng.shape[3]))
out_pose = torch.zeros((4, 4))
ngdsac.forward(
scene_coordinates,
neural_guidance,
out_pose,
opt.hypotheses,
opt.threshold,
focal_length,
float(image.size(3) / 2), #principal point assumed in image center
float(image.size(2) / 2),
opt.inlieralpha,
opt.inlierbeta,
opt.maxreprojection,
network.OUTPUT_SUBSAMPLE)
avg_time += time.time()-start_time
# calculate pose errors
t_err = float(torch.norm(gt_pose[0:3, 3] - out_pose[0:3, 3]))
gt_R = gt_pose[0:3,0:3].numpy()
out_R = out_pose[0:3,0:3].numpy()
r_err = np.matmul(out_R, np.transpose(gt_R))
r_err = cv2.Rodrigues(r_err)[0]
r_err = np.linalg.norm(r_err) * 180 / math.pi
print("\nRotation Error: %.2fdeg, Translation Error: %.1fcm" % (r_err, t_err*100))
rErrs.append(r_err)
tErrs.append(t_err * 100)
# write estimated pose to pose file
out_pose = out_pose.inverse()
t = out_pose[0:3, 3]
# rotation to axis angle
rot, _ = cv2.Rodrigues(out_pose[0:3,0:3].numpy())
angle = np.linalg.norm(rot)
axis = rot / angle
# axis angle to quaternion
q_w = math.cos(angle * 0.5)
q_xyz = math.sin(angle * 0.5) * axis
pose_log.write("%s %f %f %f %f %f %f %f %f %f\n" % (
file,
q_w, q_xyz[0], q_xyz[1], q_xyz[2],
t[0], t[1], t[2],
r_err, t_err))
median_idx = int(len(rErrs)/2)
tErrs.sort()
rErrs.sort()
avg_time /= len(rErrs)
print("\n===================================================")
print("\nTest complete.")
print("\nMedian Error: %.1fdeg, %.1fcm" % (rErrs[median_idx], tErrs[median_idx]))
print("Avg. processing time: %4.1fms" % (avg_time * 1000))
test_log.write('%f %f %f\n' % (rErrs[median_idx], tErrs[median_idx], avg_time))
test_log.close()
pose_log.close()