Ejemplo n.º 1
0
def test_train_classification_rad_3d():
    application_data = '3d_rad_segmentation'
    # read and initialize parameters for specific data dimension
    parameters = parseConfig(testingDir + '/config_classification.yaml',
                             version_check=False)
    parameters['patch_size'] = patch_size['3D']
    parameters['psize'] = patch_size['3D']
    parameters['model']['dimension'] = 3
    parameters['model']['class_list'] = [0, 1]
    # read and parse csv
    training_data, headers = parseTrainingCSV(
        inputDir + '/train_3d_rad_classification.csv')
    # loop through selected models and train for single epoch
    for model in all_models_regression:
        parameters['model']['architecture'] = model
        shutil.rmtree(outputDir)  # overwrite previous results
        Path(outputDir).mkdir(parents=True, exist_ok=True)
        TrainingManager(dataframe=training_data,
                        headers=headers,
                        outputDir=outputDir,
                        parameters=parameters,
                        device='cpu',
                        reset_prev=True)

    print('passed')
Ejemplo n.º 2
0
def test_train_segmentation_rad_3d():
  print('Starting 3D Rad segmentation tests')
  application_data = '3d_rad_segmentation'
  parameters = parseConfig(inputDir + '/' + application_data + '/sample_training.yaml', version_check = False)
  parameters['modality'] = 'rad'
  training_data, headers = parseTrainingCSV(inputDir + '/train_' + application_data + '.csv')
  for model in all_models_segmentation:
    parameters['model']['architecture'] = model 
    currentOutputDir = os.path.join(outputDir, application_data + '_' + model)
    Path(currentOutputDir).mkdir(parents=True, exist_ok=True)
    TrainingManager(dataframe=training_data, headers = headers, outputDir=currentOutputDir, parameters=parameters, device='cpu')

  print('passed')
Ejemplo n.º 3
0
def test_classification_rad_3d():
    application_data = '3d_rad_segmentation'
    parameters = parseConfig(inputDir + '/' + application_data +
                             '/sample_training_classification.yaml',
                             version_check=False)
    training_data, headers = parseTrainingCSV(
        inputDir + '/train_3d_rad_classification.csv')
    for model in all_models_regression:
        parameters['model']['architecture'] = model
        shutil.rmtree(outputDir)  # overwrite previous results
        Path(outputDir).mkdir(parents=True, exist_ok=True)
        TrainingManager(dataframe=training_data,
                        headers=headers,
                        outputDir=outputDir,
                        parameters=parameters,
                        device='cpu',
                        reset_prev=True)

    print('passed')
Ejemplo n.º 4
0
def test_train_segmentation_rad_2d():
    print('Starting 2D Rad segmentation tests')
    application_data = '2d_rad_segmentation'
    parameters = parseConfig(inputDir + '/' + application_data +
                             '/sample_training.yaml',
                             version_check=False)
    parameters['modality'] = 'rad'
    training_data, headers = parseTrainingCSV(inputDir + '/train_' +
                                              application_data + '.csv')
    for model in all_models_segmentation:
        parameters['model']['architecture'] = model
        shutil.rmtree(outputDir)  # overwrite previous results
        Path(outputDir).mkdir(parents=True, exist_ok=True)
        TrainingManager(dataframe=training_data,
                        headers=headers,
                        outputDir=outputDir,
                        parameters=parameters,
                        device='cpu',
                        reset_prev=True)

    print('passed')
Ejemplo n.º 5
0
def main_run(data_csv, config_file, output_dir, train_mode, device, resume, reset):
    """
    Main function that runs the training and inference.

    Args:
        data_csv (str): The CSV file of the training data.
        config_file (str): The YAML file of the training configuration.
        output_dir (str): The output directory.
        train_mode (bool): Whether to train or infer.
        device (str): The device type.
        resume (bool): Whether the previous run will be resumed or not.
        reset (bool): Whether the previous run will be reset or not.

    Raises:
        ValueError: Parameter check from previous run.
    """
    file_data_full = data_csv
    model_parameters = config_file
    device = device
    parameters = parseConfig(model_parameters)
    # in case the data being passed is already processed, check if the previous parameters exists,
    # and if it does, compare the two and if they are the same, ensure no preprocess is done.
    model_parameters_prev = os.path.join(os.path.dirname(output_dir), "parameters.pkl")
    if train_mode:
        if not (reset) or not (resume):
            print(
                "Trying to resume training without changing any parameters from previous run.",
                flush=True,
            )
            if os.path.exists(model_parameters_prev):
                parameters_prev = pickle.load(open(model_parameters_prev, "rb"))
                if parameters != parameters_prev:
                    raise ValueError(
                        "The parameters are not the same as the ones stored in the previous run, please re-check."
                    )

    parameters["output_dir"] = output_dir

    if "-1" in device:
        device = "cpu"

    if train_mode:  # train mode
        Path(parameters["output_dir"]).mkdir(parents=True, exist_ok=True)

    # parse training CSV
    if "," in file_data_full:
        # training and validation pre-split
        data_full = None
        both_csvs = file_data_full.split(",")
        data_train, headers_train = parseTrainingCSV(both_csvs[0], train=train_mode)
        data_validation, headers_validation = parseTrainingCSV(
            both_csvs[1], train=train_mode
        )

        if headers_train != headers_validation:
            sys.exit(
                "The training and validation CSVs do not have the same header information."
            )

        parameters = populate_header_in_parameters(parameters, headers_train)
        # if we are here, it is assumed that the user wants to do training
        if train_mode:
            TrainingManager_split(
                dataframe_train=data_train,
                dataframe_validation=data_validation,
                outputDir=parameters["output_dir"],
                parameters=parameters,
                device=device,
                resume=resume,
                reset=reset,
            )
    else:
        data_full, headers = parseTrainingCSV(file_data_full, train=train_mode)
        parameters = populate_header_in_parameters(parameters, headers)
        if train_mode:
            TrainingManager(
                dataframe=data_full,
                outputDir=parameters["output_dir"],
                parameters=parameters,
                device=device,
                resume=resume,
                reset=reset,
            )
        else:
            InferenceManager(
                dataframe=data_full,
                outputDir=parameters["output_dir"],
                parameters=parameters,
                device=device,
            )