-
Notifications
You must be signed in to change notification settings - Fork 4
/
reorderStages.asv
92 lines (83 loc) · 1.9 KB
/
reorderStages.asv
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
function [Cn Rn]= reorderStages(data, labels, v, C)
%reorderStages
%labesl = {0, 1}
%v is rejection distribution vector
%C is array of structures representing the stage classifier, numel(C) ==
%numel(v)
%total stages
T = numel(v);
%positive rejection fraction
p = 0;
%execution time
m = 0;
labels = labels(:)';
a = sum(labels);
b = numel(labels) - a;
%number of negative samples used so far
A = a;
at = 0;
bt = 0;
totalData = size(data,1);
ind = ones(totalData, 1)==1;
indC = ones(T, 1)==1;
%sample responses
dt = zeros(totalData,1)';
Cn = [];
Rn = [];
rs = evaluate(C, data);
for t=1:T
p = p + v(t);
at = sum(labels(ind));
bt = sum(labels(ind)==0);
i = findMaxStage(C, labels(ind), dt(ind), at, bt, rs(:, ind));
ct = i;
dt(ind) = dt(ind) + rs(i, ind);
rt = findMaxThresh(dt(ind), labels(ind), p*b);
p = p - sum((dt(ind) <= rt).*labels(ind))/b;
%step 5
Cn = [Cn, C(ct)];
C(ct) = [];
rs(ct,:) = [];
Rn(t) = rt;
ind(find(dt(ind) < rt)) = 0;
%step 6
%dt(ind) <
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function rt = findMaxThresh(dt, lb, pb)
dt = sort(dt, 'ascend');
i = numel(dt);
flags = 1;
while flags == 1 && i >=1
rt = dt(i);
if(sum((dt <= rt).*) <= pb)
flags = 0;
else
i = i-1;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function rs = evaluate(C, data)
T = numel(C);
for j=1:T
rs(j,:) = (data*C(j).w)';
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mxInd = findMaxStage(C, labels, dt, at, bt, rs)
mxInd = 0;
maxQ = -100000000;
T = numel(C);
for j=1:T
ft = dt+rs(j,:);
val = sum(ft.*labels)/bt + sum(ft.*(1-labels))/at;
if val > maxQ || j == 1
maxQ = val;
mxInd = j;
end
end
%return mxInd;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%