-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify.py
255 lines (215 loc) · 7.93 KB
/
identify.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from pybrain.datasets import ClassificationDataSet
from pybrain.utilities import percentError
from pybrain.tools.shortcuts import buildNetwork
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.structure.modules import SoftmaxLayer
from pybrain.tools.customxml.networkwriter import NetworkWriter
from pybrain.tools.customxml.networkreader import NetworkReader
import os
from pylab import ion, ioff, figure, draw, contourf, clf, show, hold, plot
from scipy import diag, arange, meshgrid, where
from numpy.random import multivariate_normal
from sklearn import datasets
import numpy as np
import cv2
import pylab
import eigenfaces
# import config
class Recognize:
def __init__(self):
self.d = {
"train_numbers.xml": {
'hidden_dim': 32,
'nb_classes': 10,
'in_dim': 64,
'train_func': self.train_digits,
'identify_func': self.identify_digits,
},
"net_test.xml": {
'hidden_dim': 150,
'nb_classes': 40,
'in_dim': 300,
'train_func': self.train_images,
'identify_func': self.identify3,
'split_percent': .80,
'faces_per_person': 10,
},
"net_color_caltech.xml": {
'hidden_dim': 150,
'nb_classes': 19,
'in_dim': 142,
'train_func': self.train_images,
'identify_func': self.identify3,
'split_percent': .80,
'faces_per_person': 10,
},
"net_color_caltech_eq.xml": {
'hidden_dim': 150,
'nb_classes': 19,
'in_dim': 142,
'train_func': self.train_images,
'identify_func': self.identify3,
'split_percent': .80,
'faces_per_person': 10,
},
"net.xml": {
'hidden_dim': 106,
'nb_classes': 40,
'in_dim': 10304,
'train_func': self.train_images,
'identify_func': self.identify1,
},
"net_sklearn.xml": {
'hidden_dim': 64,
'nb_classes': 40,
'in_dim': 4096,
'train_func': self.train_images2,
'identify_func': self.identify2,
},
}
self.trained = False
# When changing from net_color_caltech to net_color_caltech_eq change line 42 in eigenfaces.py
self.path = "net_color_caltech.xml"
self.x = None
self.all_data = self.classify()
self.net = self.buildNet()
self.trainer = self.train()
# This call returns a network that has 10304 inputs, 64 hidden and 1 output neurons.
# In PyBrain, these layers are Module objects and they are already connected with FullConnection objects.
def buildNet(self):
print "Building a network..."
if os.path.isfile(self.path):
self.trained = True
return NetworkReader.readFrom(self.path)
else:
return buildNetwork(self.all_data.indim, self.d[self.path]['hidden_dim'], self.all_data.outdim, outclass=SoftmaxLayer)
def get_max_index(self, l):
max_index, max_value = max(enumerate(l), key=lambda x: x[1])
return max_index
def classify(self):
print "self.d[self.path]['in_dim'] = ", self.d[self.path]['in_dim']
self.all_data = ClassificationDataSet(self.d[self.path]['in_dim'], target=1, nb_classes=self.d[self.path]['nb_classes'])
self.train_data = ClassificationDataSet(self.d[self.path]['in_dim'], target=1, nb_classes=self.d[self.path]['nb_classes'])
self.test_data = ClassificationDataSet(self.d[self.path]['in_dim'], target=1, nb_classes=self.d[self.path]['nb_classes'])
# add data to self.all_data dataset
self.d[self.path]['train_func']()
# turns 1 => [0,1,...]
self.all_data._convertToOneOfMany()
self.train_data._convertToOneOfMany()
self.test_data._convertToOneOfMany()
print "self.all_data.outdim = ", self.all_data.outdim
print "Input and output dimensions: ", self.all_data.indim, self.all_data.outdim
print "Number of training patterns: ", len(self.all_data)
print "Input and output dimensions: ", self.all_data.indim, self.all_data.outdim
return self.all_data
def identify_digits(self, i):
for image, label in self.images_and_labels:
if label == i:
l = self.net.activate(np.ravel(image))
max_index, max_value = max(enumerate(l), key=lambda x: x[1])
print str(i)+"\t"+str(max_index)+"\t"+str(i == max_index)
def identify(self, i):
self.d[self.path]['identify_func'](i)
# For original Att data
def identify1(self, i):
print "Identifying Image 1"
for num in range(1,11):
img = cv2.imread('faces/s'+str(i)+'/'+str(num)+'.pgm', 0)
l = self.net.activate(np.ravel(img))
max_index, max_value = max(enumerate(l), key=lambda x: x[1])
print str(i)+" "+str(max_index), i == max_index
print l
# Handwritten Digits
def identify2(self, i):
for m in range(1,11):
l = self.net.activate(np.ravel(self.x.data[i * 10 + m]))
max_index, max_value = max(enumerate(l), key=lambda x: x[1])
print str(i)+" "+str(max_index), i == max_index
# For caltech images
def identify3(self, m):
# idx = 1
# fig = figure()
for i in range(len(self.test_data['input'])):
img = self.test_data['input'][i]
label = self.test_data['target'][i]
l = self.net.activate(img)
result = self.get_max_index(l)
label = self.get_max_index(label)
print str(label)+"\t"+str(result)+"\t"+ str(int(label) == int(result))
# if i == 0 or i == 2 or i == 4:
# print 'labeled_faces/'+format(int(label)+1,'02')+'_08.jpg'
# print 'labeled_faces/'+format(int(result)+1,'02')+'_08.jpg'
# img_label = pylab.imread('labeled_faces/'+format(int(label)+1,'02')+'_08.jpg')
# img_resut = pylab.imread('labeled_faces/'+format(int(result)+1,'02')+'_08.jpg')
# if idx == 2:
# pylab.title('Results for the Neural Network')
# fig.add_subplot(3, 2, idx)
# pylab.imshow(img_label)
# fig.add_subplot(3, 2, idx+1)
# pylab.imshow(img_resut)
# idx = idx + 2
# show()
def train(self):
print "Enter the number of times to train, -1 means train until convergence:"
t = int(raw_input())
print "Training the Neural Net"
print "self.net.indim = "+str(self.net.indim)
print "self.train_data.indim = "+str(self.train_data.indim)
trainer = BackpropTrainer(self.net, dataset=self.train_data, momentum=0.1, verbose=True, weightdecay=0.01)
if t == -1:
trainer.trainUntilConvergence()
else:
for i in range(t):
trainer.trainEpochs(1)
trnresult = percentError( trainer.testOnClassData(), self.train_data['class'])
# print self.test_data
tstresult = percentError( trainer.testOnClassData(dataset=self.test_data), self.test_data['class'] )
print "epoch: %4d" % trainer.totalepochs, \
" train error: %5.2f%%" % trnresult, \
" test error: %5.2f%%" % tstresult
if i % 10 == 0 and i > 1:
print "Saving Progress... Writing to a file"
NetworkWriter.writeToFile(self.net, self.path)
print "Done training... Writing to a file"
NetworkWriter.writeToFile(self.net, self.path)
return trainer
def train_digits(self):
x = datasets.load_digits()
self.images_and_labels = list(zip(x.images, x.target))
for image, label in self.images_and_labels:
self.all_data.addSample(np.ravel(image), label)
def train_images(self):
# Call eigenfaces here
train_loc, self.train_class = eigenfaces.read_csv()
self.omega, train_array, u, u_reduced = eigenfaces.read_train_images(train_loc, self.train_class)
# 0 < split_percent < 1
# 0 < test_number < 10
test_number = int(self.d[self.path]['split_percent'] * self.d[self.path]['faces_per_person'])
print "test_number", test_number
for i in range(len(self.omega)):
img = self.omega[i]
label = self.train_class[i]
# add data
self.all_data.addSample(img, int(label)-1)
if (i % 10) >= test_number:
self.test_data.addSample(img, int(label) - 1)
else:
self.train_data.addSample(img, int(label) - 1)
print "size of test data", len(self.test_data['input'])
print "size of train data", len(self.train_data['input'])
def train_images2(self):
self.x = datasets.fetch_olivetti_faces()
for i in range(41):
self.all_data.addSample(self.x.data[i], self.x.target[i])
if __name__ == "__main__":
m = Recognize()
m.identify(1)
m.identify(2)
m.identify(3)
m.identify(4)
m.identify(5)
m.identify(6)
m.identify(7)
m.identify(8)
m.identify(9)
m.identify(0)