def test(): model = SYMNet(2, 3, opt.start_channel).cuda() transform = SpatialTransform().cuda() diff_transform = DiffeomorphicTransform(time_step=7).cuda() com_transform = CompositionTransform().cuda() model.load_state_dict(torch.load(opt.modelpath)) model.eval() transform.eval() diff_transform.eval() com_transform.eval() grid = generate_grid(imgshape) grid = torch.from_numpy(np.reshape(grid, (1, ) + grid.shape)).cuda().float() use_cuda = True device = torch.device("cuda" if use_cuda else "cpu") fixed_img = load_4D(fixed_path) moved_img = load_4D(moving_path) fixed_img = torch.from_numpy(fixed_img).float().to(device).unsqueeze(dim=0) moved_img = torch.from_numpy(moved_img).float().to(device).unsqueeze(dim=0) with torch.no_grad(): F_xy, F_yx = model(fixed_img, moved_img) F_X_Y_half = diff_transform(F_xy, grid, range_flow) F_Y_X_half = diff_transform(F_yx, grid, range_flow) F_X_Y_half_inv = diff_transform(-F_xy, grid, range_flow) F_Y_X_half_inv = diff_transform(-F_yx, grid, range_flow) F_X_Y = com_transform(F_X_Y_half, F_Y_X_half_inv, grid, range_flow) F_Y_X = com_transform(F_Y_X_half, F_X_Y_half_inv, grid, range_flow) F_BA = F_Y_X.permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] F_BA = F_BA.astype(np.float32) * range_flow F_AB = F_X_Y.permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] F_AB = F_AB.astype(np.float32) * range_flow warped_B = transform(moved_img, F_Y_X.permute(0, 2, 3, 4, 1) * range_flow, grid).data.cpu().numpy()[0, 0, :, :, :] warped_A = transform(fixed_img, F_X_Y.permute(0, 2, 3, 4, 1) * range_flow, grid).data.cpu().numpy()[0, 0, :, :, :] save_flow(F_BA, savepath + '/wrapped_flow_B_to_A.nii.gz') save_flow(F_AB, savepath + '/wrapped_flow_A_to_B.nii.gz') save_img(warped_B, savepath + '/wrapped_norm_B_to_A.nii.gz') save_img(warped_A, savepath + '/wrapped_norm_A_to_B.nii.gz') print("Finished.")
def test(): model = ModelFlow_stride(2, 3, opt.start_channel).cuda() transform = SpatialTransform().cuda() model.load_state_dict(torch.load(opt.modelpath)) model.eval() transform.eval() grid = generate_grid(imgshape) grid = Variable(torch.from_numpy(np.reshape(grid, (1, ) + grid.shape))).cuda().float() start = timeit.default_timer() A = Variable(torch.from_numpy(load_5D(opt.fixed))).cuda().float() B = Variable(torch.from_numpy(load_5D(opt.moving))).cuda().float() start2 = timeit.default_timer() print('Time for loading data: ', start2 - start) pred = model(A, B) F_AB = pred.permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] F_AB = F_AB.astype(np.float32) * range_flow warped_A = transform(A, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).data.cpu().numpy()[0, 0, :, :, :] start3 = timeit.default_timer() print('Time for registration: ', start3 - start2) warped_F_BA = transform(-pred, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] warped_F_BA = warped_F_BA.astype(np.float32) * range_flow start4 = timeit.default_timer() print('Time for generating inverse flow: ', start4 - start3) save_flow(F_AB, savepath + '/flow_A_B.nii.gz') save_flow(warped_F_BA, savepath + '/inverse_flow_B_A.nii.gz') save_img(warped_A, savepath + '/warped_A.nii.gz') start5 = timeit.default_timer() print('Time for saving results: ', start5 - start4) del pred pred = model(B, A) F_BA = pred.permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] F_BA = F_BA.astype(np.float32) * range_flow warped_B = transform(B, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).data.cpu().numpy()[0, 0, :, :, :] warped_F_AB = transform(-pred, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] warped_F_AB = warped_F_AB.astype(np.float32) * range_flow save_flow(F_BA, savepath + '/flow_B_A.nii.gz') save_flow(warped_F_AB, savepath + '/inverse_flow_A_B.nii.gz') save_img(warped_B, savepath + '/warped_B.nii.gz')
def test(): imgshape_4 = (160 / 4, 192 / 4, 144 / 4) imgshape_2 = (160 / 2, 192 / 2, 144 / 2) model_lvl1 = Miccai2020_LDR_laplacian_unit_disp_add_lvl1(2, 3, start_channel, is_train=True, imgshape=imgshape_4, range_flow=range_flow).cuda() model_lvl2 = Miccai2020_LDR_laplacian_unit_disp_add_lvl2(2, 3, start_channel, is_train=True, imgshape=imgshape_2, range_flow=range_flow, model_lvl1=model_lvl1).cuda() model = Miccai2020_LDR_laplacian_unit_disp_add_lvl3(2, 3, start_channel, is_train=False, imgshape=imgshape, range_flow=range_flow, model_lvl2=model_lvl2).cuda() transform = SpatialTransform_unit().cuda() model.load_state_dict(torch.load(opt.modelpath)) model.eval() transform.eval() grid = generate_grid_unit(imgshape) grid = torch.from_numpy(np.reshape(grid, (1,) + grid.shape)).cuda().float() use_cuda = True device = torch.device("cuda" if use_cuda else "cpu") fixed_img = load_4D(fixed_path) moving_img = load_4D(moving_path) fixed_img = torch.from_numpy(fixed_img).float().to(device).unsqueeze(dim=0) moving_img = torch.from_numpy(moving_img).float().to(device).unsqueeze(dim=0) with torch.no_grad(): F_X_Y = model(moving_img, fixed_img) X_Y = transform(moving_img, F_X_Y.permute(0, 2, 3, 4, 1), grid).data.cpu().numpy()[0, 0, :, :, :] F_X_Y_cpu = F_X_Y.data.cpu().numpy()[0, :, :, :, :].transpose(1, 2, 3, 0) F_X_Y_cpu = transform_unit_flow_to_flow(F_X_Y_cpu) save_flow(F_X_Y_cpu, savepath+'/warpped_flow.nii.gz') save_img(X_Y, savepath+'/warpped_moving.nii.gz') print("Finished")
def test(): device = torch.device("cuda:6") model = ModelFlow_stride(2, 3, opt.start_channel).cuda(device) # model =ModelFlow_stride(2,3,opt.start_channel).cpu() # transform = SpatialTransform().cpu() transform = SpatialTransform().cuda(device) # model.load_state_dict(torch.load(opt.modelpath)) # model.load_state_dict(torch.load(opt.modelpath, map_location=torch.device('cpu'))) model.load_state_dict(torch.load(opt.modelpath, map_location=device)) model.eval() transform.eval() grid = generate_grid(imgshape) grid = Variable(torch.from_numpy(np.reshape( grid, (1, ) + grid.shape))).cuda(device).float() # grid = Variable(torch.from_numpy(np.reshape(grid, (1,) + grid.shape))).cpu().float() start = timeit.default_timer() A = sitk.GetArrayFromImage(sitk.ReadImage(opt.fixed, sitk.sitkFloat32)) B = sitk.GetArrayFromImage(sitk.ReadImage(opt.moving, sitk.sitkFloat32)) #A = Variable(torch.from_numpy(A)).cuda(device).float() #B = Variable(torch.from_numpy(B)).cuda(device).float() # A, B = padding(A,B) #A = load_5D(A).cuda(device).float() #B = load_5D(B).cuda(device).float() A = load_5D(A) B = load_5D(B) A = Variable(torch.from_numpy(A)).cuda(device).float() B = Variable(torch.from_numpy(B)).cuda(device).float() #A = Variable(torch.from_numpy( load_5D(opt.fixed))).cuda(device).float() #B = Variable(torch.from_numpy( load_5D(opt.moving))).cuda(device).float() start2 = timeit.default_timer() print('Time for loading data: ', start2 - start) pred = model(A, B) F_AB = pred.permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] #F_AB = pred.permute(0,2,3,4,1).data.cuda(device).numpy()[0, :, :, :, :] F_AB = F_AB.astype(np.float32) * range_flow warped_A = transform(A, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).data.cpu().numpy()[0, 0, :, :, :] #warped_A = transform(A,pred.permute(0,2,3,4,1)*range_flow,grid).data.cuda(device).numpy()[0, 0, :, :, :] start3 = timeit.default_timer() print('Time for registration: ', start3 - start2) warped_F_BA = transform(-pred, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] #warped_F_BA = transform(-pred,pred.permute(0,2,3,4,1)*range_flow,grid).permute(0,2,3,4,1).data.cuda(device).numpy()[0, :, :, :, :] warped_F_BA = warped_F_BA.astype(np.float32) * range_flow start4 = timeit.default_timer() print('Time for generating inverse flow: ', start4 - start3) save_flow(F_AB, savepath + '/flow_A_B.nii.gz') save_flow(warped_F_BA, savepath + '/inverse_flow_B_A.nii.gz') save_img(warped_A, savepath + '/warped_A.nii.gz') start5 = timeit.default_timer() print('Time for saving results: ', start5 - start4) del pred pred = model(B, A) F_BA = pred.permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] #F_BA = pred.permute(0,2,3,4,1).data.cuda(device).numpy()[0, :, :, :, :] F_BA = F_BA.astype(np.float32) * range_flow warped_B = transform(B, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).data.cpu().numpy()[0, 0, :, :, :] #warped_B = transform(B,pred.permute(0,2,3,4,1)*range_flow,grid).data.cuda(device).numpy()[0, 0, :, :, :] warped_F_AB = transform(-pred, pred.permute(0, 2, 3, 4, 1) * range_flow, grid).permute(0, 2, 3, 4, 1).data.cpu().numpy()[0, :, :, :, :] #warped_F_AB = transform(-pred,pred.permute(0,2,3,4,1)*range_flow,grid).permute(0,2,3,4,1).data.cuda(device).numpy()[0, :, :, :, :] warped_F_AB = warped_F_AB.astype(np.float32) * range_flow save_flow(F_BA, savepath + '/flow_B_A.nii.gz') save_flow(warped_F_AB, savepath + '/inverse_flow_A_B.nii.gz') save_img(warped_B, savepath + '/warped_B.nii.gz')