Skip to content

Commit f1f5b5e

Browse files
committed
Updated within-code function documentation for readability
1 parent c87f50b commit f1f5b5e

File tree

5 files changed

+163
-99
lines changed

5 files changed

+163
-99
lines changed

setup.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ done
113113

114114

115115
main() {
116-
setupWD
117-
checkPackages
118-
obtainDataset1
116+
# setupWD
117+
# checkPackages
118+
# obtainDataset1
119119
obtainDataset2
120120
}
121121

src/Adaboost.jl

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ include("HaarLikeFeature.jl")
1616

1717

1818
function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassifiers::Int64=-1, minFeatureWidth::Int64=1, maxFeatureWidth::Int64=-1, minFeatureHeight::Int64=1, maxFeatureHeight::Int64=-1)
19-
"""
20-
Selects a set of classifiers. Iteratively takes the best classifiers based
21-
on a weighted error.
22-
:param positive_iis: List of positive integral image examples
23-
:type positive_iis: list[numpy.ndarray]
24-
:param negative_iis: List of negative integral image examples
25-
:type negative_iis: list[numpy.ndarray]
26-
:param num_classifiers: Number of classifiers to select, -1 will use all
27-
classifiers
28-
:type num_classifiers: int
29-
:return: List of selected features
30-
:rtype: list[violajones.HaarLikeFeature.HaarLikeFeature]
31-
"""
19+
#=
20+
The boosting algorithm for learning a query online. $T$ hypotheses are constructed, each using a single feature. The final hypothesis is a weighted linear combination of the $T$ hypotheses, where the weights are inverselt proportional to the training errors.
21+
This function selects a set of classifiers. Iteratively takes the best classifiers based on a weighted error.
22+
23+
parameter `positiveIIs`: List of positive integral image examples [type: Abstracy Array]
24+
parameter `negativeIIs`: List of negative integral image examples [type: Abstract Array]
25+
parameter `numClassifiers`: Number of classifiers to select. -1 will use all
26+
classifiers [type: Integer]
27+
28+
return `classifiers`: List of selected features [type: HaarLikeFeature]
29+
=#
30+
3231
numPos = length(positiveIIs)
3332
numNeg = length(negativeIIs)
3433
global numImgs = numPos + numNeg
@@ -61,6 +60,7 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
6160
The series of `deep*` functions——though useful in general——were designed from awkward arrays of tuples of arrays, which came about from a translation error in this case.
6261
=#
6362
weights = vcat(posWeights, negWeights)
63+
# println(weights)
6464
# weights = vcat(posWeights, negWeights)
6565
# weights = hcat((posWeights, negWeights))
6666
# weights = vcat([posWeights, negWeights])
@@ -86,6 +86,7 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
8686
8787
# bar = progressbar.ProgressBar()
8888
# @everywhere numImgs begin
89+
# println(votes)
8990
@everywhere begin
9091
n = numImgs
9192
processes = length(numImgs)
@@ -107,7 +108,7 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
107108
108109
n = numClassifiers
109110
p = Progress(n, 1) # minimum update interval: 1 second
110-
for i in processes
111+
for _ in processes
111112
# println(typeof(length(featureIndices)))
112113
classificationErrors = zeros(length(featureIndices))
113114
@@ -139,6 +140,8 @@ function learn(positiveIIs::AbstractArray, negativeIIs::AbstractArray, numClassi
139140
bestError = classificationErrors[minErrorIDX]
140141
bestFeatureIDX = featureIndices[minErrorIDX]
141142
143+
# println(classificationErrors)
144+
# println(weights)
142145
# set feature weight
143146
bestFeature = features[bestFeatureIDX]
144147
featureWeight = 0.5 * log((1 - bestError) / bestError)
@@ -173,6 +176,19 @@ end
173176

174177

175178
function _create_features(imgHeight::Int64, imgWidth::Int64, minFeatureWidth::Int64, maxFeatureWidth::Int64, minFeatureHeight::Int64, maxFeatureHeight::Int64)
179+
#=
180+
Iteratively creates the Haar-like feautures
181+
182+
parameter `imgHeight`: The height of the image [type: Integer]
183+
parameter `imgWidth`: The width of the image [type: Integer]
184+
parameter `minFeatureWidth`: The minimum width of the feature (used for computation efficiency purposes) [type: Integer]
185+
parameter `maxFeatureWidth`: The maximum width of the feature [type: Integer]
186+
parameter `minFeatureHeight`: The minimum height of the feature [type: Integer]
187+
parameter `maxFeatureHeight`: The maximum height of the feature [type: Integer]
188+
189+
return `features`: an array of Haar-like features found for an image [type: Abstract Array]
190+
=#
191+
176192
println("Creating Haar-like features...")
177193
# features = Array()
178194
features = []

src/HaarLikeFeature.jl

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ abstract type HaarFeatureType end
2323

2424
# make structure
2525
struct HaarLikeFeature <: HaarFeatureType#struct HaarLikeFeature{T} <: HaarObject where {T <: HaarFeatureType}
26+
#=
27+
Struct representing a Haar-like feature.
28+
=#
29+
2630
featureType::Tuple{Int64, Int64}
2731
position::Tuple{Int64, Int64}
2832
topLeft::Tuple{Int64, Int64}
@@ -32,6 +36,8 @@ struct HaarLikeFeature <: HaarFeatureType#struct HaarLikeFeature{T} <: HaarObjec
3236
threshold::Int64
3337
polarity::Int64
3438
weight::Number# number because it can be -Inf due to log(0)
39+
# weight::Int64
40+
# weight::Any
3541

3642
# constructor; equivalent of __init__ method within class # ::CartesianIndex
3743
function HaarLikeFeature(featureType::Tuple{Int64,Int64}, position::Tuple{Int64, Int64}, width::Int64, height::Int64, threshold::Int64, polarity::Int64)
@@ -108,13 +114,15 @@ end # end structure
108114
# end
109115

110116
function getScore(feature::HaarLikeFeature, intImg::Array)
111-
"""
117+
#=
112118
Get score for given integral image array.
113-
:param int_img: Integral image array
114-
:type int_img: numpy.ndarray
115-
:return: Score for given feature
116-
:rtype: float
117-
"""
119+
120+
parameter `feature`: given Haar-like feature (parameterised replacement of Python's `self`) [type: HaarLikeFeature]
121+
parameter `intImg`: Integral image array [type: Abstract Array]
122+
123+
return `score`: Score for given feature [type: Float]
124+
=#
125+
118126
score = 0
119127
120128
if feature.featureType == FeatureTypes[1] # two vertical
@@ -187,20 +195,25 @@ end
187195
188196
189197
function getVote(feature::HaarLikeFeature, intImg::AbstractArray)
190-
"""
198+
#=
191199
Get vote of this feature for given integral image.
192-
:param int_img: Integral image array
193-
:type int_img: numpy.ndarray
194-
:return: 1 iff this feature votes positively, otherwise -1
195-
:rtype: int
196-
"""
200+
201+
parameter `feature`: given Haar-like feature (parameterised replacement of Python's `self`) [type: HaarLikeFeature]
202+
parameter `intImg`: Integral image array [type: Abstract Array]
203+
204+
return:
205+
1 ⟺ this feature votes positively
206+
-1 otherwise
207+
[type: Integer]
208+
=#
209+
197210
score = getScore(feature, intImg)
198211

199212

200213
# return feature.weight * (1 if score < feature.polarity * feature.threshold else -1)
201214

202-
203215
return feature.weight * ((score < (feature.polarity * feature.threshold)) ? 1 : -1)
216+
# return 1 * ((score < (feature.polarity * feature.threshold)) ? 1 : -1)
204217
end
205218

206219

src/IntegralImage.jl

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@
44
"${BASH_SOURCE[0]}" "$@"
55
=#
66

7-
"""
8-
Any one pixel in a given image has the value that is the sum of all of the pixels above it and to the left.
7+
#=
8+
Rectangle features can be computed very rapidly using an intermediate representation for the image, which we call the integral image.
9+
The integral image at location $x,y$ contains the sum of the pixels above and to the left of $x,y$ inclusive.
910
Original Integral
1011
+-------- +------------
1112
| 1 2 3 . | 1 3 6 .
1213
| 4 5 6 . | 5 12 21 .
1314
| . . . . | . . . . .
14-
"""
15+
=#
1516

1617
function toIntegralImage(imgArr::AbstractArray)
17-
"""
18+
#=
19+
Calculates the integral image based on this instance's original image data.
20+
21+
parameter `imgArr`: Image source data [type: Abstract Array]
22+
23+
return `integralImageArr`: Integral image for given image [type: Abstract Array]
24+
1825
https://www.ipol.im/pub/art/2014/57/article_lr.pdf, p. 346
19-
"""
26+
=#
2027
2128
arrRows, arrCols = size(imgArr) # get size only once in case
2229
rowSum = zeros(arrRows, arrCols)
@@ -62,12 +69,15 @@ end
6269
6370
6471
function sumRegion(integralImageArr::AbstractArray, topLeft::Tuple{Int64,Int64}, bottomRight::Tuple{Int64,Int64})
65-
"""
66-
Calculates the sum in the rectangle specified by the given tuples
67-
topLeft: (x, y) of the rectangle's top left corner
68-
bottomRight: (x, y) of the rectangle's bottom right corner
69-
return The sum of all pixels in the given rectangle
70-
"""
72+
#=
73+
parameter `integralImageArr`: The intermediate Integral Image [type: Abstract Array]
74+
Calculates the sum in the rectangle specified by the given tuples:
75+
parameter `topLeft`: (x,y) of the rectangle's top left corner [type: Tuple]
76+
parameter `bottomRight`: (x,y) of the rectangle's bottom right corner [type: Tuple]
77+
78+
return: The sum of all pixels in the given rectangle defined by the parameters topLeft and bottomRight
79+
=#
80+
7181
# swap tuples
7282
# topLeft = [topLeft[2], topLeft[1]]
7383
topLeft = (topLeft[2], topLeft[1])

0 commit comments

Comments
 (0)