Exemple #1
0
    def preprocess_predict_nifti(self,
                                 input_files,
                                 output_file=None,
                                 softmax_ouput_file=None):
        """
        Use this to predict new data
        :param input_files:
        :param output_file:
        :param softmax_ouput_file:
        :return:
        """
        print("preprocessing...")
        d, s, properties = self.preprocess_patient(input_files)
        print("predicting...")
        pred = self.predict_preprocessed_data_return_softmax(
            d, self.data_aug_params["mirror"], 1, False, 1,
            self.data_aug_params['mirror_axes'], True, True, 2,
            self.patch_size, True)
        pred = pred.transpose([0] + [i + 1 for i in self.transpose_backward])

        print("resampling to original spacing and nifti export...")
        save_segmentation_nifti_from_softmax(pred, output_file, properties, 3,
                                             None, None, None,
                                             softmax_ouput_file, None)
        print("done")
Exemple #2
0
def merge_files(args):
    files, properties_file, out_file, only_keep_largest_connected_component, min_region_size_per_class, override = args
    if override or not isfile(out_file):
        softmax = [np.load(f)['softmax'][None] for f in files]
        softmax = np.vstack(softmax)
        softmax = np.mean(softmax, 0)
        props = load_pickle(properties_file)
        save_segmentation_nifti_from_softmax(softmax, out_file, props, 1, None,
                                             None, None)
Exemple #3
0
def merge(args):
    file1, file2, properties_file, out_file = args
    if not isfile(out_file):
        res1 = np.load(file1)['softmax']
        res2 = np.load(file2)['softmax']
        props = load_pickle(properties_file)
        mn = np.mean((res1, res2), 0)
        save_segmentation_nifti_from_softmax(mn, out_file, props, 1, None,
                                             None, None)
Exemple #4
0
    def preprocess_predict_nifti(self,
                                 input_files: List[str],
                                 output_file: str = None,
                                 softmax_ouput_file: str = None,
                                 mixed_precision: bool = True) -> None:
        """
        Use this to predict new data
        :param input_files:
        :param output_file:
        :param softmax_ouput_file:
        :param mixed_precision:
        :return:
        """
        print("preprocessing...")
        d, s, properties = self.preprocess_patient(input_files)
        print("predicting...")
        pred = self.predict_preprocessed_data_return_seg_and_softmax(
            d,
            do_mirroring=self.data_aug_params["do_mirror"],
            mirror_axes=self.data_aug_params['mirror_axes'],
            use_sliding_window=True,
            step_size=0.5,
            use_gaussian=True,
            pad_border_mode='constant',
            pad_kwargs={'constant_values': 0},
            verbose=True,
            all_in_gpu=False,
            mixed_precision=mixed_precision)[1]
        pred = pred.transpose([0] + [i + 1 for i in self.transpose_backward])

        if 'segmentation_export_params' in self.plans.keys():
            force_separate_z = self.plans['segmentation_export_params'][
                'force_separate_z']
            interpolation_order = self.plans['segmentation_export_params'][
                'interpolation_order']
            interpolation_order_z = self.plans['segmentation_export_params'][
                'interpolation_order_z']
        else:
            force_separate_z = None
            interpolation_order = 1
            interpolation_order_z = 0

        print("resampling to original spacing and nifti export...")
        save_segmentation_nifti_from_softmax(
            pred,
            output_file,
            properties,
            interpolation_order,
            self.regions_class_order,
            None,
            None,
            softmax_ouput_file,
            None,
            force_separate_z=force_separate_z,
            interpolation_order_z=interpolation_order_z)
        print("done")
Exemple #5
0
def merge(args):
    file1, file2, properties_file, out_file = args
    if not isfile(out_file):
        res1 = np.load(file1)['softmax']
        res2 = np.load(file2)['softmax']
        props = load_pickle(properties_file)
        mn = np.mean((res1, res2), 0)
        save_segmentation_nifti_from_softmax(mn,
                                             out_file,
                                             props,
                                             3,
                                             None,
                                             None,
                                             None,
                                             force_separate_z=None,
                                             interpolation_order_z=0)
    def preprocess_predict_nifti(self,
                                 input_files,
                                 output_file=None,
                                 softmax_ouput_file=None,
                                 mixed_precision: bool = True):
        """
        Use this to predict new data
        :param input_files:
        :param output_file:
        :param softmax_ouput_file:
        :param mixed_precision:
        :return:
        """
        print("preprocessing...")
        d, s, properties = self.preprocess_patient(input_files)
        print("predicting...")
        pred = self.predict_preprocessed_data_return_seg_and_softmax(
            d,
            do_mirroring=self.data_aug_params["do_mirror"],
            mirror_axes=self.data_aug_params['mirror_axes'],
            use_sliding_window=True,
            step_size=0.5,
            use_gaussian=True,
            pad_border_mode='constant',
            pad_kwargs={'constant_values': 0},
            all_in_gpu=True,
            mixed_precision=mixed_precision)[1]
        pred = pred.transpose([0] + [i + 1 for i in self.transpose_backward])

        print("resampling to original spacing and nifti export...")
        save_segmentation_nifti_from_softmax(pred,
                                             output_file,
                                             properties,
                                             3,
                                             None,
                                             None,
                                             None,
                                             softmax_ouput_file,
                                             None,
                                             force_separate_z=False,
                                             interpolation_order_z=3)
        print("done")
def merge_files(files, properties_files, out_file, override, store_npz):
    if override or not isfile(out_file):
        softmax = [np.load(f)['softmax'][None] for f in files]
        softmax = np.vstack(softmax)
        softmax = np.mean(softmax, 0)
        props = [load_pickle(f) for f in properties_files]

        reg_class_orders = [
            p['regions_class_order']
            if 'regions_class_order' in p.keys() else None for p in props
        ]

        if not all([i is None for i in reg_class_orders]):
            # if reg_class_orders are not None then they must be the same in all pkls
            tmp = reg_class_orders[0]
            for r in reg_class_orders[1:]:
                assert tmp == r, 'If merging files with regions_class_order, the regions_class_orders of all ' \
                                 'files must be the same. regions_class_order: %s, \n files: %s' % \
                                 (str(reg_class_orders), str(files))
            regions_class_order = tmp
        else:
            regions_class_order = None

        # Softmax probabilities are already at target spacing so this will not do any resampling (resampling parameters
        # don't matter here)
        save_segmentation_nifti_from_softmax(softmax,
                                             out_file,
                                             props[0],
                                             3,
                                             regions_class_order,
                                             None,
                                             None,
                                             force_separate_z=None)
        if store_npz:
            np.savez_compressed(out_file[:-7] + ".npz", softmax=softmax)
            save_pickle(props, out_file[:-7] + ".pkl")