def __init__(
        self, x_set, y_set, parameters
    ):  # x_set = path to exam_list_cropped, y_set= path to xls file
        # INPUT
        val_split = parameters["validation_split"]  # split rate
        train_split = 1 - val_split
        total_patients = len(pickling.unpickle_from_file(x_set))
        number_patients_for_training = int((total_patients) * (train_split))

        if parameters["training"] == True:  # if doing the validation split
            x_set = pickling.unpickle_from_file(
                x_set
            )[:
              number_patients_for_training]  # list of dictionaries from exam_list_cropped.pkl
        else:
            x_set = pickling.unpickle_from_file(
                x_set)[number_patients_for_training:]

        y_set = get_output_dict(
            xls_path=y_set,
            images_path=parameters["image_path"])  # get output dict

        self.random_number_generator = np.random.RandomState(
            parameters["seed"])
        self.x, self.y = x_set, y_set
        self.batch_size = parameters['batch_size']
        self.parameters = parameters
예제 #2
0
def load_inputs(image_path,
                metadata_path,
                use_heatmaps,
                benign_heatmap_path=None,
                malignant_heatmap_path=None):
    """
    Load a single input example, optionally with heatmaps
    """
    if use_heatmaps:
        assert benign_heatmap_path is not None
        assert malignant_heatmap_path is not None
    else:
        assert benign_heatmap_path is None
        assert malignant_heatmap_path is None
    metadata = pickling.unpickle_from_file(metadata_path)
    image = loading.load_image(
        image_path=image_path,
        view=metadata["full_view"],
        horizontal_flip=metadata["horizontal_flip"],
    )
    if use_heatmaps:
        heatmaps = loading.load_heatmaps(
            benign_heatmap_path=benign_heatmap_path,
            malignant_heatmap_path=malignant_heatmap_path,
            view=metadata["full_view"],
            horizontal_flip=metadata["horizontal_flip"],
        )
    else:
        heatmaps = None
    return ModelInput(image=image, heatmaps=heatmaps, metadata=metadata)
예제 #3
0
    def __init__(self, data_dir, file_data_list, 
        data_list_index=0, channels=3, randomly_select_image_from_exam=False, 
        reject_unknowns=True, before_first_adverse_event_only=True, include_post_inh_admission_cases=False, labels_in_pkl_loader=[], transform=None, multiclass=False):
        """
        Args:
            data_dir: path to image directory.
            image_list_file: path to the file containing images
                with corresponding labels.
            transform: optional transform to be applied on a sample.
        """
        self.channels = channels
        data_list = list(unpickle_from_file(file_data_list)[data_list_index])
        exam_list_per_patient = get_exams(data_list, reject_unknowns, before_first_adverse_event_only, include_post_inh_admission_cases)
        self.flattened_exam_list, self.patient_to_exam_indices_dict = flatten_exams_list(exam_list_per_patient)
        self.data_dir = data_dir
        self.randomly_select_image_from_exam = randomly_select_image_from_exam
        
        if multiclass:
            assert labels_in_pkl_loader == ['label_24', 'label_48', 'label_72','label_96']

        self.labels_in_pkl_loader = labels_in_pkl_loader
        self.labels = []

        for exam in self.flattened_exam_list:
            if multiclass:
                vectorized_label = self.map_to_multicls(exam['label'])
            else:
                vectorized_label = self.vectorize_label(exam['label'])
            self.labels.append(vectorized_label)
            
        self.transform = transform
예제 #4
0
def produce_heatmaps(model, device, parameters):
    """
    Generates heatmaps for all exams
    """
    # Load exam info
    exam_list = pickling.unpickle_from_file(parameters['data_file'])    

    # Create heatmaps
    making_heatmap_with_large_minibatch_potential(parameters, model, exam_list, device)
def get_optimal_center_single(cropped_mammogram_path, metadata_path):
    """
    Get optimal center for single example
    """
    metadata = pickling.unpickle_from_file(metadata_path)
    image = reading_images.read_image_png(cropped_mammogram_path)
    optimal_center = get_optimal_centers.extract_center(metadata, image)
    metadata["best_center"] = optimal_center
    pickling.pickle_to_file(metadata_path, metadata)
