new_name = fname + '_pred_dice_' + str(dice_score) save(n_results, os.path.join(save_path, new_name + '.nii'), hdr) save(o_image, os.path.join(save_path, fname + '_img.nii'), hdr) save(o_mask, os.path.join(save_path, fname + '_mask.nii'), hdr) def eval_prediction(image, mask): return model.evaluate(image, mask, verbose=1) images_dir = './data/test/images/*' masks_dir = './data/test/masks/*' glob_images = sorted(glob.glob(images_dir)) glob_masks = sorted(glob.glob(masks_dir)) for file_image, file_mask in zip(glob_images, glob_masks): original_img, img, hdr = dh.getImageData(file_image, is_eval=True) original_msk, msk, _ = dh.getImageData(file_mask, is_mask=True, is_eval=True) res = eval_prediction(img, msk) fname = getFileName(file_image) predict_masks(fname, img, original_img, original_msk, hdr, res[1]) print(res)
import numpy as np import os import skimage.io as io from datahandler import DataHandler from unet import getUnet from medpy.io import save from keras import backend as K # get keras model model = getUnet() model.load_weights('unet_brain_seg_axial.h5') # get image data and header dh = DataHandler() image, hdr = dh.getImageData('./data/prev/04.nii') save_path = './data/prev/' # predict mask results = model.predict(image, verbose = 1) # save mask to nifti format # remove extra axis n_results = np.squeeze(results) # swap axis so they are (size, size, num slices) n_results = np.moveaxis(n_results, 0, -1) save(n_results, os.path.join(save_path, 'mask_04.nii'), hdr)