-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLLC_coding_appr.m
executable file
·70 lines (63 loc) · 2.4 KB
/
LLC_coding_appr.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
% ========================================================================
% USAGE: [Coeff]=LLC_coding_appr(B,X,knn,lambda)
% Approximated Locality-constraint Linear Coding
%
% Inputs
% B -M x d codebook, M entries in a d-dim space
% X -N x d matrix, N data points in a d-dim space
% knn -number of nearest neighboring
% lambda -regulerization to improve condition
%
% Outputs
% Coeff -N x M matrix, each row is a code for corresponding X
%
% Jinjun Wang, march 19, 2010
% ========================================================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% This file is part of SparseSampling@CVPR2018.
%
% SparseSampling@CVPR2018 is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% SparseSampling@CVPR2018 is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with SparseSampling@CVPR2018. If not, see <http://www.gnu.org/licenses/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Coeff] = LLC_coding_appr(B, X, knn, beta)
if ~exist('knn', 'var') || isempty(knn),
knn = 5;
end
if ~exist('beta', 'var') || isempty(beta),
beta = 1e-4;
end
nframe=size(X,1); % M: 1
nbase=size(B,1); % M: frames
% find k nearest neighbors
XX = sum(X.*X, 2);
BB = sum(B.*B, 2);
D = repmat(XX, 1, nbase)-2*X*B'+repmat(BB', nframe, 1);
IDX = zeros(nframe, knn);
for i = 1:nframe,
d = D(i,:);
[dummy, idx] = sort(d, 'ascend');
IDX(i, :) = idx(1:knn);
end
% llc approximation coding
II = eye(knn, knn);
Coeff = zeros(nframe, nbase);
for i=1:nframe
idx = IDX(i,:);
z = B(idx,:) - repmat(X(i,:), knn, 1); % shift ith pt to origin
C = z*z'; % local covariance
C = C + II*beta*trace(C); % regularlization (K>D)
w = C\ones(knn,1);
w = w/sum(w); % enforce sum(w)=1
Coeff(i,idx) = w';
end