예제 #6
0
def convert_output_results(input_data_folder, heatmaps_path, 
                           data_path, output_data_folder):
    exam_list = pickling.unpickle_from_file(data_path)
    os.makedirs(os.path.dirname(output_data_folder), exist_ok=True)
    dcm_files = glob.glob(os.path.join(input_data_folder,"**","*.dcm"), recursive=True)
    image_extension = ".png"
    for datum in tqdm.tqdm(exam_list):
        loaded_heatmaps_dict = {view: [] for view in VIEWS.LIST}
        for view in VIEWS.LIST:
            for short_file_path in datum[view]:
                loaded_heatmaps = loading.load_heatmaps(
                    benign_heatmap_path=os.path.join(heatmaps_path, "heatmap_benign",
                                                     short_file_path + image_extension),
                    malignant_heatmap_path=os.path.join(heatmaps_path, "heatmap_malignant",
                                                        short_file_path + image_extension),
                    view=view,
                    horizontal_flip=datum["horizontal_flip"],
                )
                loaded_heatmaps_dict[view].append(loaded_heatmaps)
                loaded_heatmaps = np.stack([loaded_heatmaps[:,:,1:2], 
                                            loaded_heatmaps[:,:,0:1], 
                                            np.zeros(loaded_heatmaps[:,:,1:2].shape)], 
                                            axis=2)[:,:,:,0].astype(np.uint8)

                laterality = view.split("-")[0]
                projection_view = view.split("-")[1]
                dcm_file = find_view(dcm_files, laterality, projection_view)
                ds = pydicom.read_file(dcm_file)
                pixel_array = ds.pixel_array
                pixel_array = (pixel_array - np.min(pixel_array)) / float(np.max(pixel_array) - np.min(pixel_array)) * 255
                pixel_array = np.expand_dims(pixel_array.astype(np.uint8), axis=2)
                pixel_array = np.stack([pixel_array, pixel_array, pixel_array], axis=2)[:,:,:,0]
                coords = datum["window_location"][view][0]
                if laterality == "R":
                    loaded_heatmaps = np.fliplr(loaded_heatmaps)
                sub_pixel_array = pixel_array[coords[0]:coords[1],coords[2]:coords[3],0:3]
                bg = Image.fromarray(sub_pixel_array)
                fg = Image.fromarray(loaded_heatmaps)
                blended = Image.blend(bg, fg, 0.25)
                pixel_array[coords[0]:coords[1],coords[2]:coords[3],0:3] = np.asarray(blended)
                sc_image = SCImage()
                sc_image.create_empty_iod()
                sc_image.initiate()
                sc_image.set_dicom_attribute("PatientName", ds.PatientName)
                sc_image.set_dicom_attribute("PatientID", ds.PatientID)
                sc_image.set_dicom_attribute("AccessionNumber", ds.AccessionNumber)
                sc_image.set_dicom_attribute("StudyID", ds.StudyID)
                sc_image.set_dicom_attribute("StudyInstanceUID", ds.StudyInstanceUID)
                sc_image.set_dicom_attribute("StudyDate", ds.StudyDate if "StudyDate" in ds else "")
                sc_image.set_dicom_attribute("StudyTime", ds.StudyTime if "StudyTime" in ds else "")
                sc_image.set_dicom_attribute("StudyDescription", ds.StudyTime if "StudyDescription" in ds else "")
                sc_image.set_dicom_attribute("SeriesDescription", f"Original {view} + heatmap")
                # sc_image.add_pixel_data(loaded_heatmaps)
                sc_image.add_pixel_data(pixel_array)
                output_file = os.path.join(output_data_folder, "SC_" + view + ".dcm")
                sc_image.write_to_file(output_file)
