generated from Altaf-Hussain-AI/CE-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cenet.py
243 lines (189 loc) · 8.3 KB
/
test_cenet.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
import torch
import torch.nn as nn
import torch.utils.data as data
from torch.autograd import Variable as V
import sklearn.metrics as metrics
import cv2
import os
import numpy as np
from time import time
from PIL import Image
import warnings
warnings.filterwarnings('ignore')
from networks.cenet import CE_Net_
os.environ['CUDA_VISIBLE_DEVICES'] = '8'
BATCHSIZE_PER_CARD = 8
def calculate_auc_test(prediction, label):
# read images
# convert 2D array into 1D array
result_1D = prediction.flatten()
label_1D = label.flatten()
label_1D = label_1D / 255
auc = metrics.roc_auc_score(label_1D, result_1D)
# print("AUC={0:.4f}".format(auc))
return auc
def accuracy(pred_mask, label):
'''
acc=(TP+TN)/(TP+FN+TN+FP)
'''
pred_mask = pred_mask.astype(np.uint8)
TP, FN, TN, FP = [0, 0, 0, 0]
for i in range(label.shape[0]):
for j in range(label.shape[1]):
if label[i][j] == 1:
if pred_mask[i][j] == 1:
TP += 1
elif pred_mask[i][j] == 0:
FN += 1
elif label[i][j] == 0:
if pred_mask[i][j] == 1:
FP += 1
elif pred_mask[i][j] == 0:
TN += 1
acc = (TP + TN) / (TP + FN + TN + FP)
sen = TP / (TP + FN)
return acc, sen
class TTAFrame():
def __init__(self, net):
self.net = net().cuda()
# self.net = torch.nn.DataParallel(self.net, device_ids=range(torch.cuda.device_count()))
def test_one_img_from_path(self, path, evalmode=True):
if evalmode:
self.net.eval()
batchsize = torch.cuda.device_count() * BATCHSIZE_PER_CARD
if batchsize >= 8:
return self.test_one_img_from_path_1(path)
elif batchsize >= 4:
return self.test_one_img_from_path_2(path)
elif batchsize >= 2:
return self.test_one_img_from_path_4(path)
def test_one_img_from_path_8(self, path):
img = cv2.imread(path) # .transpose(2,0,1)[None]
img90 = np.array(np.rot90(img))
img1 = np.concatenate([img[None], img90[None]])
img2 = np.array(img1)[:, ::-1]
img3 = np.array(img1)[:, :, ::-1]
img4 = np.array(img2)[:, :, ::-1]
img1 = img1.transpose(0, 3, 1, 2)
img2 = img2.transpose(0, 3, 1, 2)
img3 = img3.transpose(0, 3, 1, 2)
img4 = img4.transpose(0, 3, 1, 2)
img1 = V(torch.Tensor(np.array(img1, np.float32) / 255.0 * 3.2 - 1.6).cuda())
img2 = V(torch.Tensor(np.array(img2, np.float32) / 255.0 * 3.2 - 1.6).cuda())
img3 = V(torch.Tensor(np.array(img3, np.float32) / 255.0 * 3.2 - 1.6).cuda())
img4 = V(torch.Tensor(np.array(img4, np.float32) / 255.0 * 3.2 - 1.6).cuda())
maska = self.net.forward(img1).squeeze().cpu().data.numpy()
maskb = self.net.forward(img2).squeeze().cpu().data.numpy()
maskc = self.net.forward(img3).squeeze().cpu().data.numpy()
maskd = self.net.forward(img4).squeeze().cpu().data.numpy()
mask1 = maska + maskb[:, ::-1] + maskc[:, :, ::-1] + maskd[:, ::-1, ::-1]
mask2 = mask1[0] + np.rot90(mask1[1])[::-1, ::-1]
return mask2
def test_one_img_from_path_4(self, path):
img = cv2.imread(path) # .transpose(2,0,1)[None]
img90 = np.array(np.rot90(img))
img1 = np.concatenate([img[None], img90[None]])
img2 = np.array(img1)[:, ::-1]
img3 = np.array(img1)[:, :, ::-1]
img4 = np.array(img2)[:, :, ::-1]
img1 = img1.transpose(0, 3, 1, 2)
img2 = img2.transpose(0, 3, 1, 2)
img3 = img3.transpose(0, 3, 1, 2)
img4 = img4.transpose(0, 3, 1, 2)
img1 = V(torch.Tensor(np.array(img1, np.float32) / 255.0 * 3.2 - 1.6).cuda())
img2 = V(torch.Tensor(np.array(img2, np.float32) / 255.0 * 3.2 - 1.6).cuda())
img3 = V(torch.Tensor(np.array(img3, np.float32) / 255.0 * 3.2 - 1.6).cuda())
img4 = V(torch.Tensor(np.array(img4, np.float32) / 255.0 * 3.2 - 1.6).cuda())
maska = self.net.forward(img1).squeeze().cpu().data.numpy()
maskb = self.net.forward(img2).squeeze().cpu().data.numpy()
maskc = self.net.forward(img3).squeeze().cpu().data.numpy()
maskd = self.net.forward(img4).squeeze().cpu().data.numpy()
mask1 = maska + maskb[:, ::-1] + maskc[:, :, ::-1] + maskd[:, ::-1, ::-1]
mask2 = mask1[0] + np.rot90(mask1[1])[::-1, ::-1]
return mask2
def test_one_img_from_path_2(self, path):
img = cv2.imread(path) # .transpose(2,0,1)[None]
img90 = np.array(np.rot90(img))
img1 = np.concatenate([img[None], img90[None]])
img2 = np.array(img1)[:, ::-1]
img3 = np.concatenate([img1, img2])
img4 = np.array(img3)[:, :, ::-1]
img5 = img3.transpose(0, 3, 1, 2)
img5 = np.array(img5, np.float32) / 255.0 * 3.2 - 1.6
img5 = V(torch.Tensor(img5).cuda())
img6 = img4.transpose(0, 3, 1, 2)
img6 = np.array(img6, np.float32) / 255.0 * 3.2 - 1.6
img6 = V(torch.Tensor(img6).cuda())
maska = self.net.forward(img5).squeeze().cpu().data.numpy() # .squeeze(1)
maskb = self.net.forward(img6).squeeze().cpu().data.numpy()
mask1 = maska + maskb[:, :, ::-1]
mask2 = mask1[:2] + mask1[2:, ::-1]
mask3 = mask2[0] + np.rot90(mask2[1])[::-1, ::-1]
return mask3
def test_one_img_from_path_1(self, path):
img = cv2.imread(path) # .transpose(2,0,1)[None]
img = cv2.resize(img, (448, 448))
img90 = np.array(np.rot90(img))
img1 = np.concatenate([img[None], img90[None]])
img2 = np.array(img1)[:, ::-1]
img3 = np.concatenate([img1, img2])
img4 = np.array(img3)[:, :, ::-1]
img5 = np.concatenate([img3, img4]).transpose(0, 3, 1, 2)
img5 = np.array(img5, np.float32) / 255.0 * 3.2 - 1.6
img5 = V(torch.Tensor(img5).cuda())
mask = self.net.forward(img5).squeeze().cpu().data.numpy() # .squeeze(1)
mask1 = mask[:4] + mask[4:, :, ::-1]
mask2 = mask1[:2] + mask1[2:, ::-1]
mask3 = mask2[0] + np.rot90(mask2[1])[::-1, ::-1]
return mask3
def load(self, path):
model = torch.load(path)
self.net.load_state_dict(model)
def test_ce_net_vessel():
source = './dataset/DRIVE/test/images/'
val = os.listdir(source)
disc = 20
solver = TTAFrame(CE_Net_)
# solver.load('weights/log01_dink34-DCENET-DRIVE.th')
solver.load('/raid/Jimmyliu/shaoming/Shaoming/embc/CE-Net/weights/CE-Net_DRIVE_auc_0.9819.th')
tic = time()
target = './submits/log_CE_Net/'
if not os.path.exists(target):
os.mkdir(target)
gt_root = './dataset/DRIVE/test/1st_manual'
total_m1 = 0
hausdorff = 0
total_acc = []
total_sen = []
threshold = 4
total_auc = []
for i, name in enumerate(val):
# if i%10 == 0:
# print(i/10, ' ','%.2f'%(time()-tic))
image_path = os.path.join(source, name.split('.')[0] + '.tif')
print(image_path)
mask = solver.test_one_img_from_path(image_path)
new_mask = mask.copy()
mask[mask > threshold] = 255
mask[mask <= threshold] = 0
mask = np.concatenate([mask[:, :, None], mask[:, :, None], mask[:, :, None]], axis=2)
ground_truth_path = os.path.join(gt_root, name.split('_')[0] + '_manual1.gif')
# print(ground_truth_path)
ground_truth = np.array(Image.open(ground_truth_path))
mask = cv2.resize(mask, dsize=(np.shape(ground_truth)[1], np.shape(ground_truth)[0]))
new_mask = cv2.resize(new_mask, dsize=(np.shape(ground_truth)[1], np.shape(ground_truth)[0]))
total_auc.append(calculate_auc_test(new_mask / 8., ground_truth))
predi_mask = np.zeros(shape=np.shape(mask))
predi_mask[mask > disc] = 1
gt = np.zeros(shape=np.shape(ground_truth))
gt[ground_truth > 0] = 1
acc, sen = accuracy(predi_mask[:, :, 0], gt)
total_acc.append(acc)
total_sen.append(sen)
print(i + 1, acc, sen, calculate_auc_test(new_mask / 8., ground_truth))
cv2.imwrite(target + name.split('.')[0] + '-mask.png', mask.astype(np.uint8))
print(np.mean(total_acc), np.std(total_acc))
print(np.mean(total_sen), np.std(total_sen))
print(np.mean(total_auc), np.std(total_auc))
if __name__ == '__main__':
test_ce_net_vessel()