forked from sightmachine/SimpleCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVMClassifier.py
More file actions
463 lines (399 loc) · 16.9 KB
/
SVMClassifier.py
File metadata and controls
463 lines (399 loc) · 16.9 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
from SimpleCV.base import *
from SimpleCV.ImageClass import Image, ImageSet
from SimpleCV.DrawingLayer import *
from SimpleCV.Features import FeatureExtractorBase
"""
This class is encapsulates almost everything needed to train, test, and deploy a
multiclass support vector machine for an image classifier. Training data should
be stored in separate directories for each class. This class uses the feature
extractor base class to convert images into a feature vector. The basic workflow
is as follows.
1. Get data.
2. Setup Feature Extractors (roll your own or use the ones I have written).
3. Train the classifier.
4. Test the classifier.
5. Tweak parameters as necessary.
6. Repeat until you reach the desired accuracy.
7. Save the classifier.
8. Deploy using the classify method.
"""
class SVMClassifier:
"""
This class encapsulates a Naive Bayes Classifier.
See:
http://en.wikipedia.org/wiki/Support_vector_machine
"""
mClassNames = []
mDataSetRaw = []
mDataSetOrange = []
mClassifier = None
mFeatureExtractors = None
mOrangeDomain = None
mSVMPrototype = None
mKernelType = {}
mSVMType = {}
mSVMProperties = {
'KernelType':'RBF', #default is a RBF Kernel
'SVMType':'NU', #default is C
'nu':None, # NU for SVM NU
'c':None, #C for SVM C - the slack variable
'degree':None, #degree for poly kernels - defaults to 3
'coef':None, #coef for Poly/Sigmoid defaults to 0
'gamma':None, #kernel param for poly/rbf/sigma - default is 1/#samples
}
#human readable to CV constant property mapping
def __init__(self,featureExtractors,properties=None):
if not ORANGE_ENABLED:
logger.warning("The required orange machine learning library is not installed")
return None
self.mKernelType = {
'RBF':orange.SVMLearner.RBF, #Radial basis kernel
'Linear':orange.SVMLearner.Linear, #Linear basis kernel
'Poly':orange.SVMLearner.Polynomial, #Polynomial kernel
'Sigmoid':orange.SVMLearner.Sigmoid #Sigmoid Kernel
}
self.mSVMType = {
'NU':orange.SVMLearner.Nu_SVC,
'C':orange.SVMLearner.C_SVC
}
self.mFeatureExtractors = featureExtractors
if(properties is not None):
self.mSVMProperties = properties
self._parameterizeKernel()
self.mClassNames = []
self.mDataSetRaw = []
self.mDataSetOrange = []
self.mClassifier = None
self.mOrangeDomain = None
def setProperties(self, properties):
"""
Note that resetting the properties will reset the SVM and you will need
to retrain.
"""
if(properties is not None):
self.mSVMProperties = properties
self._parameterizeKernel()
def _parameterizeKernel(self):
#Set the parameters for our SVM
self.mSVMPrototype = orange.SVMLearner()
self.mSVMPrototype.svm_type = self.mSVMType[self.mSVMProperties["SVMType"]]
self.mSVMPrototype.kernel_type = self.mKernelType[self.mSVMProperties["KernelType"]]
if(self.mSVMProperties["nu"] is not None):
self.mSVMPrototype.nu = self.mSVMProperties["nu"]
if(self.mSVMProperties["c"] is not None):
self.mSVMPrototype.C = self.mSVMProperties["c"]
if(self.mSVMProperties["degree"] is not None):
self.mSVMPrototype.degree = self.mSVMProperties["degree"]
if(self.mSVMProperties["coef"] is not None):
self.mSVMPrototype.coef0 = self.mSVMProperties["coef"]
if(self.mSVMProperties["gamma"] is not None):
self.mSVMPrototype.gamma = self.mSVMProperties["gamma"]
def load(cls, fname):
"""
Load the classifier from file
"""
return pickle.load(file(fname, 'rb'))
load = classmethod(load)
def save(self, fname):
"""
Save the classifier to file
"""
output = open(fname, 'wb')
pickle.dump(self,output,2) # use two otherwise it w
output.close()
def __getstate__(self):
mydict = self.__dict__.copy()
self.mDataSetOrange = None
del mydict['mDataSetOrange']
self.mOrangeDomain = None
del mydict['mOrangeDomain']
self.mClassifier = None
del mydict['mClassifier']
return mydict
def __setstate__(self, mydict):
self.__dict__ = mydict
colNames = []
for extractor in self.mFeatureExtractors:
colNames.extend(extractor.getFieldNames())
self.mOrangeDomain = orange.Domain(map(orange.FloatVariable,colNames),orange.EnumVariable("type",values=self.mClassNames))
self.mDataSetOrange = orange.ExampleTable(self.mOrangeDomain,self.mDataSetRaw)
self.mClassifier = self.mSVMPrototype(self.mDataSetOrange)
def classify(self, image):
"""
Classify a single image. Takes in an image and returns the string
of the classification.
Make sure you haved loaded the feauture extractors and the training data.
"""
featureVector = []
for extractor in self.mFeatureExtractors: #get the features
feats = extractor.extract(image)
if( feats is not None ):
featureVector.extend(feats)
featureVector.extend([self.mClassNames[0]])
test = orange.ExampleTable(self.mOrangeDomain,[featureVector])
c = self.mClassifier(test[0]) #classify
return str(c) #return to class name
def setFeatureExtractors(self, extractors):
"""
Add a list of feature extractors to the classifier. These feature extractors
must match the ones used to train the classifier. If the classifier is already
trained then this method will require that you retrain the data.
"""
self.mFeatureExtractors = extractors
return None
def _trainPath(self,path,className,subset,disp,verbose):
count = 0
files = []
for ext in IMAGE_FORMATS:
files.extend(glob.glob( os.path.join(path, ext)))
if(subset > 0):
nfiles = min(subset,len(files))
else:
nfiles = len(files)
badFeat = False
for i in range(nfiles):
infile = files[i]
if verbose:
print "Opening file: " + infile
img = Image(infile)
featureVector = []
for extractor in self.mFeatureExtractors:
feats = extractor.extract(img)
if( feats is not None ):
featureVector.extend(feats)
else:
badFeat = True
if(badFeat):
badFeat = False
continue
featureVector.extend([className])
self.mDataSetRaw.append(featureVector)
text = 'Training: ' + className
self._WriteText(disp,img,text,Color.WHITE)
count = count + 1
del img
return count
def _trainImageSet(self,imageset,className,subset,disp,verbose):
count = 0
badFeat = False
if (subset>0):
imageset = imageset[0:subset]
for img in imageset:
if verbose:
print "Opening file: " + img.filename
featureVector = []
for extractor in self.mFeatureExtractors:
feats = extractor.extract(img)
if( feats is not None ):
featureVector.extend(feats)
else:
badFeat = True
if(badFeat):
badFeat = False
continue
featureVector.extend([className])
self.mDataSetRaw.append(featureVector)
text = 'Training: ' + className
self._WriteText(disp,img,text,Color.WHITE)
count = count + 1
del img
return count
def train(self,images,classNames,disp=None,subset=-1,savedata=None,verbose=True):
"""
Train the classifier.
images paramater can take in a list of paths or a list of imagesets
images - the order of the paths or imagesets must be in the same order as the class type
- Note all image classes must be in seperate directories
- The class names must also align to the directories
disp - if display is a display we show images and class label,
otherwise nothing is done.
subset - if subset = -1 we use the whole dataset. If subset = # then we
use min(#images,subset)
savedata - if save data is None nothing is saved. If savedata is a file
name we save the data to a tab delimited file.
verbose - print confusion matrix and file names
returns [%Correct %Incorrect Confusion_Matrix]
"""
count = 0
self.mClassNames = classNames
# fore each class, get all of the data in the path and train
for i in range(len(classNames)):
if ( isinstance(images[i], str) ):
count = count + self._trainPath(images[i],classNames[i],subset,disp,verbose)
else:
count = count + self._trainImageSet(images[i],classNames[i],subset,disp,verbose)
colNames = []
for extractor in self.mFeatureExtractors:
colNames.extend(extractor.getFieldNames())
if(count <= 0):
logger.warning("No features extracted - bailing")
return None
# push our data into an orange example table
self.mOrangeDomain = orange.Domain(map(orange.FloatVariable,colNames),orange.EnumVariable("type",values=self.mClassNames))
self.mDataSetOrange = orange.ExampleTable(self.mOrangeDomain,self.mDataSetRaw)
if(savedata is not None):
orange.saveTabDelimited (savedata, self.mDataSetOrange)
self.mClassifier = self.mSVMPrototype(self.mDataSetOrange)
correct = 0
incorrect = 0
for i in range(count):
c = self.mClassifier(self.mDataSetOrange[i])
test = self.mDataSetOrange[i].getclass()
if verbose:
print "original", test, "classified as", c
if(test==c):
correct = correct + 1
else:
incorrect = incorrect + 1
good = 100*(float(correct)/float(count))
bad = 100*(float(incorrect)/float(count))
confusion = 0
if( len(self.mClassNames) > 2 ):
crossValidator = orngTest.learnAndTestOnLearnData([self.mSVMPrototype],self.mDataSetOrange)
confusion = orngStat.confusionMatrices(crossValidator)[0]
if verbose:
print("Correct: "+str(good))
print("Incorrect: "+str(bad))
classes = self.mDataSetOrange.domain.classVar.values
print confusion
#print "\t"+"\t".join(classes)
#for className, classConfusions in zip(classes, confusion):
# print ("%s" + ("\t%i" * len(classes))) % ((className, ) + tuple(classConfusions))
return [good, bad, confusion]
def test(self,images,classNames,disp=None,subset=-1,savedata=None,verbose=True):
"""
Test the classifier.
images paramater can take in a list of paths or a list of imagesets
images - the order of the paths or imagesets must be in the same order as the class type
- Note all image classes must be in seperate directories
- The class names must also align to the directories
disp - if display is a display we show images and class label,
otherwise nothing is done.
subset - if subset = -1 we use the whole dataset. If subset = # then we
use min(#images,subset)
savedata - if save data is None nothing is saved. If savedata is a file
name we save the data to a tab delimited file.
verbose - print confusion matrix and file names
returns [%Correct %Incorrect Confusion_Matrix]
"""
count = 0
correct = 0
self.mClassNames = classNames
colNames = []
for extractor in self.mFeatureExtractors:
colNames.extend(extractor.getFieldNames())
self.mOrangeDomain = orange.Domain(map(orange.FloatVariable,colNames),orange.EnumVariable("type",values=self.mClassNames))
dataset = []
for i in range(len(classNames)):
if ( isinstance(images[i],str) ):
[dataset,cnt,crct] =self._testPath(images[i],classNames[i],dataset,subset,disp,verbose)
count = count + cnt
correct = correct + crct
else:
[dataset,cnt,crct] =self._testImageSet(images[i],classNames[i],dataset,subset,disp,verbose)
count = count + cnt
correct = correct + crct
testData = orange.ExampleTable(self.mOrangeDomain,dataset)
if savedata is not None:
orange.saveTabDelimited (savedata, testdata)
confusion = 0
if( len(self.mClassNames) > 2 ):
crossValidator = orngTest.learnAndTestOnTestData([self.mSVMPrototype],self.mDataSetOrange,testData)
confusion = orngStat.confusionMatrices(crossValidator)[0]
good = 100*(float(correct)/float(count))
bad = 100*(float(count-correct)/float(count))
if verbose:
print("Correct: "+str(good))
print("Incorrect: "+str(bad))
classes = self.mDataSetOrange.domain.classVar.values
print "\t"+"\t".join(classes)
for className, classConfusions in zip(classes, confusion):
print ("%s" + ("\t%i" * len(classes))) % ((className, ) + tuple(classConfusions))
return [good, bad, confusion]
def _testPath(self,path,className,dataset,subset,disp,verbose):
count = 0
correct = 0
badFeat = False
files = []
for ext in IMAGE_FORMATS:
files.extend(glob.glob( os.path.join(path, ext)))
if(subset > 0):
nfiles = min(subset,len(files))
else:
nfiles = len(files)
for i in range(nfiles):
infile = files[i]
if verbose:
print "Opening file: " + infile
img = Image(infile)
featureVector = []
for extractor in self.mFeatureExtractors:
feats = extractor.extract(img)
if( feats is not None ):
featureVector.extend(feats)
else:
badFeat = True
if( badFeat ):
del img
badFeat = False
continue
featureVector.extend([className])
dataset.append(featureVector)
test = orange.ExampleTable(self.mOrangeDomain,[featureVector])
c = self.mClassifier(test[0])
testClass = test[0].getclass()
if(testClass==c):
text = "Classified as " + str(c)
self._WriteText(disp,img,text, Color.GREEN)
correct = correct + 1
else:
text = "Mislassified as " + str(c)
self._WriteText(disp,img,text, Color.RED)
count = count + 1
del img
return([dataset,count,correct])
def _testImageSet(self,imageset,className,dataset,subset,disp,verbose):
count = 0
correct = 0
badFeat = False
if(subset > 0):
imageset = imageset[0:subset]
for img in imageset:
if verbose:
print "Opening file: " + img.filename
featureVector = []
for extractor in self.mFeatureExtractors:
feats = extractor.extract(img)
if( feats is not None ):
featureVector.extend(feats)
else:
badFeat = True
if( badFeat ):
del img
badFeat = False
continue
featureVector.extend([className])
dataset.append(featureVector)
test = orange.ExampleTable(self.mOrangeDomain,[featureVector])
c = self.mClassifier(test[0])
testClass = test[0].getclass()
if(testClass==c):
text = "Classified as " + str(c)
self._WriteText(disp,img,text, Color.GREEN)
correct = correct + 1
else:
text = "Mislassified as " + str(c)
self._WriteText(disp,img,text, Color.RED)
count = count + 1
del img
return([dataset,count,correct])
def _WriteText(self, disp, img, txt,color):
if(disp is not None):
txt = ' ' + txt + ' '
img = img.adaptiveScale(disp.resolution)
layer = DrawingLayer((img.width,img.height))
layer.setFontSize(60)
layer.ezViewText(txt,(20,20),fgcolor=color)
img.addDrawingLayer(layer)
img.applyLayers()
img.save(disp)