예제 #7
0
def produce_heatmaps(parameters):
    """
    Generate heatmaps for single example
    """
    random.seed(parameters['seed'])
    image_path = parameters["cropped_mammogram_path"]
    model, device = run_producer.load_model(parameters)
    metadata = pickling.unpickle_from_file(parameters['metadata_path'])
    patches, case = run_producer.sample_patches_single(
        image_path=image_path,
        view=metadata["view"],
        horizontal_flip=metadata['horizontal_flip'],
        parameters=parameters,
    )
    all_prob = run_producer.get_all_prob(
        all_patches=patches,
        minibatch_size=parameters["minibatch_size"],
        model=model,
        device=device,
        parameters=parameters)
    heatmap_malignant, _ = run_producer.probabilities_to_heatmap(
        patch_counter=0,
        all_prob=all_prob,
        image_shape=case[0],
        length_stride_list=case[4],
        width_stride_list=case[3],
        patch_size=parameters['patch_size'],
        heatmap_type=parameters['heatmap_type'][0],
    )
    heatmap_benign, patch_counter = run_producer.probabilities_to_heatmap(
        patch_counter=0,
        all_prob=all_prob,
        image_shape=case[0],
        length_stride_list=case[4],
        width_stride_list=case[3],
        patch_size=parameters['patch_size'],
        heatmap_type=parameters['heatmap_type'][1],
    )
    heatmap_malignant = loading.flip_image(
        image=heatmap_malignant,
        view=metadata["view"],
        horizontal_flip=metadata["horizontal_flip"],
    )
    heatmap_benign = loading.flip_image(
        image=heatmap_benign,
        view=metadata["view"],
        horizontal_flip=metadata["horizontal_flip"],
    )
    saving_images.save_image_as_hdf5(
        image=heatmap_malignant,
        filename=parameters["heatmap_path_malignant"],
    )
    saving_images.save_image_as_hdf5(
        image=heatmap_benign,
        filename=parameters["heatmap_path_benign"],
    )
예제 #8
0
파일: run_model.py 프로젝트: BigOsoft/IMA
def load_run_save(data_path, output_path, parameters):
    """
    Outputs the predictions as csv file
    """
    exam_list = pickling.unpickle_from_file(data_path)
    model, device = load_model(parameters)
    predictions = run_model(model, device, exam_list, parameters)
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    # Take the positive prediction
    df = pd.DataFrame(predictions, columns=LABELS.LIST)
    df.to_csv(output_path, index=False, float_format='%.4f')
예제 #9
0
def main(cropped_exam_list_path, data_prefix, output_exam_list_path, num_processes=1):
    exam_list = pickling.unpickle_from_file(cropped_exam_list_path)
    data_list = data_handling.unpack_exam_into_images(exam_list, cropped=True)
    optimal_centers = get_optimal_centers(
        data_list=data_list,
        data_prefix=data_prefix,
        num_processes=num_processes
    )
    data_handling.add_metadata(exam_list, "best_center", optimal_centers)
    os.makedirs(os.path.dirname(output_exam_list_path), exist_ok=True)
    pickling.pickle_to_file(output_exam_list_path, exam_list)
def load_run_save(model_path, data_path, output_path, parameters):
    """
    Outputs the predictions as csv file
    """
    input_channels = 3 if parameters["use_heatmaps"] else 1
    model = models.SplitBreastModel(input_channels)
    model.load_state_dict(torch.load(model_path)["model"])
    exam_list = pickling.unpickle_from_file(data_path)
    predictions = run_model(model, exam_list, parameters)
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    # Take the positive prediction
    df = pd.DataFrame(predictions, columns=LABELS.LIST)
    df.to_csv(output_path, index=False, float_format='%.4f')
