forked from xxw1122/mPCCA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fisherVector.m
34 lines (31 loc) · 1.49 KB
/
fisherVector.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
function [code]=fisherVector(obj, features)
%FISHERVECTOR
% obj: gmdistribution class initialized
% features: samples x dimFeature
%% initialization
code = zeros(1, 2 * obj.NDimensions * obj.NComponents) ;
%% posterior gamma
P = posterior(obj, features) ;
%% ensure sparsity (better fit the assumption)
P(find(P < 1e-4)) = 0 ;
%% re-normalization for the sparse posterior
P = bsxfun(@rdivide, P, sum(P, 2));
%% computing fisher vector %%
if ~isempty(features)
for indexComponent = 1 : obj.NComponents
%% component-wise coordinate transform matrix
[transform, eigenValues] = eig(squeeze(obj.Sigma(:, :, indexComponent))) ;
%% generate code for a bag of features
code([1 : 2 * obj.NDimensions] + 2 * (indexComponent - 1) * obj.NDimensions) = ...
[ ...
(1 / (size(features, 1) * obj.PComponents(indexComponent) ^ 0.5)) * ... %% coefficient
sum(bsxfun(@times, P(:, indexComponent), bsxfun(@rdivide, bsxfun(@minus, features, obj.mu(indexComponent, :)) * transform, eigenValues)), 1), ... %% mean term
(1 / (size(features, 1) * (2 * obj.PComponents(indexComponent)) ^ 0.5)) * ... %% coefficient
sum(bsxfun(@times, P(:, indexComponent), bsxfun(@rdivide, (bsxfun(@minus, features, obj.mu(indexComponent, :)) * transform) .^ 2, (eigenValues .^ 2)) - 1), 1) ... %% variance term
];
end
end
%% power normalization %%
% code = sign(code) .* (abs(code) .^ alpha) ;
%% l2 normalization %%
% code = code ./ repmat(sum(code .^ 2, 2) .^ 0.5 + eps, [1, size(code, 2)]) ;