forked from quantombone/exemplarsvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esvm_detect_imageset.m
207 lines (168 loc) · 5.26 KB
/
esvm_detect_imageset.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
function grid = esvm_detect_imageset(imageset, models, ...
params, setname)
% Apply a set of models (raw exemplars, trained exemplars, dalals,
% poselets, components, etc) to a set of images.
%
% imageset: a (virtual) set of images, such that
% convert_to_I(imageset{i}) returns an image
% models: Cell array of models
% params(optional): detection parameters
% setname(optional): a name of the set, which lets us cache results
% on disk
%
% Copyright (C) 2011-12 by Tomasz Malisiewicz
% All rights reserved.
%
% This file is part of the Exemplar-SVM library and is made
% available under the terms of the MIT license (see COPYING file).
% Project homepage: https://github.com/quantombone/exemplarsvm
if ~exist('params','var')
params = esvm_get_default_params;
end
if ~exist('setname','var')
params.detect_images_per_chunk = 1;
setname = '';
end
if isfield(params,'dataset_params') && ...
isfield(params.dataset_params,'localdir') && ...
length(params.dataset_params.localdir)>0 && length(setname)>0
save_files = 1;
else
save_files = 0;
end
if isempty(imageset)
grid = {};
return;
end
if save_files == 1
models_name = '';
if length(models)>=1 && isfield(models{1},'models_name') && ...
isstr(models{1}.models_name)
models_name = models{1}.models_name;
end
final_file = sprintf('%s/detections/%s-%s.mat',...
params.dataset_params.localdir,setname, ...
models_name);
if fileexists(final_file)
res = load(final_file);
grid = res.grid;
return;
end
end
if save_files == 1
baser = sprintf('%s/detections/%s-%s/',params.dataset_params.localdir,setname, ...
models_name);
else
baser = '';
end
if (save_files==1) && (~exist(baser,'dir'))
fprintf(1,'Making directory %s\n',baser);
mkdir(baser);
end
%% Chunk the data into detect_images_per_chunk images per chunk so that we
%process several images, then write results for entire chunk
inds = do_partition(1:length(imageset),params.detect_images_per_chunk);
% randomize chunk orderings
myRandomize;
ordering = randperm(length(inds));
%[v,host_string]=unix('hostname');
allfiles = cell(length(ordering), 1);
counter = 0;
for i = 1:length(ordering)
ind1 = inds{ordering(i)}(1);
ind2 = inds{ordering(i)}(end);
filer = sprintf('%s/result_%05d-%05d.mat',baser,ind1,ind2);
allfiles{i} = filer;
filerlock = [filer '.lock'];
if save_files == 1
if (fileexists(filer) || (mymkdir_dist(filerlock) == 0))
continue
end
end
res = cell(0,1);
%% pre-load all images in a chunk
%fprintf(1,'Preloading %d images\n',length(inds{ordering(i)}));
clear Is;
Is = imageset(inds{ordering(i)});
%Is = cellfun2(@(x)convert_to_I(x),imageset(inds{ordering(i)}));
%for j = 1:length(inds{ordering(i)})
% Is{j} = convert_to_I(imageset{inds{ordering(i)}(j)});
%end
L = length(inds{ordering(i)});
for j = 1:L
index = inds{ordering(i)}(j);
fprintf(1,' --image %05d/%05d:',counter+j,length(imageset));
Iname = imageset{index};
try
hit = strfind(Iname,'JPEGImages/');
curid = Iname((hit+11):end);
hit = strfind(curid,'.');
curid = curid(1:(hit(end)-1));
%[tmp,curid,tmp] = fileparts(Iname);
catch
curid = '';
end
I = convert_to_I(Is{j});
starter = tic;
rs = esvm_detect(I, models, params);
% for q = 1:length(rs.bbs)
% if ~isempty(rs.bbs{q})
% rs.bbs{q}(:,11) = index;
% if length(rs.bbs{q}(1,:))~=12
% error('BUG: Invalid length bb');
% end
% end
% end
coarse_boxes = cat(1,rs.bbs{:});
if ~isempty(coarse_boxes)
coarse_boxes(:,11) = index;
scores = coarse_boxes(:,end);
else
scores = [];
end
[aa,bb] = max(scores);
fprintf(1,' %d exemplars took %.3fsec, #windows=%05d, max=%.3f \n',...
length(models),toc(starter),length(scores),aa);
% Transfer GT boxes from models onto the detection windows
boxes = esvm_adjust_boxes(coarse_boxes,models);
if (params.detect_min_scene_os > 0.0)
os = getosmatrix_bb(boxes,[1 1 size(I,2) size(I,1)]);
goods = find(os >= params.detect_min_scene_os);
boxes = boxes(goods,:);
coarse_boxes = coarse_boxes(goods,:);
end
extras = [];
res{j}.coarse_boxes = coarse_boxes;
res{j}.bboxes = boxes;
res{j}.index = index;
res{j}.extras = extras;
res{j}.imbb = [1 1 size(I,2) size(I,1)];
res{j}.curid = curid;
%%%NOTE: the gt-function is well-defined for VOC-exemplars
if isfield(params,'gt_function') && ...
~isempty(params.gt_function)
res{j}.extras = params.gt_function(params.dataset_params, ...
Iname, res{j}.bboxes);
end
end
counter = counter + L;
% save results into file and remove lock file
if save_files == 1
save(filer,'res');
try
rmdir(filerlock);
catch
fprintf(1,'Directory %s already gone\n',filerlock);
end
else
allfiles{i} = res;
end
end
if save_files == 0
grid = cellfun2(@(x)x{1},allfiles);
return;
end
[allfiles] = sort(allfiles);
grid = esvm_load_result_grid(params.dataset_params, models, ...
setname, ...
allfiles);