-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracking_frame_lorenzo.m
306 lines (255 loc) · 8.18 KB
/
tracking_frame_lorenzo.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
%% Dense SDP relaxation for certifiable tracking
% Version with outlier rejection through Lorenzo+GNC, frame level
%
% Lorenzo Shaikewitz for SPARK Lab
clc; clear; close all
% restoredefaultpath
% rng("default")
%% Define settings for batch processing
problem.json = "../datasets/racecar_offline/racecar_fast2.json";
problem.L = 12; % CAST-L
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.covar_measure_base = 1;
problem.covar_velocity_base = 10; % inf for CAST-U
problem.covar_rotrate_base = 10; % inf for CAST-U
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] = json2frameproblem(problem);
min_max_dists = robin_min_max_dists(problems{1}.shapes);
%% Solve for each batch
solns = [];
last_L = 0;
disp("Solving " + string(length(problems)) + " problems...")
for j = 1:length(problems)
% regen if batch size changes.
curproblem = problems{j};
curproblem.regen_sdp = (curproblem.L~=last_L);
last_L = curproblem.L;
% data for GNC
curproblem.type = "tracking";
curproblem.N = curproblem.N_VAR*curproblem.L; % How many measurements this problem has (updated by ROBIN)
curproblem.outliers = []; % outlier indicies
curproblem.priors = [];
curproblem.dof = 3;
curproblem = lorenzo_prune(curproblem, min_max_dists);
% preprocess inliers
% if isfield(curproblem,'prioroutliers')
% curproblem.prioroutliers = sort(curproblem.prioroutliers);
% curproblem.N = curproblem.N - length(curproblem.prioroutliers);
% end
% run GNC
t = tic;
[inliers, info] = gnc2(curproblem, @solver_for_gnc, 'barc2', curproblem.noiseBound_GNC);
disp("GNC finished " + string(j) + " (" + info.Iterations + " iterations)")
info.f_info.soln.fulltime = toc(t) + curproblem.milptime;
soln = info.f_info.soln;
ef = eig(soln.raw.Xopt{1});
if (ef(end-1) > 1e-4)
disp("**Not convergent: " + string(soln.gap_stable))
end
% view_gnc(curproblem,info);
solns = [solns; soln];
if (mod(j,5) == 0)
disp(j);
end
end
% save("racecar_L4.mat","solns");
%% Check solutions
est = struct();
est.p = zeros(3,1,length(solns)+2);
est.R = zeros(3,3,length(solns)+2);
est.gap = zeros(length(solns)+2,1);
for j = 1:length(solns)
problem = problems{j};
soln = solns(j);
% true horizon-level estimate
if j == 1
est.p(:,:,1:3) = soln.p_est;
est.R(:,:,1:3) = soln.R_est;
est.gap(1:3) = soln.gap_stable;
else
est.p(:,:,j+2) = soln.p_est(:,:,end);
est.R(:,:,j+2) = soln.R_est(:,:,end);
est.gap(j+2) = soln.gap_stable;
end
end
est.p(:,:,est.gap > 0.01) = NaN;
est.R(:,:,est.gap > 0.01) = NaN;
%% Plot solutions
options = {est, gt, teaser};
titles = ["OURS", "Ground Truth", "Teaser"];
for i = 1:length(options)
figure
p = reshape(options{i}.p,[3,size(options{i}.p,3),1]);
% p_gt = p_gt(:,12*8:15*8)
R = options{i}.R;
plot3(p(1,:),p(2,:),p(3,:),'.k', 'MarkerSize',10);
hold on
axis equal
% R_est = soln.R_est;
% quiver3(p(1,:)',p(2,:)',p(3,:)',squeeze(R(1,1,:)),squeeze(R(2,1,:)),squeeze(R(3,1,:)),'r');
% quiver3(p(1,:)',p(2,:)',p(3,:)',squeeze(R(1,2,:)),squeeze(R(2,2,:)),squeeze(R(3,2,:)),'g');
% quiver3(p(1,:)',p(2,:)',p(3,:)',squeeze(R(1,3,:)),squeeze(R(2,3,:)),squeeze(R(3,3,:)),'b');
title(titles(i))
end
figure
tab = table();
tab.x = est.p(1,1:1880)';
tab.y = est.p(2,1:1880)';
tab.z = est.p(3,1:1880)';
% tab.tight = options{i}.gap < 1e-4;
tab.tight = abs(est.gap(1:1880));
scatter(tab,'x','y','filled','ColorVariable','tight','SizeData',20)
% view(0,90)
axis equal
% colorbar %('Ticks',[1e-4,1])
cMap = interp1([0;1],[0 0 0; 1 0 0],linspace(0,1,256));
colormap(cMap);
set(gca,'ColorScale','log')
axis off
colorbar('Location','south','AxisLocation','out')
%% Plot Together
% position
p_gt = gt.p;
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")
% rotation
eul_gt = zeros(3,length(est.R));
eul_est = zeros(3,length(est.R));
for l = 1:length(est.R)
eul_gt(:,l) = rotm2eul(gt.R(:,:,l));
eul_est(:,l) = rotm2eul(est.R(:,:,l));
end
t = 1:length(eul_est);
figure
subplot(3,1,1)
plot(t,eul_gt(1,1:length(eul_est)),'DisplayName','Ground Truth')
hold on
plot(t,eul_est(1,:),'DisplayName','Estimate')
ylabel("x")
legend('Location','ne')
title("Explict Comparison of Evaluated Trajectories")
subplot(3,1,2)
plot(t,eul_gt(2,1:length(eul_est)),'DisplayName','Ground Truth')
hold on
plot(t,eul_est(2,:),'DisplayName','Estimate')
ylabel("y")
subplot(3,1,3)
plot(t,eul_gt(3,1:length(eul_est)),'DisplayName','Ground Truth')
hold on
plot(t,eul_est(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).cast_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
% degcm
[est.degcm, est.p_err, est.R_err] = compute_degcm(gt,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 = vecnorm([solns.c_est] - repmat([0;0;0;0;0;0;0;0;0;1],[1,length(solns)]));
% ADD
score_add = auc_add(solns, est, gt, problem.shapes, problem.shapes(:,:,end), 0.1);
% auc_add(solns,teaser,gt,problem.shapes(:,:,end),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 transforms)
add = zeros(L,1);
for l = 3:L
soln = solns(l-2);
% soln.c_est = 1;
shape_est = reshape(B*soln.c_est,[3,N]);
pc_pred = est.R(:,:,l)*shape_est + est.p(:,:,l);
pc_gt = gt.R(:,:,l)*shape_gt + gt.p(:,:,l);
add(l) = mean(vecnorm(pc_pred - pc_gt));
if l == 3
% fill in first and second p
for ll = 1:2
pc_pred = est.R(:,:,ll)*shape_est + est.p(:,:,ll);
pc_gt = gt.R(:,:,ll)*shape_gt + gt.p(:,:,ll);
add(ll) = mean(vecnorm(pc_pred - pc_gt));
end
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(est.p)-2;
B = reshape(shapes, [3*N,K]);
cerr = zeros(L,N);
for l = 1:L
b_est = reshape(B*solns(l).c_est,[3,N]);
cerr(l,:) = vecnorm(b_est - shape_gt);
end
end