-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
295 lines (243 loc) · 11.6 KB
/
test.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
import os,tqdm,sys,time,argparse
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
import torch.distributed as dist
from utils.EndoMetric import general_dice, general_jaccard
from utils.summary import create_logger, DisablePrint
from utils.LoadModel import load_model_full_fortest
from skimage import io
# Training settings
parser = argparse.ArgumentParser(description='real-time segmentation')
parser.add_argument('--local_rank', type=int, default=0)
parser.add_argument('--dist', action='store_true')
parser.add_argument('--root_dir', type=str, default='./results/endo18')
parser.add_argument('--dataset', type=str, choices=['endovis2018','colon_oct'],default='endovis2018')
parser.add_argument('--data_tag', type=str, default='type')
parser.add_argument('--log_name', type=str, default='DLV3PLUS_clean_ver_0')
parser.add_argument('--checkpoint', type=str,default='1')
parser.add_argument('--layer', type=int, default=18)
parser.add_argument('--load_model', type=str, default=None)
parser.add_argument('--arch', type=str, choices=['puredeeplab18','RAUNet','swinPlus'], default='puredeeplab18') #!!
parser.add_argument('--gpus', type=str, default='1')
parser.add_argument('--downsample', type=int, default=1)
parser.add_argument('--h', type=int, default=256)
parser.add_argument('--w', type=int, default=320)
parser.add_argument('--num_workers', type=int, default=3)
parser.add_argument('--test_bs', type=int, default=1)
parser.add_argument('--t', type=int, default=1)
parser.add_argument('--step', type=int, default=1)
parser.add_argument('--global_n', type=int, default=0)
cfg = parser.parse_args()
#bg = black
color_map = {
0: [0,0,0], # background-tissue
1: [0,255,0], # instrument-shaft
2: [0,255,255], # instrument-clasper
3: [125,255,12], # instrument-wrist
4: [255,55,0], # kidney-parenchyma,
5: [24,55,125], # covered-kidney,
6: [187,155,25], # thread,
7: [0,255,125], # clamps,
8: [255,255,125], # suturing-needle
9: [123,15,175], # suction-instrument,
10: [124,155,5], # small-intestine
11: [12,255,141] # ultrasound-probe,
}
color_map_oct = {
0: [0,0,0], # background-tissue
1: [0,255,0], # instrument-shaft
2: [0,255,255], # instrument-clasper
3: [255,220,100], # instrument-wrist
4: [255,55,0], # kidney-parenchyma,
5: [62,110,218] # covered-kidney,
}
def label2rgb(ind_im, color_map=color_map):
rgb_im = np.zeros((ind_im.shape[0], ind_im.shape[1], 3))
for i, rgb in color_map.items():
rgb_im[(ind_im==i)] = rgb
return rgb_im
def main():
##------------------------------ Enviroment ------------------------------##
os.environ['CUDA_VISIBLE_DEVICES']=cfg.gpus
torch.backends.cudnn.benchmark = True # disable this if OOM at beginning of training
num_gpus = torch.cuda.device_count()
if cfg.dist:
cfg.device = torch.device('cuda:%d' % cfg.local_rank)
torch.cuda.set_device(cfg.local_rank)
dist.init_process_group(backend='nccl', init_method='env://',
world_size=num_gpus, rank=cfg.local_rank)
else:
cfg.device = torch.device('cuda')
# logger
cfg.log_dir = os.path.join(cfg.root_dir, cfg.log_name, 'logs_test_time')
os.makedirs(cfg.log_dir, exist_ok=True)
cfg.ckpt_dir = os.path.join(cfg.root_dir, cfg.log_name, 'ckpt')
if cfg.dataset=='endovis2018':
for k in range(1,5):
cfg.vis_dir = os.path.join(cfg.log_dir, 'visualization_'+str(cfg.checkpoint),'seq_'+str(k))
os.makedirs(cfg.vis_dir, exist_ok=True)
cfg.vis_path = os.path.join(cfg.log_dir, 'visualization_'+str(cfg.checkpoint), 'seq_{}/frame{:03d}.png')
elif cfg.dataset=='colon_oct':
Procedures_mini = {'train':['2T1','3C1','3T1','3T2','7C','10C','13C','15C'],'test':['C1','C4','T1']}
for k in range(0,3):
cfg.vis_dir = os.path.join(cfg.log_dir, 'visualization_'+str(cfg.checkpoint),Procedures_mini['test'][k])
os.makedirs(cfg.vis_dir, exist_ok=True)
cfg.vis_path = os.path.join(cfg.log_dir, 'visualization_'+str(cfg.checkpoint), '{}/{}.png')
logger = create_logger(cfg.local_rank, save_dir=cfg.log_dir)
print = logger.info
print(cfg)
##------------------------------ dataset ------------------------------##
print('Setting up data...')
if cfg.dataset=='endovis2018':
h,w = [cfg.h,cfg.w]
ori_h, ori_w = [1024, 1280]
print('size of endovis2018 data %d, %d.' %(h,w))
from dataset.Endovis2018_backbone import endovis2018
test_dataset = endovis2018('test', t=cfg.t, rate=1, global_n=cfg.global_n,h = h, w = w)
classes = test_dataset.class_num
elif cfg.dataset=='colon_oct':
h,w = [cfg.h,cfg.w]
ori_h, ori_w = [1024, 1024]
from dataset.Colon_OCT import Colon_OCT
test_dataset = Colon_OCT('test', t=cfg.t, rate=1, global_n=cfg.global_n,h = h, w = w)
classes = test_dataset.class_num
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1,
shuffle=False, num_workers=cfg.num_workers, pin_memory=True, drop_last=False)
##------------------------------ build model ------------------------------##
if 'puredeeplab' in cfg.arch:
from net.Ours.base18 import DeepLabV3Plus
model = DeepLabV3Plus(test_dataset.class_num, int(cfg.arch[-2:]))
elif 'swinPlus' in cfg.arch:
from net.Ours.base18 import TswinPlus
model = TswinPlus(test_dataset.class_num,h,w)
elif 'RAUNet' in cfg.arch:
from net.Ours.RAUNet import RAUNet
model = RAUNet(test_dataset.class_num)
else:
raise NotImplementedError
# combile model
torch.cuda.empty_cache()
gpus = cfg.gpus.split(',')
if len(cfg.gpus)>1:
model = nn.DataParallel(model, device_ids=gpus).cuda()
else:
model = model.to(cfg.device)
if cfg.load_model is None:
cfg.load_model = os.path.join(cfg.ckpt_dir, 'epoch_{}_checkpoint.t7'.format(cfg.checkpoint))
print('model path: %s' % cfg.load_model)
model = load_model_full_fortest(model, cfg.load_model)
################################################ def part ################################################
def val_map_endo(epoch):
print('\n Val@Epoch: %d' % epoch)
model.eval()
torch.cuda.empty_cache()
metrics = np.zeros((2,))
metrics_seq = np.zeros((2, 4))
count_seq = np.zeros((4,))
dice_each = np.zeros((12,))
iou_each = np.zeros((12,))
tool_eac = np.zeros((12,))
count = 0
with torch.no_grad():
for inputs in tqdm.tqdm(test_loader):
inputs['image'] = inputs['image'].to(cfg.device).float()
#print('shape:', inputs['image'].shape) #1,3,256,480
tic = time.perf_counter()
output,_ = model(inputs['image'])
output = F.interpolate(output, (ori_h,ori_w), mode='bilinear', align_corners=True)
output = F.softmax(output,dim=1)
output = torch.argmax(output,dim=1)
output = output.cpu().numpy()
duration = time.perf_counter() - tic
# print('duration: %f' % duration)
# #=====visualize figure======
predict = output.astype(np.uint8)
ins = int(inputs['path'][0])
i = int(inputs['path'][1])
save_pth = cfg.vis_path.format(ins, i)
# print('input path:', save_pth)
predict = label2rgb(predict[0]).astype(np.uint8)
io.imsave(save_pth, predict)
dice = general_dice(inputs['label'].numpy(),output) # dice containing each tool class
iou = general_jaccard(inputs['label'].numpy(), output)
for i in range(len(dice)):
tool_id = dice[i][0]
dice_each[tool_id] += dice[i][1]
iou_each[tool_id] += iou[i][1]
tool_eac[tool_id] += 1
frame_dice = np.mean([dice[i][1] for i in range(len(dice))])
frame_iou = np.mean([iou[i][1] for i in range(len(dice))])
#overall
metrics[0] += frame_dice # dice of each frame
metrics[1] += frame_iou
count += 1
#----for seq
seq_ind = int(inputs['path'][0]) - 1 #seq: 0-3
metrics_seq[0][seq_ind] += frame_dice
metrics_seq[1][seq_ind] += frame_iou
count_seq[seq_ind] += 1
print(count)
metrics[0] /= count
metrics[1] /= count
print(metrics)
dc, jc = metrics[0], metrics[1]
metrics_seq[0] /= count_seq
dice_seq = [float('{:.4f}'.format(i)) for i in metrics_seq[0]]
metrics_seq[1] /= count_seq
iou_seq = [float('{:.4f}'.format(i)) for i in metrics_seq[1]]
print('Dice:{:.4f} IoU:{:.4f} Time:{:.4f}'.format(dc, jc, duration))
print('Dice_seq1:{:.4f}, seq2:{:.4f}, seq3:{:.4f}, seq4:{:.4f}'.format(dice_seq[0], dice_seq[1], dice_seq[2],dice_seq[3]))
print('IOU_seq1:{:.4f}, seq2:{:.4f}, seq3:{:.4f}, seq4:{:.4f}'.format(iou_seq[0], iou_seq[1], iou_seq[2],iou_seq[3]))
return jc
def val_map_oct(epoch):
print('\n Val@Epoch: %d' % epoch)
model.eval()
torch.cuda.empty_cache()
metrics = np.zeros((2,))
count = 0
with torch.no_grad():
for inputs in tqdm.tqdm(test_loader):
inputs['image'] = inputs['image'].to(cfg.device).float()
# print('shape:', inputs['image'].shape) #1,3,256,480
tic = time.perf_counter()
output,_ = model(inputs['image'])
output = F.interpolate(output, (ori_h,ori_w), mode='bilinear', align_corners=True)
output = F.softmax(output,dim=1)
output = torch.argmax(output,dim=1)
output = output.cpu().numpy()
duration = time.perf_counter() - tic
# #=====visualize figure======
# predict = output.astype(np.uint8)
# ins = inputs['path'][0][0]
# i = int(inputs['path'][1])
# save_pth = cfg.vis_path.format(ins, i)
# # print('input path:', save_pth)
# predict = label2rgb(predict[0],color_map=color_map_oct).astype(np.uint8)
# io.imsave(save_pth, predict)
dice = general_dice(inputs['label'].numpy(),
output) # dice containing each tool class
iou = general_jaccard(inputs['label'].numpy(), output)
frame_dice = np.mean([dice[i][1] for i in range(len(dice))])
frame_iou = np.mean([iou[i][1] for i in range(len(dice))])
#overall
metrics[0] += frame_dice # dice of each frame
metrics[1] += frame_iou
count += 1
print(count)
metrics[0] /= count
metrics[1] /= count
print(metrics)
dc, jc = metrics[0], metrics[1]
print('Dice:{:.4f} IoU:{:.4f} Time:{:.4f}'.format(dc, jc, duration))
return jc
################################################ def part ################################################
if cfg.dataset=='endovis2018':
val_map_endo(0)
elif cfg.dataset=='colon_oct':
val_map_oct(0)
if __name__ == '__main__':
with DisablePrint(local_rank=cfg.local_rank):
main()