-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_classifier.py
63 lines (50 loc) · 1.87 KB
/
test_classifier.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
from imfractal import *
import Image
import time
import csv
import sys
import os
from subprocess import *
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn import cross_validation
def do_test():
cant = 10
dDFs = 20
cantClasses = 2
breadtrain = np.zeros((cant, dDFs)).astype(np.float32)
breadtest = np.zeros((cant, dDFs)).astype(np.float32)
nonbreadtrain = np.zeros((cant, dDFs)).astype(np.float32)
nonbreadtest = np.zeros((cant, dDFs)).astype(np.float32)
pathbtr = 'images/train/bread/'
dirListbtr=os.listdir(pathbtr)
pathnbtr = 'images/train/nonbread/'
dirListnbtr=os.listdir(pathnbtr)
pathbte = 'images/test/bread/'
dirListbte=os.listdir(pathbte)
pathnbte = 'images/test/nonbread/'
dirListnbte=os.listdir(pathnbte)
#print len(dirListbtr), dirListbtr
ins = MFS()
print 'Training: calculating MFS for the bread database...'
for i in range(cant):
ins.setDef(1,20,3,True)
filename = pathbtr+dirListbtr[i]
breadtrain[i] = ins.getFDs(filename)
filename = pathbte+dirListbte[i]
breadtest[i] = ins.getFDs(filename)
filename = pathnbtr+dirListnbtr[i]
nonbreadtrain[i] = ins.getFDs(filename)
filename = pathnbte+dirListnbte[i]
nonbreadtest[i] = ins.getFDs(filename)
cfr = RandomForestClassifier(n_estimators=100)
data = np.vstack((breadtrain,breadtest,nonbreadtrain,nonbreadtest))
labels = np.zeros((len(data),1)) # FIX ME
for i in range(len(data)):
labels[i] = i
labels = map(lambda i: np.floor(i/(2*(cant)))+1, labels)
labels = np.array(labels)
labels = np.transpose(labels)[0] # FIX ME
print "Testing..."
scores = cross_validation.cross_val_score(cfr, data, labels, cv=4)
print "Classification performance (Random Forest classifier): " + str( np.array(scores).mean() )