-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_single_sequence.m
289 lines (233 loc) · 9.33 KB
/
process_single_sequence.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
function events = process_single_sequence(camera, folder, params, features)
%% Load paths
addpath('Adwin;Data_Loading;Evaluation;Features_Preprocessing');
addpath('GCMex;GraphCuts;PCA;Tests;Utils;SpectralClust');
%% If we do not provide features, they must be calculated in the corresponding folder
if(nargin < 4)
load_features = true;
else
load_features = false;
end
%% Parameters loading
if nargin < 3
directorio_im = '/media/lifelogging/HDD_2TB/LIFELOG_DATASETS';
directorio_results = '/media/lifelogging/HDD_2TB/R-Clustering_Results_Informative';
formats = '.jpg';
doEvaluation = false;
GT = [];
%% Clustering parameters
methods_indx={'single'};
% methods_indx={'centroid'};
cut_indx=(0.5:0.1:0.5);
% cut_indx = [0.45];
paramsPCA.usePCA_Clustering = true;
%% R-Clustering parameters
clus_type = 'Both1';
%% GraphCuts parameters
W_unary = 0.1; % 0 <= W_unary <= 1 for evalType == 1
W_pairwise = 0.5; % 0 <= W_pairwise <= 1 for evalType == 1
evalType = 2;
nUnaryDivisions = 5; % number of equally spaces W_unary values for evalType == 2
nPairwiseDivisions = 5; % number of equally spaced W_pairwise values for evalType == 2
else
directorio_im = params.files_path;
directorio_results = params.RC_results_path;
formats = params.formats;
doEvaluation = params.doEvaluation;
if(doEvaluation);
GT = params.GT;
else
GT = [];
end
%% Clustering parameters
methods_indx= params.methods_indx;
cut_indx= params.cut_indx_use;
paramsPCA.usePCA_Clustering = true;
%% R-Clustering parameters
clus_type = params.clus_type;
%% GraphCuts parameters
evalType = 1;
W_unary = params.W_unary; % 0 <= W_unary <= 1
W_pairwise = params.W_pairwise; % 0 <= W_pairwise <= 1
end
paramsfeatures.type = 'CNN'; %CNN ....
paramsPCA.minVarPCA=0.95;
paramsPCA.standarizePCA=false;
paramsPCA.usePCA_Clustering = true;
plotFigResults = false;
%% Adwin parameters
pnorm = 2;
confidence = 0.1;
paramsPCA.usePCA_Adwin = true;
%% GraphCuts parameters
paramsPCA.usePCA_GC = false;
window_len = 11;
%% Evaluation parameters
tol=5; % tolerance for the final evaluation
%% Build paths for images, features and results
if(isempty(camera))
fichero=([directorio_im '/' folder]);
else
fichero=([directorio_im '/' camera '/imageSets/' folder]);
end
path_features = [directorio_im '/' camera '/CNNfeatures/CNNfeatures_' folder '.mat'];
path_features_PCA = [directorio_im '/' camera '/CNNfeatures/CNNfeaturesPCA_' folder '.mat'];
if(evalType == 2)
root_results = [directorio_results '/' folder];
mkdir(root_results);
end
%% Images
files_aux=dir([fichero '/*' formats]);
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);
%% Features
if strcmp(paramsfeatures.type, 'CNN')
if(load_features)
load(path_features);
end
%PCA FEATURES
if(exist(path_features_PCA) > 0)
load(path_features_PCA);
else
[features_norm] = signedRootNormalization(features);
[ featuresPCA, ~, ~ ] = applyPCA( features_norm, paramsPCA ) ;
if(load_features) % if we wanted to load the stored features, then we will also store PCA features
save(path_features_PCA, 'featuresPCA');
end
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
% 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');
[features_norm] = signedRootNormalization(features);
elseif( strcmp(paramsfeatures.type, 'CNN'))
similarities=pdist(features_norm,'cosine');
[features_norm] = signedRootNormalization(features);
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']);
offset_results = 0;
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');
automatic = compute_boundaries(clustersId,files);
RPAF_Clustering.clustersIDs = clustersId;
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
elseif(evalType == 1)
[ labels, start_GC ] = doSingleTest(LH_Clus, start_clus, bound_GC ,window_len, W_unary, W_pairwise, features_GC, tol, GT, doEvaluation, previousMethods);
end % end GC
% %% SAVE
% save([root_results '/' folder '_' num2str(idx_cut)],'automatic');
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
nFrames = length(labels);
events = zeros(1, nFrames); events(1) = 1;
prev = 1;
for i = 1:nFrames
if(labels(i) == 0)
events(i) = 0;
else
if(labels(i) == labels(prev))
events(i) = events(prev);
else
events(i) = events(prev)+1;
end
prev = i;
end
end