-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_original.m
362 lines (289 loc) · 14.1 KB
/
Main_original.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
%% Loads parameters
loadParameters;
%% Folders
for i_fold=1:length(folders)
%% Build paths for images, excel, features and results
folder=folders{i_fold};
fichero=([directorio_im '/' camera{i_fold} '/imageSets/' folder]);
path_excel = [directorio_im '/' camera{i_fold} '/GT/GT_' folder '.xls'];
path_features_MOPCNNnoLSH = [directorio_im '/' camera{i_fold} '/CNNfeatures/' folder '/MOPCNNnoLSH.mat'];
path_features_MOPCNN = [directorio_im '/' camera{i_fold} '/CNNfeatures/' folder '/MOPCNN.mat'];
path_features = [directorio_im '/' camera{i_fold} '/CNNfeatures/CNNfeatures_' folder '.mat'];
path_features_PCA = [directorio_im '/' camera{i_fold} '/CNNfeatures/CNNfeaturesPCA_' folder '.mat'];
root_results = [directorio_results '/' folder];
mkdir(root_results);
%% Images
files_aux=dir([fichero '/*' formats{i_fold}]);
count = 1;
files = struct('name', []);
for n_files = 1:length(files_aux)
if(files_aux(n_files).name(1) ~= '.')
files(count).name = files_aux(n_files).name;
count = count+1;
end
end
Nframes=length(files);
%% Excel
[clust_man,clustersIdGT,cl_limGT, ~]=analizarExcel_Narrative(path_excel, files);
delim=cl_limGT';
if delim(1) == 1, delim=delim(2:end); end
clust_manId = {};
for i=1:length(clust_man)
[a,b]=find(clustersIdGT==i);
clust_manId{i,1}=b;
end
%% Features
if strcmp(paramsfeatures.type, 'CNN')
load(path_features);
[features_norm] = signedRootNormalization(features);
%PCA FEATURES
if(exist(path_features_PCA) > 0)
load(path_features_PCA);
else
[ featuresPCA, ~, ~ ] = applyPCA( features_norm, paramsPCA ) ;
save(path_features_PCA, 'featuresPCA');
end
elseif strcmp(paramsfeatures.type, 'MOPCNN')
load(path_features_MOPCNN);
features_adwin = X;
clearvars X;
load(path_features_MOPCNNnoLSH);
features = X;
clearvars X;
end
%% CLUSTERING
LH_Clus={};
start_clus={};
previousMethods = {};
%% ADWIN
if strcmp(clus_type,'Both1')||strcmp(clus_type,'Both2')
disp(['Start ADWIN ' folder]);
% PCA
if(paramsPCA.usePCA_Adwin && strcmp(paramsfeatures.type, 'CNN'))
[labels,dist2mean] = runAdwin(featuresPCA, confidence, pnorm);
elseif( strcmp(paramsfeatures.type, 'CNN'))
[features_norm] = signedRootNormalization(features);
[labels,dist2mean] = runAdwin(features_norm, confidence, pnorm);
else
[labels,dist2mean] = runAdwin(features_adwin, confidence, pnorm);
end
index=1;
automatic2 = [];
for pos=1:length(labels)-1
if (labels(pos)~=labels(pos+1))>0
automatic2(index)=pos;
index=index+1;
end
end
if (exist('automatic2','var')==0)
automatic2=0;
end
[~,~,~,fMeasure_Adwin]=Rec_Pre_Acc_Evaluation(delim,automatic2,Nframes,tol);
% Normalize distances
dist2mean = normalizeAll(dist2mean);
%dist2mean = signedRootNormalization(dist2mean')';
bound_GC{2}=automatic2;
LH_Clus{2}=getLHFromDists(dist2mean);
start_clus{2}=labels;
previousMethods{2} = 'ADWIN';
end % end Adwin
%% Clustering
if strcmp(clus_type,'Both1')||strcmp(clus_type,'Clustering')
%% PCA
if(paramsPCA.usePCA_Clustering && strcmp(paramsfeatures.type, 'CNN'))
similarities=pdist(featuresPCA,'cosine');
elseif( strcmp(paramsfeatures.type, 'CNN'))
similarities=pdist(features_norm,'cosine');
else
similarities=pdist(features,'euclidean');
end
for met_indx=1:length(methods_indx)
method=methods_indx{met_indx};
%% Load Results file if exists
if(evalType == 2)
file_save=(['Results_' method '_Res_' clus_type '_' folder '.mat']);
% if(exist([root_results '/' file_save]) > 0)
% load([root_results '/' file_save]);
% offset_results = length(Results);
% else
offset_results = 0;
% end
end
%% Clustering
Z = linkage(similarities, method);
%% Cut value
for idx_cut=1:length(cut_indx)
cut=cut_indx(idx_cut);
disp(['Start Clustering ' folder ', method ' method ', cutval ' num2str(cut)]);
clustersId = cluster(Z, 'cutoff', cut, 'criterion', 'distance');
%% AFTER IDs EXTRACTION - Evaluation
% [JIndex , FM_Bound , FM_Clust , automatic]=evaluationClustIDs(clustersId,clustersIdGT,tol,delim,clust_manId,files);
[JIndex,FM_Bound,automatic]=evaluationClustIDs(clustersId,tol,delim,clust_manId,files);
RPAF_Clustering.clustersIDs = clustersId;
RPAF_Clustering.fMeasure_Clustering = FM_Bound;
% RPAF_Clustering.fMeasure_Events = FM_Clust;
RPAF_Clustering.JaccardIndex = JIndex;
if( strcmp(paramsfeatures.type, 'CNN'))
P=getLHFromClustering(features_norm,clustersId);
else
P=getLHFromClustering(features,clustersId);
end
LH_Clus{1} = P;
start_clus{1}=clustersId';
bound_GC{1}=automatic;
previousMethods{1} = 'AC';
%% Graph Cut
% Build and calculate the Graph-Cuts
disp('Start GC');
%% PCA
if(paramsPCA.usePCA_GC && strcmp(paramsfeatures.type, 'CNN'))
features_GC = featuresPCA;
else
features_GC = features;
end
[features_GC, ~, ~] = normalize(features_GC);
if(evalType == 2)
[ fig , num_clus_GC, fMeasure_GC, eventsIDs, W_u_tested, W_p_tested ] = doIterativeTest(LH_Clus, start_clus, bound_GC, window_len, features_GC, tol, delim,1, nUnaryDivisions, nPairwiseDivisions, previousMethods, plotFigResults);
%% Store results
% Plot
if(plotFigResults)
if(~isempty(fig))
fig_save = ([method '_cutVal_' num2str(cut) '.fig']);
end
saveas(fig,[root_results '/' fig_save]);
end
% Results Evaluation
Results{idx_cut+offset_results}.cut_value = cut;
Results{idx_cut+offset_results}.RPAF_Clustering = RPAF_Clustering;
Results{idx_cut+offset_results}.num_clus_GC = num_clus_GC;
Results{idx_cut+offset_results}.Wunary_tested = W_u_tested;
Results{idx_cut+offset_results}.Wpairwise_tested = W_p_tested;
Results{idx_cut+offset_results}.eventsIDs = eventsIDs;
Results{idx_cut+offset_results}.fMeasure_GC = fMeasure_GC;
if strcmp(clus_type,'Both1')
Results{idx_cut+offset_results}.fMeasure_Adwin = fMeasure_Adwin;
Results{idx_cut+offset_results}.fMeasure_Clustering = FM_Bound;
end
elseif(evalType == 1)
[ labels, start_GC ] = doSingleTest(LH_Clus, start_clus, bound_GC ,window_len, W_unary, W_pairwise, features_GC, tol, delim, doEvaluation, previousMethods);
end % end GC
close all;
end%end cut
%% SAVE
if(evalType == 2)
save([root_results '/' file_save], 'Results');
end
end %end method
clearvars LH_Clus start_clus
end %end if clustering || both1
%% Spectral Clustering
if strcmp(clus_type,'Both2')||strcmp(clus_type,'Spectral')
%% PCA
if(paramsPCA.usePCA_Spect && strcmp(paramsfeatures.type, 'CNN') )
features_Sp = featuresPCA;
elseif(strcmp(paramsfeatures.type, 'CNN'))
[features_Sp] = signedRootNormalization(features);
else
features_Sp = features;
end
for matrix_indx=1:length(sim_matrix)
SimM=sim_matrix{matrix_indx};
if strcmp(SimM,'NN')==1,
sigmaNN=0.5; Type_NN=1;
Spectral_Param=NN;
W = SimGraph_NearestNeighbors(features_Sp', NN, Type_NN, sigmaNN);
elseif strcmp(SimM,'Sigma')==1,
Spectral_Param=Sig;
W = SimGraph_Full(features_Sp', Sig);
elseif strcmp(SimM,'Epsilon')==1,
Spectral_Param=Eps;
W = SimGraph_Epsilon(features_Sp', Eps);
end
%%SpectralClust Type
for Type=1:3
Results={};
%% Load Results file if exists
if(evalType == 2)
file_save=(['Results_' SimM '_Type_' num2str(Type) '_Parm_' num2str(Spectral_Param) '_' folder '.mat']);
% if(exist([root_results '/' file_save]) > 0)
% load([root_results '/' file_save]);
% offset_results = length(Results);
% else
offset_results = 0;
% end
end
%%Kvalue
for k_indx=1:length(k_valuesSp)
k_Sp=k_valuesSp(k_indx);
disp([folder ' Clusters Id - ' SimM ' k=' num2str(k_Sp) ' Type=' num2str(Type) ' & Val=' num2str(Spectral_Param)])
[C, L, U] = SpectralClustering(W, k_Sp, Type);
clustersId=[];
for num_k=1:k_Sp
vect_pos=find(C(:,num_k)==1)';
clustersId(1,vect_pos)=num_k;
end
%% AFTER IDs EXTRACTION - Evaluation
% [JIndex , FM_Bound , FM_Clust ,~]=evaluationClustIDs(clustersId,clustersIdGT,tol,delim,clust_manId,files);
[JIndex,FM_Bound,automatic]=evaluationClustIDs(clustersId,tol,delim,clust_manId,files);
RPAF_Spectral.clustersIDs = clustersId;
RPAF_Spectral.fMeasure_Bound = FM_Bound;
% RPAF_Spectral.fMeasure_Clusters = FM_Clust;
RPAF_Spectral.JaccardIndex = JIndex;
if(strcmp(paramsfeatures.type, 'CNN'))
P=getLHFromClustering(features_norm,clustersId);
else
P=getLHFromClustering(features,clustersId);
end
LH_Clus{1} = P;
start_clus{1}=clustersId';
bound_GC{1}=automatic;
previousMethods{1} = 'Spectral';
%% Graph Cut
% Build and calculate the Graph-Cuts
disp('Start GC');
%% PCA
if(paramsPCA.usePCA_GC && strcmp(paramsfeatures.type, 'CNN') )
features_GC = featuresPCA;
elseif(strcmp(paramsfeatures.type, 'CNN'))
[features_GC] = signedRootNormalization(features);
else
features_GC = features;
end
[features_GC, ~, ~] = normalize(features_GC);
if(evalType == 2)
[ fig , num_clus_GC, fMeasure_GC, eventsIDs, W_u_tested, W_p_tested ] = doIterativeTest(LH_Clus, start_clus, bound_GC, window_len, features_GC, tol, delim,1, nUnaryDivisions, nPairwiseDivisions, previousMethods, plotFigResults);
%% Store results
% Plot
if(plotFigResults)
if(~isempty(fig))
fig_save = ([SimM '_Type_' num2str(Type) '_Parm_' num2str(Spectral_Param) '_k_' num2str(k_Sp) '.fig']);
end
saveas(fig,[root_results '/' fig_save]);
end
% Results Evaluation
Results{k_indx+offset_results}.Similarity_Matrix = SimM;
Results{k_indx+offset_results}.RPAF_Clustering = RPAF_Spectral;
Results{k_indx+offset_results}.Type = Type;
Results{k_indx+offset_results}.k_valueSP = k_Sp;
Results{k_indx+offset_results}.num_clus_GC = num_clus_GC;
Results{k_indx+offset_results}.Wunary_tested = W_u_tested;
Results{k_indx+offset_results}.Wpairwise_tested = W_p_tested;
Results{k_indx+offset_results}.eventsIDs = eventsIDs;
Results{k_indx+offset_results}.fMeasure_GC = fMeasure_GC;
if strcmp(clus_type,'Both2')
Results{idx_cut+offset_results}.fMeasure_Adwin = fMeasure_Adwin;
Results{idx_cut+offset_results}.fMeasure_Clustering = FM_Bound;
end
elseif(evalType == 1)
[ labels, start_GC ] = doSingleTest(LH_Clus, start_clus, bound_GC ,window_len, W_unary, W_pairwise, features_GC, tol, delim, doEvaluation, previousMethods);
end % end GC
close all;
end%end k value
%% SAVE
if(evalType == 2)
save([root_results '/' file_save], 'Results');
end
end %end Type
end%similarity matrices
end %end if spectral clustering || both2
end %end folder