-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap_metrics.py
executable file
·368 lines (323 loc) · 14.5 KB
/
Bootstrap_metrics.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
# Using bootstrap to evaluate metrics
# Author: Haomiao Ni
import os
from libtiff import TIFF
import numpy as np
from scipy.sparse import load_npz
import glob
from scipy.misc import imread, imresize
def open_slide(tif_path, set_current_level):
slide = TIFF.open(tif_path)
set_res = slide.SetDirectory(set_current_level)
if set_res == 1:
tiff_array = slide.read_image()
else:
set_res = slide.SetDirectory(set_current_level - 1)
assert set_res == 1
tiff_array = slide.read_image()
height = tiff_array.shape[0]
width = tiff_array.shape[1]
tiff_array = tiff_array[0:height:2, 0:width:2]
return tiff_array
def Metrics(MaskPath, MatPath, MatList, BootstrapName):
n_00_list = []
n_01_list = []
n_02_list = []
n_10_list = []
n_11_list = []
n_12_list = []
n_20_list = []
n_21_list = []
n_22_list = []
tt_0_list = []
tt_1_list = []
tt_2_list = []
N_00 = 0.0
N_01 = 0.0
N_02 = 0.0
N_10 = 0.0
N_11 = 0.0
N_12 = 0.0
N_20 = 0.0
N_21 = 0.0
N_22 = 0.0
t_0 = 0.0
t_1 = 0.0
t_2 = 0.0
for MatName in MatList:
if MatName[-4:] == '.npz':
print MatName
MatFile = os.path.join(MatPath, MatName)
PredMsk = load_npz(MatFile)
PredMsk = PredMsk.todense()
MaskName = MatName.replace('_Map.npz', '_Mask.tif')
MaskFile = os.path.join(MaskPath, MaskName)
if os.path.exists(MaskFile):
low_dim_array = open_slide(MaskFile, set_current_level)
normal_bin = low_dim_array < 100
low_dim_array[normal_bin] = 0
if MaskName[0] == 'T':
JR_bin = low_dim_array >= 130
low_dim_array[JR_bin] = 254
else:
DG_bin = np.logical_and(low_dim_array >= 130, low_dim_array < 230)
JR_bin = low_dim_array >= 230
low_dim_array[normal_bin] = 0
low_dim_array[DG_bin] = 127
low_dim_array[JR_bin] = 254
else:
low_dim_array = np.zeros(PredMsk.shape)
# calculate Pixel Accuracy
Normal_out = PredMsk == 0
Normal_tar = low_dim_array == 0
DG_out = PredMsk == 127
DG_tar = low_dim_array == 127
JR_out = PredMsk == 254
JR_tar = low_dim_array == 254
n_00 = np.sum(np.logical_and(Normal_out, Normal_tar))
n_00_list.append(n_00)
N_00 += np.sum(np.logical_and(Normal_out, Normal_tar))
n_01 = np.sum(np.logical_and(DG_out, Normal_tar))
n_01_list.append(n_01)
N_01 += np.sum(np.logical_and(DG_out, Normal_tar))
n_02 = np.sum(np.logical_and(JR_out, Normal_tar))
n_02_list.append(n_02)
N_02 += np.sum(np.logical_and(JR_out, Normal_tar))
n_10 = np.sum(np.logical_and(Normal_out, DG_tar))
n_10_list.append(n_10)
N_10 += np.sum(np.logical_and(Normal_out, DG_tar))
n_11 = np.sum(np.logical_and(DG_out, DG_tar))
n_11_list.append(n_11)
N_11 += np.sum(np.logical_and(DG_out, DG_tar))
n_12 = np.sum(np.logical_and(JR_out, DG_tar))
n_12_list.append(n_12)
N_12 += np.sum(np.logical_and(JR_out, DG_tar))
n_20 = np.sum(np.logical_and(Normal_out, JR_tar))
n_20_list.append(n_20)
N_20 += np.sum(np.logical_and(Normal_out, JR_tar))
n_21 = np.sum(np.logical_and(DG_out, JR_tar))
n_21_list.append(n_21)
N_21 += np.sum(np.logical_and(DG_out, JR_tar))
n_22 = np.sum(np.logical_and(JR_out, JR_tar))
n_22_list.append(n_22)
N_22 += np.sum(np.logical_and(JR_out, JR_tar))
tt_0 = np.sum(Normal_tar, dtype=np.float32)
tt_0_list.append(tt_0)
t_0 += np.sum(Normal_tar, dtype=np.float32)
tt_1 = np.sum(DG_tar, dtype=np.float32)
tt_1_list.append(tt_1)
t_1 += np.sum(DG_tar, dtype=np.float32)
tt_2 = np.sum(JR_tar, dtype=np.float32)
tt_2_list.append(tt_2)
t_2 += np.sum(JR_tar, dtype=np.float32)
mIoU = (1/3.0)*(N_00/(t_0+N_10+N_20)+N_11/(t_1+N_01+N_21)+N_22/(t_2+N_02+N_12))
tumor_mIoU = (1/2.0)*(N_11/(t_1+N_01+N_21)+N_22/(t_2+N_02+N_12))
DG_mIoU = N_11/(t_1+N_01+N_21)
JR_mIoU = N_22/(t_2+N_02+N_12)
print MatPath.split('/')[-1]
print "mIoU:{0:.4f}".format(mIoU)
print "C_mIoU:{0:.4f}".format(tumor_mIoU)
print "D_mIoU:{0:.4f}".format(DG_mIoU)
print "I_mIoU:{0:.4f}".format(JR_mIoU)
np.save(os.path.join(BootstrapName, 'n_00.npy'), n_00_list)
np.save(os.path.join(BootstrapName, 'n_01.npy'), n_01_list)
np.save(os.path.join(BootstrapName, 'n_02.npy'), n_02_list)
np.save(os.path.join(BootstrapName, 'n_10.npy'), n_10_list)
np.save(os.path.join(BootstrapName, 'n_11.npy'), n_11_list)
np.save(os.path.join(BootstrapName, 'n_12.npy'), n_12_list)
np.save(os.path.join(BootstrapName, 'n_20.npy'), n_20_list)
np.save(os.path.join(BootstrapName, 'n_21.npy'), n_21_list)
np.save(os.path.join(BootstrapName, 'n_22.npy'), n_22_list)
np.save(os.path.join(BootstrapName, 't_0.npy'), tt_0_list)
np.save(os.path.join(BootstrapName, 't_1.npy'), tt_1_list)
np.save(os.path.join(BootstrapName, 't_2.npy'), tt_2_list)
# Evaluating the performance of inception model
def InceptionMetrics(MaskPath, MatPath, MatList, BootstrapName):
n_00_list = []
n_01_list = []
n_02_list = []
n_10_list = []
n_11_list = []
n_12_list = []
n_20_list = []
n_21_list = []
n_22_list = []
tt_0_list = []
tt_1_list = []
tt_2_list = []
N_00 = 0.0
N_01 = 0.0
N_02 = 0.0
N_10 = 0.0
N_11 = 0.0
N_12 = 0.0
N_20 = 0.0
N_21 = 0.0
N_22 = 0.0
t_0 = 0.0
t_1 = 0.0
t_2 = 0.0
for MatName in MatList:
if MatName[-4:] == '.png':
print MatName
MatFile = os.path.join(MatPath, MatName)
PredMsk = imread(MatFile)
postfix = '_' + MatName.split('_')[-1]
MaskName = MatName.replace(postfix, '_Mask.tif')
MaskFile = os.path.join(MaskPath, MaskName)
if os.path.exists(MaskFile):
low_dim_array = open_slide(MaskFile, set_current_level)
normal_bin = low_dim_array < 100
low_dim_array[normal_bin] = 0
if MaskName[0] == 'T':
JR_bin = low_dim_array >= 130
low_dim_array[JR_bin] = 254
else:
DG_bin = np.logical_and(low_dim_array >= 130, low_dim_array < 230)
JR_bin = low_dim_array >= 230
low_dim_array[normal_bin] = 0
low_dim_array[DG_bin] = 127
low_dim_array[JR_bin] = 254
else:
low_dim_array = np.zeros(PredMsk.shape)
if PredMsk.shape != low_dim_array.shape:
PredMsk = imresize(PredMsk, low_dim_array.shape)
# calculate Pixel Accuracy
Normal_out = PredMsk == 0
Normal_tar = low_dim_array == 0
DG_out = np.logical_or(PredMsk == 127, PredMsk == 128)
DG_tar = low_dim_array == 127
JR_out = np.logical_or(PredMsk == 254, PredMsk == 255)
JR_tar = low_dim_array == 254
n_00 = np.sum(np.logical_and(Normal_out, Normal_tar))
n_00_list.append(n_00)
N_00 += np.sum(np.logical_and(Normal_out, Normal_tar))
n_01 = np.sum(np.logical_and(DG_out, Normal_tar))
n_01_list.append(n_01)
N_01 += np.sum(np.logical_and(DG_out, Normal_tar))
n_02 = np.sum(np.logical_and(JR_out, Normal_tar))
n_02_list.append(n_02)
N_02 += np.sum(np.logical_and(JR_out, Normal_tar))
n_10 = np.sum(np.logical_and(Normal_out, DG_tar))
n_10_list.append(n_10)
N_10 += np.sum(np.logical_and(Normal_out, DG_tar))
n_11 = np.sum(np.logical_and(DG_out, DG_tar))
n_11_list.append(n_11)
N_11 += np.sum(np.logical_and(DG_out, DG_tar))
n_12 = np.sum(np.logical_and(JR_out, DG_tar))
n_12_list.append(n_12)
N_12 += np.sum(np.logical_and(JR_out, DG_tar))
n_20 = np.sum(np.logical_and(Normal_out, JR_tar))
n_20_list.append(n_20)
N_20 += np.sum(np.logical_and(Normal_out, JR_tar))
n_21 = np.sum(np.logical_and(DG_out, JR_tar))
n_21_list.append(n_21)
N_21 += np.sum(np.logical_and(DG_out, JR_tar))
n_22 = np.sum(np.logical_and(JR_out, JR_tar))
n_22_list.append(n_22)
N_22 += np.sum(np.logical_and(JR_out, JR_tar))
tt_0 = np.sum(Normal_tar, dtype=np.float32)
tt_0_list.append(tt_0)
t_0 += np.sum(Normal_tar, dtype=np.float32)
tt_1 = np.sum(DG_tar, dtype=np.float32)
tt_1_list.append(tt_1)
t_1 += np.sum(DG_tar, dtype=np.float32)
tt_2 = np.sum(JR_tar, dtype=np.float32)
tt_2_list.append(tt_2)
t_2 += np.sum(JR_tar, dtype=np.float32)
mIoU = (1/3.0)*(N_00/(t_0+N_10+N_20)+N_11/(t_1+N_01+N_21)+N_22/(t_2+N_02+N_12))
tumor_mIoU = (1/2.0)*(N_11/(t_1+N_01+N_21)+N_22/(t_2+N_02+N_12))
DG_mIoU = N_11/(t_1+N_01+N_21)
JR_mIoU = N_22/(t_2+N_02+N_12)
print MatPath.split('/')[-1]
print "mIoU:{0:.4f}".format(mIoU)
print "C_mIoU:{0:.4f}".format(tumor_mIoU)
print "D_mIoU:{0:.4f}".format(DG_mIoU)
print "I_mIoU:{0:.4f}".format(JR_mIoU)
np.save(os.path.join(BootstrapName, 'n_00.npy'), n_00_list)
np.save(os.path.join(BootstrapName, 'n_01.npy'), n_01_list)
np.save(os.path.join(BootstrapName, 'n_02.npy'), n_02_list)
np.save(os.path.join(BootstrapName, 'n_10.npy'), n_10_list)
np.save(os.path.join(BootstrapName, 'n_11.npy'), n_11_list)
np.save(os.path.join(BootstrapName, 'n_12.npy'), n_12_list)
np.save(os.path.join(BootstrapName, 'n_20.npy'), n_20_list)
np.save(os.path.join(BootstrapName, 'n_21.npy'), n_21_list)
np.save(os.path.join(BootstrapName, 'n_22.npy'), n_22_list)
np.save(os.path.join(BootstrapName, 't_0.npy'), tt_0_list)
np.save(os.path.join(BootstrapName, 't_1.npy'), tt_1_list)
np.save(os.path.join(BootstrapName, 't_2.npy'), tt_2_list)
def BootstrapMetrics(BootstrapDir, BootstrapNum, MatSize):
BootstrapDirList = os.listdir(BootstrapDir)
BootstrapDirList.sort()
for BootstrapDirName in BootstrapDirList:
print BootstrapDirName
BootstrapName = os.path.join(BootstrapDir, BootstrapDirName)
n_00_list = np.load(os.path.join(BootstrapName, 'n_00.npy'))
n_01_list = np.load(os.path.join(BootstrapName, 'n_01.npy'))
n_02_list = np.load(os.path.join(BootstrapName, 'n_02.npy'))
n_10_list = np.load(os.path.join(BootstrapName, 'n_10.npy'))
n_11_list = np.load(os.path.join(BootstrapName, 'n_11.npy'))
n_12_list = np.load(os.path.join(BootstrapName, 'n_12.npy'))
n_20_list = np.load(os.path.join(BootstrapName, 'n_20.npy'))
n_21_list = np.load(os.path.join(BootstrapName, 'n_21.npy'))
n_22_list = np.load(os.path.join(BootstrapName, 'n_22.npy'))
tt_0_list = np.load(os.path.join(BootstrapName, 't_0.npy'))
tt_1_list = np.load(os.path.join(BootstrapName, 't_1.npy'))
tt_2_list = np.load(os.path.join(BootstrapName, 't_2.npy'))
mIoU = np.zeros(BootstrapNum)
C_mIoU = np.zeros(BootstrapNum)
D_mIoU = np.zeros(BootstrapNum)
I_mIoU = np.zeros(BootstrapNum)
for j in range(BootstrapNum):
temp_n_00_list = np.zeros(MatSize)
temp_n_01_list = np.zeros(MatSize)
temp_n_02_list = np.zeros(MatSize)
temp_n_10_list = np.zeros(MatSize)
temp_n_11_list = np.zeros(MatSize)
temp_n_12_list = np.zeros(MatSize)
temp_n_20_list = np.zeros(MatSize)
temp_n_21_list = np.zeros(MatSize)
temp_n_22_list = np.zeros(MatSize)
temp_tt_0_list = np.zeros(MatSize)
temp_tt_1_list = np.zeros(MatSize)
temp_tt_2_list = np.zeros(MatSize)
for i in range(MatSize):
tar = np.random.randint(0, MatSize)
temp_n_00_list[i] = n_00_list[tar]
temp_n_01_list[i] = n_01_list[tar]
temp_n_02_list[i] = n_02_list[tar]
temp_n_10_list[i] = n_10_list[tar]
temp_n_11_list[i] = n_11_list[tar]
temp_n_12_list[i] = n_12_list[tar]
temp_n_20_list[i] = n_20_list[tar]
temp_n_21_list[i] = n_21_list[tar]
temp_n_22_list[i] = n_22_list[tar]
temp_tt_0_list[i] = tt_0_list[tar]
temp_tt_1_list[i] = tt_1_list[tar]
temp_tt_2_list[i] = tt_2_list[tar]
N_00 = np.sum(temp_n_00_list)
N_01 = np.sum(temp_n_01_list)
N_02 = np.sum(temp_n_02_list)
N_10 = np.sum(temp_n_10_list)
N_11 = np.sum(temp_n_11_list)
N_12 = np.sum(temp_n_12_list)
N_20 = np.sum(temp_n_20_list)
N_21 = np.sum(temp_n_21_list)
N_22 = np.sum(temp_n_22_list)
t_0 = np.sum(temp_tt_0_list)
t_1 = np.sum(temp_tt_1_list)
t_2 = np.sum(temp_tt_2_list)
mIoU[j] = (1 / 3.0) * (N_00 / (t_0 + N_10 + N_20) + N_11 / (t_1 + N_01 + N_21) + N_22 / (t_2 + N_02 + N_12))
C_mIoU[j] = (1 / 2.0) * (N_11 / (t_1 + N_01 + N_21) + N_22 / (t_2 + N_02 + N_12))
D_mIoU[j] = N_11 / (t_1 + N_01 + N_21)
I_mIoU[j] = N_22 / (t_2 + N_02 + N_12)
print np.percentile(mIoU, [2.5, 97.5])
print np.percentile(C_mIoU, [2.5, 97.5])
print np.percentile(D_mIoU, [2.5, 97.5])
print np.percentile(I_mIoU, [2.5, 97.5])
if __name__ == '__main__':
# set_current_level = 3 # 5 for 10X resolution
MatSize = 100
MaskPath = '/disk8t-1/Xiangya2/Mask_test'
BootstrapDir = '/home/nihaomiao/PycharmProjects/research/deeplabForXiangya2/ICME/BootstrapDir'
BootstrapMetrics(BootstrapDir, 2000, MatSize)