-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
166 lines (131 loc) · 4.68 KB
/
test.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
"""Library test."""
# import matplotlib
import matplotlib.pyplot as plt
# from matplotlib import cm
import numpy as np
# from skimage.io import imshow
from pickle import dump, load
# from library.utils import medial_axis_skeleton
# from library.utils import skeleton_lines
from sklearn.multiclass import OneVsRestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVC
from sklearn import cross_validation
from sklearn.preprocessing import scale
from sklearn.neighbors import KNeighborsClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.metrics import confusion_matrix
from library.data_utils import load_database
from library.feature_extraction import extract_features
# from library.feature_extraction import preprocess_image
def custom_test(clf, X, y):
score = 0.0
proba = clf.predict_proba(X)
for i, sample_proba in enumerate(proba):
classes = np.argsort(sample_proba)[::-1][:10]
labels = clf.classes_[classes]
if y[i] in labels:
score += 1.0
print(score)
return score / proba.shape[0]
# Util for plotting the confusion matrix
# from http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
def plot_confusion_matrix(cm, target_names, title='Confusion matrix', cmap=plt.cm.Blues):
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
if __name__ == '__main__':
images, labels = load_database("database", "classes.csv")
# Tests for features extraction pipeline
X = []
# # print("FEATURE EXTRACTION....")
# for i, image in enumerate(images):
# X.append(extract_features(image))
# # -------------
# # Machine learning pasrt
X = np.array(X)
y = np.array(labels)
# dump(X, open("X", "wb"))
# dump(y, open("y", "wb"))
X = load(open("X", "rb"))
y = load(open("y", "rb"))
# Scale features
X = scale(X)
print("DATA: %d x %d" % X.shape)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42)
print("\nKNeighborsClassifier (5 neighbors):")
print("+++++++++++++++++++++++++++++++++++")
clf = KNeighborsClassifier(n_neighbors=5, n_jobs=4)
print("TRAINING....")
# clf.fit(X_train, y_train)
print("SCORE:")
score = cross_validation.cross_val_score(
clf, X, y, cv=5)
print(sum(score) / float(len(score)))
clf.fit(X_train, y_train)
print("TEACHERS TEST:")
print(custom_test(clf, X_test, y_test))
print("\nKNeighborsClassifier (20 neighbors):")
print("+++++++++++++++++++++++++++++++++++")
clf = KNeighborsClassifier(n_neighbors=20, n_jobs=4)
print("TRAINING....")
# clf.fit(X_train, y_train)
print("SCORE:")
score = cross_validation.cross_val_score(
clf, X, y, cv=5)
print(sum(score) / float(len(score)))
clf.fit(X_train, y_train)
print("TEACHERS TEST:")
print(custom_test(clf, X_test, y_test))
print("\nNaive Bayes:")
print("+++++++++++++")
clf = OneVsRestClassifier(GaussianNB(), n_jobs=4)
print("TRAINING....")
print("SCORE:")
score = cross_validation.cross_val_score(
clf, X, y, cv=5)
print(sum(score) / float(len(score)))
clf.fit(X_train, y_train)
print("TEACHERS TEST:")
print(custom_test(clf, X_test, y_test))
print("\nLinear SVC:")
print("++++++++++++")
C = 1
clf = OneVsRestClassifier(
SVC(kernel='linear', C=C, probability=True), n_jobs=4)
print("SCORE:")
score = cross_validation.cross_val_score(
clf, X, y, cv=5)
print(sum(score) / float(len(score)))
clf.fit(X_train, y_train)
print("TEACHERS TEST:")
print(custom_test(clf, X_test, y_test))
print("\n\nSVC RBF kernel:")
print("++++++++++++")
gamma = 0.1
C = 10
clf = OneVsRestClassifier(
SVC(kernel='rbf', C=C, gamma=gamma, probability=True), n_jobs=4)
print("SCORE:")
score = cross_validation.cross_val_score(
clf, X, y, cv=5)
print(sum(score) / float(len(score)))
clf.fit(X_train, y_train)
print("TEACHERS TEST:")
print(custom_test(clf, X_test, y_test))
# Here for the selected model we plot confusion matrix
y_pred = clf.predict(X_test)
target_names = np.unique(labels)
cm = confusion_matrix(y_test, y_pred, labels=target_names)
np.set_printoptions(precision=2)
plt.figure()
plot_confusion_matrix(cm, target_names)
plt.show()