-
Notifications
You must be signed in to change notification settings - Fork 0
/
tumor_models.py
653 lines (476 loc) · 20.5 KB
/
tumor_models.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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 13:20:13 2020
@author: Armando
"""
import os
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn import svm
from sklearn import linear_model as lm
from itertools import cycle
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from sklearn.metrics.pairwise import chi2_kernel,additive_chi2_kernel
import itertools
from sklearn.metrics import confusion_matrix,plot_confusion_matrix,precision_recall_curve,auc,roc_auc_score,roc_curve,recall_score,classification_report
from skimage.feature import hog
from skimage.color import rgb2grey
#plot roc curve
def plot_roc_curve(fpr, tpr):
'''
A method that plots the roc curve
Parameters
----------
fpr : float
False Positive Rate
tpr : float
True Positive Rate
'''
plt.figure(10)
plt.plot(fpr, tpr, color='orange',label='ROC curve (area = %0.2f)' % auc(fpr, tpr))
plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend()
plt.show()
def my_plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
Parameters
----------
cm : array , int
confusion matrix
classes : list , str
A list with the name of classes
title : str
The title of the plot
"""
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=0)
plt.yticks(tick_marks, classes)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
#print("Normalized confusion matrix")
else:
#print('Confusion matrix, without normalization')
#print(cm)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
#logistic Regression k-folds
def printing_Kfold_scoresLR(features,targets):
'''
A method that's find the best parameters for a LogisticRegression model by using 5-fold cross validation.
The metrics to find those parameters are Recall and Accuracy.
Parameters
----------
features : array,float
the train set
y_test : array,float
the targets of the train set
Returns
-------
dictionary
A dictionary with the best parameters of the model. Also the best accuracy and recall
'''
n_folds=5
kf = KFold(n_splits=n_folds)
Cs=[0.001,0.01,0.1,1,10,100]
#variables for best param
best_Param={}
best_score=-1
for C in Cs:
print("C:{} \n".format(C))
accuracy = precision = recall = sensitivity = specificity=fmeasure = 0
for train_index, test_index in kf.split(features):
X_train, X_test, y_train, y_test = features[train_index], features[test_index], targets[train_index], targets[test_index]
model=lm.LogisticRegression(C=C,penalty='l1',n_jobs=-1,solver='saga')
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
tn, fp, fn, tp =confusion_matrix(y_test,y_pred).ravel()
accuracy=((tn+tp)/(tn+tp+fn+fp))+accuracy
precision=(tp/(tp+fp))+precision
recall=(tp/(tp+fn))+recall
fmeasure=(((precision*recall*2)/((precision+recall))))+fmeasure
sensitivity=(tp/(tp+fn))+sensitivity
specificity=(tn/(tn+fp))+specificity
print("Mean Accuracy :",accuracy/n_folds)
print("Mean Precision :",precision/n_folds)
print("Mean recall :",recall/n_folds)
print("Mean Fmeasure :",fmeasure/n_folds)
print("Mean Sensitivity :",sensitivity/n_folds)
print("Mean Specificity :",specificity/n_folds)
print()
#find the best recall
if ((recall/n_folds) +(accuracy/n_folds))/2 > best_score :
best_score=((recall/n_folds) +(accuracy/n_folds))/2
best_Param={'C':C,'recall':recall/n_folds,'accuracy':accuracy/n_folds}
print("Best param is:{}".format(best_Param))
return best_Param
#support vector machine k-folds
def printing_Kfold_scoresSVM(features,targets,kernel):
'''
A method that's find the best parameters for a SVM model by using 5-fold cross validation.
The metrics to find those parameters are Recall and Accuracy.
Parameters
----------
features : array,float
the train set
y_test : array,float
the targets of the train set
kernel: str
the kernel of the svm model.It must be linear or fbf
Returns
-------
dictionary
A dictionary with the best parameters of the model. Also the best accuracy and recall
'''
n_folds=5
kf = KFold(n_splits=n_folds)
Cs=[1, 10, 100, 1000]
gammas=[0.1,0.01,0.001, 0.0001]
#variables for best param
best_Param={}
best_score=-1
for C in Cs:
for gamma in gammas:
print("C:{} and gamma:{} \n".format(C,gamma))
accuracy = precision = recall = sensitivity = specificity=fmeasure = 0
for train_index, test_index in kf.split(features):
X_train, X_test, y_train, y_test = features[train_index], features[test_index], targets[train_index], targets[test_index]
model=svm.SVC(C=C,gamma=gamma,kernel=kernel)
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
tn, fp, fn, tp =confusion_matrix(y_test,y_pred).ravel()
accuracy=((tn+tp)/(tn+tp+fn+fp))+accuracy
precision=(tp/(tp+fp))+precision
recall=(tp/(tp+fn))+recall
fmeasure=(((precision*recall*2)/((precision+recall))))+fmeasure
sensitivity=(tp/(tp+fn))+sensitivity
specificity=(tn/(tn+fp))+specificity
print("Mean Accuracy :",accuracy/n_folds)
print("Mean Precision :",precision/n_folds)
print("Mean recall :",recall/n_folds)
print("Mean Fmeasure :",fmeasure/n_folds)
print("Mean Sensitivity :",sensitivity/n_folds)
print("Mean Specificity :",specificity/n_folds)
print()
#find the best recall
if ((recall/n_folds) +(accuracy/n_folds))/2 > best_score :
best_score=((recall/n_folds) +(accuracy/n_folds))/2
best_Param={'C':C,'gamma':gamma,'recall':recall/n_folds,'accuracy':accuracy/n_folds}
print("Best param is:{}".format(best_Param))
return best_Param
#Random Forest k-folds
def printing_Kfold_scoresRF(features,targets):
'''
A method that's find the best parameters for a Random Forest model by using 5-fold cross validation.
The metrics to find those parameters are Recall and Accuracy.
Parameters
----------
features : array,float
the train set
y_test : array,float
the targets of the train set
Returns
-------
dictionary
A dictionary with the best parameters of the model. Also the best accuracy and recall
'''
n_folds=5
kf = KFold(n_splits=n_folds)
estimators= [50,100, 500, 1000]
max_depths=[80, 100, 110, 120]
best_Param={}
best_score=-1
for n_estimators in estimators:
for max_depth in max_depths:
print("n_estimators:{} and max_depth:{} \n".format(n_estimators,max_depth))
accuracy = precision = recall = sensitivity = specificity=fmeasure = 0
for train_index, test_index in kf.split(features):
X_train, X_test, y_train, y_test = features[train_index], features[test_index], targets[train_index], targets[test_index]
model=RandomForestClassifier(n_estimators=n_estimators,max_depth=max_depth,n_jobs=-1)
model.fit(X_train,y_train)
y_pred=model.predict(X_test)
tn, fp, fn, tp =confusion_matrix(y_test,y_pred).ravel()
accuracy=((tn+tp)/(tn+tp+fn+fp))+accuracy
precision=(tp/(tp+fp))+precision
recall=(tp/(tp+fn))+recall
fmeasure=(((precision*recall*2)/((precision+recall))))+fmeasure
sensitivity=(tp/(tp+fn))+sensitivity
specificity=(tn/(tn+fp))+specificity
print("Mean Accuracy :",accuracy/n_folds)
print("Mean Precision :",precision/n_folds)
print("Mean recall :",recall/n_folds)
print("Mean Fmeasure :",fmeasure/n_folds)
print("Mean Sensitivity :",sensitivity/n_folds)
print("Mean Specificity :",specificity/n_folds)
print()
#find the best recall and accu
if ((recall/n_folds) +(accuracy/n_folds))/2 > best_score :
best_score=((recall/n_folds) +(accuracy/n_folds))/2
best_Param={'n_estimators':n_estimators,'max_depth':max_depth,'recall':recall/n_folds,'accuracy':accuracy/n_folds}
print("Best param is:{}".format(best_Param))
return best_Param
def getFeatures(path):
'''
a method that takes as input the path of images and return a vector with
features of the images by using as feature extractore HOG method
Parameters
----------
path : str
the path of the images
Returns
-------
list
A list of features for each image
'''
features_list = []
for (dirpath,_, files) in os.walk(path):
for f in files:
img = Image.open(os.path.join(dirpath, f))
out=img.resize((250,250)) #250
img_array=np.array(out)
#color_features = img_array.flatten()
grey_image = rgb2grey(img_array)
#hog_features = hog(grey_image, block_norm='L2-Hys', pixels_per_cell=(32, 32))
hog_features = hog(grey_image, pixels_per_cell=(32, 32))
features_list.append(hog_features)
return features_list
def findBestThresholds(accuracy,recall):
'''
a method that takes as input two vectors and create a vector with te best score
and returns the the position with the best score
Parameters
----------
accuracy : list,float
a vector with the accuray of each threshold
recall : list, float
a vector with the recall of each threshold
Returns
-------
int
the position of the best score
'''
score=np.zeros(len(accuracy))
for i in range(0,len(accuracy)):
score[i]=(accuracy[i]+recall[i])/2
#print(score)
index=np.argmax(score)
return index
def main():
info={}
option=-1
stop=False
while not stop:
print("Choose Dataset : \n");
print("1. Dataset with total of 200 images\n")
print('2. Dataset with total of 253 images \n')
print('3. Exit\n')
option=int(input("Choose option : "))
#we init the lists to the null list []
feature_matrix_no=[]
feature_matrix_yes=[]
target_no=[]
target_yes=[]
# we load the images
if option == 1:
info["dataset"]="Dataset with total of 200 images"
feature_matrix_no=getFeatures('./brain-tumor-images-dataset/no')
feature_matrix_yes=getFeatures('./brain-tumor-images-dataset/yes')
elif option ==2:
info["dataset"]="Dataset with total of 253 images"
feature_matrix_no=getFeatures('./brain-mri-images-for-brain-tumor-detection/no')
feature_matrix_yes=getFeatures('./brain-mri-images-for-brain-tumor-detection/yes')
elif option == 3:
print("Bye\n")
break
else:
print("Wrong input, please type 1 or 2 or 3\n")
continue
# we create the targets of the images
target_no=np.zeros(len(feature_matrix_no))
target_yes=np.ones(len(feature_matrix_yes))
#finally we combine the two array features and targets
targets=np.concatenate((target_no,target_yes))
feature_matrix=np.array(feature_matrix_no+feature_matrix_yes)
# Let's print the feature matrix shape
print('Feature matrix shape is: ', feature_matrix.shape)
#plot a frequency bar
classes = ('Class No','Class Yes')
y_pos = np.arange(len(classes))
performance = [len(target_no),len(target_yes)]
plt.bar(y_pos, performance, align='center', alpha=0.5,color=('b','red'))
plt.xticks(y_pos, classes)
plt.title('Tumor class histogram')
plt.xlabel("Frequency")
plt.show()
print("Choose 1 for feature reduction : \n");
print("1. Principal Component Analysis\n")
print("2. For no change to the features\n")
print('3. Exit\n')
option=int(input("Choose option : "))
final_features=[]
if option == 1:
info["feature reduction"]=True
# after try and error we choose 0.9 parameter for pca
pca=PCA(0.9)
pca_features = pca.fit_transform(feature_matrix)
final_features=pca_features
print('Feature matrix shape after PCA: \n', final_features.shape)
elif option ==2:
info["feature reduction"]=False
final_features=feature_matrix
elif option == 3:
print("Bye")
break
else:
print("Wrong input, please type 1 or 2 or 3 \n")
continue
#split the data 70% training ,15% validation and 15% test
#X_train, X_test, y_train, y_test = train_test_split(final_features,targets,test_size = 0.3)
X_train, X_test, y_train, y_test = train_test_split(final_features,targets,test_size = 0.3, random_state = 0)
X_val, X_test, y_val, y_test = train_test_split(X_test,y_test,test_size = 0.5, random_state = 0)
print
print("\nThe training set is :"+str(X_train.shape[0]))
print("\nThe test set is :"+str(X_test.shape[0]))
print("\nThe val set is :"+str(X_val.shape[0]))
print("Choose a method to train your model\n")
print("1. SVM\n")
print("2. Linear-SVM\n")
print("3. Additive Chi-squared kernel\n")
print('4. LogisticRegression\n')
print("5. RandomForest\n")
print('6. Exit\n')
option=int(input("Choose option : "))
final_features=[]
if option == 1:
info["model"]="SVM"
kernel="rbf"
#we create the svm model with the best parameters
b_param=printing_Kfold_scoresSVM(X_train,y_train,kernel)
model= svm.SVC(C=b_param['C'],gamma=b_param['gamma'],kernel=kernel,probability=True)
elif option ==2:
info["model"]="Linear-SVM"
kernel="linear"
#we create the linear-svm model with the best parameters
b_param=printing_Kfold_scoresSVM(X_train,y_train,kernel)
model= svm.SVC(C=b_param['C'],gamma=b_param['gamma'],kernel=kernel,probability=True)
elif option == 3:
info["model"]="Additive Chi-squared kernel"
model=svm.SVC(kernel=additive_chi2_kernel,probability=True)
elif option == 4:
info["model"]="LogisticRegression"
#we create the Logistic Regression model
b_param=printing_Kfold_scoresLR(X_train,y_train)
model=lm.LogisticRegression(C=b_param['C'],solver='saga',penalty='l1')
elif option == 5:
info["model"]="RandomForest"
#we create the random forest model with the best parameters
b_param=printing_Kfold_scoresRF(X_train,y_train)
model=RandomForestClassifier(n_estimators=b_param['n_estimators'],max_depth=b_param['max_depth'],n_jobs=-1)
elif option ==6:
print("Bye")
break
else:
print("Wrong input, please type 1 or 2 or 3 or 4 or 5\n")
continue
# we train the model
model.fit(X_train,y_train)
#probs =model.decision_function(X_test)
# get the prediction and probability
y_pred=model.predict(X_val)
y_pred_proba = model.predict_proba(X_val)
# create the confusion_matrix
tn, fp, fn, tp =confusion_matrix(y_val,y_pred).ravel()
# The list of thresholds
thresholds = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
plt.figure(figsize=(10,10))
accuracy=np.zeros(9)
precision=np.zeros(9)
recall=np.zeros(9)
sensitivity=np.zeros(9)
specificity=np.zeros(9)
fmeasure = np.zeros(9)
#now let's try to find the most suitable threshold
j = 1
for i in thresholds:
y_val_predictions_high_recall = y_pred_proba[:,1] > i
plt.subplot(3,3,j)
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_val,y_val_predictions_high_recall)
np.set_printoptions(precision=2)
print('Threshold >= %s'%i)
tn, fp, fn, tp =confusion_matrix(y_val,y_val_predictions_high_recall).ravel()
print("tn:{} fp:{} fn:{} tp:{}".format(tn,fp,fn,tp))
accuracy[j-1]=((tn+tp)/(tn+tp+fn+fp))
precision[j-1]=(tp/(tp+fp))
recall[j-1]=(tp/(tp+fn))
fmeasure[j-1]=((precision[j-1]*recall[j-1]*2)/((precision[j-1]+recall[j-1])))
sensitivity[j-1]=(tp/(tp+fn))
specificity[j-1]=(tn/(tn+fp))
print()
print("Val set")
print("Accuracy :",accuracy[j-1])
print("Precision :",precision[j-1])
print("recall :",recall[j-1])
print("Fmeasure :",fmeasure[j-1])
print("Sensitivity :",sensitivity[j-1])
print("Specificity :",specificity[j-1])
print()
j = j+1
# Plot non-normalized confusion matrix
class_names = ['No','Yes']
my_plot_confusion_matrix(cnf_matrix
, classes=class_names
, title='Threshold >= %s'%i)
index=findBestThresholds(accuracy,recall)
#print(index)
# now let's test the model at the test set
y_pred=model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)
tn, fp, fn, tp =confusion_matrix(y_test,y_pred).ravel()
y_test_predictions_high_recall = y_pred_proba[:,1] > thresholds[index]
tn, fp, fn, tp = confusion_matrix(y_test,y_test_predictions_high_recall).ravel()
accuracy = precision = recall = sensitivity = specificity = fmeasure = 0
accuracy=((tn+tp)/(tn+tp+fn+fp))
precision=(tp/(tp+fp))
recall=(tp/(tp+fn))
fmeasure=(((precision*recall*2)/((precision+recall))))
sensitivity=(tp/(tp+fn))
specificity=(tn/(tn+fp))
print("{}\n".format(info))
print("**** Test set ****")
print("tn:{} fp:{} fn:{} tp:{}".format(tn,fp,fn,tp))
print("Accuracy :",accuracy)
print("Precision :",precision)
print("recall :",recall)
print("Fmeasure :",fmeasure)
print("Sensitivity :",sensitivity)
print("Specificity :",specificity)
print()
#now draw roc curve
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
plot_roc_curve(fpr, tpr)
if __name__ == "__main__":
""" Executed only when the file is run as a script. """
main()