예제 #11
0
def crop_mammogram(input_data_folder, exam_list_path, cropped_exam_list_path,
                   output_data_folder, num_processes, num_iterations,
                   buffer_size):
    """
    In parallel, crops mammograms in DICOM format found in input_data_folder and save as png format in
    output_data_folder and saves new image list in cropped_image_list_path
    """

    # list of exams (one dictionary per exam)
    exam_list = pickling.unpickle_from_file(exam_list_path)

    # list per image (one dictionary per image). It contains same information than in list of exams + cropped information if present.
    image_list = data_handling.unpack_exam_into_images(exam_list)

    if os.path.exists(output_data_folder):
        # Prevent overwriting to an existing directory
        print("Error: the directory to save cropped images already exists.")
        return
    else:
        os.makedirs(output_data_folder)

    crop_mammogram_one_image_func = partial(
        crop_mammogram_one_image_short_path,
        input_data_folder=input_data_folder,
        output_data_folder=output_data_folder,
        num_iterations=num_iterations,
        buffer_size=buffer_size,
    )
    with Pool(num_processes) as pool:
        cropped_image_info = pool.map(crop_mammogram_one_image_func,
                                      image_list)
    # F: cropped image info returns a list. Each entry is the return of a single execution
    # F: of crop_mammogram_one_image_func.

    # F: each dict defined contains the information of all images
    window_location_dict = dict([x[0] for x in cropped_image_info])
    rightmost_points_dict = dict([x[1] for x in cropped_image_info])
    bottommost_points_dict = dict([x[2] for x in cropped_image_info])
    distance_from_starting_side_dict = dict([x[3] for x in cropped_image_info])

    data_handling.add_metadata(exam_list, "window_location",
                               window_location_dict)
    data_handling.add_metadata(exam_list, "rightmost_points",
                               rightmost_points_dict)
    data_handling.add_metadata(exam_list, "bottommost_points",
                               bottommost_points_dict)
    data_handling.add_metadata(exam_list, "distance_from_starting_side",
                               distance_from_starting_side_dict)

    pickling.pickle_to_file(cropped_exam_list_path, exam_list)
            def crop_mammogram(input_data_folder, exam_list_path,
                               cropped_exam_list_path, output_data_folder,
                               num_processes, num_iterations, buffer_size):
                """
                In parallel, crops mammograms in DICOM format found in input_data_folder and save as png format in
                output_data_folder and saves new image list in cropped_image_list_path
                """
                exam_list = pickling.unpickle_from_file(exam_list_path)

                image_list = data_handling.unpack_exam_into_images(exam_list)

                if os.path.exists(output_data_folder):
                    # Prevent overwriting to an existing directory
                    print(
                        "Error: the directory to save cropped images already exists."
                    )
                    return
                else:
                    os.makedirs(output_data_folder)
                #global crop_mammogram_one_image_func
                crop_mammogram_one_image_func = partial(
                    crop_mammogram_one_image_short_path,
                    input_data_folder=input_data_folder,
                    output_data_folder=output_data_folder,
                    num_iterations=num_iterations,
                    buffer_size=buffer_size,
                )
                with Pool(num_processes) as pool:
                    cropped_image_info = pool.map(
                        crop_mammogram_one_image_func, image_list)

                window_location_dict = dict([x[0] for x in cropped_image_info])
                rightmost_points_dict = dict(
                    [x[1] for x in cropped_image_info])
                bottommost_points_dict = dict(
                    [x[2] for x in cropped_image_info])
                distance_from_starting_side_dict = dict(
                    [x[3] for x in cropped_image_info])

                data_handling.add_metadata(exam_list, "window_location",
                                           window_location_dict)
                data_handling.add_metadata(exam_list, "rightmost_points",
                                           rightmost_points_dict)
                data_handling.add_metadata(exam_list, "bottommost_points",
                                           bottommost_points_dict)
                data_handling.add_metadata(exam_list,
                                           "distance_from_starting_side",
                                           distance_from_starting_side_dict)

                pickling.pickle_to_file(cropped_exam_list_path, exam_list)
