forked from Robert0812/salience_reid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_salience_reid_viper.m
352 lines (287 loc) · 12.8 KB
/
demo_salience_reid_viper.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
% Main function entry for evaluating on VIPeR dataset
%
% Created by Rui Zhao, [email protected]
% This code is release under BSD license, please cite our paper if you use this code:
%
% Rui Zhao, Wanli Ouyang, and Xiaogang Wang. Unsupervised Salience Learning
% for Person Re-identification. In IEEE Conference of Computer Vision and
% Pattern Recognition (CVPR), 2013.
%
% clear all; clc;
% test_trial = 3;
function CMC = demo_salience_reid_viper(test_trial)
global dataset baseExp TRIAL gridstep patchsize par
par = struct(...
'dataset', 'viper', ... % 'viper'
'baseExp', 'unsupervised_salience', ...
'method', 'salience', ... % 'patchmatch', 'salience' ...
'TRIAL', test_trial, ... % average over 10 trials to obtain stable result
'gridstep', 4, ...
'patchsize', 10, ...
'Nr', 100, ...
'sigma1', 2.8, ...
'msk_thr', 0.2, ...
'norm_data', 0, ...
'new_feat', 0, ...
'use_mask', 1, ...
'use_salience', 1, ... % set 1 to use knn salience, and set 2 to use ocsvm salience
'alpha', [-1, 0, 0, 0, 0], ... %[-1, 0.4, 1, 0.6, 0], ... %
'L2', 1, ...
'swap', 1 ...
);
dataset = par.dataset;
baseExp = par.baseExp;
TRIAL = par.TRIAL;
gridstep = par.gridstep;
patchsize = par.patchsize;
if par.L2
par.nor = 2;
else
par.nor = 1;
end
nor = par.nor;
switch par.method
case 'patchmatch'
phiFun = @(x, y, m1, s1, s2, m2, par) (exp(-double(x).^2/par.sigma1^2).*m1).^nor;
par.use_salience = 0;
case 'salience'
phiFun = @(x, y, m1, s1, s2, m2, par) s1.*((exp(-double(x).^2/par.sigma1^2).*m1).^nor).*get_saj(y, s2);
end
project_dir = strcat(pwd, '/');
set_paths;
if par.norm_data
norm_data;
end
initialcontext_general;
if par.swap
trnSg = trnS1;
trnSp = trnS2;
Sg = S1;
Sp = S2;
else
trnSg = trnS2;
trnSp = trnS1;
Sg = S2;
Sp = S1;
end
%% extract dense feature
if par.new_feat
build_densefeature_general;
end
%% Compute patch match for computing salience
clear ref_pwmap_prb ref_pwmap_gal;
Nr = par.Nr;
if par.use_salience
if ~exist([pwdist_dir, 'pwmap_ref_trial', num2str(TRIAL), '.mat'], 'file')
% load testing data
feat_gal = zeros(dim, ny*nx, gsize);
feat_prb = zeros(dim, ny*nx, gsize);
hwait = waitbar(0, 'Loading data for testing ...');
for i = 1:gsize
load([feat_dir, 'feat', num2str(Sg(i)), '.mat']);
feat_gal(:, :, i) = densefeat;
load([feat_dir, 'feat', num2str(Sp(i)), '.mat']);
feat_prb(:, :, i) = densefeat;
waitbar(i/gsize, hwait);
end
% load reference data
ref_feat_gal = zeros(dim, ny*nx, Nr);
ref_feat_prb = zeros(dim, ny*nx, Nr);
hwait = waitbar(0, 'Loading reference data ...');
for i = 1:Nr
load([feat_dir, 'feat', num2str(trnSg(i)), '.mat']);
ref_feat_gal(:, :, i) = densefeat;
load([feat_dir, 'feat', num2str(trnSp(i)), '.mat']);
ref_feat_prb(:, :, i) = densefeat;
waitbar(i/Nr, hwait);
end
% patch match with reference data for computing salience
for i = 1:gsize
for j = 1:Nr
[ref_pwmap_prb(i, j).forward, ref_pwmap_prb(i, j).fpos, ~] = ...
mutualmap(feat_prb(:, :, i), ref_feat_gal(:, :, j));
[ref_pwmap_gal(i, j).forward, ref_pwmap_gal(i, j).fpos, ~] = ...
mutualmap(feat_gal(:, :, i), ref_feat_prb(:, :, j));
waitbar(j/Nr, hwait, ['Computing mutual map with reference sample ', ...
num2str(i), ' out of ', num2str(gsize), '...']);
end
end
save([pwdist_dir, 'pwmap_ref_trial', num2str(TRIAL), '.mat'], 'ref_pwmap_prb', 'ref_pwmap_gal');
close(hwait);
else
load([pwdist_dir, 'pwmap_ref_trial', num2str(TRIAL), '.mat']);
end
end
%% Compute patch match for testing
clear pwmap_prb pwmap_gal;
if ~exist([pwdist_dir, 'pwmap_test_trial', num2str(TRIAL), '.mat'], 'file')
% load testing data
feat_gal = zeros(dim, ny*nx, gsize);
feat_prb = zeros(dim, ny*nx, gsize);
hwait = waitbar(0, 'Loading data for testing ...');
for i = 1:gsize
load([feat_dir, 'feat', num2str(Sg(i)), '.mat']);
feat_gal(:, :, i) = densefeat;
load([feat_dir, 'feat', num2str(Sp(i)), '.mat']);
feat_prb(:, :, i) = densefeat;
waitbar(i/gsize, hwait);
end
% patch match for testing
for i = 1:gsize
for j = 1:gsize
[pwmap_prb(i, j).forward, pwmap_prb(i, j).fpos, ~] = ...
mutualmap(feat_prb(:, :, i), feat_gal(:, :, j));
[pwmap_gal(i, j).forward, pwmap_gal(i, j).fpos, ~] = ...
mutualmap(feat_gal(:, :, i), feat_prb(:, :, j));
waitbar(j/gsize, hwait, ['Computing mutual map for sample ', ...
num2str(i), ' out of ', num2str(gsize), '...']);
end
end
save([pwdist_dir, 'pwmap_test_trial', num2str(TRIAL), '.mat'], 'pwmap_prb', 'pwmap_gal');
close(hwait);
else
load([pwdist_dir, 'pwmap_test_trial', num2str(TRIAL), '.mat']);
end
%% convert pairwise distances and displacement matrix to cells for computation convenience
pwmap_tst_cell = struct2cell(pwmap_prb);
D_cell = squeeze(pwmap_tst_cell(1, :, :));
P_cell = squeeze(pwmap_tst_cell(2, :, :));
%% load mask produced by pose estimation
if par.use_mask
load([salience_dir, 'posemask_viper.mat']);
mask_prb = squeeze(mat2cell(mask(:, :, Sp) >= par.msk_thr, ny, nx, ones(1, length(Sp))));
mp_cell = repmat(mask_prb, 1, gsize);
mask_gal = squeeze(mat2cell(mask(:, :, Sg) >= par.msk_thr, ny, nx, ones(1, length(Sg))));
mg_cell = repmat(mask_gal', gsize, 1);
end
%% compute salience (knn or ocsvm)
switch par.use_salience
case 0
sg_cell = cell(gsize, gsize);
sp_cell = cell(gsize, gsize);
case 1 % KNN salience
if ~exist([salience_dir, 'knn_salience_trial', num2str(TRIAL), '.mat'], 'file')
cellmap_gal = struct2cell(ref_pwmap_gal);
cellmap_prb = struct2cell(ref_pwmap_prb);
dists_gal = cell2mat(reshape(cellmap_gal(1, :, :), 1, 1, gsize, Nr));
dists_prb = cell2mat(reshape(cellmap_prb(1, :, :), 1, 1, gsize, Nr));
rdists_gal = sort(dists_gal, 4);
rdists_prb = sort(dists_prb, 4);
maxdist_gal = rdists_gal(:, :, :, floor(Nr/2));
maxdist_prb = rdists_prb(:, :, :, floor(Nr/2));
% normalize
lwdist = min(maxdist_gal(:));
updist = max(maxdist_gal(:));
salience_gal = (maxdist_gal-lwdist)./(updist-lwdist);
salience_prb = (maxdist_prb-lwdist)./(updist-lwdist);
save([salience_dir, 'knn_salience_trial', num2str(TRIAL), '.mat'], 'salience_gal', 'salience_prb');
else
load([salience_dir, 'knn_salience_trial', num2str(TRIAL), '.mat']);
end
% convert to cell for computation convenience
salience_gal_cell = squeeze(mat2cell(salience_gal, ny, nx, ones(1, gsize)));
salience_prb_cell = squeeze(mat2cell(salience_prb, ny, nx, ones(1, gsize)));
sg_cell = repmat(salience_gal_cell', gsize, 1);
sp_cell = repmat(salience_prb_cell, 1, gsize);
case 2 % One-Class SVM salience, which may consume more computation time
if ~exist([salience_dir, 'ocsvm_salience_trial', num2str(TRIAL), '.mat'], 'file')
% load reference data
ref_feat_gal = zeros(dim, ny*nx, Nr);
ref_feat_prb = zeros(dim, ny*nx, Nr);
hwait = waitbar(0, 'Loading reference data ...');
for i = 1:Nr
load([feat_dir, 'feat', num2str(trnSg(i)), '.mat']);
ref_feat_gal(:, :, i) = densefeat;
load([feat_dir, 'feat', num2str(trnSp(i)), '.mat']);
ref_feat_prb(:, :, i) = densefeat;
waitbar(i/Nr, hwait);
end
cellmap_gal = struct2cell(ref_pwmap_gal);
cellmap_prb = struct2cell(ref_pwmap_prb);
dists_gal = cell2mat(reshape(cellmap_gal(1, :, :), 1, 1, gsize, Nr));
dists_prb = cell2mat(reshape(cellmap_prb(1, :, :), 1, 1, gsize, Nr));
fpos_gal = squeeze(cellmap_gal(2, :, :));
fpos_prb = squeeze(cellmap_prb(2, :, :));
maxdist_gal = zeros(ny*nx, gsize);
maxdist_prb = zeros(ny*nx, gsize);
% compute ocsvm salience for gallery images
for i = 1:gsize
xnn = {};
for j = 1:Nr
feat_cell = reshape(mat2cell(ref_feat_prb(:, :, j), dim, ones(1, ny*nx)), ny, nx);
P = fpos_gal{i, j};
xnn = cat(3, xnn, feat_cell(sub2ind([ny, nx], repmat((1:ny)', 1, nx), double(P))));
end
xnn = reshape(xnn, ny*nx, Nr);
dists = reshape(dists_gal(:, :, i, :), ny*nx, Nr);
maxidx = zeros(1, ny*nx);
parfor j = 1:ny*nx
X = cell2mat(xnn(j, :))';
[~, maxidx(j)] = ocsvm_max(X);
end
maxdist_gal(:, i) = dists(sub2ind([ny*nx, Nr], 1:ny*nx, maxidx));
waitbar(i/gsize, hwait, ['Computing OCSVM salience for gallery image ', num2str(i), '/', num2str(gsize)]);
end
maxdist_gal = reshape(maxdist_gal, ny, nx, gsize);
% compute ocsvm salience for probe images
for i = 1:gsize
xnn = {};
for j = 1:Nr
feat_cell = reshape(mat2cell(ref_feat_gal(:, :, j), dim, ones(1, ny*nx)), ny, nx);
P = fpos_prb{i, j};
xnn = cat(3, xnn, feat_cell(sub2ind([ny, nx], repmat((1:ny)', 1, nx), double(P))));
end
xnn = reshape(xnn, ny*nx, Nr);
dists = reshape(dists_prb(:, :, i, :), ny*nx, Nr);
maxidx = zeros(1, ny*nx);
parfor j = 1:ny*nx
X = cell2mat(xnn(j, :))';
[~, maxidx(j)] = ocsvm_max(X);
end
maxdist_prb(:, i) = dists(sub2ind([ny*nx, Nr], 1:ny*nx, maxidx));
waitbar(i/gsize, hwait, ['Computing OCSVM salience for probe image ', num2str(i), '/', num2str(gsize)]);
end
maxdist_prb = reshape(maxdist_prb, ny, nx, gsize);
% normalization
lwdist = min(maxdist_gal(:));
updist = max(maxdist_gal(:));
salience_gal = (maxdist_gal-lwdist)./(updist-lwdist);
salience_prb = (maxdist_prb-lwdist)./(updist-lwdist);
save([salience_dir, 'ocsvm_salience_trial', num2str(TRIAL), '.mat'], 'salience_gal', 'salience_prb');
close(hwait);
else
load([salience_dir, 'ocsvm_salience_trial', num2str(TRIAL), '.mat']);
end
% convert to cells for computation convenience
salience_gal_cell = squeeze(mat2cell(salience_gal, ny, nx, ones(1, gsize)));
salience_prb_cell = squeeze(mat2cell(salience_prb, ny, nx, ones(1, gsize)));
sg_cell = repmat(salience_gal_cell', gsize, 1);
sp_cell = repmat(salience_prb_cell, 1, gsize);
otherwise
error('unknown option');
end
%% compute pairwise matching scores
clear phi;
phi = cell(gsize, gsize);
parfor i = 1:numel(D_cell)
phi{i} = phiFun(D_cell{i}, P_cell{i}, mp_cell{i}, sp_cell{i}, sg_cell{i}, mg_cell{i}, par);
end
pwdist_sal = cellfun(@(x) dot(ones(1, numel(x)), x(:)), phi);
pwdist_sal = pwdist_sal';
%% combine with features proposed in SDALF
load([pwdist_dir, 'MSCRmatch_VIPeR_f1_Exp007.mat']);
load([pwdist_dir, 'txpatchmatch_VIPeR_f1_Exp007.mat']);
load([pwdist_dir, 'wHSVmatch_VIPeR_f1_Exp007.mat']);
pwdist_y = final_dist_y(Sg, Sp);
pwdist_color = final_dist_color(Sg, Sp);
pwdist_y = pwdist_y./repmat(max(pwdist_y, [], 1), gsize, 1);
pwdist_color = pwdist_color./repmat(max(pwdist_color, [], 1), gsize, 1);
pwdist_hist = final_dist_hist(Sg, Sp);
pwdist_epitext = dist_epitext(Sg, Sp);
pwdist = par.alpha(1).*pwdist_sal + ...
par.alpha(2).*pwdist_y + par.alpha(3).*pwdist_color + ...
par.alpha(4).*pwdist_hist + par.alpha(5).*pwdist_epitext;
%% evaluate re-identification performance
CMC = evaluate_pwdist(pwdist);
fprintf('CMC-rank1:%2.2f%%\n', CMC(1)*100);
close all force;