-
Notifications
You must be signed in to change notification settings - Fork 4
/
runVIOV2_KITTI_FullLearning.m
262 lines (192 loc) · 8.61 KB
/
runVIOV2_KITTI_FullLearning.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
%% Clean up
clc;
clear rosbag_wrapper;
clear ros.Bag;
clear all;
close all;
addpath('helpers');
addpath('gp');
addpath('keyframe_imu');
addpath('../MATLAB/utils');
addpath('kitti/devkit');
addpath('kitti');
addpath('training');
if ismac
addpath('/Users/valentinp/Research/mexopencv');
addpath('/Users/valentinp/Research/gtsam_toolbox');
else
addpath('~/mexopencv/');
addpath('~/Dropbox/Research/Ubuntu/gtsam_toolbox/');
end
import gtsam.*;
%% Where is the data?
%Karslrugh city centre
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_29/2011_09_29_drive_0071_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_29';
%Open street
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0036_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%Foresty road
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0028_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%Cityish
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0059_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%Trail through forest
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0117_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0061_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%27: Good Turn
% dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_30/2011_09_30_drive_0027_sync';
% dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_30';
%96: Good
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0096_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%Train
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0001_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_30/2011_09_30_drive_0027_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_30';
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0002_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%36: Good
dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0036_sync';
dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0023_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%42: Good
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_10_03/2011_10_03_drive_0042_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_10_03';
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_26/2011_09_26_drive_0035_extract';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_26';
%34: Good
%dataBaseDir = '/home/valentin/Desktop/KITTI/2011_09_30/2011_09_30_drive_0034_sync';
%dataCalibDir = '/home/valentin/Desktop/KITTI/2011_09_30';
%% Get ground truth and import data
frameRange = 1:500;
%Images
monoImageData = loadImageData([dataBaseDir '/image_00'], frameRange);
rgbImageData = loadImageDataRGB([dataBaseDir '/image_02'], frameRange);
%IMU data
[imuData, imuFrames] = loadImuData(dataBaseDir, monoImageData.timestamps);
%Ground Truth
T_wIMU_GT = getGroundTruth(dataBaseDir, imuFrames);
%Add motion blur!
% for image_i = 3:size(monoImageData.rectImages,3)
% if norm(imuData.measOmega(:, image_i)) > 0.1
% printf('Blurring image %d', image_i)
% monoImageData.rectImages(:,:,image_i) = 0.5*monoImageData.rectImages(:,:,image_i) + 0.25*monoImageData.rectImages(:,:,image_i-1) + 0.25*monoImageData.rectImages(:,:,image_i-2);
% end
% end
%% Load calibration
[T_camvelo_struct, K] = loadCalibration(dataCalibDir);
T_camvelo = T_camvelo_struct{1}; %We are using camera 1 (left rect grayscale)
T_veloimu = loadCalibrationRigid(fullfile(dataCalibDir,'calib_imu_to_velo.txt'));
T_camimu = T_camvelo*T_veloimu;
%Add camera ground truth
T_wCam_GT = T_wIMU_GT;
for i = 1:size(T_wIMU_GT, 3)
T_wCam_GT(:,:,i) = T_wIMU_GT(:,:,i)*inv(T_camimu);
end
% camCalib = loadCalibrationCamToCam(fullfile(dataCalibDir,'calib_cam_to_cam.txt') );
% K = camCalib.P_rect{1}*[ camCalib.R_rect{1} zeros(3,1); zeros(1,3) 1 ];
% K = K(1:3,1:3);
% K = camCalib.K{1};
%% VIO pipeline
%Set parameters
close all;
xInit.p = T_wIMU_GT(1:3,4,1);
xInit.v = imuData.initialVelocity;
xInit.b_g = zeros(3,1);
xInit.b_a = zeros(3,1);
xInit.q = [1;zeros(3,1)];
%g_w = rotmat_from_quat(imuData.measOrient(:,1))'*[0 0 -9.81]';
g_w = [0;0;0];
%Pipeline
pipelineOptions.featureCount = 200;
pipelineOptions.initDisparityThreshold = 1;
pipelineOptions.kfDisparityThreshold = 5;
pipelineOptions.showFeatureTracks = false;
pipelineOptions.inlierThreshold = 0.75^2;
pipelineOptions.inlierMinDisparity = 1;
pipelineOptions.inlierMaxForwardDistance = 20;
pipelineOptions.verbose = true;
%GTSAM
pipelineOptions.minViewingsForLandmark = 3;
pipelineOptions.obsNoiseSigma = 1;
pipelineOptions.useRobustMEst = true;
pipelineOptions.mEstWeight = 0.5;
pipelineOptions.triangPointSigma = 5;
pipelineOptions.maxBatchOptimizerError = 5;
noiseParams.sigma_g = 1e-3*ones(3,1);
noiseParams.sigma_a = 1e-2*ones(3,1);
noiseParams.sigma_bg = 1e-5;
noiseParams.sigma_ba = 1e-5;
noiseParams.init_ba = zeros(3,1);
noiseParams.init_bg = zeros(3,1);
noiseParams.tau = 10^12;
%% Learn the weights
% pipelineOptions.featureCount = 25;
%
% %The learning pipeline
% numSubsets = 10;
% %[clusterWeights] = VIOPipelineV2_LearnClusters(K, T_camimu, monoImageData, rgbImageData, imuData, pipelineOptions, noiseParams, xInit, g_w, clusteringModel, T_wIMU_GT);
% [predVectorSpace] = VIOPipelineV2_LearnRandomSubsets(K, T_camimu, monoImageData, rgbImageData, imuData, pipelineOptions, noiseParams, xInit, g_w, numSubsets, T_wIMU_GT);
%
% searcherObject = KDTreeSearcher(predVectorSpace.predVectors');
%
% save('0001_learned.mat', 'searcherObject', 'predVectorSpace')
load('0001_learned.mat')
%% Use the weights!
%The pipeline
%[T_wc_estimated,T_wimu_estimated, T_wimu_gtsam, keyFrames] = VIOPipelineV2_UseKNN(K, T_camimu, monoImageData, rgbImageData, imuData, pipelineOptions, noiseParams, xInit, g_w, searcherObject, predVectorSpace.weights);
% What about non-weights?
[T_wc_estimated,T_wimu_estimated, T_wimu_gtsam, keyFrames] = VIOPipelineV2_GTSAM(K, T_camimu, monoImageData, imuData, pipelineOptions, noiseParams, xInit, g_w);
% Do some analysis!
% Plot the estimated values
figure;
p_IMUw_w_GT = zeros(3, length(keyFrames));
p_IMUw_w_int = zeros(3, length(keyFrames));
p_IMUw_w_gtsam = zeros(3, length(keyFrames));
% Get the keyframe IMU ids
keyFrameIdsIMU = zeros(1, length(keyFrames));
for kf_i = 1:length(keyFrames)
keyFrameIdsIMU(kf_i) = keyFrames(kf_i).imuMeasId;
p_IMUw_w_GT(:,kf_i) = homo2cart(T_wIMU_GT(:,:,keyFrames(kf_i).imuMeasId)*[0;0;0;1]);
p_IMUw_w_int(:,kf_i) = homo2cart(T_wimu_estimated(:,:,keyFrames(kf_i).imuMeasId)*[0;0;0;1]);
p_IMUw_w_gtsam(:,kf_i) = homo2cart(T_wimu_gtsam(:,:,kf_i)*[0;0;0;1]);
end
%Calculate approximate distance travelled
totalDistance = 0;
for p_i = 1:(size(T_wIMU_GT, 3)-1)
currPose = T_wIMU_GT(:,:,p_i);
nextPose = T_wIMU_GT(:,:,p_i+1);
interPose = inv(currPose)*nextPose;
distTravelled = norm(interPose(1:3,4));
totalDistance = totalDistance + distTravelled;
end
plot3(p_IMUw_w_GT(1,:),p_IMUw_w_GT(2,:),p_IMUw_w_GT(3,:), '.-k');
hold on; grid on;
plot3(p_IMUw_w_int(1,:), p_IMUw_w_int(2,:), p_IMUw_w_int(3,:),'.-r');
plot3(p_IMUw_w_gtsam(1,:),p_IMUw_w_gtsam(2,:),p_IMUw_w_gtsam(3,:), '.-g');
set (gcf(), 'outerposition', [25 1000, 560, 470])
view([0 90]);
legend('Ground Truth', 'Integrated','GTSAM', 4)
% Calculate Relative Pose Error
% Take only the poses at the keyframes
T_wIMU_GT_sync = T_wIMU_GT(:,:,keyFrameIdsIMU);
T_wimu_est_sync = T_wimu_estimated(:,:, keyFrameIdsIMU);
RMSE_RPE_opt = calcRelativePoseError( T_wIMU_GT_sync, T_wimu_gtsam );
RMSE_RPE_imuonly = calcRelativePoseError( T_wIMU_GT_sync, T_wimu_est_sync );
%Add to the title
title(sprintf('Mean RMSE RPE (Optimized/IMU Only): %.5f / %.5f ', RMSE_RPE_opt, RMSE_RPE_imuonly));
printf('--------- \n Mean RMSE RPE (Optimized/IMU Only): %.5f / %.5f ', RMSE_RPE_opt, RMSE_RPE_imuonly);
printf('--------- \n End Euclidian Error (Opt/IMU): %.5f / %.5f', norm(p_IMUw_w_GT(:,end) - p_IMUw_w_gtsam(:, end)) ,norm(p_IMUw_w_GT(:,end) - p_IMUw_w_int(:, end)));
printf('--------- \n Total Distance Travelled: %.5f m ', totalDistance);
% Display mean errors
opt_errors = p_IMUw_w_GT - p_IMUw_w_gtsam;
imu_errors = p_IMUw_w_GT - p_IMUw_w_int;
mean_opt_euc = mean(sqrt(sum(opt_errors.^2, 1)));
mean_imu_euc = mean(sqrt(sum(imu_errors.^2, 1)));
printf('--------- \n Mean Euclidian Error (Opt/IMU): %.5f / %.5f',mean_opt_euc , mean_imu_euc);