예제 #13
0
def crop_mammogram():
    id_patient = request.args.get('id')
    input_data_folder = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/' + id_patient
    # exam_list_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/exam_list_before_cropping.pkl'
    exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/filename.pkl'
    cropped_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient + '/' + id_patient + '.pkl'
    output_data_folder = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient
    num_processes = 10
    num_iterations = 100
    buffer_size = 50

    exam_list = pickling.unpickle_from_file(exam_list_path)

    image_list = data_handling.unpack_exam_into_images(exam_list)

    if os.path.exists(output_data_folder):
        # Prevent overwriting to an existing directory
        print("Error: the directory to save cropped images already exists.")
        sh.rmtree(
            'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/cropped_images'
        )
        os.makedirs(output_data_folder)

    crop_mammogram_one_image_func = partial(
        crop_mammogram_one_image,
        input_data_folder=input_data_folder,
        output_data_folder=output_data_folder,
        num_iterations=num_iterations,
        buffer_size=buffer_size,
    )
    with Pool(num_processes) as pool:
        cropped_image_info = pool.map(crop_mammogram_one_image_func,
                                      image_list)

    window_location_dict = dict([x[0] for x in cropped_image_info])
    rightmost_points_dict = dict([x[1] for x in cropped_image_info])
    bottommost_points_dict = dict([x[2] for x in cropped_image_info])
    distance_from_starting_side_dict = dict([x[3] for x in cropped_image_info])

    data_handling.add_metadata(exam_list, "window_location",
                               window_location_dict)
    data_handling.add_metadata(exam_list, "rightmost_points",
                               rightmost_points_dict)
    data_handling.add_metadata(exam_list, "bottommost_points",
                               bottommost_points_dict)
    data_handling.add_metadata(exam_list, "distance_from_starting_side",
                               distance_from_starting_side_dict)

    pickling.pickle_to_file(cropped_exam_list_path, exam_list)
    return jsonify("Done")
예제 #14
0
def run_single_model(model_path, data_path, parameters, turn_on_visualization):
    """
    Load a single model and run on sample data
    """
    # construct model
    model = gmic.GMIC(parameters)
    # load parameters
    if parameters["device_type"] == "gpu":
        model.load_state_dict(torch.load(model_path), strict=False)
    else:
        model.load_state_dict(torch.load(model_path, map_location="cpu"),
                              strict=False)
    # load metadata
    exam_list = pickling.unpickle_from_file(data_path)
    # run the model on the dataset
    output_df = run_model(model, exam_list, parameters, turn_on_visualization)
    return output_df
예제 #15
0
def main():
    parser = argparse.ArgumentParser(
        description='Run image-only model or image+heatmap model')
    parser.add_argument('--model-mode',
                        default=MODELMODES.VIEW_SPLIT,
                        type=str)
    parser.add_argument('--model-path', required=True)
    parser.add_argument('--data-path', required=True)
    parser.add_argument('--image-path', required=True)
    parser.add_argument('--output-path', required=True)
    parser.add_argument('--batch-size', default=1, type=int)
    parser.add_argument('--seed', default=0, type=int)
    parser.add_argument('--use-heatmaps', action="store_true")
    parser.add_argument('--heatmaps-path')
    parser.add_argument('--use-augmentation', action="store_true")
    parser.add_argument('--use-hdf5', action="store_true")
    parser.add_argument('--num-epochs', default=1, type=int)
    parser.add_argument('--device-type', default="cpu", choices=['gpu', 'cpu'])
    parser.add_argument("--gpu-number", type=int, default=0)
    args = parser.parse_args()

    parameters = {
        "device_type": args.device_type,
        "gpu_number": args.gpu_number,
        "max_crop_noise": (100, 100),
        "max_crop_size_noise": 100,
        "image_path": args.image_path,
        "batch_size": args.batch_size,
        "seed": args.seed,
        "augmentation": args.use_augmentation,
        "num_epochs": args.num_epochs,
        "use_heatmaps": args.use_heatmaps,
        "heatmaps_path": args.heatmaps_path,
        "use_hdf5": args.use_hdf5,
        "model_mode": args.model_mode,
        "model_path": args.model_path,
    }

    exam_list = pickling.unpickle_from_file(args.data_path)

    load_run_save(
        data_path=args.data_path,
        output_path=args.output_path,
        parameters=parameters,
    )
예제 #16
0
def main():
    id_patient = request.args.get('id');
    cropped_exam_list_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'+id_patient+'/'+id_patient+'.pkl'
    data_prefix='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'+id_patient
    output_exam_list_path='sample_output/'+id_patient+'.pkl'
    num_processes = 10
    id_patient = request.args.get('id');
    exam_list = pickling.unpickle_from_file(cropped_exam_list_path)
    data_list = data_handling.unpack_exam_into_images(exam_list, cropped=True)
    optimal_centers = get_optimal_centers(
        data_list=data_list,
        data_prefix=data_prefix,
        num_processes=num_processes
    )
    data_handling.add_metadata(exam_list, "best_center", optimal_centers)
    os.makedirs(os.path.dirname(output_exam_list_path), exist_ok=True)
    pickling.pickle_to_file(output_exam_list_path, exam_list)
    return jsonify("Done")
