Skip to content

Commit

Permalink
add a new hash method 'PQ'
Browse files Browse the repository at this point in the history
  • Loading branch information
Lishuang322 committed Feb 22, 2019
1 parent 23459ee commit da787f8
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 1 deletion.
87 changes: 87 additions & 0 deletions Method-SP/SP.m
@@ -0,0 +1,87 @@
%function [R R2] = SP(X, bit, sparsity, n_iter)
function [R R2] = SP(X, SPparam)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solve:
% min |XR2'-B|^2 + BETA |XR2'-XR'|^2
% s.t. R2'R2=I, |R|0<= m
% X: num * dim trianing data matrix. Note in our paper, X is dim * num.
% R and R2: bit * dim projection matrix, R2 is orthogonal and R is sparse
% B: num * bit binary codes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bit = SPparam.nbits;
sparsity = SPparam.sparsity;
n_iter = SPparam.iter;


% initialize with a random rotation
dim = size(X, 2);
R = randn(bit, dim);
B = X*R';
t = (B>0);
B(t) = 1;
B(~t) = -1;

% pre compute pca matrix if bit < dim
if(bit < dim)
[pc, ~] = eigs(cov(X),bit);
X_pc = X * pc;
end

fprintf('iteration \n');
beta = 1;
for iter=0:n_iter

% fix B,R, update R2
Y = (B + beta*X*R')/(1+beta);
if(bit >= dim)
R2 = OrthogonalConstrainOpt(X,Y);
else
Rtmp = OrthogonalConstrainOpt(X_pc,Y);
R2 = Rtmp * pc';
end

% fix B,R2, update R
R = SparseConstrainOpt(R2, sparsity);

% fix R,R2, update B
B = X*R2';
t = (B>0);
B(t) = 1;
B(~t) = -1;
end
fprintf('\n');


function R = OrthogonalConstrainOpt(X, Y)
%%% min |XR'-Y|^2, s.t. R'R=I
data_dim = size(X,2);
bit_num = size(Y,2);

[U Sigma V] = svd(X'*Y);
if(bit_num >= data_dim)
V = V(:,1:data_dim);
else
U = U(:,1:bit_num);
end
R = V*U';
end

function R = SparseConstrainOpt(R2, sparsity)
n_total = numel(R2);
n_nonzero = ceil(n_total * (1-sparsity));
values = abs(R2(:));
values = sort(values, 'descend');
thresh = values(n_nonzero);
R=R2;
R(abs(R)<thresh) = 0;
end

end







87 changes: 87 additions & 0 deletions Method-SP/SP.m~
@@ -0,0 +1,87 @@
%function [R R2] = SP(X, bit, sparsity, n_iter)
function [R R2] = SP(X, SPparam)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Solve:
% min |XR2'-B|^2 + BETA |XR2'-XR'|^2
% s.t. R2'R2=I, |R|0<= m
% X: num * dim trianing data matrix. Note in our paper, X is dim * num.
% R and R2: bit * dim projection matrix, R2 is orthogonal and R is sparse
% B: num * bit binary codes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bit = SPparam.nbits;
sparsity = SPparam.sparsity;
n_iter = SPparam.iter;


% initialize with a random rotation
dim = size(X, 2);
R = randn(bit, dim);
B = X*R';
t = (B>0);
B(t) = 1;
B(~t) = -1;

% pre compute pca matrix if bit < dim
if(bit < dim)
[pc, ~] = eigs(cov(X),bit);
X_pc = X * pc;
end

fprintf('iteration \n');
beta = 1;
for iter=0:n_iter

% fix B,R, update R2
Y = (B + beta*X*R')/(1+beta);
if(bit >= dim)
R2 = OrthogonalConstrainOpt(X,Y);
else
Rtmp = OrthogonalConstrainOpt(X_pc,Y);
R2 = Rtmp * pc';
end

% fix B,R2, update R
R = SparseConstrainOpt(R2, sparsity);

% fix R,R2, update B
B = X*R2';
t = (B>0);
B(t) = 1;
B(~t) = -1;
end
fprintf('\n');


function R = OrthogonalConstrainOpt(X, Y)
%%% min |XR'-Y|^2, s.t. R'R=I
data_dim = size(X,2);
bit_num = size(Y,2);

[U Sigma V] = svd(X'*Y);
if(bit_num >= data_dim)
V = V(:,1:data_dim);
else
U = U(:,1:bit_num);
end
R = V*U';
end

function R = SparseConstrainOpt(R2, sparsity)
n_total = numel(R2);
n_nonzero = ceil(n_total * (1-sparsity));
values = abs(R2(:));
values = sort(values, 'descend');
thresh = values(n_nonzero);
R=R2;
R(abs(R)<thresh) = 0;
end

end







17 changes: 17 additions & 0 deletions demo.m
Expand Up @@ -285,6 +285,23 @@
[B_trn, ~] = compressBRE(H);
[B_tst, ~] = compressBRE(H_query);
clear db_data BREparam;

case 'SP'
%Yan Xia,Kaiming He,Pushmeet Kohli,and Jian Sun.
%"Sparse Projections for High-Dimensional Binary Codes." In CVPR 2015.
addpath('./Method-SP/');
fprintf('......%s start...... \n\n', 'SP');
SPparam.nbits = param.nbits;
SPparam.sparsity = 0.9;
SPparam.iter = 50;
%train
R = SP(train_data,SPparam);
%coding
B_trn = (train_data*R' >=0);
B_tst = (test_data*R' >=0);
B_trn = compactbit(B_trn);
B_tst = compactbit(B_tst);
clear db_data SPparam;
end

% compute Hamming metric and compute recall precision
Expand Down
2 changes: 1 addition & 1 deletion main_demo.m
Expand Up @@ -54,7 +54,7 @@
db_data = db_datalabel(:, 1:end);
end

hashmethods = {'LSH', 'ITQ'};
hashmethods = {'SP', 'ITQ'};
%hashmethods = {'Our Method', 'SELVE', 'CBE-opt', 'LSH', 'PCAH', 'SH', 'SKLSH', 'DSH', 'SpH'};
%hashmethods = {'CBE-rand', 'CBE-opt', 'ITQ', 'LSH', 'PCAH', 'SH', 'SKLSH', 'PCA-RR', 'DSH', 'SpH'};
nhmethods = length(hashmethods);
Expand Down

0 comments on commit da787f8

Please sign in to comment.