-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFisherEncodingNV.m
181 lines (116 loc) · 6.83 KB
/
FisherEncodingNV.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function Total_Fisher_Kernel = FisherEncodingNV(GMM_Params, Features)
arguments
GMM_Params {mustBeUnderlyingType(GMM_Params,'struct')}
Features {mustBeUnderlyingType(Features,'struct')}
end
dimFeatures = size(Features(1).reduced_RGB_features,2);
numClusters = length(GMM_Params);
%% Initialization
SIFT_Fisher_Kernel = zeros(length(Features),2*dimFeatures*numClusters);
RGB_Fisher_Kernel = zeros(length(Features),2*dimFeatures*numClusters);
%% Gradient Vectors for the SIFT features
tic
for numImages = 1 : length(Features)
fprintf("Now on SIFT Encoding in image: %d of %d \n", numImages, length(Features))
% Initialize the gradient vector
gradient_vector = zeros(1, 2*dimFeatures*numClusters);
for Cluster = 1 : numClusters
% Get the current clusters parameters
Means = gpuArray(GMM_Params(Cluster).Training_SIFT_mus);
Sigmas = gpuArray(GMM_Params(Cluster).Training_SIFT_Sigmas);
Weights = gpuArray(GMM_Params(Cluster).Training_SIFT_weights);
% Get the current feature matrix
currentFeatureMatrix = gpuArray(Features(numImages).reduced_SIFT_features);
% Initialize the covariance matrices
covMatrices = gpuArray(zeros(1, dimFeatures, Cluster));
% Loop over each cluster
for i = 1:Cluster
% Create a square diagonal matrix from the covariance vector
covMatrices(1, :, i) = Sigmas(i, :);
end
% Create a gmdistribution object
gmModel = gmdistribution(Means, covMatrices);
ImagePosterior = posterior(gmModel,currentFeatureMatrix);
% Compute F_k for the means
F_k_means = (currentFeatureMatrix - Means(Cluster, :)) ...
./ (Sigmas(Cluster, :).^2);
F_k_means = sum(F_k_means .* ImagePosterior(:, Cluster) * ...
Weights(Cluster),1);
f_mui = (size(currentFeatureMatrix,1) * Weights(Cluster)) ./ ...
Sigmas(Cluster, :).^2;
% Compute the normalized gradient of the mean parameter
F_k_means_normalized = F_k_means ./ f_mui;
% Compute F_k for the variances
F_k_variances = ((currentFeatureMatrix - Means(Cluster, :)).^2)...
./ (Sigmas(Cluster, :).^3)...
- 1 ./ (Sigmas(Cluster, :));
F_k_variances = sum(F_k_variances .* ImagePosterior(:, Cluster)...
* Weights(Cluster),1);
f_si = 2 * size(currentFeatureMatrix,1) * Weights(Cluster) ./...
Sigmas(Cluster, :).^2;
% Compute the normalized gradient of the mean parameter
F_k_variances_normalized = F_k_variances ./ f_si;
% Assign F_k_means and F_k_variances to gradient_vector
gradient_vector(1, (Cluster-1)*2*dimFeatures+1: ...
Cluster*2*dimFeatures) = [F_k_means_normalized,...
F_k_variances_normalized];
end
% Update the Fisher Information Matrix
SIFT_Fisher_Kernel(numImages,:) = gradient_vector;
end
SIFT_encoding_time = toc;
fprintf("Finished encoding SIFT features. Time: %f \n",SIFT_encoding_time);
%% Gradient Vectors for the RGB features
tic
for numImages = 1 : length(Features)
fprintf("Now on RGB Encoding on image: %d of %d \n", numImages, length(Features))
% Initialize the gradient vector
gradient_vector = zeros(1, 2*dimFeatures*numClusters);
for Cluster = 1 : numClusters
% Get the current clusters parameters
Means = gpuArray(GMM_Params(Cluster).Training_RGB_mus);
Sigmas = gpuArray(GMM_Params(Cluster).Training_RGB_Sigmas);
Weights = gpuArray(GMM_Params(Cluster).Training_RGB_weights);
% Get the current feature matrix
currentFeatureMatrix = gpuArray(Features(numImages).reduced_RGB_features);
% Initialize the covariance matrices
covMatrices = gpuArray(zeros(1, dimFeatures, Cluster));
% Loop over each cluster
for i = 1:Cluster
% Create a square diagonal matrix from the covariance vector
covMatrices(1, :, i) = Sigmas(i, :);
end
% Create a gmdistribution object
gmModel = gmdistribution(Means, covMatrices);
ImagePosterior = posterior(gmModel,currentFeatureMatrix);
% Compute F_k for the means
F_k_means = (currentFeatureMatrix - Means(Cluster, :)) ...
./ (Sigmas(Cluster, :).^2);
F_k_means = sum(F_k_means .* ImagePosterior(:, Cluster) * ...
Weights(Cluster),1);
f_mui = (size(currentFeatureMatrix,1) * Weights(Cluster)) ./ ...
Sigmas(Cluster, :).^2;
% Compute the normalized gradient of the mean parameter
F_k_means_normalized = F_k_means ./ f_mui;
% Compute F_k for the variances
F_k_variances = ((currentFeatureMatrix - Means(Cluster, :)).^2)...
./ (Sigmas(Cluster, :).^3)...
- 1 ./ (Sigmas(Cluster, :));
F_k_variances = sum(F_k_variances .* ImagePosterior(:, Cluster)...
* Weights(Cluster),1);
f_si = 2 * size(currentFeatureMatrix,1) * Weights(Cluster) ./...
Sigmas(Cluster, :).^2;
% Compute the normalized gradient of the mean parameter
F_k_variances_normalized = F_k_variances ./ f_si;
% Assign F_k_means and F_k_variances to gradient_vector
gradient_vector(1, (Cluster-1)*2*dimFeatures+1: ...
Cluster*2*dimFeatures) = [F_k_means_normalized,...
F_k_variances_normalized];
end
% Update the Fisher Information Matrix
RGB_Fisher_Kernel(numImages,:) = gradient_vector;
end
RGB_encoding_time = toc;
fprintf("Finished encoding RGB features. Time: %f \n",RGB_encoding_time);
Total_Fisher_Kernel = [SIFT_Fisher_Kernel;RGB_Fisher_Kernel];
end