-
Notifications
You must be signed in to change notification settings - Fork 2
/
compressSH.m
52 lines (42 loc) · 1.19 KB
/
compressSH.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
function [B, U] = compressSH(X, SHparam)
%
% [B, U] = compressSH(X, SHparam)
%
% Input
% X = features matrix [Nsamples, Nfeatures]
% SHparam = parameters (output of trainSH)
%
% Output
% B = bits (compacted in 8 bits words)
% U = value of eigenfunctions (bits in B correspond to U>0)
%
%
% Spectral Hashing
% Y. Weiss, A. Torralba, R. Fergus.
% Advances in Neural Information Processing Systems, 2008.
[Nsamples Ndim] = size(X);
nbits = SHparam.nbits;
X = X*SHparam.pc;
X = X-repmat(SHparam.mn, [Nsamples 1]);
omega0=pi./(SHparam.mx-SHparam.mn);
omegas=SHparam.modes.*repmat(omega0, [nbits 1]);
U = zeros([Nsamples nbits]);
for i=1:nbits
omegai = repmat(omegas(i,:), [Nsamples 1]);
ys = sin(X.*omegai+pi/2);
yi = prod(ys,2);
U(:,i)=yi;
end
B = compactbit(U>0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cb = compactbit(b)
%
% b = bits array
% cb = compacted string of bits (using words of 'word' bits)
[nSamples nbits] = size(b);
nwords = ceil(nbits/8);
cb = zeros([nSamples nwords], 'uint8');
for j = 1:nbits
w = ceil(j/8);
cb(:,w) = bitset(cb(:,w), mod(j-1,8)+1, b(:,j));
end