def test_fracvol_predictor_with_acc_v2(dl_model, test_data, save_path): # An attempt to reduce inference time as the v1 takes over 15 minutes per volume... vol_saver_path = os.path.join(save_path, 'predicted_volumes') if os.path.exists(vol_saver_path) is False: os.mkdir(vol_saver_path) for vol_index, each_vol in enumerate(test_data): start_time = time.time() # Load Nifti Volumes of Input, Output and Mask input_vol = load_nifty(each_vol['input_image'], data_type='float32') output_vol = load_nifty(each_vol['output_image'], data_type='float32') mask_vol = load_nifty(each_vol['mask'], data_type='float32') # Convert mask_vol to int to save space mask_vol = np.int16(mask_vol) vol_dims = mask_vol.shape pred_vol = np.zeros((vol_dims[0], vol_dims[1], vol_dims[2], 48)) for x in range(vol_dims[0]): print(x) for y in range(vol_dims[1]): for z in range(vol_dims[2]): if mask_vol[x, y, z] == 1: ip_voxel = np.squeeze(input_vol[x, y, z, :]) ip_voxel = np.reshape(ip_voxel, [1, 45]) t_pred = dl_model.predict(ip_voxel) pred_vol[x, y, z, :] = np.squeeze(t_pred) end_time = time.time() time_taken = end_time - start_time print('Predictions Completed for Vol {} & Time Taken was {} \n'.format( vol_index, time_taken)) print('Saving predicted volume') save_nifti(pred_vol, each_vol['output_image'], vol_saver_path) print('Predicted Volume Saved ... \n ') #### Calculate ACC print('Calculating ACC') acc_vol = np.zeros((vol_dims[0], vol_dims[1], vol_dims[2])) for x in range(vol_dims[0]): print(x) for y in range(vol_dims[1]): for z in range(vol_dims[2]): if mask_vol[x, y, z] == 1: op_voxel = np.squeeze(output_vol[x, y, z, :]) pred_voxel = np.squeeze(pred_vol[x, y, z, :]) acc_vol[x, y, z] = calc_acc_numpy(op_voxel, pred_voxel) save_nifti_acc(acc_vol, each_vol['output_image'], vol_saver_path)
def test_save_csd_acc_v2(test_data, save_path): # An attempt to reduce inference time as the v1 takes over 15 minutes per volume... vol_saver_path = os.path.join(save_path, 'predicted_volumes') if os.path.exists(vol_saver_path) is False: os.mkdir(vol_saver_path) batch_size = 1000 for vol_index, each_vol in enumerate(test_data): start_time = time.time() # Load Nifti Volumes of Input, Output and Mask #input_vol = load_nifty(each_vol['input_image'], data_type='float32') output_vol = load_nifty(each_vol['output_image'], data_type='float32') csd_vol = load_nifty(each_vol['csd_img'], data_type='float32') mask_vol = load_nifty(each_vol['mask'], data_type='float32') # Convert mask_vol to int to save space mask_vol = np.int16(mask_vol) vol_dims = mask_vol.shape #### Calculate ACC print('Calculating ACC') acc_vol = np.zeros((vol_dims[0], vol_dims[1], vol_dims[2])) for x in range(vol_dims[0]): print(x) for y in range(vol_dims[1]): for z in range(vol_dims[2]): if mask_vol[x, y, z] == 1: op_voxel = np.squeeze(output_vol[x, y, z, :]) pred_voxel = np.squeeze(csd_vol[x, y, z, :]) acc_vol[x, y, z] = calc_acc_numpy(op_voxel, pred_voxel) save_nifti_acc(acc_vol, each_vol['output_image'], vol_saver_path)