-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_project.m
309 lines (224 loc) · 10.8 KB
/
Final_project.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
clear;clc;close all
s = rng(1);
mkdir Workspace
%%
delete(gcp('nocreate'))
maxWorkers = maxNumCompThreads;
disp("Maximum number of workers: " + maxWorkers);
pool=parpool(maxWorkers/2);
%% Load the data
filelocation = uigetdir;
imds = imageDatastore(filelocation,"IncludeSubfolders",true,"LabelSource","foldernames");
%% Partition the data into training and testing sets
Labels = countEachLabel(imds);
[Trainds,Testds] = splitTheDatastore(imds,Labels);
%% Feature Extraction
tic
[Training_features,removedTrainingIndices] = extractImageFeatures(Trainds);
[Testing_features,removedTestingIndices]= extractImageFeatures(Testds);
% Create a struct that contains the vertically concatenated training features
% Concatenate each feature category into a single matrix and put it into a
% structure
% FeatureMatrix.SIFT_Features_Matrix = vertcat(features(:).SIFT_Features);
% FeatureMatrix.RGB_Features_Matrix = vertcat(features(:).RGBfeatures);
Training_FeatureMatrix.Reduced_SIFT_Features_Matrix = ...
vertcat(Training_features(:).reduced_SIFT_features);
Training_FeatureMatrix.Reduced_RGB_Features_Matrix = ...
vertcat(Training_features(:).reduced_RGB_features);
preprocessing_time = toc;
%% Initializations
numModels = 128;
fitGMMNeglogLikelihoods_SIFT = zeros(1, numModels);
fitGMMLog_Likelihoods_SIFT = cell(1, numModels);
fitGMMNeglogLikelihoods_RGB = zeros(1, numModels);
fitGMMLog_Likelihoods_RGB = cell(1, numModels);
GMMNeglogLikelihoods_SIFT = zeros(1, numModels);
GMMLog_Likelihoods_SIFT = cell(1, numModels);
GMMNeglogLikelihoods_RGB = zeros(1, numModels);
GMMLog_Likelihoods_RGB = cell(1, numModels);
sEMNeglogLikelihoods_SIFT = zeros(1, numModels);
sEMLog_Likelihoods_SIFT = cell(1, numModels);
sEMNeglogLikelihoods_RGB = zeros(1, numModels);
sEMLog_Likelihoods_RGB = cell(1, numModels);
Training_RGB_data = gpuArray(Training_FeatureMatrix.Reduced_RGB_Features_Matrix);
% Calculate the index for one fourth of the data
oneFourthIndex = floor(size(Training_FeatureMatrix.Reduced_SIFT_Features_Matrix, 1) / 4);
Training_SIFT_data = gpuArray(Training_FeatureMatrix.Reduced_SIFT_Features_Matrix(1:oneFourthIndex,:));
clear Training_FeatureMatrix
%% fitgmdist Gaussian Mixture Model
% Για τα RGB δεδομένα
opts = statset('Display','final','MaxIter',100);
regularizationValue = 1e-5; % Insert a regularization value to avoid ill-conditioned covariance.
% This can happen when the covariance matrices during the fitting of a
% Gaussian Mixture Model (GMM) become nearly singular or not positive
% definite.
tic
for i = 1 : numModels
fprintf("Number of cluster:%d \n",i)
fitGMMs = fitgmdist(gather(Training_RGB_data),i,"Replicates",1,"Options",opts, ...
"CovarianceType","diagonal","RegularizationValue",regularizationValue);
fitGMMNeglogLikelihoods_RGB(i) = fitGMMs.NegativeLogLikelihood;
fitGMMLog_Likelihoods_RGB{i} = -fitGMMs.NegativeLogLikelihood;
end
fitGMM_RGB_time = toc;
% Για τα SIFT δεδομένα
tic
for i = 1 : numModels
fprintf("Number of cluster:%d \n",i)
fitGMMs = fitgmdist(gather(Training_SIFT_data),i,"CovarianceType","diagonal","Replicates",1, ...
"Options",opts);
fitGMMNeglogLikelihoods_SIFT(i) = fitGMMs.NegativeLogLikelihood;
fitGMMLog_Likelihoods_SIFT{i} = -fitGMMs.NegativeLogLikelihood;
end
fitGMM_SIFT_time = toc;
%% Gaussian Mixture Model
% Για τα RGB δεδομένα
tic
for i = 1 : numModels
fprintf("Number of cluster:%d \n ",i)
GMMs = GMM_NV(Training_RGB_data,i,"NumReplicates",1,"MaxIterations",100);
GMMNeglogLikelihoods_RGB(i) = -GMMs.logLikelihood;
GMMLog_Likelihoods_RGB{i} = GMMs.LogLikelihoodMatrix;
fprintf(" >> Log-Likelihood:%e\n", GMMNeglogLikelihoods_RGB(i))
end
GMM_RGB_time = toc;
% Για τα SIFT δεδομένα
tic
for i = 1 : numModels
fprintf("Number of cluster:%d \n ",i)
GMMs{i} = GMM_NV(Training_SIFT_data,i,"NumReplicates",1,"MaxIterations",100);
GMMNeglogLikelihoods_SIFT(i) = -GMMs{i}.logLikelihood;
GMMLog_Likelihoods_SIFT{i} = GMMs{i}.logLikelihood;
fprintf(" >> Negative Log-Likelihood:%e\n", GMMNeglogLikelihoods_SIFT(i))
end
GMM_SIFT_time = toc;
%% sEM
% Για τα RGB δεδομένα
for i = 1 : numModels
fprintf("Number of cluster:%d \n",i)
sEM_GMMs = sEM(Training_RGB_data, i,"Alpha",0.5,"BatchSize",100,"MaxIterations",100);
sEMNeglogLikelihoods_RGB(i) = sEM_GMMs.NegLogLikelihood;
sEMLog_Likelihoods_RGB{i} = -sEM_GMMs.Log_Likelihood;
fprintf(" >> Negative Log-Likelihood:%e\n", sEMNeglogLikelihoods_RGB(i))
end
sEM_RGB_time = toc;
% Για τα SIFT δεδομένα
tic
for i = 1 : numModels
fprintf("Number of cluster:%d \n",i)
sEM_GMMs = sEM(Training_SIFT_data,i,"Alpha",0.5,"BatchSize",10000,"MaxIterations",100);
sEMNeglogLikelihoods_SIFT(i) = sEM_GMMs.NegLogLikelihood;
sEMLog_Likelihoods_SIFT{i} = -sEM_GMMs.Log_Likelihood;
fprintf(" >> Negative Log-Likelihood:%e\n", sEMNeglogLikelihoods_SIFT(i))
end
sEM_SIFT_time = toc;
%% Calculate the statistics for the SIFT and RGB features from the function
% For the fitgmdist function
% fitgmdist_Params = CalculateParamsNV(Training_SIFT_data,Training_RGB_data,fitGMMLog_Likelihoods_SIFT, ...
% fitGMMLog_Likelihoods_RGB);
% From the GMM function
GMM_Params = CalculateParamsNV(Training_SIFT_data,Training_RGB_data,GMMLog_Likelihoods_SIFT, ...
GMMLog_Likelihoods_RGB);
% From the sEM function
sEM_Params = CalculateParamsNV(Training_SIFT_data,Training_RGB_data,sEMLog_Likelihoods_SIFT, ...
sEMLog_Likelihoods_RGB);
clear Training_SIFT_data Training_RGB_data
%% Create the gradient vectors for the function
% For the fitgmdist function
fprintf("Encoding the Training Features\n")
fitgmdist_Total_Training_Fisher_Kernel = FisherEncodingNV(fitgmdist_Params,Training_features);
fprintf("Encoding the Training Features\n")
fitgmdist_Total_Testing_Fisher_Kernel = FisherEncodingNV(fitgmdist_Params,Testing_features);
%For the GMM function
fprintf("Encoding the Training Features\n")
GMM_Total_Training_Fisher_Kernel = FisherEncodingNV(GMM_Params,Training_features);
fprintf("Encoding the Training Features\n")
GMM_Total_Testing_Fisher_Kernel = FisherEncodingNV(GMM_Params,Testing_features);
% For the sEM function
tic
fprintf("Encoding the Training Features\n")
sEM_Total_Training_Fisher_Kernel = FisherEncodingNV(sEM_Params,Training_features);
fprintf("Encoding the Training Features\n")
sEM_Total_Testing_Fisher_Kernel = FisherEncodingNV(sEM_Params,Testing_features);
ParallelFisherEncodingTime = toc;
clear Training_features Testing_features
%% It's classification time
% Remove the corresponding indices from the labels
mask = true(1, numel(Trainds.Files));
mask(removedTrainingIndices) = false;
new_Trainds = subset(Trainds, mask);
mask = true(1, numel(Testds.Files));
mask(removedTestingIndices) = false;
new_Testds = subset(Testds, mask);
t = templateSVM('SaveSupportVectors',true,'Type','classification');
%% For fitgmdist
[Model1,HyperparameterOptimizationResults1] = fitcecoc(fitgmdist_Total_Training_Fisher_Kernel, ...
[new_Trainds.Labels;new_Trainds.Labels],"Learners",t,"Coding", "onevsall", ...
'OptimizeHyperparameters','all', 'HyperparameterOptimizationOptions', ...
struct('Holdout',0.1,"UseParallel",true,"ShowPlots",false));
Mdl1 = Model1.Trained{1};
[predictedLabels1, scores1]= predict(Model1,fitgmdist_Total_Testing_Fisher_Kernel);
%% For GMM
[Model2,HyperparameterOptimizationResults2] = fitcecoc(GMM_Total_Training_Fisher_Kernel, ...
[new_Trainds.Labels;new_Trainds.Labels],"Learners",t,"Coding", "onevsall", ...
'OptimizeHyperparameters','all', 'HyperparameterOptimizationOptions', ...
struct('Holdout',0.1,"UseParallel",true,"ShowPlots",false));
Mdl2 = Model2.Trained{1};
[predictedLabels2, scores2]= predict(Model2,GMM_Total_Testing_Fisher_Kernel);
%% For sEM
[Model3,HyperparameterOptimizationResults3] = fitsvm(sEM_Total_Training_Fisher_Kernel, ...
[new_Trainds.Labels;new_Trainds.Labels],"Learners",t,"Coding", "onevsall", ...
'OptimizeHyperparameters','all', 'HyperparameterOptimizationOptions', ...
struct('Holdout',0.1,"UseParallel",true,"ShowPlots",false));
Mdl3 = Model3.Trained{1};
[predictedLabels3, scores3]= predict(Model3,sEM_Total_Testing_Fisher_Kernel);
confusionMatrix1 = confusionmat(new_Testing_Labels,predictedLabels1);
confusionMatrix2 = confusionmat(new_Testing_Labels,predictedLabels2);
confusionMatrix3 = confusionmat(new_Testing_Labels,predictedLabels3);
accuracy1 = sum(diag(confusionMatrix1)) / sum(confusionMatrix1(:));
accuracy2 = sum(diag(confusionMatrix2)) / sum(confusionMatrix2(:));
accuracy3 = sum(diag(confusionMatrix3)) / sum(confusionMatrix3(:));
% Initialize variables for total times
totalTimes = struct('fitgmdist', 0, 'GMM', 0, 'sEM', 0);
% After each algorithm's execution, store its total time
totalTimes.fitgmdist = fitGMM_RGB_time + fitGMM_SIFT_time;
totalTimes.GMM = GMM_RGB_time + GMM_SIFT_time;
% Assuming sEM_RGB_time and sEM_SIFT_time are calculated similarly for sEM
totalTimes.sEM = sEM_RGB_time + sEM_SIFT_time;
% Create a table for times
timesTable = struct2table(totalTimes);
% After calculating accuracies, store them
accuracies = [accuracy1, accuracy2, accuracy3];
% Create a table for accuracies
accuraciesTable = array2table(accuracies, 'VariableNames', {'fitgmdist', 'GMM', 'sEM'});
%% Plotting the progression of the negative log likelihood per iteration per algorithm
% Plot for RGB Data
figure;
hold on;
plot(fitGMMNeglogLikelihoods_RGB, 'r-', 'DisplayName', 'fitgmdist RGB');
plot(GMMNeglogLikelihoods_RGB, 'g-', 'DisplayName', 'GMM RGB');
plot(sEMNeglogLikelihoods_RGB, 'b-', 'DisplayName', 'sEM RGB');
legend('show');
xlabel('Number of Clusters');
ylabel('Negative Log Likelihood');
title('Negative Log Likelihood for RGB Data Across Algorithms');
hold off;
% Save the figure
savefig('Workspace/NLL_RGB.fig');
% Plot for SIFT Data
figure;
hold on;
plot(fitGMMNeglogLikelihoods_SIFT, 'r-', 'DisplayName', 'fitgmdist SIFT');
plot(GMMNeglogLikelihoods_SIFT, 'g-', 'DisplayName', 'GMM SIFT');
plot(sEMNeglogLikelihoods_SIFT, 'b-', 'DisplayName', 'sEM SIFT');
legend('show');
xlabel('Number of Clusters');
ylabel('Negative Log Likelihood');
title('Negative Log Likelihood for SIFT Data Across Algorithms');
hold off;
% Save the figure
savefig('Workspace/NLL_SIFT.fig');
% Assuming timesTable and accuraciesTable are already defined as MATLAB tables
% Save the tables
writetable(timesTable, 'Workspace/timesTable.csv');
writetable(accuraciesTable, 'Workspace/accuraciesTable.csv');