-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainSH.m
51 lines (42 loc) · 1.11 KB
/
trainSH.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
function SHparam = trainSH(X, SHparam)
%
% Input
% X = features matrix [Nsamples, Nfeatures]
% SHparam.nbits = number of bits (nbits do not need to be a multiple of 8)
%
%
% Spectral Hashing
% Y. Weiss, A. Torralba, R. Fergus.
% Advances in Neural Information Processing Systems, 2008.
[Nsamples Ndim] = size(X);
nbits = SHparam.nbits;
% algo:
% 1) PCA
npca = min(nbits, Ndim);
[pc, l] = eigs(cov(X), npca);
X = X * pc; % no need to remove the mean
% 2) fit uniform distribution
mn = prctile(X, 5); mn = min(X)-eps;
mx = prctile(X, 95); mx = max(X)+eps;
% 3) enumerate eigenfunctions
R=(mx-mn);
maxMode=ceil((nbits+1)*R/max(R));
nModes=sum(maxMode)-length(maxMode)+1;
modes = ones([nModes npca]);
m = 1;
for i=1:npca
modes(m+1:m+maxMode(i)-1,i) = 2:maxMode(i);
m = m+maxMode(i)-1;
end
modes = modes - 1;
omega0 = pi./R;
omegas = modes.*repmat(omega0, [nModes 1]);
eigVal = -sum(omegas.^2,2);
[yy,ii]= sort(-eigVal);
modes=modes(ii(2:nbits+1),:);
% 4) store paramaters
SHparam.pc = pc;
SHparam.mn = mn;
SHparam.mx = mx;
SHparam.mx = mx;
SHparam.modes = modes;