예제 #17
0
 def __init__(self, data_dir, file_data_list, uncertainty_label,
              data_list_index=0, channels=3, randomly_select_image_from_exam=False, transform=None):
     '''
     Args:
         data_dir: path to image directory.
         image_list_file: path to the file containing images
             with corresponding labels.
         transform: optional transform to be applied on a sample.
     '''
     self.data_dir = data_dir
     self.uncertainty_label = uncertainty_label
     self.channels = channels
     self.randomly_select_image_from_exam = randomly_select_image_from_exam
     self.transform = transform
     data_list = unpickle_from_file(file_data_list)[data_list_index]
     exam_list_per_patient = get_exams(data_list, False, False, False)
     self.flattened_exam_list, self.patient_to_exam_indices_dict = flatten_exams_list(exam_list_per_patient)
     self.labels = []
     for exam in self.flattened_exam_list:
         self.labels.append(self.vectorize_label(exam['label']))
     self.labels = np.array(self.labels)
예제 #18
0
def load_model_and_produce_heatmaps(parameters):
    """
    Loads trained patch classifier and generates heatmaps for all exams
    """
    # set random seed at the beginning of program
    random.seed(parameters['seed'])

    if (parameters["device_type"] == "gpu") and torch.has_cudnn:
        device = torch.device("cuda:{}".format(parameters["gpu_number"]))
    else:
        device = torch.device("cpu")

    model = models.ModifiedDenseNet121(num_classes=parameters['number_of_classes'])
    model.load_from_path(parameters["initial_parameters"])
    model = model.to(device)
    model.eval()
    
    # Load exam info
    exam_list = pickling.unpickle_from_file(parameters['data_file'])    

    # Create heatmaps
    making_heatmap_with_large_minibatch_potential(parameters, model, exam_list, device)
