-
Notifications
You must be signed in to change notification settings - Fork 17
/
eval_one.m
237 lines (216 loc) · 7.55 KB
/
eval_one.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
config;
min_overlap = 0.5;
% assertions
assert(ismember(score_blob,{'n/a','h','o','p'}) == 1);
% set detection root
det_root = './output/%s/hico_det_%s/%s_iter_%d/';
det_root = sprintf(det_root, exp_dir, image_set, prefix, iter);
if ismember(score_blob, {'h','o','p'})
det_root = [det_root(1:end-1) '_' score_blob '/'];
end
% set res file
res_root = './evaluation/result/%s/';
res_root = sprintf(res_root, exp_name);
res_file = '%s%s_%s_%06d.mat';
res_file = sprintf(res_file, res_root, eval_mode, image_set, iter);
if ismember(score_blob, {'h','o','p'})
res_file = [res_file(1:end-4) '_' score_blob '.mat'];
end
makedir(res_root);
% load annotations
anno = load(anno_file);
bbox = load(bbox_file);
% get gt bbox
switch image_set
case 'train2015'
gt_bbox = bbox.bbox_train;
list_im = anno.list_train;
anno_im = anno.anno_train;
case 'test2015'
gt_bbox = bbox.bbox_test;
list_im = anno.list_test;
anno_im = anno.anno_test;
otherwise
error('image_set error\n');
end
assert(numel(gt_bbox) == numel(list_im));
% copy variables
list_action = anno.list_action;
num_action = numel(list_action);
num_image = numel(gt_bbox);
% get object list
det_file = './cache/det_base_caffenet/train2015/HICO_train2015_00000001.mat';
if exist(det_file,'file') ~= 0
ld = load(det_file);
list_coco_obj = cellfun(@(x)strrep(x,' ','_'),ld.cls,'UniformOutput',false);
list_coco_obj = list_coco_obj(2:end)';
else
list_coco_obj = get_list_coco_obj();
end
% get HOI index intervals for object classes
obj_hoi_int = zeros(numel(list_coco_obj), 2);
for i = 1:numel(list_coco_obj)
hoi_int = cell_find_string({list_action.nname}', list_coco_obj{i});
assert(~isempty(hoi_int));
obj_hoi_int(i, 1) = hoi_int(1);
obj_hoi_int(i, 2) = hoi_int(end);
end
fprintf('start evaluation\n');
fprintf('setting: %s\n', eval_mode);
fprintf('exp_name: %s\n', exp_name);
fprintf('score_blob: %s\n', score_blob)
fprintf('\n')
if exist(res_file, 'file')
% load result file
fprintf('results loaded from %s\n', res_file);
ld = load(res_file);
AP = ld.AP;
REC = ld.REC;
% print ap for each class
for i = 1:num_action
nname = list_action(i).nname;
aname = [list_action(i).vname_ing '_' list_action(i).nname];
fprintf(' %03d/%03d %-30s', i, num_action, aname);
fprintf(' ap: %.4f rec: %.4f\n', AP(i), REC(i));
end
else
% convert gt format
gt_all = cell(num_action, num_image);
fprintf('converting gt bbox format ... \n')
for i = 1:num_image
assert(strcmp(gt_bbox(i).filename, list_im{i}) == 1)
for j = 1:numel(gt_bbox(i).hoi)
if ~gt_bbox(i).hoi(j).invis
hoi_id = gt_bbox(i).hoi(j).id;
bbox_h = gt_bbox(i).hoi(j).bboxhuman;
bbox_o = gt_bbox(i).hoi(j).bboxobject;
conn = gt_bbox(i).hoi(j).connection;
boxes = zeros(size(conn, 1), 8);
for k = 1:size(conn, 1)
boxes(k, 1) = bbox_h(conn(k, 1)).x1;
boxes(k, 2) = bbox_h(conn(k, 1)).y1;
boxes(k, 3) = bbox_h(conn(k, 1)).x2;
boxes(k, 4) = bbox_h(conn(k, 1)).y2;
boxes(k, 5) = bbox_o(conn(k, 2)).x1;
boxes(k, 6) = bbox_o(conn(k, 2)).y1;
boxes(k, 7) = bbox_o(conn(k, 2)).x2;
boxes(k, 8) = bbox_o(conn(k, 2)).y2;
end
gt_all{hoi_id, i} = boxes;
end
end
end
fprintf('done.\n');
% load detection
switch format
case 'obj'
% dummy variable
all_boxes = zeros(num_action, 1);
case 'all'
% load detection res (all object mode)
det_file = [det_root 'detections.mat'];
ld = load(det_file);
all_boxes = ld.all_boxes;
end
% start parpool
if ~exist('pool_size','var')
poolobj = parpool();
else
poolobj = parpool(pool_size);
end
% warning off
warning('off','MATLAB:mir_warning_maybe_uninitialized_temporary');
% compute ap for each class
AP = zeros(num_action, 1);
REC = zeros(num_action, 1);
fprintf('start computing ap ... \n');
parfor i = 1:num_action
nname = list_action(i).nname;
aname = [list_action(i).vname_ing '_' list_action(i).nname];
fprintf(' %03d/%03d %-30s', i, num_action, aname);
tic;
% get detection results
switch format
case 'obj'
% get object id and action id within the object category
obj_id = cell_find_string(list_coco_obj, nname);
act_id = i - obj_hoi_int(obj_id, 1) + 1; %#ok
assert(numel(obj_id) == 1);
% load detection res (one object mode)
det_file = [det_root 'detections_' num2str(obj_id,'%02d') '.mat'];
ld = load(det_file);
det = ld.all_boxes(act_id, :);
case 'all'
det = all_boxes(i, :);
end
% convert detection results
det_id = zeros(0, 1);
det_bb = zeros(0, 8);
det_conf = zeros(0, 1);
for j = 1:numel(det)
if ~isempty(det{j})
num_det = size(det{j}, 1);
det_id = [det_id; j * ones(num_det, 1)];
det_bb = [det_bb; det{j}(:, 1:8)];
det_conf = [det_conf; det{j}(:, 9)];
end
end
% convert zero-based to one-based indices
det_bb = det_bb + 1;
% get gt bbox
assert(numel(det) == numel(gt_bbox));
gt = gt_all(i, :);
% adjust det & gt bbox by the evaluation mode
switch eval_mode
case 'def'
% do nothing
case 'ko'
nid = cell_find_string({list_action.nname}', nname); %#ok
iid = find(any(anno_im(nid, :) == 1, 1)); %#ok
assert(all(cellfun(@(x)isempty(x),gt(setdiff(1:numel(gt), iid)))) == 1);
keep = ismember(det_id, iid);
det_id = det_id(keep);
det_bb = det_bb(keep, :);
det_conf = det_conf(keep, :);
end
% compute ap
[rec, prec, ap] = VOCevaldet_bboxpair(det_id, det_bb, det_conf, gt, ...
min_overlap, aname, false);
AP(i) = ap;
if ~isempty(rec)
REC(i) = rec(end);
end
fprintf(' ap: %.4f rec: %.4f', ap, REC(i));
fprintf(' time: %.3fs\n', toc);
end
fprintf('done.\n');
% warning on
warning('on','MATLAB:mir_warning_maybe_uninitialized_temporary');
% delete parpool
delete(poolobj);
% save AP
save(res_file, 'AP', 'REC');
end
% get number of instances for each class
num_inst = zeros(num_action, 1);
for i = 1:numel(bbox.bbox_train)
for j = 1:numel(bbox.bbox_train(i).hoi)
if ~bbox.bbox_train(i).hoi(j).invis
hoi_id = bbox.bbox_train(i).hoi(j).id;
num_inst(hoi_id) = ...
num_inst(hoi_id) + size(bbox.bbox_train(i).hoi(j).connection,1);
end
end
end
s_ind = num_inst < 10;
p_ind = num_inst >= 10;
fprintf('\n');
fprintf('setting: %s\n', eval_mode);
fprintf('exp_name: %s\n', exp_name);
fprintf('score_blob: %s\n', score_blob)
fprintf('\n');
fprintf(' mAP / mRec (full): %.4f / %.4f\n', mean(AP), mean(REC));
fprintf('\n');
fprintf(' mAP / mRec (rare): %.4f / %.4f\n', mean(AP(s_ind)), mean(REC(s_ind)));
fprintf(' mAP / mRec (non-rare): %.4f / %.4f\n', mean(AP(p_ind)), mean(REC(p_ind)));
fprintf('\n');