def preprocess_patient(self, input_files):
        """
        Used to predict new unseen data. Not used for the preprocessing of the training/test data
        :param input_files:
        :return:
        """
        from tuframework.training.model_restore import recursive_find_python_class
        preprocessor_name = self.plans.get('preprocessor_name')
        if preprocessor_name is None:
            if self.threeD:
                preprocessor_name = "GenericPreprocessor"
            else:
                preprocessor_name = "PreprocessorFor2D"

        print("using preprocessor", preprocessor_name)
        preprocessor_class = recursive_find_python_class([join(tuframework.__path__[0], "preprocessing")],
                                                         preprocessor_name,
                                                         current_module="tuframework.preprocessing")
        assert preprocessor_class is not None, "Could not find preprocessor %s in tuframework.preprocessing" % \
                                               preprocessor_name
        preprocessor = preprocessor_class(self.normalization_schemes, self.use_mask_for_norm,
                                          self.transpose_forward, self.intensity_properties)

        d, s, properties = preprocessor.preprocess_test_case(input_files,
                                                             self.plans['plans_per_stage'][self.stage][
                                                                 'current_spacing'])
        return d, s, properties
예제 #2
0
    def run_preprocessing(self, num_threads):
        if os.path.isdir(self.preprocessed_output_folder+"/"+"gt_segmentations"):
            shutil.rmtree(self.preprocessed_output_folder+"/"+ "gt_segmentations")
        shutil.copytree(self.folder_with_cropped_data+"/"+"gt_segmentations",
                        self.preprocessed_output_folder+"/"+"gt_segmentations")
        normalization_schemes = self.plans['normalization_schemes']
        use_nonzero_mask_for_normalization = self.plans['use_mask_for_norm']
        intensityproperties = self.plans['dataset_properties']['intensityproperties']
        preprocessor_class = recursive_find_python_class([tuframework.__path__[0]+"/"+ "preprocessing"],
                                                         self.preprocessor_name, current_module="tuframework.preprocessing")
        assert preprocessor_class is not None
        preprocessor = preprocessor_class(normalization_schemes, use_nonzero_mask_for_normalization,
                                         self.transpose_forward,
                                          intensityproperties)
        target_spacings = [i["current_spacing"] for i in self.plans_per_stage.values()]
        if self.plans['num_stages'] > 1 and not isinstance(num_threads, (list, tuple)):
            num_threads = (default_num_threads, num_threads)
        elif self.plans['num_stages'] == 1 and isinstance(num_threads, (list, tuple)):
            num_threads = num_threads[-1]
        #print("run_preprocessing:'data_identifier'", self.plans['data_identifier'])
        #print("run_preprocessing:folder_with_cropped_data", self.folder_with_cropped_data)
        #print("run_preprocessing:preprocessed_output_folder",  self.preprocessed_output_folder)

        preprocessor.run(target_spacings, self.folder_with_cropped_data, self.preprocessed_output_folder,
                         self.plans['data_identifier'], num_threads)
def get_default_configuration(
        network,
        task,
        network_trainer,
        plans_identifier=default_plans_identifier,
        search_in=(tuframework.__path__[0], "training", "network_training"),
        base_module='tuframework.training.network_training'):
    assert network in ['2d', '3d_lowres', '3d_fullres', '3d_cascade_fullres'], \
        "network can only be one of the following: \'3d\', \'3d_lowres\', \'3d_fullres\', \'3d_cascade_fullres\'"

    dataset_directory = preprocessing_output_dir + "/" + task

    if network == '2d':
        plans_file = preprocessing_output_dir + "/" + task + "/" + plans_identifier + "_plans_2D.pkl"
    else:
        plans_file = preprocessing_output_dir + "/" + task + "/" + plans_identifier + "_plans_3D.pkl"

    plans = load_pickle(plans_file)
    possible_stages = list(plans['plans_per_stage'].keys())

    if (network == '3d_cascade_fullres'
            or network == "3d_lowres") and len(possible_stages) == 1:
        raise RuntimeError(
            "3d_lowres/3d_cascade_fullres only applies if there is more than one stage. This task does "
            "not require the cascade. Run 3d_fullres instead")

    if network == '2d' or network == "3d_lowres":
        stage = 0
    else:
        stage = possible_stages[-1]

    trainer_class = recursive_find_python_class([join(*search_in)],
                                                network_trainer,
                                                current_module=base_module)

    output_folder_name = network_training_output_dir + "/" + network + "/" + task + "/" + network_trainer + "__" + plans_identifier

    print("###############################################")
    print("I am running the following tuframework: %s" % network)
    print("My trainer class is: ", trainer_class)
    print("For that I will be using the following configuration:")
    summarize_plans(plans_file)
    print("I am using stage %d from these plans" % stage)

    if (network == '2d'
            or len(possible_stages) > 1) and not network == '3d_lowres':
        batch_dice = True
        print("I am using batch dice + CE loss")
    else:
        batch_dice = False
        print("I am using sample dice + CE loss")

    print("\nI am using data from this folder: ",
          dataset_directory + "/" + plans['data_identifier'])
    print("###############################################")
    return plans_file, output_folder_name, dataset_directory, batch_dice, stage, trainer_class
예제 #4
0
    parser = argparse.ArgumentParser()
    parser.add_argument("network_trainer")
    parser.add_argument("task")
    parser.add_argument("fold", type=int)

    args = parser.parse_args()

    trainerclass = args.network_trainer
    task = args.task
    fold = args.fold

    plans_file, folder_with_preprocessed_data, output_folder_name, dataset_directory, batch_dice, stage = \
        get_default_configuration("3d_lowres", task)

    trainer_class = recursive_find_python_class(
        [tuframework.__path__[0] + "/" + "training", "network_training"],
        trainerclass, "tuframework.training.network_training")

    if trainer_class is None:
        raise RuntimeError(
            "Could not find trainer class in tuframework.training.network_training"
        )
    else:
        assert issubclass(
            trainer_class, tuframeworkTrainer
        ), "network_trainer was found but is not derived from tuframeworkTrainer"

    trainer = trainer_class(plans_file,
                            fold,
                            folder_with_preprocessed_data,
                            output_folder=output_folder_name,