-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking_pace.m
260 lines (214 loc) · 6.84 KB
/
tracking_pace.m
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
%% Dense SDP relaxation for certifiable tracking
% Version with outlier rejection through Lorenzo+GNC, batch level
%
% Lorenzo Shaikewitz for SPARK Lab
clc; clear; close all
% restoredefaultpath
% rng("default")
%% Define settings for batch processing
problem.json = "../datasets/racecar_offline/racecar_fullsize_test_ours.json";
problem.L = 3; % batch size
problem.savefile = "../datasets/racecar_offline/racecar_fullsize_test_ours.json";
% Set bounds based on problem setting
problem.translationBound = 10.0; % [m]
problem.velocityBound = 5.0; % [m/s]
problem.noiseBound_GNC = 0.05;
problem.noiseBound_GNC_residuals = 1;
problem.noiseBound_GRAPH = 0.01;
problem.noiseBound = 0.05;
problem.cBound = 1;
problem.noiseBoundSq = problem.noiseBound^2;
problem.covar_measure_base = 1;
problem.covar_velocity_base = 10;
problem.covar_rotrate_base = 10;
problem.velprior = "body"; % constant body frame velocity
problem.usecBound = false;
% add shape, measurements, outliers
% load("racecar_cad.mat");
% problem.shapes = racecar_cad' / 1000; % 3 x N x K [m]
[problems, gt, teaser] = json2batchproblem(problem);
min_max_dists = robin_min_max_dists(problems{1}.shapes);
%% Solve for each batch
solns = [];
disp("Solving " + string(length(problems)) + " problems...")
for j = 1:length(problems)
% regen if batch size changes.
curproblem = problems{j};
curproblem.regen_sdp = (j==1); % when in doubt, set to true
% run pace with GNC + ROBIN
t = tic;
pace = pace_raw(curproblem,true,true);
pace.fulltime = toc(t);
solns = [solns; pace];
if (mod(j,5) == 0)
disp(j);
end
end
%% Check solutions
L = problem.L;
N = problems{1}.N_VAR;
p_err = zeros(L*length(solns),1);
R_err = zeros(L*length(solns),1);
est.p = zeros(3,1,L*length(solns));
est.R = zeros(3,3,L*length(solns));
figure(1);
for j = 1:length(solns)
problem = problems{j};
soln = solns(j);
idx = ((j-1)*L + 1):j*L;
for l = 1:L
p_err(idx(l)) = norm(soln.p(:,:,l) - gt.p(:,:,idx(l)));
R_err(idx(l)) = getAngularError(gt.R(:,:,idx(l)),soln.R(:,:,l));
% if (p_err(idx(l)) > 10)
% % don't plot
% soln.gap = 1;
% end
end
% if (soln.gap > 0.5)
% % don't plot
% est.p(:,:,idx) = NaN;
% est.R(:,:,idx) = NaN;
% continue
% end
est.p(:,:,idx) = soln.p;
est.R(:,:,idx) = soln.R;
% eigenvalue plot
% figure; bar(eig(soln.raw.Xopt{1})); % if rank = 1, then relaxation is exact/tight
% hold on
% Plot trajectory!
% figure(1);
% axis equal
% p_est = reshape(soln.p,[3,L,1]);
% plot3(p_est(1,:),p_est(2,:),p_est(3,:),'.k', 'MarkerSize',10);
% hold on
%
% R_est = soln.R;
% quiver3(p_est(1,:)',p_est(2,:)',p_est(3,:)',squeeze(R_est(1,1,:)),squeeze(R_est(2,1,:)),squeeze(R_est(3,1,:)),'r');
% quiver3(p_est(1,:)',p_est(2,:)',p_est(3,:)',squeeze(R_est(1,2,:)),squeeze(R_est(2,2,:)),squeeze(R_est(3,2,:)),'g');
% quiver3(p_est(1,:)',p_est(2,:)',p_est(3,:)',squeeze(R_est(1,3,:)),squeeze(R_est(2,3,:)),squeeze(R_est(3,3,:)),'b');
end
%% Plot Ground Truth
plotgt = gt;
figure
p_gt = reshape(plotgt.p,[3,size(plotgt.p,3),1]);
% p_gt = p_gt(:,12*8:15*8)
R_gt = plotgt.R;
plot3(p_gt(1,:),p_gt(2,:),p_gt(3,:),'.k', 'MarkerSize',10);
hold on
axis equal
% R_est = soln.R_est;
quiver3(p_gt(1,:)',p_gt(2,:)',p_gt(3,:)',squeeze(R_gt(1,1,:)),squeeze(R_gt(2,1,:)),squeeze(R_gt(3,1,:)),'r');
quiver3(p_gt(1,:)',p_gt(2,:)',p_gt(3,:)',squeeze(R_gt(1,2,:)),squeeze(R_gt(2,2,:)),squeeze(R_gt(3,2,:)),'g');
quiver3(p_gt(1,:)',p_gt(2,:)',p_gt(3,:)',squeeze(R_gt(1,3,:)),squeeze(R_gt(2,3,:)),squeeze(R_gt(3,3,:)),'b');
%% Plot Together
t = 1:length(est.p);
figure
subplot(3,1,1)
plot(t,p_gt(1,1:length(est.p)),'DisplayName','Ground Truth')
hold on
plot(t,est.p(1,:),'DisplayName','Estimate')
ylabel("x")
legend('Location','ne')
title("Explict Comparison of Evaluated Trajectories")
subplot(3,1,2)
plot(t,p_gt(2,1:length(est.p)),'DisplayName','Ground Truth')
hold on
plot(t,est.p(2,:),'DisplayName','Estimate')
ylabel("y")
subplot(3,1,3)
plot(t,p_gt(3,1:length(est.p)),'DisplayName','Ground Truth')
hold on
plot(t,est.p(3,:),'DisplayName','Estimate')
xlabel("time")
ylabel("z")
%% Save Poses into JSON
L_big = length(est.p);
T_est = repmat(eye(4),[1,1,L_big]);
for l = 1:L_big
T_est(1:3,1:3,l) = est.R(:,:,l);
T_est(1:3,4,l) = est.p(:,:,l)*1000.0;
end
fid = fopen(problem.json);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);
data = jsondecode(str);
cam_wrt_world = [data.cam_wrt_world];
cam_wrt_world = reshape(cam_wrt_world, [4,4,size(data,1)]);
cam_wrt_world(1:3,4,:) = cam_wrt_world(1:3,4,:); % [mm]
for l = 1:length(T_est)
T = inv(cam_wrt_world(:,:,l));
data(l).pace_pose = T*T_est(:,:,l);
end
cocoString = jsonencode(data, "PrettyPrint",true);
fid = fopen(problem.savefile, 'w');
fprintf(fid, '%s', cocoString);
fclose(fid);
%% Print error metrics
gt.p = gt.p(:,:,1:length(est.p));
gt.R = gt.R(:,:,1:length(est.R));
% degcm
[est.degcm, est.p_err, est.R_err] = compute_degcm(gt(1:length(est)),est);
% [teaser.degcm, teaser.p_err, teaser.R_err] = compute_degcm(gt,teaser); % should remove 0s--those are where TEASER failed
degcm_10_5 = compute_degcm(gt,est,'degThreshold',10);
% c error
cerr = compute_cerr(solns, est, problem.shapes, problem.shapes(:,:,end));
cerr_true = compute_cerr_true(solns);
% ADD
score_add = auc_add(solns, est, gt, problem.shapes, problem.shapes(:,:,end), 0.1);
function score_add = auc_add(solns, est, gt, shapes, shape_gt, threshold)
N = size(shapes,2);
K = size(shapes,3);
L = length(est.p);
B = reshape(shapes, [3*N,K]);
% step 1: compute ADD score for each pose estimate
% this score is mean distance between predicted and gt point clouds
% (including predicted/gt t ransforms)
add = zeros(L,1);
for l = 1:length(solns)
soln = solns(l);
for i = 1:3
shape_est = reshape(B*soln.c_raw(:,:,i),[3,N]);
pc_pred = est.R(:,:,l)*shape_est + est.p(:,:,l);
pc_gt = gt.R(:,:,l)*shape_gt + gt.p(:,:,l);
add(3*(l-1)+i) = mean(vecnorm(pc_pred - pc_gt));
end
end
% step 2: compute area under curve (AUC)
% curve in question is accuracy-threshold curve
% see pose-cnn figure 8
% generate curve
thresh = linspace(0,threshold,100); % x-axis
accuracy = zeros(length(thresh),1); % y-axis
for t = 1:length(thresh)
accuracy(t) = sum(add < thresh(t))/length(add);
end
% figure
plot(thresh,accuracy);
% area under curve!
max_score = threshold*1;
score_add = trapz(thresh, accuracy) / max_score;
end
function cerr = compute_cerr(solns, est, shapes, shape_gt)
N = size(shapes,2);
K = size(shapes,3);
L = length(solns);
B = reshape(shapes, [3*N,K]);
cerr = zeros(3*L,N);
for l = 1:L
for i = 1:3
b_est = reshape(B*solns(l).c(:,:,i),[3,N]);
cerr(3*(l-1)+i,:) = vecnorm(b_est - shape_gt);
end
end
end
function cerr = compute_cerr_true(solns)
L = length(solns);
cerr = zeros(3*L,1);
for l = 1:L
c = solns(l).c_raw;
for i = 1:3
cerr(3*(l-1)+i,:) = vecnorm(c(:,:,i) - [0;0;0;0;0;0;0;0;0;1]);
end
end
end