-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize2.m
161 lines (137 loc) · 4.31 KB
/
visualize2.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
%% Step 4: Visualize Results
% Load solved problems and display results
% Run Experiment Settings section before this
%
% Lorenzo Shaikewitz for SPARK Lab
videoNumber = "8";
savename = "ycbineoat_" + videoNumber;
load(savename);
%% Reformat solutions
est = getEstimates(problems, solns);
% est.p = est.p - [0.0016; 0.0076; -0.0072]; % offset for cracker
% plot x,y,z
plotSolns(gt, est);
% compute scores
adds = false; % takes a while
thresh = 0.1;
figure; hold on;
tab_new = computeScores(problems{1}, gt, teaser, est, params.video, adds, thresh)
% save to json
% problems{1}.savefile="test.json";
save2json(est, problems{1})
% get errors
% est = getErrors(gt, est);
%% Helper function: reformat solutions
function est = getEstimates(problems, solns)
% convert solns (list of structs) to compact form
est.p = zeros(3,1,length(solns)+2)*NaN;
est.R = zeros(3,3,length(solns)+2)*NaN;
est.c = zeros(length(solns)+2,1)*NaN;
for j = 1:length(solns)
problem = problems{j};
soln = solns{j};
L_cur = problem.L;
idx = problem.startIdx:(problem.startIdx + L_cur-1);
if j == 1
% update estimates for times 1, 2, 3
est.p(:,:,idx) = soln.p;
est.R(:,:,idx) = soln.R;
[~, est.c(idx)] = max(soln.c);
else
% update estimate for current time only
est.p(:,:,idx(end)) = soln.p(:,:,end);
est.R(:,:,idx(end)) = soln.R(:,:,end);
[~, est.c(idx(end))] = max(soln.c);
end
end
end
%% Helper function: plot solutions
function plotSolns(gt, est)
% plot x, y, z
p_gt = reshape(gt.p,[3,size(gt.p,3),1]);
p_est = reshape(est.p,[3,size(est.p,3),1]);
L = length(est.R);
w_est = zeros(3,L);
w_gt = zeros(3,L);
for l = 1:length(est.R)
axang = rotm2axang(est.R(:,:,l));
w_est(:,l) = axang(1:3)*axang(4);
axang = rotm2axang(gt.R(:,:,l));
w_gt(:,l) = axang(1:3)*axang(4);
end
figure
t=tiledlayout(3,2);
nexttile
plot(p_gt(1,:)); hold on; plot(p_est(1,:))
ylabel("x")
title("Positions")
nexttile
plot(w_est(1,:)); hold on; plot(w_gt(1,:),'k');
title("Rotations")
nexttile
plot(p_gt(2,:)); hold on; plot(p_est(2,:))
ylabel("y")
nexttile
plot(w_est(2,:)); hold on; plot(w_gt(2,:),'k');
nexttile
plot(p_gt(3,:)); hold on; plot(p_est(3,:))
ylabel("z")
nexttile
plot(w_est(3,:)); hold on; plot(w_gt(3,:),'k');
end
%% Helper function: compute scores
function tab = computeScores(problem, gt, teaser, est, video, adds, threshold)
models_dir = "~/research/tracking/datasets/YCBInEOAT/models3/";
% models_dir = "~/research/tracking/datasets/YCBInEOAT/DATA/models_centered/";
if (problem.object == "cracker") || (problem.object == "sugar")
pcfiles = models_dir + ["cracker.ply", "sugar.ply"];%, "jello.ply"];
elseif (problem.object == "mustard") || (problem.object == "bleach")
pcfiles = models_dir + ["mustard.ply", "bleach.ply"];
elseif (problem.object == "tomato")
pcfiles = models_dir + ["coffee.ply", "tomato.ply", "tuna.ply"];
end
gt.c = find(contains(pcfiles, problem.object));
teaser.c = gt.c*ones(length(teaser.p),1);
% Compute scores!
[add_ours, adds_ours] = get_adds(gt, est, pcfiles, adds); % takes a while if calc adds
score_add_ours = get_auc(add_ours, threshold);
score_adds_ours = get_auc(adds_ours, threshold);
[add_teaser, adds_teaser] = get_adds(gt, teaser, pcfiles, false); % takes a while
score_add_teaser = get_auc(add_teaser, threshold);
score_adds_teaser = get_auc(adds_teaser, threshold);
% put into table
methods = ["CAST"; "TEASER"];
add_adds = [score_add_ours*100, score_adds_ours*100;
score_add_teaser*100, score_adds_teaser*100];
vid = [video; video];
tab = table(methods,add_adds,vid);
end
%% Helper function: save to JSON
function save2json(est, problem)
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);
for l = 1:length(T_est)
data(l).cast_pose = T_est(:,:,l);
end
cocoString = jsonencode(data, "PrettyPrint",true);
fid = fopen(problem.savefile, 'w');
fprintf(fid, '%s', cocoString);
fclose(fid);
end
%% Helper function: get errors
function est = getErrors(gt, est)
est.p_err = squeeze(vecnorm(est.p - gt.p));
est.R_err = zeros(length(est.R),1);
for l = 1:length(est.R)
est.R_err(l) = getAngularError(gt.R(:,:,l),est.R(:,:,l));
end
end