예제 #19
0
def crop_mammogram():
    id_patient = request.args.get('id')
    input_data_folder = 'C:/Users/ultra/PycharmProjects/BIRADS_classifier-master/images/' + id_patient
    # exam_list_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/exam_list_before_cropping.pkl'
    exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/sample_data/filename.pkl'
    cropped_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient + '/' + id_patient + '.pkl'
    output_data_folder = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient
    num_processes = 10
    num_iterations = 100
    buffer_size = 50

    exam_list = pickling.unpickle_from_file(exam_list_path)

    image_list = data_handling.unpack_exam_into_images(exam_list)

    if os.path.exists(output_data_folder):
        # Prevent overwriting to an existing directory
        print("Error: the directory to save cropped images already exists.")
        sh.rmtree(
            'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'
            + id_patient)
        os.makedirs(output_data_folder)

    crop_mammogram_one_image_func = ps(
        crop_mammogram_one_image,
        input_data_folder=input_data_folder,
        output_data_folder=output_data_folder,
        num_iterations=num_iterations,
        buffer_size=buffer_size,
    )
    with Pool(num_processes) as pool:
        cropped_image_info = pool.map(crop_mammogram_one_image_func,
                                      image_list)

    window_location_dict = dict([x[0] for x in cropped_image_info])
    rightmost_points_dict = dict([x[1] for x in cropped_image_info])
    bottommost_points_dict = dict([x[2] for x in cropped_image_info])
    distance_from_starting_side_dict = dict([x[3] for x in cropped_image_info])

    data_handling.add_metadata(exam_list, "window_location",
                               window_location_dict)
    data_handling.add_metadata(exam_list, "rightmost_points",
                               rightmost_points_dict)
    data_handling.add_metadata(exam_list, "bottommost_points",
                               bottommost_points_dict)
    data_handling.add_metadata(exam_list, "distance_from_starting_side",
                               distance_from_starting_side_dict)

    pickling.pickle_to_file(cropped_exam_list_path, exam_list)

    ###NEXT####

    cropped_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient + '/' + id_patient + '.pkl'
    data_prefix = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/' + id_patient
    output_exam_list_path = 'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/' + id_patient + '.pkl'
    num_processes = 10
    id_patient = request.args.get('id')
    exam_list = pickling.unpickle_from_file(cropped_exam_list_path)
    data_list = data_handling.unpack_exam_into_images(exam_list, cropped=True)
    optimal_centers = get_optimal_centers(data_list=data_list,
                                          data_prefix=data_prefix,
                                          num_processes=num_processes)
    data_handling.add_metadata(exam_list, "best_center", optimal_centers)
    os.makedirs(os.path.dirname(output_exam_list_path), exist_ok=True)
    pickling.pickle_to_file(output_exam_list_path, exam_list)

    ###NEXT #####
    parser = argparse.ArgumentParser(
        description='Run image-only model or image+heatmap model')
    # parser.add_argument('--model-path', required=True)
    # parser.add_argument('--data-path', required=True)
    # parser.add_argument('--image-path', required=True)
    # parser.add_argument('--output-path', required=True)
    parser.add_argument('--batch-size', default=1, type=int)
    parser.add_argument('--seed', default=0, type=int)
    parser.add_argument('--use-heatmaps', action="store_true")
    parser.add_argument('--heatmaps-path')
    parser.add_argument('--use-augmentation', action="store_true")
    parser.add_argument('--use-hdf5', action="store_true")
    # parser.add_argument('--num-epochs', default=1, type=int)
    # parser.add_argument('--device-type', default="cpu", choices=['gpu', 'cpu'])
    # parser.add_argument("--gpu-number", type=int, default=0)
    args = parser.parse_args()

    parameters = {
        "device_type":
        'gpu',
        "gpu_number":
        0,
        "max_crop_noise": (100, 100),
        "max_crop_size_noise":
        100,
        "image_path":
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/cropping/sample_output/'
        + id_patient,
        "batch_size":
        args.batch_size,
        "seed":
        args.seed,
        "augmentation":
        args.use_augmentation,
        "num_epochs":
        10,
        "use_heatmaps":
        args.use_heatmaps,
        "heatmaps_path":
        args.heatmaps_path,
        #   "heatmaps_path": 'C:/Users/ultra/jPycharmProjects/breast_cancer_classifier/src/cropping/sample_output/heatmaps_'+id_patient,
        "use_hdf5":
        args.use_hdf5
    }
    # load_run_save(
    #    model_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/models/sample_image_model.p',
    #    data_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/' + id_patient + '.pkl',
    #   # output_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/image_predictions.csv',
    #   output_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/' + id_patient + '.csv',
    #   parameters=parameters,
    # )
    return load_run_save(
        model_path=
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/models/sample_image_model.p',
        data_path=
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/'
        + id_patient + '.pkl',
        # output_path='C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/image_predictions.csv',
        output_path=
        'C:/Users/ultra/PycharmProjects/breast_cancer_classifier/src/optimal_centers/sample_output/'
        + id_patient + '.csv',
        parameters=parameters,
    )
예제 #20
0
import os, sys
# print(sys.path)
current_dir = os.path.dirname(os.path.abspath(__file__))
# appending parent dir of current_dir to sys.path
sys.path.append(os.path.dirname(current_dir))

from multiprocessing import Pool
import argparse
from functools import partial
import scipy.ndimage
import numpy as np
import pandas as pd

import src.utilities.pickling as pickling
import src.utilities.reading_images as reading_images
import src.utilities.saving_images as saving_images
import src.utilities.data_handling as data_handling

# list of exams (one dictionary per exam)
exam_list = pickling.unpickle_from_file('exam_list_before_cropping.pkl')
cropped_exam_list_path = '../sample_output/cropped_images/cropped_exam_list_test.pkl'
print(exam_list)
exam_list[0]['L-CC'] = []
# list per image (one dictionary per image). It contains same information than in list of exams + cropped information if present.
image_list = data_handling.unpack_exam_into_images(exam_list)

pickling.pickle_to_file(cropped_exam_list_path, exam_list)