-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdemo_reid_kcca.m
163 lines (144 loc) · 5.11 KB
/
demo_reid_kcca.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% (c) Copyright 2013-14 MICC - Media Integration and Communication Center,
% University of Florence. Giuseppe Lisanti <giuseppe.lisanti@unifi.it> and
% Iacopo Masi <iacopo.masi@unifi.it>.
%
% If you use this code please cite:
%
% @article{lisanti:icdsc14,
% author = {Lisanti, Giuseppe and Masi, Iacopo and {Del Bimbo}, Alberto},
% title = {Matching People across Camera Views using Kernel Canonical Correlation Analysis},
% booktitle = {Eighth ACM/IEEE International Conference on Distributed Smart Cameras},
% year = {2014}, }
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Attempting to automatically download third-party libraries
getFiles
%%%%%%%%%%%%%%%%%%%
clear;clc; close all force;
addpath('kcca_package');
%% Plot settings
set(0,'DefaultFigureWindowStyle','docked');
colors = repmat('rgbkmcyrgbk',1,200);
markers = repmat('+o*.xsd^v<>ph',1,200);
%% Load data%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
datasetname='VIPeR'; %VIPeR %PRID
ccaON = 1; %% can be turned off since it is slow.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp('VIPeR',datasetname)
load data/VIPeR_split.mat
maxNumTemplate = 1;
num_gallery = 316;
num_train = 316;
num_test = 316;
end
if strcmp('PRID',datasetname)
load data/PRID_split.mat
maxNumTemplate = 1;
num_gallery = 649;
num_train = 100;
num_test = 100;
end
%% KCCA/CCA Parameters
eta_kcca = 1;
kapa_kcca = 0.5;
kapa_cca = 1;
nTrial = 10;
%%%%%%%%%%%%%%%%%%%%%
%% Initialize cmc matrix
cmc_kcca = zeros(num_gallery,3);
cmc_kcca(:,1) = 1:num_gallery;
cmc_cca = zeros(num_gallery,3);
cmc_cca(:,1) = 1:num_gallery;
cmc_nn = zeros(num_gallery,3);
cmc_nn(:,1) = 1:num_gallery;
for nt=1:nTrial
disp(['>Computing Trial ' num2str(nt) '...' ]);
train_a = trials(nt).featAtrain;
train_b = trials(nt).featBtrain;
test_a = trials(nt).featAtest;
test_b = trials(nt).featBtest;
%% Permutation indices
idxTrain_a = 1:num_train;
idxTrain_b = idxTrain_a;
idxProbe = randperm(num_test);
idxGallery = randperm(num_gallery);
%% Permutation on Train, Gallery and Test set
test_a = test_a(:,idxProbe);
test_b = test_b(:,idxGallery);
train_a = train_a(:,idxTrain_a);
train_b = train_b(:,idxTrain_b);
disp('>Applying Kernel to Train and Test...');
%% Applying Kernel
[train_a_ker, omegaA] = kernel_expchi2(train_a',train_a');
[train_b_ker, omegaB] = kernel_expchi2(train_b',train_b');
[test_a_ker] = kernel_expchi2(test_a',train_a',omegaA);
[test_b_ker] = kernel_expchi2(test_b',train_b',omegaB);
if ccaON
disp('>Computing CCA on the training set...');
%% Computing CCA on the training set
[Wx_cca Wy_cca r] = cca(train_b,train_a,kapa_cca);
Wx_cca = real(Wx_cca);
Wy_cca = real(Wy_cca);
end
disp('>Computing KCCA on the training set...');
%% Computing KCCA on the training set
[Wx, Wy, r] = kcanonca_reg_ver2(train_b_ker,train_a_ker,eta_kcca,kapa_kcca,0,0);
[train_a_ker,test_a_ker,train_b_ker,test_b_ker] = center_kcca(train_a_ker,test_a_ker,train_b_ker,test_b_ker);
disp('>Projecting the test data...');
%% Projecting data
if ccaON
test_b_proj = (test_b'*Wx_cca);
test_a_proj = (test_a'*Wy_cca);
end
test_b_ker_proj = test_b_ker*Wx;
test_a_ker_proj = test_a_ker*Wy;
disp('>Computing distances...');
%% Compute distances
if ccaON
score_cca = pdist2(test_b_proj,test_a_proj,'cosine');
end
score_kcca = pdist2(test_b_ker_proj,test_a_ker_proj,'cosine');
score_nn = pdist2(test_b',test_a','euclidean');
%% Re-ordering original PRID labels
if strcmp('PRID',datasetname)
idxProbe = trials(nt).labels_probe(idxProbe);
idxGallery = trials(nt).labels_gallery(idxGallery);
end
disp('>Evaluating results...');
%% Compute CMC for NN, CCA and KCCA
cmcCurrent = zeros(num_gallery,3);
cmcCurrent(:,1) = 1:num_gallery;
for k=1:num_test
finalScore = score_kcca(:,k);
[sortScore sortIndex] = sort(finalScore);
[cmc_kcca cmcCurrent] = evaluateCMC_demo(idxProbe(k),idxGallery(sortIndex),cmc_kcca,cmcCurrent);
end
plotCurrentTrial
if ccaON
cmcCurrent = zeros(num_gallery,3);
cmcCurrent(:,1) = 1:num_gallery;
for k=1:num_test
finalScore = score_cca(:,k);
[sortScore sortIndex] = sort(finalScore);
[cmc_cca cmcCurrent] = evaluateCMC_demo(idxProbe(k),idxGallery(sortIndex),cmc_cca,cmcCurrent);
end
end
cmcCurrent = zeros(num_gallery,3);
cmcCurrent(:,1) = 1:num_gallery;
for k=1:num_test
finalScore = score_nn(:,k);
[sortScore sortIndex] = sort(finalScore);
[cmc_nn cmcCurrent] = evaluateCMC_demo(idxProbe(k),idxGallery(sortIndex),cmc_nn,cmcCurrent);
end
end
figure(1);hold on;plotCMCcurve(cmc_nn,'g','',datasetname);
if ccaON
figure(1);hold on;plotCMCcurve(cmc_cca,'r','',datasetname);
end
figure(1);hold on;plotCMCcurve(cmc_kcca,'b','',datasetname);
if ccaON
legend('Nearest Neighbour (NN)','Canonical Correlation Analysis (CCA)','Our approach (KCCA)');
else
legend('Nearest Neighbour (NN)','Our approach (KCCA)');
end