-
Notifications
You must be signed in to change notification settings - Fork 2
/
Event_context_DEMOuav_rmax7by7rect_FAST.m
398 lines (330 loc) · 14.7 KB
/
Event_context_DEMOuav_rmax7by7rect_FAST.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
clear;
close all;
warning('off', 'MATLAB:MKDIR:DirectoryExists');
tic;
% Add VLFeat toolbox to MATLAB working path
run('vlfeat-0.9.17-bin/vlfeat-0.9.17/toolbox/vl_setup.m');
addpath(genpath('getNmnistDesc'));
% Put all the folders (classes) in a single folder
train_dataset_path = '../N-SOD Dataset/Train';
filenamesA = dir2(train_dataset_path);
num_classes=numel(filenamesA);
classnames = cell(1, num_classes);
for i=1:num_classes
classnames{i}=filenamesA(i).name;
end
% create training labels later
train_label=[];
num_test= 2;
filename = 'traindata_binTD4cl_CYnewobs.dat';
testfiles_save = 'testfiles_binTD4cl_CYnewobs';
try
canUseGPU = parallel.gpu.GPUDevice.isAvailable;
catch ME
canUseGPU = false;
end
param.descsize = 5; % denotes a N by N region sampled from the subsampled count matrix
param.queuesize = 5000;
param.countmatsubsamp = 2; % a 2 by 2 cell region for the count matrix
param.minNumEvents = 200; % wait for this many events before getting desc from queue
disp(param);
if exist(filename, 'file') ~= 2
% Training and testing set filenames
for i=1:num_classes
folder_path = fullfile(train_dataset_path,classnames{i});
subfolder_names = dir(folder_path);
isdir_tags = [subfolder_names.isdir];
subfolder_names = subfolder_names(~isdir_tags);
subfolder_names = {subfolder_names.name};
randomize_files{i} = randperm(numel(subfolder_names));
num_train = numel(randomize_files{i});
index= [];
for ii = randomize_files{i}
filepath = fullfile(folder_path, subfolder_names{ii});
img = read_linux(filepath, 0);
if size(img.x,2) > 5000
index = [index ii];
end
if numel(index) > 80
break
end
end
train_filenames{i} = vl_colsubset(subfolder_names(index),num_train, 'beginning');
test_filenames{i} = vl_colsubset(subfolder_names(index),num_test, 'beginning');
end
fid = fopen(filename, 'w');
% Write into file
for i = 1: numel(train_filenames)
classi = train_filenames{i};
for j = 1: numel(classi)
classi_objectj = classi{j};
fprintf(fid, '%s\n', classi_objectj );
end
end
save(testfiles_save,'test_filenames');
end
fid = fopen(filename, 'r');
A = textscan(fid,'%s', 'Delimiter','\n');
all_filenames= A{1};
fclose(fid);
load(testfiles_save);
% TD Noise Filter
% Assumes Garrick's AER functions are available (www.garrickorchard.com)
us_time_filter = 5e3; % in micro-seconds
% Debug purpose --
training_desc_done = 0; % for debug purposes only
svmtraining_done = 0;
testing_desc_done = 0;
% The training phase is here so that it is easy to change parameters
% and check the code of the FPGA implmentation. The testing stage will be
% made modular like how the FPGA implementation is intended to be, fixed
% precision and memory constraints closely followed.
count = 0;
train_desc_savefolder = './ECtraindata_DEMOrectFAST_ver2_5by5_PCA/';
mkdir(train_desc_savefolder);
fprintf('\n\n------------------------------Starting Training Stage------------------------------\n\n');
if training_desc_done == 0
fprintf('Getting events and saving descriptors for training...\n\n');
for class_i=1:num_classes
classi_name = cell2mat(classnames(class_i));
folder_path = fullfile(train_dataset_path,classi_name);
subfolder_names = dir(folder_path);
isdir_tags = [subfolder_names.isdir];
subfolder_names = subfolder_names(~isdir_tags);
subfolder_names = {subfolder_names.name};
mkdir(fullfile(train_desc_savefolder, classi_name));
for train_classi = 1:numel(subfolder_names)
count = count + 1;
filename = all_filenames{count};
filepath = fullfile(train_dataset_path,classi_name, filename);
TD = read_linux(filepath, 0);
TD = FilterTD(TD, us_time_filter);
trainimage_sizes(count,:) = [max(TD.y) max(TD.x)];
savepath_file = fullfile(train_desc_savefolder, classi_name, filename(1:end-4));
try
load(savepath_file);
catch
[frames, desc] = getDescs_RECT_FPGA(TD, param);
savepath_file = fullfile(train_desc_savefolder, classi_name, filename(1:end-4));
disp(savepath_file);
save(savepath_file,'frames','desc');
end
train_label = [train_label class_i];
fprintf('File num: %d\n\n', count);
end
end
save('./NDEMO4desc_5by5subsamp2x2_ustime5e3_PCA.mat',...
'train_label','trainimage_sizes','-v7.3');
if canUseGPU == 1
poolobj = gcp('nocreate');
delete(poolobj);
end
else
fprintf('Loading descriptors...\n\n');
load('./NDEMO4desc_5by5subsamp2x2_ustime5e3_PCA.mat');
end
histopts.num_bins = 150; % codebook size
histopts.numSpatialX = [1] ; % ignore but required for code, if [1 2] performs spatial pyramid matching (not needed)
histopts.numSpatialY = [1] ; % ignore
% SVM params
svmOpts.C = 10 ;
svmOpts.biasMultiplier = 1 ;
pcadims = 25;
fprintf('pcadims = %d\n\n', pcadims);
% Build the codebook
clearvars model net
model_str_stringname = 'modelTD4cl_DEMO_FASTver2nonorm_PCA';
fprintf("Loading previously generated codebook...");
try
load(['./ECtrainmodels/' model_str_stringname ...
num2str(histopts.num_bins) num2str(pcadims) num2str(param.countmatsubsamp) num2str(param.descsize)]);
model_done = 1;
fprintf(" Found!\n\n");
catch
model_done = 0;
fprintf(" Not Found!\n\n");
end
if model_done == 0
fprintf("Generating codebook...\n\n");
[train_data, loctrain_label, new_train_label] = readDescs(train_desc_savefolder,8,'nonorm');
traindata_befpca = single([train_data.desc]);
attr_means = mean(traindata_befpca');
[coeff, score, latent] = pca(traindata_befpca');
traindata_afterpca = score(:,1:pcadims)';
[model.vocab, model.assoc] = vl_kmeans(vl_colsubset(single(traindata_afterpca), 1e6), histopts.num_bins, 'verbose','algorithm', 'ANN') ;
model.kdtree = vl_kdtreebuild(model.vocab, 'Distance','L1') ;
model.vocab = double(model.vocab);
clearvars traindata_afterpca traindata_befpca
mkdir('./ECtrainmodels/');
save(['./ECtrainmodels/' model_str_stringname ...
num2str(histopts.num_bins) num2str(pcadims) num2str(param.countmatsubsamp) num2str(param.descsize)], ...
'model', 'attr_means', 'coeff');
disp(' ');
end
% For possbile integer comparisons on FPGA
model.kdtree.trees.nodes.splitThreshold = round(model.kdtree.trees.nodes.splitThreshold);
% Get hists
for repeat= 1
clearvars hists*
train_label = [];
count_sep = 0;
count = 0;
event_chunk_period = 1e4; % get a histogram for classification every 10k events, same for testing
if svmtraining_done == 0
for class_i=1:num_classes
classname=classnames{class_i};
folder_path = fullfile(train_dataset_path,classname);
subfolder_names = dir(folder_path);
isdir_tags = [subfolder_names.isdir];
subfolder_names = subfolder_names(~isdir_tags);
subfolder_names = {subfolder_names.name};
for j=1:numel(subfolder_names)
count = count + 1;
filename = all_filenames{count};
filepath=fullfile(train_desc_savefolder, classname, [filename(1:end-4) '.mat']);
fprintf('Binning: %s\n', filepath);
[train_data, loctrain_label, label] = readDescs(filepath, [], 'nonorm');
traindata_befpca = single([train_data.desc]);
traindata_befpca = bsxfun(@minus, traindata_befpca, attr_means');
traindata_afterpca = coeff(:,1:pcadims)'*traindata_befpca;
start_chunk = 1;
end_chunk = event_chunk_period;
for event_c = 1:ceil(size(train_data.frames,2)/event_chunk_period)
count_sep= count_sep + 1;
try
desc = traindata_afterpca(:, start_chunk:end_chunk);
frames = train_data.frames(:,start_chunk:end_chunk);
catch
desc = traindata_afterpca(:, start_chunk:end);
frames = train_data.frames(:,start_chunk:end);
end
hists{count_sep} = get_imagehist(model,trainimage_sizes(count,:),...
double(frames), double(desc), histopts, 'nonorm');
train_label = [train_label class_i];
start_chunk = end_chunk + 1;
end_chunk = end_chunk + event_chunk_period;
if end_chunk > size(train_data.frames,2)
end_chunk = size(train_data.frames,2);
end
end
end
end
hists = cat(2, hists{:}) ;
hists=cat(1,hists);
psix = hists; % kernel removed, above line applies kernel
%SVM Calculation
lambda = 1 / (svmOpts.C * length(train_label)) ;
w = [] ;
for ci = 1:length(unique(train_label))
perm = randperm(length(train_label)) ;
y = 2 * (train_label == ci) - 1 ;
[w(:,ci) b(:, ci) info(ci)] = vl_svmtrain(psix(:,perm), ...
y(perm), lambda, ...
'MaxNumIterations', 50/lambda, ...
'BiasMultiplier', svmOpts.biasMultiplier) ;
end
svmmodel.b = b ;
svmmodel.w = w;
save('svvmodel_DEMO_FASTver2_150codebok_5by5_NONORM_PCA.mat','svmmodel');
else
fprintf("Loading previously generated SVM model...\n\n");
load('svvmodel_DEMO_FASTver2_150codebok_5by5_NONORM_PCA.mat');
end
%% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% testing stage
count = 0;
test_label = [];
test_desc_savefolder = './ECtestdata_DEMOrect_FASTver2_5by5_PCA/';
mkdir(test_desc_savefolder);
if testing_desc_done == 0
fprintf('\nGetting events and saving descriptors for testing...\n');
for class_i=1:num_classes
classi_name = cell2mat(classnames(class_i));
test_classi = test_filenames{class_i};
mkdir(fullfile(test_desc_savefolder, classi_name));
for image_classi = 1:numel(test_classi)
count = count + 1;
filename = test_classi{image_classi};
filepath = fullfile(train_dataset_path,classi_name, filename);
savepath_file = fullfile(test_desc_savefolder, classi_name, filename(1:end-4));
TD = read_linux(filepath, 0);
TD = FilterTD(TD, us_time_filter);
testimage_sizes(count,:) = [max(TD.y) max(TD.x)];
try
load(savepath_file);
catch
[frames, desc] = getDescs_RECT_FPGA(TD, param);
disp(savepath_file);
save(savepath_file,'frames','desc');
end
test_label = [test_label class_i];
fprintf('File num: %d\n\n', count);
end
end
testing_desc_done =1;
save('./NDEMO4testdesc_5by5subsamp2x2_ustime5e3_PCA.mat',...
'test_label','testimage_sizes','-v7.3');
if canUseGPU == 1
poolobj = gcp('nocreate');
delete(poolobj);
end
else
disp('Loading test descriptors...'); % needs modification of code if desc_done=0
load('./NDEMO4testdesc_5by5subsamp2x2_ustime5e3_PCA.mat');
end
%% ---------------------------------------------------------------------------
%---------------------------------------------------------------------------
% Testing phase
count = 0;
count_sep =0;
save_for_majvot = [];
test_label = [];
fprintf('\n\n------------------------------Starting Testing Stage------------------------------\n\n');
for class_i=1:num_classes
classi_name = cell2mat(classnames(class_i));
test_classi = test_filenames{class_i};
for image_classi = 1:numel(test_classi)
count = count + 1;
filename = test_classi{image_classi};
filepath = fullfile(test_desc_savefolder, classi_name, [filename(1:end-4) '.mat']);
[test_data, loctest_label, testlabel] = readDescs(filepath, [], 'nonorm');
testdata_befpca = single([test_data.desc]);
testdata_befpca = bsxfun(@minus, testdata_befpca, attr_means');
testdata_afterpca = coeff(:,1:pcadims)'*testdata_befpca;
start_chunk = 1;
end_chunk = event_chunk_period;
for event_c = 1:ceil(size(test_data.frames,2)/event_chunk_period)
count_sep= count_sep + 1;
try
desc = testdata_afterpca(:, start_chunk:end_chunk);
frames = test_data.frames(:,start_chunk:end_chunk);
catch
desc = testdata_afterpca(:, start_chunk:end);
frames = test_data.frames(:,start_chunk:end);
end
hists = get_imagehist(model,testimage_sizes(count,:),...
double(frames), double(desc), histopts, 'nonorm');
psix = hists;
%SVM Classification
scores = svmmodel.w' * psix + svmmodel.b' * ones(1,size(psix,2)) ;
[obj_cat(count_sep,1), obj_cat(count_sep,2)] = max(scores);
% keyboard
test_label = [test_label class_i];
start_chunk = end_chunk + 1;
end_chunk = end_chunk + event_chunk_period;
if end_chunk > size(test_data.frames,2)
end_chunk = size(test_data.frames,2);
end
end
save_for_majvot(count) = count_sep;
fprintf('File num: %d\n\n', count);
end
end
error_locs=find(obj_cat(:,2)'~=test_label);
error_per=length(error_locs);
accuracy(repeat)= 100*(numel(test_label)- error_per)/numel(test_label)
end
toc