def preproc_and_norm(data, preprocess_method, norm_params): if preprocess_method is not None: print('Applying preprocess by {}...'.format(preprocess_method)) if preprocess_method == 'window_1_99': data = window_intensities_data(data) else: raise Exception('Unknown preprocess: {}'.format(preprocess_method)) if norm_params is not None and any(norm_params.values()): data = normalize_data(data, mean=norm_params['mean'], std=norm_params['std']) return data
def preproc_and_norm(data, preprocess_method=None, norm_params=None, scale=None, preproc=None): if preprocess_method is not None: print('Applying preprocess by {}...'.format(preprocess_method)) if preprocess_method == 'window_1_99': data = window_intensities_data(data) else: raise Exception('Unknown preprocess: {}'.format(preprocess_method)) if scale is not None: data = ndimage.zoom(data, scale) if preproc is not None: preproc_func = getattr(fetal_net.preprocess, preproc) data = preproc_func(data) # data = normalize_data(data, mean=data.mean(), std=data.std()) if norm_params is not None and any(norm_params.values()): data = normalize_data(data, mean=norm_params['mean'], std=norm_params['std']) return data
def main(input_mat_path, output_mat_path, config, overlap_factor, model_path, preprocess_method=None, norm_params=None): print(model_path) model = load_old_model(get_last_model_path(model_path)) print('Loading mat from {}...'.format(input_mat_path)) mat = loadmat(input_mat_path) print('Predicting mask...') data = mat['volume'].astype(np.float) if preprocess_method is not None: print('Applying preprocess by {}...'.format(preprocess_method)) if preprocess_method == 'window_1_99': data = window_intensities_data(data) else: raise Exception('Unknown preprocess: {}'.format(preprocess_method)) if norm_params is not None and any(norm_params.values()): data = normalize_data(data, mean=norm_params['mean'], std=norm_params['std']) prediction = \ patch_wise_prediction(model=model, data=np.expand_dims(data, 0), overlap_factor=overlap_factor, patch_shape=config["patch_shape"] + [config["patch_depth"]]) print('Post-processing mask...') if prediction.shape[-1] > 1: prediction = prediction[..., 1] prediction = prediction.squeeze() mat['masks'][0, 9] = \ process_pred(prediction, gaussian_std=0, threshold=0.2) # .astype(np.uint8) mat['masks'][0, 8] = \ process_pred(prediction, gaussian_std=1, threshold=0.5) # .astype(np.uint8) mat['masks'][0, 7] = \ process_pred(prediction, gaussian_std=0.5, threshold=0.5) # .astype(np.uint8) print('Saving mat to {}'.format(output_mat_path)) savemat(output_mat_path, mat) print('Finished.')
def main(pred_dir, config, split='test', overlap_factor=1, preprocess_method=None): padding = [16, 16, 8] prediction2_dir = os.path.abspath( os.path.join(config['base_dir'], 'predictions2', split)) model = load_old_model(get_last_model_path(config["model_file"])) with open(os.path.join(opts.config_dir, 'norm_params.json'), 'r') as f: norm_params = json.load(f) for sample_folder in glob(os.path.join(pred_dir, split, '*')): mask_path = os.path.join(sample_folder, 'prediction.nii.gz') truth_path = os.path.join(sample_folder, 'truth.nii.gz') subject_id = Path(sample_folder).name dest_folder = os.path.join(prediction2_dir, subject_id) Path(dest_folder).mkdir(parents=True, exist_ok=True) truth = nib.load(truth_path) nib.save(truth, os.path.join(dest_folder, Path(truth_path).name)) mask = nib.load(mask_path) mask = process_pred(mask.get_data(), gaussian_std=0.5, threshold=0.5) bbox_start, bbox_end = find_bounding_box(mask) check_bounding_box(mask, bbox_start, bbox_end) if padding is not None: bbox_start = np.maximum(bbox_start - padding, 0) bbox_end = np.minimum(bbox_end + padding, mask.shape) print("BBox: {}-{}".format(bbox_start, bbox_end)) volume = nib.load( os.path.join(original_data_folder, subject_id, 'volume.nii')) orig_volume_shape = np.array(volume.get_data().shape) volume = cut_bounding_box(volume, bbox_start, bbox_end).get_data().astype(np.float) if preprocess_method is not None: print('Applying preprocess by {}...'.format(preprocess_method)) if preprocess_method == 'window_1_99': volume = window_intensities_data(volume) else: raise Exception( 'Unknown preprocess: {}'.format(preprocess_method)) if norm_params is not None and any(norm_params.values()): volume = normalize_data(volume, mean=norm_params['mean'], std=norm_params['std']) prediction = patch_wise_prediction(model=model, data=np.expand_dims(volume, 0), patch_shape=config["patch_shape"] + [config["patch_depth"]], overlap_factor=overlap_factor) prediction = prediction.squeeze() padding2 = list(zip(bbox_start, orig_volume_shape - bbox_end)) print(padding2) prediction = np.pad(prediction, padding2, mode='constant', constant_values=0) assert all( [s1 == s2 for s1, s2 in zip(prediction.shape, orig_volume_shape)]) prediction = get_image(prediction) nib.save(prediction, os.path.join(dest_folder, Path(mask_path).name))