-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfusion.py
More file actions
108 lines (79 loc) · 2.72 KB
/
Copy pathfusion.py
File metadata and controls
108 lines (79 loc) · 2.72 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
import numpy as np
from feature_extraction import extraction
from classification import classifier
from sklearn import preprocessing
from sklearn.metrics import confusion_matrix
if __name__=='__main__':
# fusions_list = ['borda', 'sum', 'max', 'min', 'product']
fusions_list = ['max']
for fusion in fusions_list:
# features_list = ['color_histogram']
# classifiers = ['svm']
# features_list = ['color_histogram', 'hu_moments', 'histogram_of_oriented_gradients']
# classifiers = ['svm', 'lda', 'knn']
features_list = ['color_histogram', 'hu_moments']
classifiers = ['svm', 'lda', 'knn']
preds = []
pred_probs = []
for descriptor in features_list :
X_train = []
y_train = []
X_test = []
y_test = []
X_train, y_train = extraction ("data/Train/", descriptor)
X_test, y_test = extraction ("data/Valid/", descriptor)
scaler = preprocessing.MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
X_train = np.asarray(X_train)
X_test = np.asarray(X_test)
for method in classifiers:
clf = classifier (method, X_train, y_train)
y_pred = clf.predict(X_test)
y_predProb = clf.predict_proba(X_test)
preds.append(y_pred)
pred_probs.append(y_predProb)
if fusion == 'sum' :
matrix = np.zeros(pred_probs[0].shape)
for pred_prob in pred_probs:
matrix = matrix + pred_prob
y_pred = np.argmax(matrix, axis=1)
if fusion == 'max' :
matrix = np.zeros(pred_probs[0].shape)
for pred_prob in pred_probs:
for i in xrange(0,len(pred_prob)):
for j in xrange(0,len(pred_prob[i])):
if (pred_prob[i][j] > matrix [i][j]):
matrix[i][j] = pred_prob[i][j]
y_pred = np.argmax(matrix, axis=1)
if fusion == 'min' :
matrix = np.ones(pred_probs[0].shape)
for pred_prob in pred_probs:
for i in xrange(0,len(pred_prob)):
for j in xrange(0,len(pred_prob[i])):
if (pred_prob[i][j] < matrix [i][j]):
matrix[i][j] = pred_prob[i][j]
y_pred = np.argmax(matrix, axis=1)
if fusion == 'product' :
matrix = np.ones(pred_probs[0].shape)
for pred_prob in pred_probs:
matrix = np.multiply(matrix, pred_prob)
y_pred = np.argmax(matrix, axis=1)
if fusion == 'borda' :
matrix = np.zeros(pred_probs[0].shape)
for pred_prob in pred_probs:
for i in xrange(0,len(pred_prob)):
line = pred_prob[i]
temp = line.argsort()
ranks = np.empty(len(line), int)
ranks[temp] = np.arange(len(line))
matrix[i] = matrix[i] + ranks
y_pred = np.argmax(matrix, axis=1)
print (fusion)
size = len(y_test)
score = 0
for x in xrange(0, size):
score += (y_test[x] == y_pred[x])
print ("Score:", float(score)/float(size))
cm = confusion_matrix(y_test, y_pred)
print cm