-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdcaFuse.m
207 lines (171 loc) · 6.73 KB
/
dcaFuse.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
function [Ax,Ay,Xs,Ys] = dcaFuse(X,Y,label)
% DCAFUSE calculates the Discriminant Correlation Analysis (DCA) for
% feature-level fusion in multimodal systems.
%
%
% Inputs:
% X : pxn matrix containing the first set of training feature vectors
% p: dimensionality of the first feature set
% n: number of training samples
%
% Y : qxn matrix containing the second set of training feature vectors
% q: dimensionality of the second feature set
%
% label : 1xn row vector of length n containing the class labels
%
% Outputs:
% Ax : Transformation matrix for the first data set (rxp)
% r: maximum dimensionality in the new subspace
% Ay : Transformation matrix for the second data set (rxq)
% Xs : First set of transformed feature vectors (rxn)
% Xy : Second set of transformed feature vectors (rxn)
%
%
% Sample use:
%
% % Calculate the transformation matrices Ax and Ay and project the
% % training data into the DCA subspace
% >> [Ax, Ay, trainXdca, trainYdca] = dcaFuse(trainX, trainY, label);
%
% % Project the test data into the DCA subspace
% >> testXdca = Ax * testX;
% >> testYdca = Ay * testY;
%
% % Fuse the two transformed feature matrices with either concatenation or summation:
% % Fusion by concatenation (Z1)
% >> trainZ1 = [trainXdca ; trainYdca];
% >> testZ1 = [testXdca ; testYdca];
%
% % Fusion by summation (Z2)
% >> trainZ2 = [trainXdca + trainYdca];
% >> testZ2 = [testXdca + testYdca];
%
%
%
% Details can be found in:
%
% M. Haghighat, M. Abdel-Mottaleb, W. Alhalabi, "Discriminant Correlation
% Analysis: Real-Time Feature Level Fusion for Multimodal Biometric
% Recognition," IEEE Transactions on Information Forensics and Security,
% vol. 11, no. 9, pp. 1984-1996, Sept. 2016.
% http://dx.doi.org/10.1109/TIFS.2016.2569061
%
% and
%
% M. Haghighat, M. Abdel-Mottaleb W. Alhalabi, "Discriminant Correlation
% Analysis for Feature Level Fusion with application to multimodal
% biometrics," IEEE International Conference on Acoustics, Speech and
% Signal Processing (ICASSP), 2016, pp. 1866-1870.
% http://dx.doi.org/10.1109/ICASSP.2016.7472000
%
%
%
% (C) Mohammad Haghighat, University of Miami
% haghighat@ieee.org
% PLEASE CITE THE ABOVE PAPERS IF YOU USE THIS CODE.
[p,n] = size(X);
if size(Y,2) ~= n
error('X and Y must have the same number of columns (samples).');
elseif length(label) ~= n
error('The length of the label must be equal to the number of samples.');
elseif n == 1
error('X and Y must have more than one column (samples)');
end
q = size(Y,1);
% % Normalize features (this has to be done for both train and test data)
% X = (X - repmat(mean(X,2), 1, n))./repmat(std(X,0,2), 1, n);
% Y = (Y - repmat(mean(Y,2), 1, n))./repmat(std(Y,0,2), 1, n);
%% Compute mean vectors for each class and for all training data
classes = unique(label);
c = numel(classes);
cellX = cell(1,c);
cellY = cell(1,c);
nSample = zeros(1,c);
for i = 1:c
index = find(label==classes(i));
nSample(i) = length(index);
cellX{1,i} = X(:,index);
cellY{1,i} = Y(:,index);
end
meanX = mean(X,2); % Mean of all training data in X
meanY = mean(Y,2); % Mean of all training data in Y
classMeanX = zeros(p,c);
classMeanY = zeros(q,c);
for i = 1:c
classMeanX(:,i) = mean(cellX{1,i},2); % Mean of each class in X
classMeanY(:,i) = mean(cellY{1,i},2); % Mean of each class in Y
end
PhibX = zeros(p,c);
PhibY = zeros(q,c);
for i = 1:c
PhibX(:,i) = sqrt(nSample(i)) * (classMeanX(:,i)-meanX);
PhibY(:,i) = sqrt(nSample(i)) * (classMeanY(:,i)-meanY);
end
clear label index cellX cellY meanX meanY classMeanX classMeanY
%% Diagolalize the between-class scatter matrix (Sb) for X
artSbx = (PhibX') * (PhibX); % Artificial Sbx (artSbx) is a (c x c) matrix
[eigVecs,eigVals] = eig(artSbx);
eigVals = abs(diag(eigVals));
% Ignore zero eigenvalues
maxEigVal = max(eigVals);
zeroEigIndx = find(eigVals/maxEigVal<1e-6);
eigVals(zeroEigIndx) = [];
eigVecs(:,zeroEigIndx) = [];
% Sort in descending order
[~,index] = sort(eigVals,'descend');
eigVals = eigVals(index);
eigVecs = eigVecs(:,index);
% Calculate the actual eigenvectors for the between-class scatter matrix (Sbx)
SbxEigVecs = (PhibX) * (eigVecs);
% Normalize to unit length to create orthonormal eigenvectors for Sbx:
cx = length(eigVals); % Rank of Sbx
for i = 1:cx
SbxEigVecs(:,i) = SbxEigVecs(:,i)/norm(SbxEigVecs(:,i));
end
% Unitize the between-class scatter matrix (Sbx) for X
SbxEigVals = diag(eigVals); % SbxEigVals is a (cx x cx) diagonal matrix
Wbx = (SbxEigVecs) * (SbxEigVals^(-1/2)); % Wbx is a (p x cx) matrix which unitizes Sbx
clear index eigVecs eigVals maxEigVal zeroEigIndx
clear PhibX artSbx SbxEigVecs SbxEigVals
%% Diagolalize the between-class scatter matrix (Sb) for Y
artSby = (PhibY') * (PhibY); % Artificial Sby (artSby) is a (c x c) matrix
[eigVecs,eigVals] = eig(artSby);
eigVals = abs(diag(eigVals));
% Ignore zero eigenvalues
maxEigVal = max(eigVals);
zeroEigIndx = find(eigVals/maxEigVal<1e-6);
eigVals(zeroEigIndx) = [];
eigVecs(:,zeroEigIndx) = [];
% Sort in descending order
[~,index] = sort(eigVals,'descend');
eigVals = eigVals(index);
eigVecs = eigVecs(:,index);
% Calculate the actual eigenvectors for the between-class scatter matrix (Sby)
SbyEigVecs = (PhibY) * (eigVecs);
% Normalize to unit length to create orthonormal eigenvectors for Sby:
cy = length(eigVals); % Rank of Sby
for i = 1:cy
SbyEigVecs(:,i) = SbyEigVecs(:,i)/norm(SbyEigVecs(:,i));
end
% Unitize the between-class scatter matrix (Sby) for Y
SbyEigVals = diag(eigVals); % SbyEigVals is a (cy x cy) diagonal matrix
Wby = (SbyEigVecs) * (SbyEigVals^(-1/2)); % Wby is a (q x cy) matrix which unitizes Sby
clear index eigVecs eigVals maxEigVal zeroEigIndx
clear PhibY artSby SbyEigVecs SbyEigVals
%% Project data in a space, where the between-class scatter matrices are
% identity and the classes are separated
r = min(cx,cy); % Maximum length of the desired feature vector
Wbx = Wbx(:,1:r);
Wby = Wby(:,1:r);
Xp = Wbx' * X; % Transform X (pxn) to Xprime (rxn)
Yp = Wby' * Y; % Transform Y (qxn) to Yprime (rxn)
%% Unitize the between-set covariance matrix (Sxy)
% Note that Syx == Sxy'
Sxy = Xp * Yp'; % Between-set covariance matrix
[Wcx,S,Wcy] = svd(Sxy); % Singular Value Decomposition (SVD)
Wcx = Wcx * (S^(-1/2)); % Transformation matrix for Xp
Wcy = Wcy * (S^(-1/2)); % Transformation matrix for Yp
Xs = Wcx' * Xp; % Transform Xprime to XStar
Ys = Wcy' * Yp; % Transform Yprime to YStar
Ax = (Wcx') * (Wbx'); % Final transformation Matrix of size (rxp) for X
Ay = (Wcy') * (Wby'); % Final transformation Matrix of size (rxq) for Y