Skip to content

Commit 3e9be4a

Browse files
committed
Slight copy-error corrections (hopefully a step towards #46)
1 parent f9b0719 commit 3e9be4a

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

src/AdaBoost.jl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ function learn(
123123
classification_errors = Vector{Float64}(undef, length(feature_indices))
124124

125125
for t in 1:num_classifiers
126-
# classification_errors = zeros(length(feature_indices))
127-
#classification_errors = Matrix{Float64}(undef, length(feature_indices), 1)
128-
129126
# normalize the weights $w_{t,i}\gets \frac{w_{t,i}}{\sum_{j=1}^n w_{t,j}}$
130127
weights .*= inv(sum(weights))
131128

@@ -135,8 +132,7 @@ function learn(
135132
labels[img_idx] !== votes[feature_indices[j], img_idx] ? weights[img_idx] : zero(Float64)
136133
end
137134
end
138-
# classification_errors[:] .= [sum([labels[img_idx] !== votes[feature_indices[j], img_idx] ? weights[img_idx] : zero(Float64) for img_idx in 1:num_imgs]) for j in 1:length(feature_indices)]
139-
135+
140136
# choose the classifier $h_t$ with the lowest error $\varepsilon_t$
141137
best_error, min_error_idx = findmin(classification_errors)
142138
best_feature_idx = feature_indices[min_error_idx]
@@ -146,6 +142,7 @@ function learn(
146142
best_feature = features[best_feature_idx]
147143
feature_weight = β(best_error)
148144
best_feature.weight = feature_weight
145+
# println(best_error)
149146

150147
# append selected features
151148
classifiers = push!(classifiers, best_feature)
@@ -244,11 +241,11 @@ function create_features(
244241

245242
for feature in values(feature_types) # (feature_types are just tuples)
246243
feature_start_width = max(min_feature_width, first(feature))
247-
for feature_width in range(feature_start_width, stop=max_feature_width, step=first(feature))
244+
for feature_width in feature_start_width:first(feature):(max_feature_width - 1)
248245
feature_start_height = max(min_feature_height, last(feature))
249-
for feature_height in range(feature_start_height, stop=max_feature_height, step=last(feature))
250-
for x in 1:(img_width - feature_width)
251-
for y in 1:(img_height - feature_height)
246+
for feature_height in feature_start_height:last(feature):(max_feature_height - 1)
247+
for x in 0:(img_width - feature_width)
248+
for y in 0:(img_height - feature_height)
252249
push!(features, HaarLikeObject(feature, (x, y), feature_width, feature_height, 0, 1))
253250
push!(features, HaarLikeObject(feature, (x, y), feature_width, feature_height, 0, -1))
254251
end # end for y

src/HaarLikeFeature.jl

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ abstract type HaarFeatureAbstractType end
1818
mutable struct HaarLikeObject{I <: Integer, F <: AbstractFloat}
1919
2020
Struct representing a Haar-like feature.
21+
22+
feature_type::Tuple{I, I}
23+
position::Tuple{I, I}
24+
top_left::Tuple{I, I}
25+
bottom_right::Tuple{I, I}
26+
width::I
27+
height::I
28+
threshold::I
29+
polarity::I
30+
weight::F
2131
"""
2232
mutable struct HaarLikeObject{I <: Integer, F <: AbstractFloat} <: HaarFeatureAbstractType
2333
#parametric struct to store the ints and floats efficiently
@@ -32,6 +42,16 @@ mutable struct HaarLikeObject{I <: Integer, F <: AbstractFloat} <: HaarFeatureAb
3242
weight::F
3343
end # end structure
3444

45+
"""
46+
HaarLikeObject(
47+
feature_type::Tuple{Integer, Integer},
48+
position::Tuple{Integer, Integer},
49+
width::Integer,
50+
height::Integer,
51+
threshold::Integer,
52+
polarity::Integer
53+
) -> HaarLikeObject
54+
"""
3555
function HaarLikeObject(
3656
feature_type::Tuple{Integer, Integer},
3757
position::Tuple{Integer, Integer},
@@ -59,7 +79,7 @@ end
5979
"""
6080
get_score(feature::HaarLikeObject, int_img::Array) -> Tuple{Number, Number}
6181
62-
Get score for given integral image array.
82+
Get score for given integral image array. This is the feature cascade.
6383
6484
# Arguments
6585
@@ -70,7 +90,7 @@ Get score for given integral image array.
7090
7191
- `score::Number`: Score for given feature
7292
"""
73-
function get_score(feature::HaarLikeObject{I,F}, int_img::Array) where {I, F}
93+
function get_score(feature::HaarLikeObject{I, F}, int_img::Array) where {I, F}
7494
score = zero(I)
7595
faceness = zero(I)
7696
_2f = F(2)
@@ -79,36 +99,36 @@ function get_score(feature::HaarLikeObject{I,F}, int_img::Array) where {I, F}
7999
_one_third = F(1.0 / 3.0)
80100

81101
if feature.feature_type == feature_types.two_vertical
82-
_first = sum_region(int_img, feature.top_left, (first(feature.top_left) + feature.width, I(round(last(feature.top_left) + feature.height / 2))))
83-
second = sum_region(int_img, (first(feature.top_left), I(round(last(feature.top_left) + feature.height / 2))), feature.bottom_right)
102+
_first = sum_region(int_img, feature.top_left, (first(feature.top_left) + feature.width, round(I, last(feature.top_left) + feature.height / 2)))
103+
second = sum_region(int_img, (first(feature.top_left), round(I, last(feature.top_left) + feature.height / 2)), feature.bottom_right)
84104
score = _first - second
85105
faceness = I(1)
86106
elseif feature.feature_type == feature_types.two_horizontal
87-
_first = sum_region(int_img, feature.top_left, (I(round(first(feature.top_left) + feature.width / 2)), last(feature.top_left) + feature.height))
88-
second = sum_region(int_img, (I(round(first(feature.top_left) + feature.width / 2)), last(feature.top_left)), feature.bottom_right)
107+
_first = sum_region(int_img, feature.top_left, (round(I, first(feature.top_left) + feature.width / 2), last(feature.top_left) + feature.height))
108+
second = sum_region(int_img, (round(I, first(feature.top_left) + feature.width / 2), last(feature.top_left)), feature.bottom_right)
89109
score = _first - second
90110
faceness = I(2)
91111
elseif feature.feature_type == feature_types.three_horizontal
92-
_first = sum_region(int_img, feature.top_left, (I(round(first(feature.top_left) + feature.width / 3)), last(feature.top_left) + feature.height))
93-
second = sum_region(int_img, (I(round(first(feature.top_left) + feature.width / 3)), last(feature.top_left)), (I(round(first(feature.top_left) + 2 * feature.width / 3)), last(feature.top_left) + feature.height))
94-
third = sum_region(int_img, (I(round(first(feature.top_left) + 2 * feature.width / 3)), last(feature.top_left)), feature.bottom_right)
112+
_first = sum_region(int_img, feature.top_left, (round(I, first(feature.top_left) + feature.width / 3), last(feature.top_left) + feature.height))
113+
second = sum_region(int_img, (round(I, first(feature.top_left) + feature.width / 3), last(feature.top_left)), (round(I, first(feature.top_left) + 2 * feature.width / 3), last(feature.top_left) + feature.height))
114+
third = sum_region(int_img, (round(I, first(feature.top_left) + 2 * feature.width / 3), last(feature.top_left)), feature.bottom_right)
95115
score = _first - second + third
96116
faceness = I(3)
97117
elseif feature.feature_type == feature_types.three_vertical
98-
_first = sum_region(int_img, feature.top_left, (first(feature.bottom_right), I(round(last(feature.top_left) + feature.height / 3))))
99-
second = sum_region(int_img, (first(feature.top_left), I(round(last(feature.top_left) + feature.height / 3))), (first(feature.bottom_right), I(round(last(feature.top_left) + 2 * feature.height / 3))))
100-
third = sum_region(int_img, (first(feature.top_left), I(round(last(feature.top_left) + 2 * feature.height / 3))), feature.bottom_right)
118+
_first = sum_region(int_img, feature.top_left, (first(feature.bottom_right), round(I, last(feature.top_left) + feature.height / 3)))
119+
second = sum_region(int_img, (first(feature.top_left), round(I, last(feature.top_left) + feature.height / 3)), (first(feature.bottom_right), round(I, last(feature.top_left) + 2 * feature.height / 3)))
120+
third = sum_region(int_img, (first(feature.top_left), round(I, last(feature.top_left) + 2 * feature.height / 3)), feature.bottom_right)
101121
score = _first - second + third
102122
faceness = I(4)
103123
elseif feature.feature_type == feature_types.four
104124
# top left area
105-
_first = sum_region(int_img, feature.top_left, (I(round(first(feature.top_left) + feature.width / 2)), I(round(last(feature.top_left) + feature.height / 2))))
125+
_first = sum_region(int_img, feature.top_left, (round(I, first(feature.top_left) + feature.width / 2), round(I, last(feature.top_left) + feature.height / 2)))
106126
# top right area
107-
second = sum_region(int_img, (I(round(first(feature.top_left) + feature.width / 2)), last(feature.top_left)), (first(feature.bottom_right), I(round(last(feature.top_left) + feature.height / 2))))
127+
second = sum_region(int_img, (round(I, first(feature.top_left) + feature.width / 2), last(feature.top_left)), (first(feature.bottom_right), round(I, last(feature.top_left) + feature.height / 2)))
108128
# bottom left area
109-
third = sum_region(int_img, (first(feature.top_left), I(round(last(feature.top_left) + feature.height / 2))), (I(round(first(feature.top_left) + feature.width / 2)), last(feature.bottom_right)))
129+
third = sum_region(int_img, (first(feature.top_left), round(I, last(feature.top_left) + feature.height / 2)), (round(I, first(feature.top_left) + feature.width / 2), last(feature.bottom_right)))
110130
# bottom right area
111-
fourth = sum_region(int_img, (I(round(first(feature.top_left) + feature.width / 2)), I(round(last(feature.top_left) + feature.height / 2))), feature.bottom_right)
131+
fourth = sum_region(int_img, (round(I, first(feature.top_left) + feature.width / 2), round(I, last(feature.top_left) + feature.height / 2)), feature.bottom_right)
112132
score = _first - second - third + fourth
113133
faceness = I(5)
114134
end
@@ -133,6 +153,9 @@ Get vote of this feature for given integral image.
133153
-1 otherwise
134154
"""
135155
function get_vote(feature::HaarLikeObject, int_img::AbstractArray)
136-
score = first(get_score(feature, int_img)) # we only care about score here
137-
return (feature.weight * score) < (feature.polarity * feature.threshold) ? one(Int8) : -one(Int8)
156+
score = first(get_score(feature, int_img)) # we only care about score here, not faceness
157+
# return (feature.weight * score) < (feature.polarity * feature.threshold) ? one(Int8) : -one(Int8)
158+
# return feature.weight * (score < feature.polarity * feature.threshold ? one(Int8) : -one(Int8))
159+
return score < feature.polarity * feature.threshold ? feature.weight : -feature.weight
160+
# self.weight * (1 if score < self.polarity * self.threshold else -1)
138161
end

0 commit comments

Comments
 (0)