Skip to content

Commit 6b35f6d

Browse files
committed
Rectified an issue which remained from a mistaken commit (ac61810). Fixes weighting error (closing #5) and accuracy (closes #11) [had to fix votes calculations due to accidental in defining , and tentatively but incorrectly implemented zerosarray---kept the function just in case]. Woohoo! That's the algorithm.
1 parent 456f8c6 commit 6b35f6d

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

src/Adaboost.jl

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,40 +96,48 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
9696
9797
# println(typeof(numImgs));println(typeof(numFeatures))
9898
# create an empty array (of zeroes) with dimensions (numImgs, numFeautures)
99-
global votes = zeros((numImgs, numFeatures)) # necessarily different from `zero.((numImgs, numFeatures))`
99+
global votes = zeros((numImgs, numFeatures)) # necessarily different from `zero.((numImgs, numFeatures))`; previously zerosarray
100100
101101
# bar = progressbar.ProgressBar()
102102
# @everywhere numImgs begin
103+
# println(size(votes))
103104
# println(votes)
104-
105+
# displaymatrix(votes)
106+
# println(length(features))
107+
# displaymatrix(features)
105108
# show progress bar
109+
# displaymatrix(images)
110+
# println(size(images))
106111
@everywhere begin
107112
n = numImgs
108-
processes = length(numImgs) # i.e., hypotheses
109-
p = Progress(n, 1) # minimum update interval: 1 second
110-
for t in 1:processes # bar(range(num_imgs)):
113+
processes = numImgs # i.e., hypotheses
114+
# println(processes)
115+
# p = Progress(n, 1) # minimum update interval: 1 second
116+
@showprogress for t in 1:processes # bar(range(num_imgs)):
117+
# println(t)
111118
# votes[i, :] = np.array(list(Pool(processes=None).map(partial(_get_feature_vote, image=images[i]), features)))
112-
# votes[i, :] = Array(map(partial(getVote, images[i]), features))
113-
votes[t, :] = Array(map(feature -> getVote(feature, images[t]), features))
119+
# votes[t, :] = Array(map(partial(_get_feature_vote, images[t]), features))
120+
votes[t, :] = Array(map(f -> _get_feature_vote(f, images[t]), features))
114121
# votes[i, :] = [map(feature -> getVote(feature, images[i]), features)]
115-
next!(p)
122+
# next!(p)
116123
end
117124
end # end everywhere (end parallel processing)
125+
# displaymatrix(votes)
118126
119127
# select classifiers
120128
# classifiers = Array()
121129
classifiers = []
122130
123-
println("Selecting classifiers...")
131+
println("\nSelecting classifiers...")
124132
125133
n = numClassifiers
126-
p = Progress(n, 1) # minimum update interval: 1 second
127-
for t in 1:numClassifiers
134+
# p = Progress(n, 1) # minimum update interval: 1 second
135+
@showprogress for t in 1:numClassifiers
128136
# for t in processes
129137
# println(typeof(length(featureIndices)))
130138
# print_matrix(stdout, weights)
131139
# println(weights)
132-
classificationErrors = zeros(length(featureIndices))
140+
classificationErrors = zeros(length(featureIndices)) # previously, zerosarray
133141
134142
# normalize the weights $w_{t,i}\gets \frac{w_{t,i}}{\sum_{j=1}^n w_{t,j}}$
135143
# weights *= 1. / np.sum(weights)
@@ -164,6 +172,7 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
164172
165173
classificationErrors[j] = ε
166174
end
175+
# print_matrix(stdout, weights)
167176
168177
# choose the classifier $h_t$ with the lowest error $\varepsilon_t$
169178
minErrorIDX = argmin(classificationErrors) # returns the index of the minimum in the array
@@ -179,7 +188,9 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
179188
# featureWeight = (1 - bestError) / bestError # β
180189
# println(typeof(featureWeight))
181190
# [println(f.weight) for f in features]
191+
# print_matrix(stdout, weights)
182192
bestFeature.weight = featureWeight
193+
# print_matrix(stdout, weights)
183194
184195
# classifiers = vcat(classifiers, bestFeature)
185196
# println(classifiers)
@@ -188,8 +199,22 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
188199
# update image weights $w_{t+1,i}=w_{t,i}\beta_{t}^{1-e_i}$
189200
# weights = list(map(lambda img_idx: weights[img_idx] * np.sqrt((1-best_error)/best_error) if labels[img_idx] != votes[img_idx, best_feature_idx] else weights[img_idx] * np.sqrt(best_error/(1-best_error)), range(num_imgs)))
190201
# weights = (imgIDX -> (labels[imgIDX] ≠ votes[imgIDX, bestFeatureIDX]) ? weights[imgIDX]*sqrt((1-bestError)/bestError) : weights[imgIDX]*sqrt(bestError/(1-bestError)), 1:numImgs)
202+
# print_matrix(stdout, weights)
203+
# weights = Array(map(imgIDX -> labels[imgIDX] ≠ votes[imgIDX, bestFeatureIDX] ? weights[imgIDX] * sqrt((1 - bestError) / bestError) : weights[imgIDX] * sqrt(bestError / (1 - bestError)), 1:numImgs))
204+
weights = Array(map(i -> labels[i] ≠ votes[i, bestFeatureIDX] ? weights[i] * sqrt((1 - bestError) / bestError) : weights[i] * sqrt(bestError / (1 - bestError)), 1:numImgs))
205+
# println(votes[:,bestFeatureIDX])
206+
207+
# print_matrix(stdout, weights)
208+
# println(typeof(weights))
191209
192-
weights = Array(map(imgIDX -> (labels[imgIDX] ≠ votes[imgIDX, bestFeatureIDX]) ? weights[imgIDX] * featureWeight : weights[imgIDX] * featureWeight, 1:numImgs))
210+
# imgIDX -> labels[imgIDX] ≠ votes[imgIDX, bestFeatureIDX] ? weights[imgIDX] * sqrt((1 - bestError) / bestError) : weights[imgIDX] * sqrt(bestError / (1 - bestError))
211+
212+
# weights = np.array(list(map(
213+
#
214+
# lambda img_idx:
215+
# weights[img_idx] * np.sqrt((1-best_error)/best_error) if labels[img_idx] != votes[img_idx, best_feature_idx] else weights[img_idx] * np.sqrt(best_error/(1-best_error)),
216+
#
217+
# range(num_imgs))))
193218
194219
# β = ε / (1 - ε)
195220
#
@@ -207,15 +232,22 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
207232
featureIndices = filter!(e -> e ∉ bestFeatureIDX, featureIndices) # note: without unicode operators, `e ∉ [a, b]` is `!(e in [a, b])`
208233
# println(bestFeature)
209234

210-
next!(p)
235+
# next!(p)
211236
end
237+
# println(weights)
212238

213239
# println(typeof(classifiers[1]))
240+
# println(votes)
214241
return classifiers
215242

216243
end
217244

218245

246+
function _get_feature_vote(feature::HaarLikeFeature, image::AbstractArray)
247+
return getVote(feature, image)
248+
end
249+
250+
219251
# function _get_feature_vote(feature::HaarLikeFeature, image::Int64)
220252
# # return getVote(image)
221253
# # return partial(getVote)(image)

src/FDA.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function main(alt::Bool=false)
7171
# println(typeof(c))
7272
# end
7373

74-
println("Loading test faces...")
74+
println("\nLoading test faces...")
7575
facesTesting = loadImages(posTestingPath)
7676
facesIITesting = map(toIntegralImage, facesTesting) # list(map(...))
7777
println("...done. ", length(facesTesting), " faces loaded.\n\nLoading test non-faces..")

src/Utils.jl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ deepfloat(a::Number) = a * 1.0
4343
deepfloat(a) = deepfloat.(a)
4444

4545

46+
function displaymatrix(M::AbstractArray)
47+
return show(IOContext(stdout, :limit => true, :compact => true, :short => true), "text/plain", M)
48+
# return show(IOContext(stdout, :limit => true, :compact => true, :short => true, :displaysize), "text/plain", M)
49+
end
50+
51+
52+
function zerosarray(a::Int64, b::Int64=1)
53+
#=
54+
To replicate numpy.zeros(...)
55+
56+
parameter `a`: A number of length for array to be [type: Integer]
57+
parameter `b`: A number of the second dimension for array of arrays of zeros (defaults to one) [type: Integer]
58+
59+
return: Either an array of zeros (i.e., `[0, 0, 0]`), or an array of arrays of zeros (if b is given; i.e., [0, 0, 0, 0; 0, 0, 0, 0; 0, 0, 0, 0]) [type: Abstract Array]
60+
=#
61+
62+
if isone(b)
63+
# return [zeros(a) for _ in 1:b]
64+
return collect(eachrow(zeros(b, a)))
65+
else
66+
# return [zeros(b) for _ in 1:a]
67+
return collect(eachrow(zeros(a, b)))
68+
end
69+
end
70+
71+
4672
# for adaboost
4773
function partial(f,a...)
4874
#=
@@ -218,7 +244,7 @@ function reconstruct(classifiers::AbstractArray, imgSize::Tuple)
218244
sign = mod((sign + 1), 2)
219245
end
220246
for y in 1:c.height
221-
image[c.topLeft[0] + x, c.topLeft[1] + y] += 1 * sign * c.weight
247+
image[c.topLeft[1] + x, c.topLeft[2] + y] += 1 * sign * c.weight
222248
end
223249
end
224250
elseif c.featureType == FeatureTypes[3] # three horizontal

0 commit comments

Comments
 (0)