-
Notifications
You must be signed in to change notification settings - Fork 6
/
temp_forest_algorithm.m
541 lines (373 loc) · 14.7 KB
/
temp_forest_algorithm.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
function tempforest
clear all;
clc;
addpath /home/trakis/Downloads/MPIIGaze/Data/%@tree
HEIGHT = 15;%9;
WIDTH = 9;%15;
NUM_OF_GROUPS = 140;
%%%%%%%%%% Open HDF5 training file %%%%%%%%%%
samplesInTree = zeros(1,NUM_OF_GROUPS);
for R = 6:10
for i = 1:NUM_OF_GROUPS %for each tree
fid = H5F.open('myfile.h5', 'H5F_ACC_RDONLY', 'H5P_DEFAULT');
%%%%%%%%%% Start with the central group %%%%%%%%%%
grpID = H5G.open(fid, strcat('/g',num2str(i)) );
curr_rnearestID = H5D.open(grpID, '10_nearestIDs');
curr_centerID = H5D.open(grpID, 'center');
curr_imgsID = H5D.open(grpID, 'data');
curr_gazesID = H5D.open(grpID, 'gaze');
curr_posesID = H5D.open(grpID, 'headpose');
curr_rnearest = H5D.read(curr_rnearestID);
curr_center = H5D.read(curr_centerID);
curr_imgs = H5D.read(curr_imgsID);
curr_gazes = H5D.read(curr_gazesID);
curr_poses = H5D.read(curr_posesID);
samplesInGroup = length( curr_imgs(1,1,1,:) );
contribOfGroup = ceil( sqrt( samplesInGroup ) );
j = 1;
samplesInTree(i) = 0;
while j <= contribOfGroup
samplesInTree(i) = samplesInTree(i) + 1;
random = randi(samplesInGroup,1,1);
treeImgs(i,:,:,samplesInTree(i) ) = curr_imgs( :, :, 1, random);
treeGazes(i,samplesInTree(i),: ) = curr_gazes(:,random);
treePoses(i,:,samplesInTree(i) ) = curr_poses(:,random);
j = j + 1;
end
%%%%%%%% Now, continue with the R-nearest %%%%%%%%%
for k = 1:R
localGrpID = H5G.open(fid, strcat('/g', num2str( curr_rnearest(k)) ));
tempImgID = H5D.open( localGrpID, strcat('/g', num2str( curr_rnearest(k) ), '/data') );
tempPoseID = H5D.open( localGrpID, strcat('/g', num2str( curr_rnearest(k) ), '/headpose') );
tempGazeID = H5D.open( localGrpID, strcat('/g', num2str( curr_rnearest(k) ), '/gaze') );
tempImgs = H5D.read( tempImgID );
tempPoses = H5D.read( tempPoseID );
tempGazes = H5D.read( tempGazeID );
samplesInGroup = length( tempImgs(1,1,1,:) );
contribOfGroup = ceil( sqrt( samplesInGroup ) );
j = 1;
while j <= contribOfGroup
samplesInTree(i) = samplesInTree(i) + 1;
random = randi(samplesInGroup,1,1);
treeImgs (i, :,:,samplesInTree(i)) = tempImgs(:,:,1, random);
treeGazes(i, samplesInTree(i), :) = tempGazes(:, random);%, :);
treePoses(i, :,samplesInTree(i)) = tempPoses( :,random);
j = j + 1;
end
H5D.close( tempImgID );
H5D.close( tempPoseID);
H5D.close( tempGazeID);
H5G.close( localGrpID ) ;
end
end
%%%%%%%% Now that we created each tree's data, lets implement the algorithm %%%%%%%%%
% - am really thankful to http://tinevez.github.io/matlab-tree/index.html
%
% - Each node:
% a) is named '(px1,px2), thres'
% b) has variable name: node(k)
%
% - node(k) can have:
% a) parent node(k/2 )
% b) left child(2k)
% c) right child(2k+1)
% - Leaves can have:
% d) left 2d gaze angle
% e) right 2d gaze angle
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% xtise mono 6 gia logous oikonomias. Meta vgale tin if
trees = buildRegressionTree( samplesInTree, treeImgs, treeGazes, HEIGHT, WIDTH);
pause(180);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%% T E S T P H A S E %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Open HDF5 test file %%%%%%%%%%
fid2 = H5F.open('mytest.h5', 'H5F_ACC_RDONLY', 'H5P_DEFAULT');
test_rnearestID = H5D.open(fid2, '_nearestIDs');
test_imgsID = H5D.open(fid2, 'data');
test_gazesID = H5D.open(fid2, 'gaze');
test_posesID = H5D.open(fid2, 'headpose');
test_rnearest = H5D.read(test_rnearestID);
test_imgs = H5D.read(test_imgsID);
test_gazes = H5D.read(test_gazesID);
test_poses = H5D.read(test_posesID);
ntestsamples = length( test_imgs(:,:,:,:) );
final_error = 0;
for j = 1:ntestsamples
gaze_predict = [0 0]';
for k = 1:(R+1)%each samples, run the R+1 trees
gaze_predict = gaze_predict + testSampleInTree( trees(test_rnearest(k,j) ), 1, test_imgs(:,:,1,j), test_gazes(:,j) );
end
gaze_predict = gaze_predict/(R+1);
final_error = final_error + abs(test_gazes(1,j) - gaze_predict(1) ) + abs( test_gazes(2,j) -gaze_predict(2) );
end
final_error = final_error/(2*ntestsamples);
rad2deg(final_error)
fileID = fopen( strcat(R,'nearest.txt'),'w');
fprintf(fileID,'%f', final_error);
fclose(fileID);
H5D.close(test_rnearestID);
H5D.close(test_imgsID);
H5D.close(test_gazesID);
H5D.close(test_posesID);
H5F.close(fid2);
%%%%%%%%% Close Central Group %%%%%%%%%%%%%%%%%%
H5D.close(curr_rnearestID);
H5D.close(curr_centerID);
H5D.close(curr_imgsID);
H5D.close(curr_gazesID);
H5D.close(curr_posesID);
H5G.close(grpID);
H5F.close(fid);
end
end
function val = testSampleInTree(tree, node, test_img, gaze )
val = [100000 100000];
if tree.isleaf(node)
val = sscanf(tree.get(node),'(%f,%f)');
else
data= sscanf(tree.get(node),'Samples:%f,px1(%f,%f)-px2(%f,%f)>=%f');
childs = tree.getchildren(node);
if abs(test_img(data(2), data(3), 1, 1) - test_img(data(4), data(5), 1,1)) >= data(6)
val = testSampleInTree(tree,childs(2) , test_img, gaze );
else
val = testSampleInTree(tree, childs(1), test_img, gaze );
end
end
end
function treesMy = buildRegressionTree( fatherSizeX, treeImgsX, treeGazesX, HEIGHTX, WIDTHX)
MAX_DEPTH = 20;
NUM_OF_WORKERS = 3;
MAX_FATHER_SIZE = 189;%200;
treeGazes = Composite();%NUM_OF_WORKERS);
fatherSizeTrees = Composite();%NUM_OF_WORKERS);
treeImgs = Composite();%NUM_OF_WORKERS);
HEIGHT = Composite();%NUM_OF_WORKERS);
WIDTH = Composite();%NUM_OF_WORKERS);
fatherSize = Composite();%NUM_OF_WORKERS);
for w=1:NUM_OF_WORKERS
treeGazes{w} = treeGazesX;
fatherSize{w} = fatherSizeX;
treeImgs{w} = treeImgsX;
HEIGHT{w} = HEIGHTX;
WIDTH{w} = WIDTHX;
currPtrs{w} = [1:MAX_FATHER_SIZE];
end
c = parcluster;
c.NumWorkers = NUM_OF_WORKERS;
saveProfile(c);
mypool = gcp('nocreate');
if isempty(mypool)
mypool = parpool('local',3);
end
spmd;
savedNodeSize = zeros(MAX_DEPTH,2);
currPtrs = zeros(1,MAX_FATHER_SIZE);
px1_vert = zeros(1);
px1_hor = zeros(1);
px2_vert = zeros(1);
px2_hor = zeros(1);
counter = zeros(1);
minSquareError = zeros(1,3);
numOfPixels = zeros(1);
numOfPixels = HEIGHT*WIDTH;
bestworker = zeros(1);
container = [];
container.data = zeros(1,7);
%%% allocate that memory in order to begin %%%
%container.currPtrs = zeros(1, fatherSize(1));
%container.savedPtrs = zeros(1, fatherSize(1));
container.saved_curr_Ptrs = zeros(2,fatherSize(1));
cache_treeImgs = zeros(fatherSize(1), 2);
l_r_fl_fr_imgs = zeros(4,fatherSize(1));
savedPtrs = zeros(MAX_DEPTH, fatherSize(1) );
bestSize = fatherSize(1);
for i = 1:140 % for every tree
if (fatherSize(i) > bestSize) || (bestSize - fatherSize(i) > 15 )
%%% reallocate memory when the condition is true %%%
bestSize = fatherSize(i);
cache_treeImgs = [];
l_r_fl_fr_imgs = [];
savedPtrs = [];
container.saved_curr_Ptrs = [];
savedPtrs = zeros(MAX_DEPTH, fatherSize(i) );
cache_treeImgs = zeros(fatherSize(i), 2);
l_r_fl_fr_imgs = zeros(4,fatherSize(i));
container.saved_curr_Ptrs = zeros(2,fatherSize(i));
end
stackindex = 0;
state = 1;
trees(i) = tree(strcat('RegressionTree_', num2str(i) ));
node_i = 1;
currPtrs = [1:fatherSize(i)];
while state ~= 2
%for each node
minSquareError = [10000 10000 10000];
minPx1_vert = 10000; % something random here
minPx1_hor = 10000; % also here
minPx2_vert= 10000; % and here..
minPx2_hor = 10000; % and here
bestThres = 10000; % ah, and here
counter = labindex;
while (counter <= numOfPixels-1)
px1_vert = ceil( (counter/WIDTH));
px1_hor = 1 + mod(counter-1, (WIDTH) );
% sorry for the huge equations below
% these equations are made in order to prevent 2 pixels
% to be examined twice
for px2_vert = ( px1_vert + floor(px1_hor/WIDTH) ):HEIGHT
for px2_hor = (1 + mod( px1_hor, WIDTH )):WIDTH
%%% create a cache array (px1_vert_px1_hor, curr %%%
for j = 1:fatherSize(i)
cache_treeImgs(j,1) = treeImgs(i, px1_vert,px1_hor, currPtrs( j) );
cache_treeImgs(j,2) = treeImgs(i, px2_vert,px2_hor, currPtrs( j) );
end
if sqrt( (px1_vert -px2_vert)^2+(px1_hor-px2_hor)^2 ) < 6.5
for thres = 1:50
l = 0;
r = 0;
meanLeftGaze = [0 0];
meanRightGaze = [0 0];
for j = 1:fatherSize(i)
if abs( cache_treeImgs(j,1) - cache_treeImgs(j,2) ) < thres
%left child
l = l + 1;
l_r_fl_fr_imgs(1,l) = currPtrs(j);
meanLeftGaze(1) = meanLeftGaze(1) + treeGazes(i,currPtrs(j),1);
meanLeftGaze(2) = meanLeftGaze(2) + treeGazes(i,currPtrs(j),2);
else
%right child
r = r + 1;
l_r_fl_fr_imgs(2,r) = currPtrs(j);
meanRightGaze(1) = meanRightGaze(1) + treeGazes(i,currPtrs(j),1);
meanRightGaze(2) = meanRightGaze(2) + treeGazes(i,currPtrs(j),2);
end
end
meanLeftGaze = meanLeftGaze / l;
meanRightGaze = meanRightGaze/ r;
squareError = 0;
for j = 1:l
squareError=squareError + (meanLeftGaze(1)-treeGazes(i, l_r_fl_fr_imgs(1,l),1))^2 + (meanLeftGaze(2)-treeGazes(i,l_r_fl_fr_imgs(1,l),2))^2;
end
for j = 1:r
squareError=squareError + (meanRightGaze(1)-treeGazes(i,l_r_fl_fr_imgs(2,r),1))^2 + (meanRightGaze(2)-treeGazes(i, l_r_fl_fr_imgs(2,r), 2))^2;
end
if squareError < minSquareError(labindex)
minSquareError(labindex) = squareError;
minPx1_vert = px1_vert; % something random here
minPx1_hor = px1_hor; % also here
minPx2_vert= px2_vert; % and here..
minPx2_hor = px2_hor; % and here
bestThres = thres;
l_r_fl_fr_imgs(3,1:l) = l_r_fl_fr_imgs(1,1:l);
l_r_fl_fr_imgs(4,1:r) = l_r_fl_fr_imgs(2,1:r);
ltreeSize = l;
rtreeSize = r;
rtree_meanGaze = meanRightGaze;
ltree_meanGaze = meanLeftGaze;
end
end%thres
end%end if < 6.5
end%px2_hor
end%px2_vers
%end %px1_hor
counter = counter + numlabs;
end %endof px1_vert
% if numlabs == 3
rcvWkrIdx = mod(labindex, numlabs) + 1; % one worker to the right
srcWkrIdx = mod(labindex - 2, numlabs) + 1; % one worker to the left
labBarrier;
%%% take data from the left and give to the right %%%
minSquareError( srcWkrIdx ) = labSendReceive(rcvWkrIdx,srcWkrIdx, minSquareError(labindex) );
labBarrier;
%%% take data from the right %%%
minSquareError(rcvWkrIdx) = labSendReceive(srcWkrIdx,rcvWkrIdx,minSquareError(labindex));
labBarrier;
%%% sychronize before finding the best worker %%%
bestworker = 1;
minError = minSquareError(1);
for k = 2:numlabs
if minSquareError(k) < minError
minError = minSquareError(k);
bestworker = k;
end
end
if bestworker == labindex
%%%%%% Recursion starts here %%%%%
if (ltreeSize > 0 && rtreeSize > 0)
state = 1;
trees(i)=trees(i).set(node_i,strcat('Samples:',num2str(fatherSize(i)),',px1(', num2str(minPx1_vert),',',num2str(minPx1_hor),')-','px2(',num2str(minPx2_vert),',',num2str(minPx2_hor),')>=', num2str(bestThres) ));
[trees(i) lnode] = trees(i).addnode(node_i, strcat('(', num2str(ltree_meanGaze(1)), ',', num2str(ltree_meanGaze(2)), ')'));
[trees(i) rnode] = trees(i).addnode(node_i, strcat('(', num2str(rtree_meanGaze (1)), ',', num2str(rtree_meanGaze (2)), ')'));
% start saving the left brother
stackindex = stackindex + 1;
savedNodeSize(stackindex,1) = lnode;
savedNodeSize(stackindex,2) = ltreeSize;
savedPtrs(stackindex, 1:ltreeSize) = l_r_fl_fr_imgs(3,1:ltreeSize);
%%% prepare data for right son %%%
node_i = rnode;
fatherSize(i) = rtreeSize;
currPtrs(1:rtreeSize) = l_r_fl_fr_imgs(4,1:rtreeSize);
else %2
if stackindex == 0
state = 2;
else
state = 3;
node_i = savedNodeSize(stackindex,1);
fatherSize(i) = savedNodeSize(stackindex,2);
currPtrs(1:fatherSize(i)) = savedPtrs(stackindex,1:fatherSize(i));
stackindex = stackindex - 1;
end
end %2
end
%%% Load to container %%%
if labindex == bestworker
if state == 1
container.data = [state numOfPixels stackindex fatherSize(i) node_i savedNodeSize(stackindex,1) savedNodeSize(stackindex,2) ];
container.trees = trees(i);
container.saved_curr_Ptrs(1, 1:ltreeSize) = l_r_fl_fr_imgs(3,1:ltreeSize);
container.saved_curr_Ptrs(2, 1:fatherSize(i) ) = currPtrs(1:fatherSize(i) );
elseif state == 2
container.data(1) = 2;
else %state == 3
container.data = [state numOfPixels stackindex fatherSize(i) node_i ];
container.saved_curr_Ptrs(2,1:fatherSize(i)) = currPtrs(1:fatherSize(i));
end
end
labBarrier;
if labindex ~= bestworker
container = labBroadcast(bestworker);
if container.data(1) == 1 %state = 1
stackindex = container.data(3);
fatherSize(i) = container.data(4);
node_i = container.data(5);
savedNodeSize(stackindex,1) = container.data(6);
savedNodeSize(stackindex,2) = container.data(7);%ltreeSize
trees(i) = container.trees;
savedPtrs(stackindex,1:savedNodeSize(stackindex,2)) = container.saved_curr_Ptrs(1,1:savedNodeSize(stackindex,2));
currPtrs(1:fatherSize(i)) = container.saved_curr_Ptrs(2,1:fatherSize(i));
elseif container.data(1) == 2
state = 2;
else %container.data(1) == 3 %[state poulo stackindex fatherSize node_i ];
state = 3;
%%% o stackindex erxetai meiwmenos kata 1 %%%
stackindex = container.data(3);
fatherSize(i) = container.data(4);
node_i = container.data(5);
currPtrs(1:fatherSize(i)) = container.saved_curr_Ptrs(2,1:fatherSize(i));
end
else
labBroadcast(bestworker, container);
end
%isws
labBarrier;
end %while loop
if labindex == 1
i
disp(trees(i).tostring);
fprintf('\n\n\n\n\n\n\n');
end
end %treeCompleted
end%end of spmd
treesMy = trees{1};
end %end of program