def get_eval_metrics(true_mask, pred_mask): true_mask_sitk = sitk.GetImageFromArray(true_mask) pred_mask_sitk = sitk.GetImageFromArray(pred_mask) dsc = getDSC(true_mask_sitk, pred_mask_sitk) h95 = getHausdorff(true_mask_sitk, pred_mask_sitk) #vs = getVS(true_mask_sitk, pred_mask_sitk) result = {} result['dsc'] = dsc result['h95'] = h95 #result['vs'] = vs return dsc, h95 #(dsc, h95, vs)
def get_eval_metrics(true_mask, pred_mask, output_file=''): true_mask_sitk = sitk.GetImageFromArray(true_mask) pred_mask_sitk = sitk.GetImageFromArray(pred_mask) dsc = getDSC(true_mask_sitk, pred_mask_sitk) h95 = getHausdorff(true_mask_sitk, pred_mask_sitk) vs = getVS(true_mask_sitk, pred_mask_sitk) result = {} result['dsc'] = dsc result['h95'] = h95 result['vs'] = vs if output_file != '': with open(output_file, 'w+') as outfile: json.dump(result, outfile) return (dsc, h95, vs)
def test_leave_one_out(patient=0, flair=True, t1=True, full=True, first5=True, aug=True, verbose=False): if patient < 20: dir = 'raw/Utrecht/' elif patient < 40: dir = 'raw/Singapore/' else: dir = 'raw/GE3T/' dirs = os.listdir(dir) dirs.sort() dir += dirs[patient % 20] FLAIR_image = sitk.ReadImage(dir + '/pre/FLAIR.nii.gz') T1_image = sitk.ReadImage(dir + '/pre/T1.nii.gz') FLAIR_array = sitk.GetArrayFromImage(FLAIR_image) T1_array = sitk.GetArrayFromImage(T1_image) if patient < 40: imgs_test = Utrecht_preprocessing(FLAIR_array, T1_array) else: imgs_test = GE3T_preprocessing(FLAIR_array, T1_array) if not flair: imgs_test = imgs_test[..., 1:2].copy() if not t1: imgs_test = imgs_test[..., 0:1].copy() img_shape = (rows_standard, cols_standard, flair + t1) model = get_unet(img_shape, first5) model_path = 'models/' #if you want to test three models ensemble, just do like this: pred = (pred_1+pred_2+pred_3)/3 model.load_weights(model_path + str(patient) + '.h5') pred = model.predict(imgs_test, batch_size=1, verbose=verbose) pred[pred > 0.5] = 1. pred[pred <= 0.5] = 0. if patient < 40: original_pred = Utrecht_postprocessing(FLAIR_array, pred) else: original_pred = GE3T_postprocessing(FLAIR_array, pred) filename_resultImage = model_path + str(patient) + '.nii.gz' sitk.WriteImage(sitk.GetImageFromArray(original_pred), filename_resultImage) filename_testImage = os.path.join(dir + '/wmh.nii.gz') testImage, resultImage = getImages(filename_testImage, filename_resultImage) dsc = getDSC(testImage, resultImage) avd = getAVD(testImage, resultImage) h95 = getHausdorff(testImage, resultImage) recall, f1 = getLesionDetection(testImage, resultImage) return dsc, h95, avd, recall, f1
original_pred = Utrecht_postprocessing(FLAIR_array, pred) elif np.array_equal(para_FLAIR[0], para_array[2]): print('GE3T!') original_pred = GE3T_postprocessing(FLAIR_array, pred) if not os.path.exists(outputDir): os.mkdir(outputDir) filename_resultImage = os.path.join(outputDir, 'result.nii.gz') sitk.WriteImage(sitk.GetImageFromArray(original_pred), filename_resultImage) filename_testImage = os.path.join(inputDir, dir_name, 'wmh.nii.gz') testImage, resultImage = getImages(filename_testImage, filename_resultImage) dsc = getDSC(testImage, resultImage) avd = getAVD(testImage, resultImage) h95 = getHausdorff(testImage, resultImage) recall, f1 = getLesionDetection(testImage, resultImage) print('Result of patient ' + str(patient_count)) print('Dice', dsc, '(higher is better, max=1)') print('HD', h95, 'mm', '(lower is better, min=0)') print('AVD', avd, '%', '(lower is better, min=0)') print('Lesion detection', recall, '(higher is better, max=1)') print('Lesion F1', f1, '(higher is better, max=1)') #Save result------------------------------------------------------- result_output_dir = os.path.join(outputDir, dir_name) #directory for images if not os.path.exists(result_output_dir): os.mkdir(result_output_dir) np.save(os.path.join(result_output_dir, 'dsc.npy'), dsc) np.save(os.path.join(result_output_dir, 'avd.npy'), avd) np.save(os.path.join(result_output_dir, 'h95.npy'), h95)