def test(fold=None, fold_idx="0", dir_name=None): with torch.no_grad(): custom_load( model, os.path.join(opt.checkpoint, "fold_" + fold_idx, "model_best.pth"), device) custom_load(vm, opt.VM_checkpoint, device) model.eval() test_set = Dataset_4D_multitime(opt.data_dir, sequence_list=fold, nb_inputs=opt.nb_inputs, horizon=opt.horizon, test=True) test_loader = DataLoader(test_set, batch_size=1, shuffle=False, num_workers=4) vol_dir = os.path.join(opt.logging_dir, "test", dir_name, "fold_" + fold_idx, "volumes") cond_mkdir(vol_dir) MSE_loss, NCC_loss, SSIM_loss = [], [], [] mse = nn.MSELoss(reduction='mean').to(device) for idx, [ref_volume, input_volume_list, current_volume_list] in enumerate(Bar(test_loader)): ref_volume = ref_volume.unsqueeze(1).to(device) vmorph_volume, dvf = [], [] for vol in range(len(current_volume_list)): current_volume_list[vol] = current_volume_list[vol].unsqueeze( 1).to(device) dvf_vm = vm(ref_volume, current_volume_list[vol]) dvf.append(dvf_vm) vmorph_volume.append(stn(ref_volume, dvf_vm)) if opt.condi_type == "1": c1 = list() c1_ref = ref_volume[:, :, opt.sag_index, :, :] for q in range(opt.nb_inputs): c1temp = input_volume_list[q].unsqueeze( 1)[:, :, opt.sag_index, :, :] c1.append( torch.cat([c1temp.to(device), c1_ref.to(device)], dim=1)) c1 = torch.stack(c1, dim=2).to(device) # sagittal c2 = None # coronal else: c2 = list() c2_ref = ref_volume[:, :, :, opt.cor_index, :] for q in range(opt.nb_inputs): c2temp = input_volume_list[q].unsqueeze( 1)[:, :, :, opt.cor_index, :] c2.append( torch.cat([c2temp.to(device), c2_ref.to(device)], dim=1)) c1 = None # sagittal c2 = torch.stack(c2, dim=2).to(device) # coronal # Inference latent = var_or_cuda(torch.as_tensor(np.random.randn( opt.latent_size), dtype=torch.float), device=device) latent = var_or_cuda(latent, device=device).unsqueeze(dim=0) generated_dvf, generated_current_volume = model( ref_volume, None, c1, c2, dvf=None, prior_post_latent=latent) avg_ncc, avg_mse, avg_ssim = 0, 0, 0 for tp in range(opt.horizon): save_tensor_as_nifti(vmorph_volume[tp][0, 0, :, :, :], "vm_volume_t" + str(tp), vol_dir, iter=idx, aff=[[3.5, 0, 0, 0], [0, 1.70 * 2, 0, 0], [0, 0, 1.70 * 2, 0], [0, 0, 0, 1]]) save_tensor_as_nifti(generated_current_volume[tp][0, 0, :, :, :], "generated_volume_t" + str(tp), vol_dir, iter=idx, aff=[[3.5, 0, 0, 0], [0, 1.70 * 2, 0, 0], [0, 0, 1.70 * 2, 0], [0, 0, 0, 1]]) avg_ncc += ncc_loss(generated_current_volume[tp], vmorph_volume[tp], device=device).item() avg_mse += mse(generated_current_volume[tp], vmorph_volume[tp]).item() avg_ssim += ss( generated_current_volume[tp][ 0, 0, :, :, :].detach().cpu().numpy(), vmorph_volume[tp][0, 0, :, :, :].detach().cpu().numpy()) NCC_loss.append(avg_ncc / opt.horizon) MSE_loss.append(avg_mse / opt.horizon) SSIM_loss.append(avg_ssim / opt.horizon) NCC_loss = np.asarray(NCC_loss) MSE_loss = np.asarray(MSE_loss) SSIM_loss = np.asarray(SSIM_loss) dir_name = os.path.join(dir_name, "fold_" + fold_idx) np.save( os.path.join(opt.logging_dir, "test", dir_name, "NCC_loss.npy"), NCC_loss) np.save( os.path.join(opt.logging_dir, "test", dir_name, "MSE_loss.npy"), MSE_loss) np.save( os.path.join(opt.logging_dir, "test", dir_name, "SSIM_loss.npy"), SSIM_loss) print("\nTest set average loss NCC: %0.4f, MSE: %0.4f, SSIM: %0.4f" % (np.mean(NCC_loss), np.mean(MSE_loss), np.mean(SSIM_loss)))
def score(self, phenotype): """Calculates SS Fitness for a specie""" fit = ss(phenotype, self.target) return fit
def SSIM_my(img1, img2): """ Structural Similarity """ return ss(img1, img2, multichannel=True)
def get_ss_fitness(self, specie): """Calculates SS Fitness for a specie""" fit = ss(specie.phenotype, self.target) return fit
def getSsFitness(self, specie): fit = ss(specie.phenotype, self.target) return fit