-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep3_query.m
139 lines (115 loc) · 3.91 KB
/
step3_query.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
createParams;
%% Build All Data Kp File
files = dir(fullfile(params.dataPath, 'quantize', '*.mat'));
files = {files.name};
numImages = numel(files);
file = params.allDataKpFile;
if ~exist(file, 'file')
allDataKp = cell(numImages, 1);
for i = 1:numImages
load(fullfile(params.dataPath, 'quantize', files{i}));
allDataKp{i} = imageKp;
end
save(file, 'allDataKp');
else
load(file);
end
%% Build All Data Bin file
files = dir(fullfile(params.dataPath, 'quantize', '*.mat'));
files = {files.name};
file = params.allDataBinsFile;
if ~exist(file, 'file')
allDataBins = cell(numImages, 1);
for i = 1:numImages
load(fullfile(params.dataPath, 'quantize', files{i}));
allDataBins{i} = bins;
clear bins;
end
save(file, 'allDataBins');
else
load(file);
end
%% Build All Data BoW file
files = dir(fullfile(params.dataPath, 'bow', '*.mat'));
files = {files.name};
file = params.allDataBoWFile;
if ~exist(file, 'file')
allDataBoW = sparse(numel(files), params.histLen);
for i = 1:numImages
load(fullfile(params.dataPath, 'bow', files{i}));
allDataBoW(i, :) = frameBoW;
end
save(file, 'allDataBoW', '-v7.3');
else
load(file);
end
fprintf('Builded all data BoW file!\n');
%% Build IDF file
file = params.idfFile;
if ~exist(file, 'file')
idfWeight = zeros(params.histLen, 1);
for i = 1:numImages
index = find(allDataBoW(i, :));
for j = 1:length(index)
idfWeight(index(j)) = idfWeight(index(j)) + 1;
end
end
for i = 1:params.histLen
if idfWeight(i) > 0
idfWeight(i) = log(numImages / idfWeight(i));
end
end
save(file, 'idfWeight');
else
load(file);
end
fprintf('Builded IDF file!\n');
%% Query
queryBoWPath = fullfile(params.queryPath, 'bow');
queryBinPath = fullfile(params.queryPath, 'quantize');
queryKpPath = fullfile(params.queryPath, 'feature');
queryFiles = dir(fullfile(queryBoWPath, '*.mat'));
queryFiles = {queryFiles.name};
for i = 1:numel(queryFiles)
file = queryFiles{i};
fprintf('%s\n', file);
load(fullfile(queryBoWPath, file));
load(fullfile(queryBinPath, file));
load(fullfile(queryKpPath, file));
queryBoW = imageBoW;
queryBin = bins;
queryKp = imageKp;
clear 'imageBoW', 'bins', 'imageKp';
% Dot product
distance = allDataBoW * (queryBoW .* idfWeight);
[~, sortedIndex] = sort(distace, 'descend');
% Query Expansion based on Geometric Verification
queryBoW = sparse(numImages, 1);
nVerified = 0;
for j = 1:params.topK
matchedWords = matchWords(queryBin(1, :), allDataBins{sortedIndex(j)}(1, :));
matchedWords = [matchedWords matchWords(queryBin(2, :), allDataBins{sortedIndex(j)}(2, :))];
matchedWords = [matchedWords matchWords(queryBin(3, :), allDataBins{sortedIndex(j)}(3, :))];
% Sampling 1000 matchedWords increases speed without affecting
% much on the performance
if (size(matchedWords, 2) > 1000)
matchedWords = matchedWords(:, randperm(size(matchedWords, 2), 1000));
end
inliers = geometricVerification(queryKp, allDataKp{sortedIndex(j)}, matchedWords);
if (numel(inlers) > 20)
temp = reshape(allDataBins{sortedIndex(j)}(:, matchedWords(2, inliers)), 1, []);
temp = sparse(temp, ones(numel(temp), 1), ones(numel(temp), 1), params.histLen, 1) > 0;
nVerified = nVerified + 1;
queryBoW = queryBoW + allDataBoW(sortedIndex(j), :)' .* temp;
end
end
query_bow = query_bow ./ cntVerified;
distance = all_data_subbow * query_bow;
[~, sorted_index] = sort(distance, 'descend');
fid = fopen(fullfile(params.rankListPath, strrep(file, 'mat', 'txt')), 'w');
for j = 1:numImages
fprintf(fid, '%s\n', files{sortedIndex(j)}(1:end - 4));
end
fclose(fid);
end
clear;