-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaCascade.m
More file actions
executable file
·238 lines (194 loc) · 6.5 KB
/
AdaCascade.m
File metadata and controls
executable file
·238 lines (194 loc) · 6.5 KB
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
numPos = 2000;
numNeg = 2000;
numTotal = numPos + numNeg;
numFeature = 34200;
numGroupFeature = 5000;
numGroup = ceil(numFeature/numGroupFeature);
numWeakClassifier = 50;
numStage = 20;
% >>> initialize the cascaded classifier <<< %
h = zeros(3*numStage, numWeakClassifier);
alpha = zeros(numStage, numWeakClassifier);
s = zeros(1, numStage);
t = zeros(1, numStage);
accuracy = zeros(4, numStage);
positive = ones(1, numTotal);
feature = zeros(numGroupFeature, numTotal);
f = zeros(numWeakClassifier, numTotal);
% >>> add stages until accuracy is 100% <<< %
stageCount = 0;
falsePos = inf;
falseNeg = inf;
while falsePos + falseNeg > 0
stageCount = stageCount + 1;
% >>> initialize the weights <<< %
lPos = sum(positive(1:numPos));
mNeg = sum(positive(numPos+1:numTotal));
weight = [ones(1,numPos)/(2*lPos), ones(1,numNeg)/(2*mNeg)];
fprintf('cascade stage %d\n', stageCount);
fprintf('negative samples pruned =');
% >>> add weak classifiers until 50% negative samples pruned <<< %
count = 0;
negCount = 0;
while negCount < mNeg/2
% >>> increment weak classifier count <<< %
count = count + 1;
% >>> normalize the weight <<< %
weight = weight/sum(positive .* weight);
% >>> search for the best weak classifier <<< %
eMin = inf;
totalPos = sum(positive(1:numPos) .* weight(1:numPos));
totalNeg = sum(positive(numPos+1:numTotal) .* weight(numPos+1:numTotal));
for i = 1:numGroup
filename = sprintf('FEAT_TRAIN%02d.mat', i);
load(filename, '-mat', 'feat');
[sfeat, ifeat] = sort(feat, 2);
numFeatures = size(feat, 1);
fNew = 0;
for j = 1:numFeatures % >>> in each feature group
SPos = 0;
SNeg = 0;
for k = 1:numTotal
if positive(ifeat(j, k)) ~= 1
continue;
end
e1 = SPos + (totalNeg - SNeg);
e2 = SNeg + (totalPos - SPos);
if e2 < e1
eTmp = e2;
pTmp = 1;
else
eTmp = e1;
pTmp = -1;
end
if eTmp < eMin
fNew = 1;
eMin = eTmp;
h(3*(stageCount-1)+1, count) = numGroupFeature*(i-1)+j;
h(3*(stageCount-1)+2, count) = pTmp;
h(3*(stageCount-1)+3, count) = sfeat(j, k);
end
if ifeat(j,k) > numPos
SNeg = SNeg + weight(ifeat(j,k));
else
SPos = SPos + weight(ifeat(j,k));
end
end
end
if fNew == 1
jFeat = h(3*(stageCount-1)+1, count) - numGroupFeature * (i-1);
f(count,:) = feat(jFeat,:);
end
end
% >>> update weights <<< %
beta = eMin/(1-eMin);
for i = 1:numPos
if positive(i) ~= 1
continue;
end
p = h(3*(stageCount-1)+2, count);
theta = h(3*(stageCount-1)+3, count);
if p * f(count, i) < p * theta
weight(i) = weight(i) * beta;
end
end
for i = numPos+1:numTotal
if positive(i) ~= 1
continue;
end
p = h(3*(stageCount-1)+2, count);
theta = h(3*(stageCount-1)+3, count);
if ~(p*f(count,i) < p*theta)
weight(i) = weight(i) * beta;
end
end
% >>> set threshold for strong classifier <<< %
alpha(stageCount, count) = log(1/beta);
HSMin = inf;
for i = 1:numPos
if positive(i) ~= 1
continue;
end
HS = 0;
for j = 1:count
p = h(3*(stageCount-1)+2, j);
theta = h(3*(stageCount-1)+3, j);
if p*f(j,i) < p * theta
HS = HS + alpha(stageCount, j);
end
end
if HS < HSMin
HSMin = HS;
end
end
% >>> test strong classifier on negative samples <<< %
negCount = 0;
for i = numPos+1:numTotal
if positive(i) ~= 1
continue;
end
HS = 0;
for j = 1:count
p = h(3*(stageCount-1)+2,j);
theta = h(3*(stageCount-1)+3,j);
if p*f(j,i) < p*theta
HS = HS+alpha(stageCount, j);
end
end
if HS < HSMin
negCount = negCount + 1;
end
end
fprintf(' %d', negCount);
end
% >>> save weak class count and the strong class threshold <<< %
s(stageCount) = count;
t(stageCount) = HSMin;
% >>> evaluate on samples classified as positive <<< %
falseNeg = 0;
for i = 1:numPos
if positive(i) ~= 1
continue;
end
HS = 0;
for j = 1:count
p = h(3*(stageCount-1)+2,j);
theta = h(3*(stageCount-1)+3,j);
if p*f(j,i) < p*theta
HS = HS+alpha(stageCount,j);
end
end
if HS >= HSMin
positive(i) = 1;
else
falseNeg = falseNeg+1;
positive(i) = 0;
end
end
falsePos = 0;
for i = numPos+1:numTotal
if positive(i) ~= 1
continue;
end
HS = 0;
for j = 1:count
p = h(3*(stageCount-1)+2,j);
theta = h(3*(stageCount-1)+3,j);
if p*f(j,i) < p*theta
HS = HS+alpha(stageCount,j);
end
end
if HS >= HSMin
falsePos = falsePos+1;
positive(i) = 1;
else
positive(i) = 0;
end
end
accuracy(1,count) = falsePos;
accuracy(2,count) = lPos;
accuracy(3,count) = falseNeg;
accuracy(4,count) = mNeg;
end
% >>> saev the result to a MAT file <<< %
save('results.mat', 'h','alpha','s','t','accuracy','-mat','-v7.3');