/
Prediction_attentionUNet_T2_adam_300epoch_costumloss2,1.py
662 lines (523 loc) · 25.8 KB
/
Prediction_attentionUNet_T2_adam_300epoch_costumloss2,1.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
654
655
656
657
658
659
660
661
662
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 24 17:49:48 2021
@author: MaryamHashemi
"""
import tensorflow as tf
print(tf.__version__)
import keras
print(keras.__version__)
import numpy as np
import cv2
from skimage.io import imread, imshow
import matplotlib.pyplot as plt
from keras.utils import plot_model
from keras.utils import np_utils
import glob
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from skimage.transform import resize
from keras.models import model_from_json
import random
seed = 42
np.random.seed = seed
IMG_WIDTH = 128
IMG_HEIGHT = 128
IMG_CHANNELS = 3
with open("C:/Users/MaryamHashemi/Desktop/MS/Results/attentionUNet-DSC-costumloss-adam/T2_attentionUNET_metric=DSC,opt=adam,loss=costumloss-2-1,epochs=300/model_architecture_T2__attentionUNET_challenge2015_Adam_Cstumloss-2-1_DSC.json", 'r') as f:
model = tf.keras.models.model_from_json(f.read())
# Load weights into the new model
model.load_weights('C:/Users/MaryamHashemi/Desktop/MS/Results/attentionUNet-DSC-costumloss-adam/T2_attentionUNET_metric=DSC,opt=adam,loss=costumloss-2-1,epochs=300/model_weights_T2_attentionUNET_challenge2015_Adam_Cstumloss-2-1_DSC.h5')
#################################################################################
TRAIN_PATH1 = 'E:/CODE/Mahsa/dataset-UNet/train-t2/images/'
MASK_PATH1 = 'E:/CODE/Mahsa/dataset-UNet/train-t2/masks/'
TEST_PATH1 = 'E:/CODE/Mahsa/dataset-UNet/test-t2/images/'
Mask_PATH2 = 'E:/CODE/Mahsa/dataset-UNet/test-t2/masks/'
train_ids=glob.glob(TRAIN_PATH1+"*.png")
masks_ids=glob.glob(MASK_PATH1+"*.png")
test_ids=glob.glob(TEST_PATH1+"*.png")
masks_ids2=glob.glob(Mask_PATH2+"*.png")
#################################################################################
############ PREPROCESSING PART ############
Y_train = np.zeros((1237, IMG_HEIGHT, IMG_WIDTH, 1))
#1238 is number of masks that are not total black and have some information to be learned
n=-1
index=0
d=0
index_=[]
zerocount=0
notzero=0
for mask_file in masks_ids:
index=index+1
mask= imread(mask_file)
mask=mask[30:187,11:168]
mask=resize(mask,(IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
if (np.all(mask==0)):
d=d+1
else:
n=n+1
index_.append(index)
for i in range (0,IMG_HEIGHT):
for j in range (0,IMG_WIDTH):
if mask[i,j]==0:
Y_train[n,i,j] =0
zerocount=zerocount+1
else:
Y_train[n,i,j] =1
notzero=notzero+1
#generally we have 128*128*1237 pixels. some them are white(lesion) and others are black(background)
# calculation the proporsion of black to white pixles
print("zerocount",zerocount,"notzero",notzero)
print("all pixels",(zerocount+notzero),"black/white pixels",(zerocount/notzero))
X_train = np.zeros((1237, IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
#print('Resizing training images and masks')
n=-1
for i in index_:
n=n+1
img=imread(train_ids[i])
img=img[30:187,11:168]
img=resize(img,(IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
for i in range (0,IMG_HEIGHT):
for j in range (0,IMG_WIDTH):
X_train[n,i,j]=img[i,j]
#################################################################################
# test images
Y_test = np.zeros((86, IMG_HEIGHT, IMG_WIDTH, 1))
n=-1
index=0
d=0
index_=[]
zerocount_t=0
notzero_t=0
for mask_file in masks_ids2:
index=index+1
mask= imread(mask_file)
mask=mask[30:187,11:168]
mask=resize(mask,(IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
if (np.all(mask==0)):
d=d+1
else:
n=n+1
index_.append(index)
for i in range (0,IMG_HEIGHT):
for j in range (0,IMG_WIDTH):
if mask[i,j]==0:
Y_test[n,i,j] =0
zerocount_t=zerocount_t+1
else:
Y_test[n,i,j] =1
notzero_t=notzero_t+1
#generally we have 128*128*1237 pixels. some them are white(lesion) and others are black(background)
# calculation the proporsion of black to white pixles
print("zerocount",zerocount_t,"notzero",notzero_t)
print("all pixels",(zerocount_t+notzero_t),"black/white pixels",(zerocount_t/notzero_t))
X_test = np.zeros((86, IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
n=-1
for i in index_:
n=n+1
img=imread(test_ids[i])
img=img[30:187,11:168]
img=resize(img,(IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
for i in range (0,IMG_HEIGHT):
for j in range (0,IMG_WIDTH):
X_test[n,i,j]=img[i,j]
print('Done!')
####################################################################
inputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
inputs = tf.keras.layers.Lambda(lambda x: x / 255)(inputs)
idx = random.randint(0, len(X_train))
preds_train = model.predict(X_train[:int(X_train.shape[0]*0.85)], verbose=1)
preds_val = model.predict(X_train[int(X_train.shape[0]*0.85):], verbose=1)
preds_test = model.predict(X_test, verbose=1)
preds_train_t = (preds_train > 0.5).astype(np.uint8)
preds_val_t = (preds_val > 0.5).astype(np.uint8)
preds_test_t = (preds_test > 0.5).astype(np.uint8)
####################################################################
#################################################################################
#calculate metrics and performance for test data
def parameters(TP_array,TN_array,FP_array,FN_array):
counter_array=[]
DSC_array=[]
IOU_array=[]
accu_array=[]
sens_array=[]
speci_array=[]
errorrate_array=[]
fnr_array=[]
exfra_array=[]
ppv_array=[]
npv_array=[]
count=0
for i in range(0,preds_test.shape[0]-1):
DSC=(2*TP_array[i])/((2*TP_array[i])+FN_array[i]+FP_array[i])
IOU=(TP_array[i])/((TP_array[i])+FN_array[i]+FP_array[i])
Accuracy= (TP_array[i]+TN_array[i])/(TP_array[i]+TN_array[i]+FN_array[i]+FP_array[i])
Sensitivity_me=(TP_array[i])/(TP_array[i]+FN_array[i])
Specificity_me=(TN_array[i])/(TN_array[i]+FP_array[i])
Error_rate=(FP_array[i]+FN_array[i])/(TP_array[i]+TN_array[i]+FN_array[i]+FP_array[i])
FNR=(FN_array[i])/(TN_array[i]+FN_array[i])
Extra_Fraction=(FP_array[i])/(TN_array[i]+FN_array[i])
NPV=(TN_array[i])/(TN_array[i]+FN_array[i])
PPV=(TP_array[i])/(TP_array[i]+FP_array[i])
count=count+1
counter_array.append(count)
IOU_array.append(IOU)
DSC_array.append(DSC)
accu_array.append(Accuracy)
sens_array.append(Sensitivity_me)
speci_array.append(Specificity_me)
errorrate_array.append(Error_rate)
fnr_array.append(FNR)
exfra_array.append(Extra_Fraction)
ppv_array.append(PPV)
npv_array.append(NPV)
return counter_array,DSC_array,IOU_array,accu_array,sens_array,speci_array,errorrate_array,fnr_array,exfra_array,ppv_array,npv_array
TP_array=[]
TN_array=[]
FP_array=[]
FN_array=[]
for i in range(preds_test_t.shape[0]):
TP=0
TN=0
FP=0
FN=0
for m in range(preds_test_t.shape[1]):
for n in range(preds_test_t.shape[2]):
if preds_test_t[i,m,n,0]==Y_test[i,m,n,0]==1:
TP=TP+1
if preds_test_t[i,m,n,0]==Y_test[i,m,n,0]==0:
TN=TN+1
if preds_test_t[i,m,n,0]==0 and Y_test[i,m,n,0]==1:
FN=FN+1
if preds_test_t[i,m,n,0]==1 and Y_test[i,m,n,0]==0:
FP=FP+1
TP_array.append(TP)
FP_array.append(FP)
TN_array.append(TN)
FN_array.append(FN)
counter_array,DSC_array,IOU_array,accu_array,sens_array,speci_array,errorrate_array,fnr_array,exfra_array,ppv_array,npv_array =parameters(TP_array,TN_array,FP_array,FN_array)
import statistics
print("Mean of DSC for Test data is:", (statistics.mean(DSC_array)))
print("Variance of DSC for Test data is:", (statistics.variance(DSC_array)))
print("Mean of IOU for Test data is:", (statistics.mean(IOU_array)))
print("Variance of IOU for Test data is:", (statistics.variance(IOU_array)))
print("Mean of Accuracy for Test data is:", (statistics.mean(accu_array)))
print("Variance of Accuracy for Test data is:", (statistics.variance(accu_array)))
print("Mean of Sensitivity for Test data is:", (statistics.mean(sens_array)))
print("Variance of Sensitivity for Test data is:", (statistics.variance(sens_array)))
print("Mean of Specificity for Test data is:", (statistics.mean(speci_array)))
print("Variance of Specificity for Test data is:", (statistics.variance(speci_array)))
print("Mean of Error Rate for Test data is:", (statistics.mean(errorrate_array)))
print("Variance of Error Rate for Test data is:", (statistics.variance(errorrate_array)))
print("Mean of FNR for Test data is:", (statistics.mean(fnr_array)))
print("Variance of FNR for Test data is:", (statistics.variance(fnr_array)))
print("Mean of Extra Fraction for Test data is:", (statistics.mean(exfra_array)))
print("Variance of Extra Fraction for Test data is:", (statistics.variance(exfra_array)))
print("Mean of PPV for Test data is:", (statistics.mean(ppv_array)))
print("Variance of PPV for Test data is:", (statistics.variance(ppv_array)))
print("Mean of NPV for Test data is:", (statistics.mean(npv_array)))
print("Variance of NPV for Test data is:", (statistics.variance(npv_array)))
plt.title("DSC Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,DSC_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("DSC",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('DSC_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("IOU Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,IOU_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("IOU",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('IOU_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Accuracy Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,accu_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("Accuracy",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Accuracy_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Sensitivity Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,sens_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("Sensitivity",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Sensitivity_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Specificity Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,speci_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("Specificity",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Specificity_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Error Rate Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,errorrate_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("Error Rate",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Error Rate_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("FNR Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,fnr_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("FNR",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('FNR_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Extra Fraction Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,exfra_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("Extra Fraction",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Extra Fraction_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("PPV Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,ppv_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("PPV",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('PPV_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("NPV Scatter Plot for Test Data", fontsize='36') #title
plt.scatter( counter_array,npv_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='30') #adds a label in the x axis
plt.ylabel("NPV",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('NPV_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
####################################################################
#calculate metrics and performance for train data
def parameters2(TP_array,TN_array,FP_array,FN_array):
counter_array=[]
DSC_array=[]
IOU_array=[]
accu_array=[]
sens_array=[]
speci_array=[]
errorrate_array=[]
fnr_array=[]
exfra_array=[]
ppv_array=[]
npv_array=[]
count=0
for i in range(0,preds_train.shape[0]-1):
DSC=(2*TP_array[i])/((2*TP_array[i])+FN_array[i]+FP_array[i])
IOU=(TP_array[i])/((TP_array[i])+FN_array[i]+FP_array[i])
Accuracy= (TP_array[i]+TN_array[i])/(TP_array[i]+TN_array[i]+FN_array[i]+FP_array[i])
Sensitivity_me=(TP_array[i])/(TP_array[i]+FN_array[i])
Specificity_me=(TN_array[i])/(TN_array[i]+FP_array[i])
Error_rate=(FP_array[i]+FN_array[i])/(TP_array[i]+TN_array[i]+FN_array[i]+FP_array[i])
FNR=(FN_array[i])/(TN_array[i]+FN_array[i])
Extra_Fraction=(FP_array[i])/(TN_array[i]+FN_array[i])
NPV=(TN_array[i])/(TN_array[i]+FN_array[i])
PPV=(TP_array[i])/(TP_array[i]+FP_array[i])
count=count+1
counter_array.append(count)
IOU_array.append(IOU)
DSC_array.append(DSC)
accu_array.append(Accuracy)
sens_array.append(Sensitivity_me)
speci_array.append(Specificity_me)
errorrate_array.append(Error_rate)
fnr_array.append(FNR)
exfra_array.append(Extra_Fraction)
ppv_array.append(PPV)
npv_array.append(NPV)
return counter_array,DSC_array,IOU_array,accu_array,sens_array,speci_array,errorrate_array,fnr_array,exfra_array,ppv_array,npv_array
TP1_array=[]
TN1_array=[]
FP1_array=[]
FN1_array=[]
for i in range(preds_train.shape[0]):
TP=0
TN=0
FP=0
FN=0
for m in range(preds_train.shape[1]):
for n in range(preds_train.shape[2]):
if preds_train_t[i,m,n,0]==Y_train[i,m,n,0]==1:
TP=TP+1
if preds_train_t[i,m,n,0]==Y_train[i,m,n,0]==0:
TN=TN+1
if preds_train_t[i,m,n,0]==0 and Y_train[i,m,n,0]==1:
FN=FN+1
if preds_train_t[i,m,n,0]==1 and Y_train[i,m,n,0]==0:
FP=FP+1
TP1_array.append(TP)
FP1_array.append(FP)
TN1_array.append(TN)
FN1_array.append(FN)
counter_array,DSC_array,IOU_array,accu_array,sens_array,speci_array,errorrate_array,fnr_array,exfra_array,ppv_array,npv_array =parameters2(TP1_array,TN1_array,FP1_array,FN1_array)
import statistics
print("Mean of DSC for Train data is:", (statistics.mean(DSC_array)))
print("Variance of DSC for Train data is:", (statistics.variance(DSC_array)))
print("Mean of IOU for Train data is:", (statistics.mean(IOU_array)))
print("Variance of IOU for Train data is:", (statistics.variance(IOU_array)))
print("Mean of Accuracy for Train data is:", (statistics.mean(accu_array)))
print("Variance of Accuracy for Train data is:", (statistics.variance(accu_array)))
print("Mean of Sensitivity for Train data is:", (statistics.mean(sens_array)))
print("Variance of Sensitivity for Train data is:", (statistics.variance(sens_array)))
print("Mean of Specificity for Train data is:", (statistics.mean(speci_array)))
print("Variance of Specificity for Train data is:", (statistics.variance(speci_array)))
print("Mean of Error Rate for Train data is:", (statistics.mean(errorrate_array)))
print("Variance of Error Rate for Train data is:", (statistics.variance(errorrate_array)))
print("Mean of FNR for Train data is:", (statistics.mean(fnr_array)))
print("Variance of FNR for Train data is:", (statistics.variance(fnr_array)))
print("Mean of Extra Fraction for Train data is:", (statistics.mean(exfra_array)))
print("Variance of Extra Fraction for Train data is:", (statistics.variance(exfra_array)))
print("Mean of PPV for Train data is:", (statistics.mean(ppv_array)))
print("Variance of PPV for Train data is:", (statistics.variance(ppv_array)))
print("Mean of NPV for Train data is:", (statistics.mean(npv_array)))
print("Variance of NPV for Train data is:", (statistics.variance(npv_array)))
plt.title("DSC Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,DSC_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("DSC",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('DSC_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("IOU Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,IOU_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("IOU",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('IOU_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Accuracy Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,accu_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("Accuracy",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Accuracy_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Sensitivity Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,sens_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("Sensitivity",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Sensitivity_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Specificity Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,speci_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("Specificity",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Specificity_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Error Rate Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,errorrate_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("Error Rate",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Error Rate_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("FNR Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,fnr_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("FNR",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('FNR_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("Extra Fraction Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,exfra_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("Extra Fraction",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('Extra Fraction_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("PPV Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,ppv_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("PPV",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('PPV_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
plt.title("NPV Scatter Plot for Train Data", fontsize='36') #title
plt.scatter( counter_array,npv_array,color='red', marker='o') #plot the points
plt.xlabel("Number of Image",fontsize='36') #adds a label in the x axis
plt.ylabel("NPV",fontsize='36') #adds a label in the y axis
#plt.legend(('YvsX'),loc='best') #creates a legend to identify the plot
plt.savefig('NPV_array.png') #saves the figure in the present directory
plt.grid() #shows a grid under the plot
plt.show()
####################################################################
# performance on metrics visualization on some random data
from matplotlib import colors
ix = random.randint(0, len(preds_test_t)-1)
a=Y_test[ix]
b=preds_test_t[ix]
eshterak = np.zeros((IMG_HEIGHT, IMG_WIDTH))
ejtema= np.zeros((IMG_HEIGHT, IMG_WIDTH))
notditectedlesion=np.zeros((IMG_HEIGHT, IMG_WIDTH))
wronglesiondetected=np.zeros((IMG_HEIGHT, IMG_WIDTH))
for i in range(a.shape[0]-1):
for j in range(a.shape[1]-1):
if a[i,j]==b[i,j]:
eshterak[i,j]=a[i,j]
ejtema[i,j]=a[i,j]
else:
if a[i,j]==1 or b[i,j]==1:
ejtema[i,j]=1
if a[i,j]==1 and b[i,j]==0:
notditectedlesion[i,j]=1
if a[i,j]==0 and b[i,j]==1:
wronglesiondetected[i,j]=1
plt.subplot(2,4,1)
plt.imshow((X_test[ix])/256)
plt.title('MRI Scan (Test Set)')
plt.subplot(2,4,2)
plt.imshow(np.squeeze(Y_test[ix]))
plt.title('Original Mask (Test Set)')
plt.subplot(2,4,3)
plt.imshow(np.squeeze(preds_test_t[ix]))
plt.title('Predicted Mask (Test Set)')
plt.subplot(2,4,4)
plt.imshow(np.squeeze(ejtema))
plt.title('(Predicted Mask) ∪ (Original Mask)')
plt.subplot(2,4,5)
plt.imshow(np.squeeze(eshterak))
plt.title('(Predicted Mask) ∩ (Original Mask)')
plt.subplot(2,4,6)
plt.imshow(np.squeeze(notditectedlesion))
#Lesion Area that Network did not Detect
plt.title('False Negative')
plt.subplot(2,4,7)
plt.imshow(np.squeeze(wronglesiondetected))
#Not Lesion Area that Network Classified as Lesion
plt.title('False Positive')
plt.subplot(2,4,8)
plt.imshow(notditectedlesion,cmap=colors.ListedColormap(['black', 'blue']))
plt.imshow(wronglesiondetected,cmap=colors.ListedColormap(['black', 'red']),alpha=0.6)
plt.title("Blue=False Negative, Red=False Positive")
####################################################################
#Metric definition
#Accuracy=(TP+TN)/(TP+TN+FN+FP)
#DSC=(2*TP)/(2*TP+FN+FP)
#Error_rate=(FP+FN)/(TP+TN+FN+FP)
#Sensitivity=(TP)/(TP+FN)
#Specificity=(TN)/(TN+FP)
#FPR=1-Specificity
#FNR=(FN)/(TN+FN)
#Extra_Fraction=(FP)/(TN+FN)
#PPV=(TP)/(TP+FP)