-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrainer.py
405 lines (343 loc) · 18.4 KB
/
Trainer.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
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
from sklearn.model_selection import train_test_split
import numpy as np
from models.LogisticRegressor import LogisticRegressor
from models.RandomForest import RandomForest
from models.SVMClassifier import SVMClassifier
from models.FisherDiscriminant import FisherDiscriminant
from models.RBF import RBFClassifier
from models.MLP import MLP
import copy
from Metrics import Metrics
import matplotlib.pyplot as plt
import os
class Trainer():
def __init__(self, cross_validation, data, classifier_type, train_size, classifier):
self.cross_validation = cross_validation
self.data = data
self.classifier_type = classifier_type
self.train_size = train_size
self.classifier = classifier
def _split_data(self):
"""
Function that split the dataset into training and testing set
:arg
self (Trainer): instance of the class
:return
train_image_all (numpy array): Numpy array containing train images
test_image_all (numpy array): Numpy array containing test images
train_labels_all (numpy array): Numpy array containing train target values including No Finding as a pathology
test_labels_all (numpy array): Numpy array containing test target values including No Finding as a pathology
train_labels_bool (numpy array): Numpy array containing train targets of sick people
test_labels_bool (numpy array): Numpy array containing test targets of sick people
train_image_sick (numpy array): Numpy array containing train targets excluding No Finding patients
train_labels_sick (numpy array): Numpy array containing test targets excluding No Finding patients
"""
image_all, labels_all = self.data.get_all_data()
_, labels_bool = self.data.get_sick_bool_data()
random_seed = 10
train_image_all, test_image_all, train_labels_all, test_labels_all = train_test_split(image_all, labels_all,
train_size=self.train_size,
random_state=random_seed)
if self.classifier_type == 2:
_, _, train_labels_bool, test_labels_bool = train_test_split(image_all, labels_bool,
train_size=self.train_size,
random_state=random_seed)
train_image_sick = train_image_all[train_labels_bool == 1]
train_labels_sick = train_labels_all[train_labels_bool == 1]
train_labels_sick = np.delete(train_labels_sick, 5, axis=1)
return train_image_all, test_image_all, train_labels_all, test_labels_all, \
train_labels_bool, test_labels_bool, train_image_sick, train_labels_sick
else:
return train_image_all, test_image_all, train_labels_all, test_labels_all
def classifier_selection(self):
"""
Function that instanciates classifiers
:arg
self (Trainer): instance of the class
:return
model (Classifier): Selected model when self.classfier is 1
model1 (Classifier): First selected model when self.classfier is 2
model2 (Classifier): Second selected model when self.classfier is 2
classifier_list (list): List with selected classifier names
"""
if self.classifier == 'SVM':
classifier_list = ['SVM']
if self.classifier_type == 1:
model = SVMClassifier(cv=self.cross_validation)
elif self.classifier_type == 2:
model1 = SVMClassifier(cv=self.cross_validation)
model2 = SVMClassifier(cv=self.cross_validation)
elif self.classifier == 'LogisticRegressor':
classifier_list = ['LogisticRegressor']
model = LogisticRegressor()
if self.classifier_type == 1:
model = LogisticRegressor(cv=self.cross_validation)
elif self.classifier_type == 2:
model1 = LogisticRegressor(cv=self.cross_validation)
model2 = LogisticRegressor(cv=self.cross_validation)
elif self.classifier == 'MLP':
classifier_list = ['MLP']
if self.classifier_type == 1:
model = MLP(cv=self.cross_validation)
elif self.classifier_type == 2:
model1 = MLP(cv=self.cross_validation)
model2 = MLP(cv=self.cross_validation)
elif self.classifier == 'RandomForest':
classifier_list = ['RandomForest']
model = RandomForest()
if self.classifier_type == 1:
model = RandomForest(cv=self.cross_validation)
elif self.classifier_type == 2:
model1 = RandomForest(cv=self.cross_validation)
model2 = RandomForest(cv=self.cross_validation)
elif self.classifier == 'RBF':
classifier_list = ['RBF']
if self.classifier_type == 1:
model = RBFClassifier(cv=self.cross_validation)
elif self.classifier_type == 2:
model1 = RBFClassifier(cv=self.cross_validation)
model2 = RBFClassifier(cv=self.cross_validation)
elif self.classifier == 'Fisher':
classifier_list = ['Fisher']
if self.classifier_type == 1:
model = FisherDiscriminant(cv=self.cross_validation)
elif self.classifier_type == 2:
model1 = FisherDiscriminant(cv=self.cross_validation)
model2 = FisherDiscriminant(cv=self.cross_validation)
elif self.classifier == 'all':
classifier_list = ['SVM', 'MLP', 'LogisticRegressor', 'RandomForest', 'RBF', 'Fischer']
if self.classifier_type == 1:
model_SVM = SVMClassifier(cv=self.cross_validation)
model_MLP = MLP(cv=self.cross_validation)
model_Logit = LogisticRegressor(cv=self.cross_validation)
model_Forest = RandomForest(cv=self.cross_validation)
model_RBF = RBFClassifier(cv=self.cross_validation)
model_Fischer = FisherDiscriminant(cv=self.cross_validation)
model = [model_SVM, model_MLP, model_Logit, model_Forest, model_RBF, model_Fischer]
elif self.classifier_type == 2:
model_SVM = SVMClassifier(cv=self.cross_validation)
model_MLP = MLP(cv=self.cross_validation)
model_Logit = LogisticRegressor(cv=self.cross_validation)
model_Forest = RandomForest(cv=self.cross_validation)
model_RBF = RBFClassifier(cv=self.cross_validation)
model_Fischer = FisherDiscriminant(cv=self.cross_validation)
model1 = [model_SVM, model_MLP, model_Logit, model_Forest, model_RBF, model_Fischer]
model2 = copy.deepcopy(model1)
else:
raise SyntaxError('Invalid model name')
if self.classifier_type == 1:
return model, classifier_list
elif self.classifier_type == 2:
return model1, model2, classifier_list
def training(self):
"""
Function that trains classifiers
:arg
self (Trainer): instance of the class
:return
None
"""
if self.classifier_type == 1:
model, classifier_list = self.classifier_selection()
train_image_all, test_image_all, train_labels_all, test_labels_all = self._split_data()
if isinstance(model, list):
pred = []
proba = []
for i, clf in enumerate(model):
try:
clf.train(train_image_all, train_labels_all)
pred_clf = clf.predict(test_image_all)
proba_clf = clf.predict_proba(test_image_all)
pred.append(pred_clf)
proba.append(proba_clf)
clf.save_model(filename=clf.__class__.__name__ + '_all')
except Exception as e:
print(e)
print('Could not train {}'.format(clf.__class__.__name__))
else:
model.train(train_image_all, train_labels_all)
pred = [model.predict(test_image_all)]
proba = [model.predict_proba(test_image_all)]
model.save_model(filename=model.__class__.__name__ + '_all')
label_list = self.data.label_.columns.values.tolist()
self.display_metrics( classifier_list, test_labels_all, pred, proba, label_list)
elif self.classifier_type == 2:
model1, model2, classifier_list = self.classifier_selection()
train_image_all, test_image_all, train_labels_all, test_labels_all, \
train_labels_bool, test_labels_bool, train_image_sick, train_labels_sick = self._split_data()
if isinstance(model1, list) and isinstance(model2, list):
pred = []
proba = []
for i, clf1 in enumerate(model1):
try:
clf1.train(train_image_all, train_labels_bool)
clf2 = model2[i]
clf2.train(train_image_sick, train_labels_sick)
clf1.save_model(filename=clf1.__class__.__name__ + '_bool')
clf2.save_model(filename=clf2.__class__.__name__ + '_sick')
prediction_matrix = np.zeros(test_labels_all.shape)
proba_matrix = np.zeros(test_labels_all.shape)
sick_bool_pred = clf1.predict(test_image_all)
idx_of_sick = np.nonzero(sick_bool_pred == 1)
test_image_sick = test_image_all[idx_of_sick]
sick_type_pred = clf2.predict(test_image_sick)
sick_type_pred = np.insert(sick_type_pred, 5, 0, axis=1)
prediction_matrix[idx_of_sick] = sick_type_pred
prediction_matrix[:, 5] = 1 - sick_bool_pred
sick_bool_proba = clf1.predict_proba(test_image_all)
sick_type_proba = clf2.predict_proba(test_image_sick)
sick_type_proba = np.insert(sick_type_proba, 5, 0, axis=1)
proba_matrix[idx_of_sick] = sick_type_proba
proba_matrix[:, 5] = sick_bool_proba[:, 0]
pred.append(prediction_matrix)
proba.append(proba_matrix)
except Exception as e:
print(e)
print('Could not train {}'.format(clf1.__class__.__name__))
else:
model1.train(train_image_all, train_labels_bool)
model2.train(train_image_sick, train_labels_sick)
model1.save_model(filename=model1.__class__.__name__ + '_bool')
model2.save_model(filename=model2.__class__.__name__ + '_sick')
prediction_matrix = np.zeros(test_labels_all.shape)
proba_matrix = np.zeros(test_labels_all.shape)
sick_bool_pred = model1.predict(test_image_all)
idx_of_sick = np.nonzero(sick_bool_pred == 1)
test_image_sick = test_image_all[idx_of_sick]
try:
sick_type_pred = model2.predict(test_image_sick)
except Exception as e:
print(e)
print('No sick patient found')
return None
sick_type_pred = np.insert(sick_type_pred, 5, 0, axis=1)
prediction_matrix[idx_of_sick] = sick_type_pred
prediction_matrix[:, 5] = 1 - sick_bool_pred
sick_bool_proba = model1.predict_proba(test_image_all)
sick_type_proba = model2.predict_proba(test_image_sick)
sick_type_proba = np.insert(sick_type_proba, 5, 0, axis=1)
proba_matrix[idx_of_sick] = sick_type_proba
proba_matrix[:, 5] = sick_bool_proba[:, 0]
pred = [prediction_matrix]
proba = [proba_matrix]
label_list = self.data.label_.columns.values.tolist()
self.display_metrics(classifier_list, test_labels_all, pred, proba, label_list)
def predict_with_saved_model(self, *args):
"""
Function that do a prediction with saved classifier
:arg
args (tuple): Tuple containing path of saved classifier
:return
None
"""
if self.classifier == 'all':
print('Cannot predict all on saved classifier')
return None
if self.classifier_type == 1:
model, classifier_list = self.classifier_selection()
train_image_all, test_image_all, train_labels_all, test_labels_all = self._split_data()
model.load_model(args[0])
pred = [model.predict(test_image_all)]
proba = [model.predict_proba(test_image_all)]
label_list = self.data.label_.columns.values.tolist()
self.display_metrics( classifier_list, test_labels_all, pred, proba, label_list)
elif self.classifier_type == 2:
model1, model2, classifier_list = self.classifier_selection()
train_image_all, test_image_all, train_labels_all, test_labels_all, \
train_labels_bool, test_labels_bool, train_image_sick, train_labels_sick = self._split_data()
model1.load_model(args[0])
model2.load_model(args[1])
prediction_matrix = np.zeros(test_labels_all.shape)
proba_matrix = np.zeros(test_labels_all.shape)
sick_bool_pred = model1.predict(test_image_all)
idx_of_sick = np.nonzero(sick_bool_pred == 1)
test_image_sick = test_image_all[idx_of_sick]
sick_type_pred = model2.predict(test_image_sick)
sick_type_pred = np.insert(sick_type_pred, 5, 0, axis=1)
prediction_matrix[idx_of_sick] = sick_type_pred
prediction_matrix[:, 5] = 1 - sick_bool_pred
sick_bool_proba = model1.predict_proba(test_image_all)
sick_type_proba = model2.predict_proba(test_image_sick)
sick_type_proba = np.insert(sick_type_proba, 5, 0, axis=1)
proba_matrix[idx_of_sick] = sick_type_proba
proba_matrix[:, 5] = sick_bool_proba[:, 0]
pred = [prediction_matrix]
proba = [proba_matrix]
label_list = self.data.label_.columns.values.tolist()
self.display_metrics(classifier_list, test_labels_all, pred, proba, label_list)
@staticmethod
def display_metrics(classifier_list: list, test_labels_all, pred: list, proba: list, label_list: list):
"""
Function that display some metrics in regard of the training results
:arg
self (Trainer): instance of the class
classifier_list (list): List with selected classifier names
test_labels_all (numpy array): Numpy array containing target values of the testing set
pred (list): list of the prediction. Each index of the list contains a numpy array
proba (list): list of the probability. Each index of the list contains a numpy array
label_list (list): list of the name of pathologies
:return
None
"""
metrics = Metrics()
plt.figure()
plt.title('ROC Curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
for i, value in enumerate(proba):
fpr, tpr = metrics.roc_metrics(test_labels_all, value)
plt.plot(fpr, tpr, label=classifier_list[i])
if len(classifier_list) > 1:
mean_proba = np.dstack(proba)
mean_proba = np.mean(mean_proba, axis=2)
fpr, tpr = metrics.roc_metrics(test_labels_all, mean_proba)
plt.plot(fpr, tpr, label='Voting classifiers')
plt.legend(loc='lower right')
plt.show(block=False)
plt.figure()
plt.title('Precision-Recall Curve')
plt.xlabel('Recall')
plt.ylabel('Precision')
for i, value in enumerate(proba):
precision, recall = metrics.precision_recall(test_labels_all, value)
plt.plot(precision, recall, label=classifier_list[i])
if len(classifier_list) > 1:
mean_proba = np.dstack(proba)
mean_proba = np.mean(mean_proba, axis=2)
fpr, tpr = metrics.precision_recall(test_labels_all, mean_proba)
plt.plot(fpr, tpr, label='Voting classifiers')
plt.legend(loc='lower left')
plt.show(block=False)
if len(classifier_list) > 1:
mean_pred = np.dstack(pred)
mean_pred = np.mean(mean_pred, axis=2)
mean_pred[mean_pred >= 0.5] = 1
mean_pred[mean_pred < 0.5] = 0
pred = mean_pred
else:
pred = pred[0]
cohen_kappa_score, kappa_class = metrics.cohen_kappa_score(test_labels_all, pred)
f1_score, f1_class = metrics.f1_score(test_labels_all, pred)
accuracy, accuracy_class = metrics.accuracy(test_labels_all, pred)
precision, precision_class = metrics.precision(test_labels_all, pred)
recall, recall_class = metrics.recall(test_labels_all, pred)
#
print('Cohen: {}'.format(cohen_kappa_score))
print('F1: {}'.format(f1_score))
print('Accuracy: {}'.format(accuracy))
print('Precision: {}'.format(precision))
print('Recall: {}'.format(recall))
titles = ['names', 'Cohen', 'F1_score', 'Accuracy', 'Precision', 'Recall']
kappa_class_disp = ['%.4f' % elem for elem in kappa_class]
f1_class_disp = ['%.4f' % elem for elem in f1_class]
accuracy_class_disp = ['%.4f' % elem for elem in accuracy_class]
precision_class_disp = ['%.4f' % elem for elem in precision_class]
recall_class_disp = ['%.4f' % elem for elem in recall_class]
element = [titles] + list(
zip(label_list, kappa_class_disp, f1_class_disp, accuracy_class_disp, precision_class_disp,
recall_class_disp))
for i, d in enumerate(element):
line = '|'.join(str(x).ljust(19) for x in d)
print(line)
if i == 0:
print('-' * (len(line)-10))