filenames = [ x for x in os.listdir(PREPROCESSING_DIR) if not os.path.isdir((os.path.join(PREPROCESSING_DIR, x))) ] filenames.sort() for filename in filenames: # load nifti file data nii_obj = nib.load(os.path.join(PREPROCESSING_DIR, filename)) nii_img = nii_obj.get_data() header = nii_obj.header affine = nii_obj.affine # reshape to account for implicit "1" channel nii_img = np.reshape(nii_img, nii_img.shape + (1, )) nii_img = pad_image(nii_img) # segment segmented_img = apply_model_single_input(nii_img, model) # save resultant image segmented_filename = os.path.join(SEG_DIR, filename) segmented_nii_obj = nib.Nifti1Image(segmented_img, affine=affine, header=header) nib.save(segmented_nii_obj, segmented_filename) # Reorient back to original before comparisons print("Reorienting...") utils.reorient(filename, DATA_DIR, SEG_DIR)
nii_img = nii_obj.get_data() header = nii_obj.header affine = nii_obj.affine # load mask file data mask_obj = nib.load(os.path.join(PREPROCESSING_DIR, mask)) mask_img = mask_obj.get_data() # pad and reshape to account for implicit "1" channel nii_img = np.reshape(nii_img, nii_img.shape + (1,)) orig_shape = nii_img.shape # if the mask is larger, pad to hardcoded value if mask_img.shape[0] > nii_img.shape[0] or mask_img.shape[1] > nii_img.shape[1]: TARGET_DIMS = (656,656,96) nii_img = pad_image(nii_img, target_dims=TARGET_DIMS) mask_img = pad_image(mask_img, target_dims=TARGET_DIMS) else: # otherwise pad normally nii_img = pad_image(nii_img) mask_img = pad_image(mask_img, target_dims=nii_img.shape[:3]) # segment segmented_img = apply_model_single_input(nii_img, model) pred_shape = segmented_img.shape # create nii obj segmented_nii_obj = nib.Nifti1Image( segmented_img, affine=affine, header=header)
# used only for printing result mean_dice = 0 pred_vols = [] gt_vols = [] for filename, mask in zip(filenames, masks): # load nifti file data nii_obj = nib.load(os.path.join(PREPROCESSING_DIR, filename)) nii_img = nii_obj.get_data() header = nii_obj.header affine = nii_obj.affine # pad and reshape to account for implicit "1" channel nii_img = np.reshape(nii_img, nii_img.shape + (1, )) nii_img = pad_image(nii_img) # segment segmented_img = apply_model_single_input(nii_img, model) # save resultant image segmented_filename = os.path.join(SEG_DIR, filename) segmented_nii_obj = nib.Nifti1Image(segmented_img, affine=affine, header=header) nib.save(segmented_nii_obj, segmented_filename) # load mask file data mask_obj = nib.load(os.path.join(PREPROCESSING_DIR, mask)) mask_img = mask_obj.get_data() mask_img = pad_image(mask_img)
src_dir=src_dir, dst_dir=PREPROCESSING_DIR, tmp_dir=TMPDIR, verbose=0, skullstrip_script_path=results.SKULLSTRIP, remove_tmp_files=True) ######################## SEGMENT FILE ######################## # load nifti file data nii_obj = nib.load(os.path.join(PREPROCESSING_DIR, filename)) nii_img = nii_obj.get_data() # reshape to account for implicit "1" channel nii_img = np.reshape(nii_img, nii_img.shape + (1,)) nii_img, pads = pad_image(nii_img) # segment segmented_img = apply_model_single_input(nii_img, model) # Crop back to original dimensions to undo the padding ( (left_pad, right_pad), (top_pad, bottom_pad), _, ) = pads if left_pad != 0: segmented_img = segmented_img[left_pad:, ...] if right_pad != 0: segmented_img = segmented_img[:-right_pad, ...] if top_pad != 0: