-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_classification.py
173 lines (156 loc) · 5.29 KB
/
image_classification.py
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
from perceptron import PerceptronNetwork
from bayes import NaiveBayes
import time
# statistics code from https://stackoverflow.com/questions/15389768/standard-deviation-of-a-list
def mean(data):
"""Return the sample arithmetic mean of data."""
n = len(data)
if n < 1:
raise ValueError('mean requires at least one data point')
return sum(data)/n # in Python 2 use sum(data)/float(n)
def _ss(data):
"""Return sum of square deviations of sequence data."""
c = mean(data)
ss = sum((x-c)**2 for x in data)
return ss
def stddev(data, ddof=0):
"""Calculates the population standard deviation
by default; specify ddof=1 to compute the sample
standard deviation."""
n = len(data)
if n < 2:
raise ValueError('variance requires at least two data points')
ss = _ss(data)
pvar = ss/(n-ddof)
return pvar**0.5
percents = list(range(1, 11))
# ----- DIGITS ----- #
print "########## DIGITS ##########"
digitWidth = 28
digitHeight = 28
digitY = list(range(0, 10))
# paths
digitTrainingImagesPath = "data/digitdata/trainingimages"
digitTrainingLabelsPath = "data/digitdata/traininglabels"
digitTestImagesPath = "data/digitdata/testimages"
digitTestLabelsPath = "data/digitdata/testlabels"
print "---------- TEST ----------"
# perceptron classification
print "---------- Perceptron ----------"
digitPercepAvgs = []
digitPercepStds = []
digitPercepTimes = []
for percent in percents:
p = percent/10.0
x = 5
res = []
times = []
for i in range(0, 5):
digitPercep = PerceptronNetwork(digitWidth*digitHeight, digitY)
t1 = time.time()
digitPercep.train(digitWidth, digitHeight, digitTrainingImagesPath, digitTrainingLabelsPath, p)
dt = time.time() - t1
percentageCorrect = digitPercep.test(digitWidth, digitHeight, digitTestImagesPath, digitTestLabelsPath)
res.append(percentageCorrect)
times.append(dt)
avgTime = mean(times)
avgAcc = mean(res)
stdAcc = stddev(res)
digitPercepTimes.append(avgTime)
digitPercepStds.append(stdAcc)
digitPercepAvgs.append(avgAcc)
print "times: {}".format(digitPercepTimes)
print "means: {}".format(digitPercepAvgs)
print "stds: {}".format(digitPercepStds)
# naive bayes classification
#print "---------- Naive Bayes ----------"
digitBayesAvgs = []
digitBayesStds = []
digitBayesTimes = []
for percent in percents:
p = percent/10.0
x = 5
res = []
times = []
for i in range(0, 5):
digitBayes = NaiveBayes(digitWidth*digitHeight, 10, 2)
t1 = time.time()
digitBayes.train(digitWidth, digitHeight, digitTrainingImagesPath, digitTrainingLabelsPath, p)
dt = time.time() - t1
percentageCorrect = digitBayes.test(digitWidth, digitHeight, digitTestImagesPath, digitTestLabelsPath)
res.append(percentageCorrect)
times.append(dt)
avgTime = mean(times)
avgAcc = mean(res)
stdAcc = stddev(res)
digitBayesAvgs.append(avgAcc)
digitBayesStds.append(stdAcc)
digitBayesTimes.append(avgTime)
print "times: {}".format(digitBayesTimes)
print "means: {}".format(digitBayesAvgs)
print "stds: {}".format(digitBayesStds)
# ----- FACES ----- #
print "########## FACES ##########"
faceWidth = 60
faceHeight = 70
faceY = [0, 1]
# paths
faceTrainingImagesPath = "data/facedata/facedatatrain"
faceTrainingLabelsPath = "data/facedata/facedatatrainlabels"
faceTestImagesPath = "data/facedata/facedatatest"
faceTestLabelsPath = "data/facedata/facedatatestlabels"
print "---------- TEST ----------"
# perceptron classification
print "---------- Perceptron ----------"
facePercepAvgs = []
facePercepStds = []
facePercepTimes = []
for percent in percents:
p = percent/10.0
x = 5
res = []
times = []
for i in range(0, 5):
facePercep = PerceptronNetwork(faceWidth*faceHeight, faceY)
t1 = time.time()
facePercep.train(faceWidth, faceHeight, faceTrainingImagesPath, faceTrainingLabelsPath, p)
dt = time.time() - t1
percentageCorrect = facePercep.test(faceWidth, faceHeight, faceTestImagesPath, faceTestLabelsPath)
res.append(percentageCorrect)
times.append(dt)
avgTime = mean(times)
avgAcc = mean(res)
stdAcc = stddev(res)
facePercepTimes.append(avgTime)
facePercepStds.append(stdAcc)
facePercepAvgs.append(avgAcc)
print "times: {}".format(facePercepTimes)
print "means: {}".format(facePercepAvgs)
print "stds: {}".format(facePercepStds)
# naive bayes classification
print "---------- Naive Bayes ----------"
faceBayesAvgs = []
faceBayesStds = []
faceBayesTimes = []
for percent in percents:
p = percent/10.0
x = 5
res = []
times = []
for i in range(0, 5):
faceBayes = NaiveBayes(faceWidth*faceHeight, 2, 2)
t1 = time.time()
faceBayes.train(faceWidth, faceHeight, faceTrainingImagesPath, faceTrainingLabelsPath, p)
dt = time.time() - t1
percentageCorrect = faceBayes.test(faceWidth, faceHeight, faceTestImagesPath, faceTestLabelsPath)
res.append(percentageCorrect)
times.append(dt)
avgTime = mean(times)
avgAcc = mean(res)
stdAcc = stddev(res)
faceBayesAvgs.append(avgAcc)
faceBayesStds.append(stdAcc)
faceBayesTimes.append(avgTime)
print "times: {}".format(faceBayesTimes)
print "means: {}".format(faceBayesAvgs)
print "stds: {}".format(faceBayesStds)