-
Notifications
You must be signed in to change notification settings - Fork 0
/
doPR.m
155 lines (128 loc) · 5.04 KB
/
doPR.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
clear;
addpath('utils');
addpath('hmm');
addpath('evaluation');
addpath('libs/flann-1.8.4-src/src/matlab');
work_dir = 'work_dir';
result_dir = [work_dir '/result'];
base_names{1} = '2014-06-26-09-24-58';
base_names{2} = '2014-06-26-08-53-56';
query_names{1} = '2014-06-23-15-41-25';
query_names{2} = '2014-06-23-15-36-04';
query_names{3} = '2014-06-23-15-14-44';
query_names{4} = '2014-06-24-14-15-17';
%% HMM parameters
hmm_params.W = 10;
hmm_params.distance_upper_bound = 2.0;
hmm_params.large_distance = 2.5;
hmm_params.sigma = 0.3;
%% Update parameters
update_params.comb_node_prob = 0.3;
update_params.do_cull = false;
update_params.do_combine = false;
%% FLANN parameters
flann.algorithm = 'kmeans';
flann.branching = 16;
flann.iterations = 50;
flann.centers_init = 'kmeanspp';
flann.checks = 128;
flann.knn = 5;
flann_build_parameters = struct('algorithm', flann.algorithm, ...
'branching', flann.branching, 'iterations', flann.iterations, ...
'centers_init', flann.centers_init);
%% Load database & build graph
tic;
[base_info, database, num_imgs_per_seq] = loadData(work_dir, base_names);
fprintf('Build graph...\n');
[node_list, aff_mat] = constructGraph(database, base_info, ...
hmm_params.W, num_imgs_per_seq);
clearvars database base_info
fprintf('\tElapsed time = %.2fs\n', toc);
%% Do PR
for qur_idx = 1 : length(query_names)
fprintf('\nQUERY = %s\n', query_names{qur_idx});
%% Building indexing using FLANN
if exist('database_index', 'var'), flann_free_index(database_index); end
fprintf('Build indexing...\n');
tic;
[database, node_IDs] = constructFeatureMatrixFromGraph(node_list);
database_index = flann_build_index(database, flann_build_parameters);
fprintf('\t---Finished in %.2fs\n', toc);
qur_name{1} = query_names{qur_idx};
[query_info, query] = loadData(work_dir, qur_name);
%% Find K-nearest neighbors
fprintf('Find K-NN...\n');
tic;
query_length = size(query,2);
[nearest_indices, nearest_distances] = flann_search(database_index, ...
query, flann.knn, struct('checks',flann.checks));
fprintf('\t---Finished in %.2fs\n', toc);
%% Create observation model from K-NN
fprintf('Make observation model from K-NN results...\n');
tic;
num_node = length(node_list);
D = ones(query_length, num_node)*hmm_params.large_distance;
D = exp(-D./hmm_params.sigma);
uncertain_ids = find(nearest_distances > hmm_params.distance_upper_bound);
nearest_distances(uncertain_ids) = hmm_params.large_distance;
nearest_probs = exp(-nearest_distances./hmm_params.sigma);
for ii = 1 : query_length
nids = node_IDs(nearest_indices(:, ii)); % get node which store feature vectors
D(ii, nids) = nearest_probs(:, ii);
end
fprintf('\t---Finished in %.2fs\n', toc);
%% Perform Bayes Filter
fprintf('Perform Bayes Filter...\n');
tic;
belief_all = doFilter(sparse(aff_mat), D');
fprintf('\t---Finished in %.2fs\n', toc);
%% Find and save matches
fprintf('Find & save matches...\n');
tic;
pred = cell(query_length, 1);
pred_file = [result_dir '/' qur_name{1} '.mat'];
matches = -ones(query_length, 2);
for T = 1 : query_length
belief = belief_all(:,T);
[matches(T,2), matches(T,1)] = max(belief);
matched_idx = matches(T,1);
pred_loc = node_list{matched_idx}.loc(:,1);
pred_rot = node_list{matched_idx}.rot(:,1);
pred{T}.loc = pred_loc;
pred{T}.rot = pred_rot;
end
save(pred_file, 'pred');
fprintf('\t---Finished in %.2fs\n', toc);
%% Do update & compression
if update_params.do_cull
fprintf('Cull new images to nodes...\n');
tic;
% Cull
feas_id = size(database, 2) + 1;
for ii = 1 : length(query_info)
if ~isnan(matches(ii, 1))
q_fea = query(:, ii);
q_loc = query_info{ii}.loc;
q_rot = query_info{ii}.rot;
node_idx = matches(ii, 1);
node_list{node_idx}.feas_id = [node_list{node_idx}.feas_id feas_id];
node_list{node_idx}.feas = [node_list{node_idx}.feas q_fea];
node_list{node_idx}.loc = [node_list{node_idx}.loc q_loc];
node_list{node_idx}.rot = [node_list{node_idx}.rot q_rot];
end
end
base_names = [base_names qur_name{1}];
fprintf('\t---Finished in %.2fs\n', toc);
% Combine nodes from belief
if update_params.do_combine
fprintf('Combine nodes from belief...\n');
tic;
fprintf('\tBefore combining: %d nodes\n', length(node_list));
[node_list, aff_mat] = combineNodesFromBelief(node_list, aff_mat,belief_all, update_params.comb_node_prob);
fprintf('\tAfter combining: %d nodes\n', length(node_list));
fprintf('\t---Finished in %.2fs\n', toc);
end
end
end
if exist('database_index', 'var'), flann_free_index(database_index); end
evaluate(query_names, work_dir, result_dir);