def inference_module(_data_path, _save_path, _pth_path): model = Network(channel=32, n_class=1) model.load_state_dict(torch.load(_pth_path)) model.cuda() model.eval() os.makedirs(_save_path, exist_ok=True) # FIXME image_root = '{}/'.format(_data_path) # gt_root = '{}/mask/'.format(data_path) test_loader = test_dataset(image_root, image_root, 352) for i in range(test_loader.size): image, name = test_loader.load_data() #gt = np.asarray(gt, np.float32) #gt /= (gt.max() + 1e-8) image = image.cuda() lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2, lateral_edge = model( image) res = lateral_map_2 # final segmentation #res = F.upsample(res, size=gt.shape, mode='bilinear', align_corners=False) res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) misc.imsave(_save_path + '/' + name, res)
def inference(): parser = argparse.ArgumentParser() parser.add_argument('--testsize', type=int, default=352, help='testing size') parser.add_argument('--data_path', type=str, default='./Dataset/TestingSet/LungSegmentation/', help='Path to test data') parser.add_argument('--pth_path', type=str, default='./snapshots/save_weights/Semi-Inf-Net/Inf-Net-100.pth', help='Path to weights file. If `semi-sup`, edit it to `Semi-Inf-Net/Semi-Inf-Net-100.pth`') parser.add_argument('--save_path', type=str, default='./Results/LungSegmentation/Inf-Net/', help='Path to save the predictions. if `semi-sup`, edit it to `Semi-Inf-Net`') parser.add_argument('--backbone', type=str, default='Res2Net50', help='change different backbone, choice: VGGNet16, ResNet50, Res2Net50') opt = parser.parse_args() print("#" * 20, "\nStart Testing (Inf-Net)\n{}\nThis code is written for 'Inf-Net: Automatic COVID-19 Lung " "Infection Segmentation from CT Scans', 2020, arXiv.\n" "----\nPlease cite the paper if you use this code and dataset. " "And any questions feel free to contact me " "via E-mail ([email protected])\n----\n".format(opt), "#" * 20) if opt.backbone == 'Res2Net50': print('Backbone loading: Res2Net50') from Code.model_lung_infection.InfNet_Res2Net import Inf_Net as Network elif opt.backbone == 'ResNet50': print('Backbone loading: ResNet50') from Code.model_lung_infection.InfNet_ResNet import Inf_Net as Network elif opt.backbone == 'VGGNet16': print('Backbone loading: VGGNet16') from Code.model_lung_infection.InfNet_VGGNet import Inf_Net as Network else: raise ValueError('Invalid backbone parameters: {}'.format(opt.backbone)) model = Network() # model = torch.nn.DataParallel(model, device_ids=[0, 1]) # uncomment it if you have multiply GPUs. model.load_state_dict(torch.load(opt.pth_path, map_location={'cuda:1':'cuda:0'})) model.cuda() model.eval() image_root = '{}/Imgs/'.format(opt.data_path) # gt_root = '{}/GT/'.format(opt.data_path) test_loader = test_dataset(image_root, opt.testsize) os.makedirs(opt.save_path, exist_ok=True) for i in range(test_loader.size): image, name = test_loader.load_data() image = image.cuda() lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2, lateral_edge = model(image) res = lateral_map_2 #res = F.upsample(res, size=(ori_size[1],ori_size[0]), mode='bilinear', align_corners=False) res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) imsave(opt.save_path + name, res) print('Test Done!')
def inference_module(_data_path, _save_path, _pth_path): model = Network(channel=32, n_class=1) model.load_state_dict(torch.load(_pth_path)) model.cuda() model.eval() os.makedirs(_save_path, exist_ok=True) image_root = '{}/'.format(_data_path) # gt_root = '{}/GT/'.format(_data_path) test_loader = test_dataset(image_root, 352) # initially test_loader = test_dataset(image_root, image_root, 352) # but test_dataset class from Code.utils.dataloader_LungInf have folloving: # def __init__(self, image_root, testsize) ... # because self doesn't have to be in input, one image_root is need to be removed. for i in range(test_loader.size): # initially image, gt, name = test_loader.load_data() # but load_data() function of class test_dataset have following: return image, name # this error have makes no possible to find shape from gt, so we find shape from image. # this is made truth sh = image.shape[2:4] image, name = test_loader.load_data() image = image.cuda() lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2, lateral_edge = model( image) sh = image.shape[2:4] res = lateral_map_2 # final segmentation res = F.upsample(res, size=sh, mode='bilinear', align_corners=False) res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) # GT map is binary image, so we need threshold. 0.5 was chosen and because some resulting images need # lower threshold, and some need higher, 0.5 good enough. res = np.where(res < np.max(res) / 2, True, False) imsave(_save_path + '/' + name, res, cmap='binary')
def inference(): parser = argparse.ArgumentParser() parser.add_argument('--testsize', type=int, default=352, help='testing size') parser.add_argument('--data_path', type=str, default='./Dataset/TestingSet/LungSegmentation/', help='Path to test data') parser.add_argument('--pth_path', type=str, default='./Snapshots/save_weights/Inf-Net/Inf-Net-100.pth', help='Path to weights file. If `semi-sup`, edit it to `Semi-Inf-Net/Semi-Inf-Net-100.pth`') parser.add_argument('--save_path', type=str, default='./Results/LungSegmentation/Inf-Net/', help='Path to save the predictions. if `semi-sup`, edit it to `Semi-Inf-Net`') opt = parser.parse_args() print("#" * 20, "\nStart Testing (Inf-Net)\n{}\n".format(opt), "#" * 20) model = Network() model.load_state_dict(torch.load(opt.pth_path, map_location={'cuda:1':'cuda:0'})) model.cuda() model.eval() image_root = '{}/Imgs/'.format(opt.data_path) test_loader = test_dataset(image_root, opt.testsize) os.makedirs(opt.save_path, exist_ok=True) for i in range(test_loader.size): image, name = test_loader.load_data() image = image.cuda() lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2, lateral_edge = model(image) res = lateral_map_2 res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) res = np.where(res < np.max(res) / 2, True, False) res = np.invert(res) imsave(opt.save_path + name, res, cmap = 'gray') print('Test Done!')
def inference(): parser = argparse.ArgumentParser() parser.add_argument('--testsize', type=int, default=352, help='testing size') parser.add_argument('--data_path', type=str, default='/Users/vignavramesh/Documents/CT_Scans/', help='Path to test data') parser.add_argument( '--pth_path', type=str, default='./Snapshots/save_weights/Inf-Net/Inf-Net-100.pth', help= 'Path to weights file. If `semi-sup`, edit it to `Semi-Inf-Net/Semi-Inf-Net-100.pth`' ) parser.add_argument( '--save_path', type=str, default='/Users/vignavramesh/Documents/CT_Masks/', help= 'Path to save the predictions. if `semi-sup`, edit it to `Semi-Inf-Net`' ) opt = parser.parse_args() print( "#" * 20, "\nStart Testing (Inf-Net)\n{}\nThis code is written for 'Inf-Net: Automatic COVID-19 Lung " "Infection Segmentation from CT Scans', 2020, arXiv.\n" "----\nPlease cite the paper if you use this code and dataset. " "And any questions feel free to contact me " "via E-mail ([email protected])\n----\n".format(opt), "#" * 20) model = Network() model.load_state_dict( torch.load(opt.pth_path, map_location=torch.device('cpu'))) model.eval() count = 0 list = glob.glob(opt.data_path + '*') list = sort_list(list) for image_root in list: #subs count = image_root[(image_root.rindex('Volume') + 6):] image_root += '/' test_loader = test_dataset(image_root, opt.testsize) os.makedirs(opt.save_path, exist_ok=True) for i in range(test_loader.size): image, name = test_loader.load_data() lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2, lateral_edge = model( image) res = lateral_map_2 res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) string = opt.save_path + 'Volume' + str(count) if not os.path.exists(string): os.makedirs(string) imageio.imwrite(string + '/' + name, res) print('Test Done!')
def inference(image_root): parser = argparse.ArgumentParser() parser.add_argument('--testsize', type=int, default=352, help='testing size') parser.add_argument('--data_path', type=str, default='./Dataset/TestingSet/LungInfection-Test/', help='Path to test data') parser.add_argument( '--pth_path', type=str, default='./Snapshots/save_weights/Inf-Net/Inf-Net-100.pth', help= 'Path to weights file. If `semi-sup`, edit it to `Inf-Net/Inf-Net-100.pth`' ) parser.add_argument( '--save_path', type=str, default='./Results/Lung infection segmentation/Inf-Net/Labels/', help='Path to save the predictions. if `semi-sup`, edit it to `Inf-Net`' ) parser.add_argument( '--save_path_append', type=str, default='./Results/Lung infection segmentation/Inf-Net/Append_result/', help='Path to save the predictions. if `semi-sup`, edit it to `Inf-Net`' ) parser.add_argument( '--save_path_mask', type=str, default='./Results/Lung infection segmentation/Inf-Net/Mask/', help='Path to save the predictions. if `semi-sup`, edit it to `Inf-Net`' ) opt = parser.parse_args() model = Network() # model = torch.nn.DataParallel(model, device_ids=[0, 1]) # uncomment it if you have multiply GPUs. model.load_state_dict( torch.load(opt.pth_path, map_location={'cuda:1': 'cuda:0'})) model.cuda() model.eval() # gt_root = '{}/GT/'.format(opt.data_path) test_loader = test_dataset(image_root, opt.testsize) os.makedirs(opt.save_path, exist_ok=True) image, name = test_loader.load_data() image = image.cuda() lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2, lateral_edge = model( image) res = lateral_map_2 # res = F.upsample(res, size=(ori_size[1],ori_size[0]), mode='bilinear', align_corners=False) res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) print(name) misc.imsave(opt.save_path + name, res) icon = show_label(res, opt.save_path_mask + name) img = Image.open(image_root) img = img.convert("RGBA") img_w, img_h = img.size icon = icon.resize((img_w, img_h), Image.ANTIALIAS) layer = Image.new("RGBA", img.size, (0, 0, 0, 0)) layer.paste(icon, (0, 0)) out = Image.composite(layer, img, layer) out.save(opt.save_path_append + name) print('Test Done!')
def inference(): parser = argparse.ArgumentParser() parser.add_argument('--testsize', type=int, default=352, help='testing size') parser.add_argument('--data_path', type=str, default='./Dataset/TestingSet/LungInfection-Test/', help='Path to test data') parser.add_argument( '--pth_path', type=str, default='./Snapshots/save_weights/Semi-Inf-Net/Semi-Inf-Net-100.pth', help= 'Path to weights fileif `semi-sup`, edit it to `Semi-Inf-Net/Semi-Inf-Net-100.pth`' ) parser.add_argument( '--save_path', type=str, default='./Results/Lung infection segmentation/Semi-Inf-Net/', help= 'Path to save the predictions. if `semi-sup`, edit it to `Semi-Inf-Net`' ) opt = parser.parse_args() print( "#" * 20, "\nStart Testing (Inf-Net)\n{}\nThis code is written for 'Inf-Net: Automatic COVID-19 Lung " "Infection Segmentation from CT Scans', 2020, arXiv.\n" "----\nPlease cite the paper if you use this code and dataset. " "And any questions feel free to contact me " "via E-mail ([email protected])\n----\n".format(opt.backbone, opt), "#" * 20) model = Network() # model = torch.nn.DataParallel(model, device_ids=[0, 1]) # uncomment it if you have multiply GPUs. model.load_state_dict(torch.load(opt.pth_path)) model.cuda() model.eval() image_root = '{}/Imgs/'.format(opt.data_path) gt_root = '{}/GT/'.format(opt.data_path) test_loader = test_dataset(image_root, gt_root, opt.testsize) os.makedirs(opt.save_path, exist_ok=True) for i in range(test_loader.size): image, gt, name = test_loader.load_data() gt = np.asarray(gt, np.float32) gt /= (gt.max() + 1e-8) image = image.cuda() lateral_map_5, lateral_map_4, lateral_map_3, lateral_map_2, lateral_edge = model( image) res = lateral_map_2 res = F.upsample(res, size=gt.shape, mode='bilinear', align_corners=False) res = res.sigmoid().data.cpu().numpy().squeeze() res = (res - res.min()) / (res.max() - res.min() + 1e-8) misc.imsave(opt.save_path + name, res) print('Test Done!')