| 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 |
|
|
| |
| 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() |
|
|
|
|
| |
| color_map = { |
| 0: [0,0,0], |
| 1: [0,255,0], |
| 2: [0,255,255], |
| 3: [125,255,12], |
| 4: [255,55,0], |
| 5: [24,55,125], |
| 6: [187,155,25], |
| 7: [0,255,125], |
| 8: [255,255,125], |
| 9: [123,15,175], |
| 10: [124,155,5], |
| 11: [12,255,141] |
| } |
|
|
| color_map_oct = { |
| 0: [0,0,0], |
| 1: [0,255,0], |
| 2: [0,255,255], |
| 3: [255,220,100], |
| 4: [255,55,0], |
| 5: [62,110,218] |
|
|
| } |
|
|
| 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(): |
| |
| os.environ['CUDA_VISIBLE_DEVICES']=cfg.gpus |
| torch.backends.cudnn.benchmark = True |
| 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') |
| |
| |
| 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) |
| |
| |
| 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) |
| |
| 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 |
| |
| |
| |
| 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 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() |
| |
| 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 |
| |
|
|
| |
| predict = output.astype(np.uint8) |
| ins = int(inputs['path'][0]) |
| i = int(inputs['path'][1]) |
| save_pth = cfg.vis_path.format(ins, i) |
| |
| predict = label2rgb(predict[0]).astype(np.uint8) |
| io.imsave(save_pth, predict) |
|
|
| dice = general_dice(inputs['label'].numpy(),output) |
| 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))]) |
| |
| metrics[0] += frame_dice |
| metrics[1] += frame_iou |
| count += 1 |
|
|
| |
| seq_ind = int(inputs['path'][0]) - 1 |
| 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() |
| |
| 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 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| dice = general_dice(inputs['label'].numpy(), |
| output) |
| 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))]) |
| |
| metrics[0] += frame_dice |
| 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 |
| |
| |
| 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() |
|
|