def main(args):
    original_data_dir = os.path.expanduser(args.original_data_dir)
    target_dir = os.path.expanduser(args.target_dir)
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    # List filenames of data
    unfiltered_filelist = getAllFiles(original_data_dir)
    input_list = [
        item for item in unfiltered_filelist if re.search('_img', item)
    ]
    mask_list = [
        item for item in unfiltered_filelist if re.search('_mask', item)
    ]
    label_list = [
        item for item in unfiltered_filelist if re.search('_grid', item)
    ]
    input_list = sorted(input_list)
    mask_list = sorted(mask_list)
    label_list = sorted(label_list)
    print(input_list)
    print(mask_list)
    print(label_list)
    # load image, mask and label stacks as matrices
    for i, j in enumerate(input_list):
        print('Loading image...')
        img_mat = helper.load_nifti_mat_from_file(j)
        print('Loading mask...')
        mask_mat = helper.load_nifti_mat_from_file(mask_list[i])
        print('Loading label...')
        label_mat = helper.load_nifti_mat_from_file(label_list[i])
        # check the dimensions
        assert img_mat.shape == mask_mat.shape == label_mat.shape, "The DIMENSIONS of image, mask and label are NOT " \
                                                                   "SAME."

        # mask images and labels (skull stripping)
        img_mat = helper.aplly_mask(img_mat, mask_mat)
        label_mat = helper.aplly_mask(label_mat, mask_mat)
        print(j.split(os.sep)[-1].split('_')[0])
        # save to new file as masked version of original data
        helper.create_and_save_nifti(
            img_mat,
            target_dir + j.split(os.sep)[-1].split('_')[0] + '_img.nii')
        helper.create_and_save_nifti(
            mask_mat,
            target_dir + j.split(os.sep)[-1].split('_')[0] + '_mask.nii')
        helper.create_and_save_nifti(
            label_mat,
            target_dir + j.split(os.sep)[-1].split('_')[0] + '_label.nii')

        print()
    print('DONE')
Пример #2
0
def main(args):
    result_dir = args.result_dir
    # List filenames of data after the skull stripping process
    unfiltered_filelist = getAllFiles(result_dir)
    label_list = [
        item for item in unfiltered_filelist if re.search('_label', item)
    ]
    prediction_list = [
        item for item in unfiltered_filelist if re.search('_prediction', item)
    ]
    label_list = sorted(label_list)
    prediction_list = sorted(prediction_list)
    print(label_list)
    print(prediction_list)
    acc_list = []
    f1_score_list = []
    # load image, mask and label stacks as matrices
    for i, j in enumerate(label_list):
        print('> Loading label...')
        label_mat = load_nifti_mat_from_file(j)
        print('> Loading prediction...')
        prediction_mat = load_nifti_mat_from_file(prediction_list[i])
        pred_class = prediction_mat
        converted_pred_class = pred_class.copy().astype(int)
        converted_pred_class[converted_pred_class == 1] = -1
        converted_pred_class[converted_pred_class == 0] = 1
        print()
        #print("Performance of patient: ", j.split(os.sep)[-1].split('_')[0])
        label_mat_f = label_mat.flatten()
        #prob_mat_f = prob_mat.flatten()
        pred_class_f = pred_class.flatten().astype(np.uint8)

        #pat_auc = roc_auc_score(label_mat_f, prob_mat_f)
        pat_acc = accuracy_score(label_mat_f, pred_class_f)
        pat_avg_acc, tn, fp, fn, tp = avg_class_acc(label_mat_f, pred_class_f)
        pat_dice = f1_score(label_mat_f, pred_class_f)
        acc_list.append(pat_acc * 100)
        f1_score_list.append(pat_dice * 100)
        print('acc:', round(pat_acc, 4))
        print('avg acc:', pat_avg_acc)
        print('dice:', round(pat_dice, 4))
        print()
    print('mean of acc: ', round(np.mean(acc_list), 2))
    print('std of acc: ', round(np.std(acc_list), 2))
    print('mean of f1 score: ', round(np.mean(f1_score_list), 2))
    print('std of f1 score: ', round(np.std(f1_score_list), 2))
    print('DONE')
Пример #3
0
def main(args):
    original_data_dir = args.original_data
    grid_filepath = args.grid_filepath
    patch_size = 32
    if not os.path.exists(grid_filepath):
        os.makedirs(grid_filepath)
    unfiltered_filelist = getAllFiles(original_data_dir)
    input_list = [
        item for item in unfiltered_filelist if re.search('_img', item)
    ]
    mask_list = [
        item for item in unfiltered_filelist if re.search('_mask', item)
    ]
    input_list = sorted(input_list)
    mask_list = sorted(mask_list)
    print(input_list)
    print(mask_list)
    # load image, mask and label stacks as matrices
    for i, j in enumerate(input_list):
        print('> Loading image...')
        img_mat = load_nifti_mat_from_file(j)
        print('Loading mask...')
        mask_mat = load_nifti_mat_from_file(mask_list[i])
        # the grid is going to be saved in this matrix
        prob_mat = np.zeros(img_mat.shape, dtype=np.float32)
        x_dim, y_dim, z_dim = prob_mat.shape
        # get the x, y and z coordinates where there is brain
        x, y, z = np.where(mask_mat)
        print('x shape:', x.shape)
        print('y shape:', y.shape)
        print('z shape:', z.shape)
        # get the z slices with brain
        z_slices = np.unique(z)
        # proceed slice by slice
        for l in z_slices:
            slice_vox_inds = np.where(z == l)
            # find all x and y coordinates with brain in given slice
            x_in_slice = x[slice_vox_inds]
            y_in_slice = y[slice_vox_inds]
            # find min and max x and y coordinates
            slice_x_min = min(x_in_slice)
            slice_x_max = max(x_in_slice)
            slice_y_min = min(y_in_slice)
            slice_y_max = max(y_in_slice)
            # calculate number of patches in x and y direction in given slice
            num_of_x_patches = np.int(
                np.ceil((slice_x_max - slice_x_min) / patch_size))
            num_of_y_patches = np.int(
                np.ceil((slice_y_max - slice_y_min) / patch_size))
            for m in range(num_of_x_patches):
                for n in range(num_of_y_patches):
                    # find the starting and ending x and y coordinates of given patch
                    patch_start_x = slice_x_min + patch_size * m
                    patch_end_x = slice_x_min + patch_size * (m + 1)
                    patch_start_y = slice_y_min + patch_size * n
                    patch_end_y = slice_y_min + patch_size * (n + 1)
                    if patch_end_x > x_dim:
                        patch_end_x = slice_x_max
                        patch_start_x = slice_x_max - patch_size
                    if patch_end_y > y_dim:
                        patch_end_y = slice_y_max
                        patch_start_y = slice_y_max - patch_size
                    prob_mat[patch_start_x:patch_end_x, patch_start_y, l] = 1
                    prob_mat[patch_start_x:patch_end_x, patch_end_y, l] = 1
                    prob_mat[patch_start_x, patch_start_y:patch_end_y, l] = 1
                    prob_mat[patch_end_x, patch_start_y:patch_end_y, l] = 1
        # SAVE AS NIFTI
        create_and_save_nifti(
            prob_mat,
            grid_filepath + j.split(os.sep)[-1].split('_')[0] + '_grid.nii')
    print('DONE')
def main(args):
    patch_sizes = [96]  # different quadratic patch sizes n x n
    nr_patches = 1000  # number of patches we want to extract from one stack (one patient)
    nr_vessel_patches = nr_patches // 2  # patches that are extracted around vessels
    nr_empty_patches = nr_patches - nr_vessel_patches  # patches that are extracted from the brain region but not around
    # vessels
    # extract patches from each data stack (patient)
    skull_stripping_dir = os.path.expanduser(args.skull_stripping_dir)
    patch_extraction_dir = os.path.expanduser(args.patch_extraction_dir)
    if not os.path.exists(patch_extraction_dir):
        os.makedirs(patch_extraction_dir)
    # List filenames of data after the skull stripping process
    unfiltered_filelist = getAllFiles(skull_stripping_dir)
    input_list = [
        item for item in unfiltered_filelist if re.search('_img', item)
    ]
    mask_list = [
        item for item in unfiltered_filelist if re.search('_mask', item)
    ]
    label_list = [
        item for item in unfiltered_filelist if re.search('_label', item)
    ]
    input_list = sorted(input_list)
    mask_list = sorted(mask_list)
    label_list = sorted(label_list)
    print(input_list)
    print(mask_list)
    print(label_list)
    # load image, mask and label stacks as matrices
    for i, j in enumerate(input_list):
        # load image and label stacks as matrices
        print('> Loading image...')
        img_mat = load_nifti_mat_from_file(j)
        print('> Loading mask...')
        mask_mat = load_nifti_mat_from_file(mask_list[i])
        print('> Loading label...')
        label_mat = load_nifti_mat_from_file(label_list[i])  # values 0 or 1

        current_nr_extracted_patches = 0  # counts already extracted patches
        img_patches = {}  # dictionary to save image patches
        label_patches = {}  # dictionary to save label patches
        # make lists in dictionaries for each extracted patch size
        for size in patch_sizes:
            img_patches[str(size)] = []
            label_patches[str(size)] = []

        # variables with sizes and ranges for searchable areas
        max_patch_size = max(patch_sizes)
        print('max_patch_size: ', max_patch_size)
        half_max_size = max_patch_size // 2
        print('half_max_size: ', half_max_size)
        max_row = label_mat.shape[0] - max_patch_size // 2
        print('max_row: ', max_row)
        max_col = label_mat.shape[1] - max_patch_size // 2
        print('max_col: ', max_col)

        # -----------------------------------------------------------
        # EXTRACT RANDOM PATCHES WITH VESSELS IN THE CENTER OF EACH PATCH
        # -----------------------------------------------------------
        # cut off half of the biggest patch on the edges to create the searchable area -> to ensure that there will be
        # enough space for getting the patch
        searchable_label_area = label_mat[half_max_size:max_row,
                                          half_max_size:max_col, :]
        # find all vessel voxel indices in searchable area
        vessel_inds = np.asarray(np.where(searchable_label_area == 1))

        # keep extracting patches while the desired number of patches has not been reached yet, this just in case some
        # patches would be skipped, because they were already extracted (we do not want to have same patches in the set
        # more than once)
        while current_nr_extracted_patches < nr_vessel_patches * len(
                patch_sizes):
            # find given number of random vessel indices
            random_vessel_inds = vessel_inds[:,
                                             np.random.
                                             choice(vessel_inds.shape[1],
                                                    nr_vessel_patches,
                                                    replace=False)]
            for i in range(nr_vessel_patches):
                # stop extracting if the desired number of patches has been reached
                if current_nr_extracted_patches == nr_vessel_patches * len(
                        patch_sizes):
                    break

                # get the coordinates of the random vessel around which the patch will be extracted
                x = random_vessel_inds[0][i] + half_max_size
                y = random_vessel_inds[1][i] + half_max_size
                z = random_vessel_inds[2][i]

                # extract patches of different quadratic sizes with the random vessel voxel in the center of each patch
                for size in patch_sizes:
                    half_size = size // 2
                    random_img_patch = img_mat[x - half_size:x + half_size,
                                               y - half_size:y + half_size, z]
                    random_label_patch = label_mat[x - half_size:x + half_size,
                                                   y - half_size:y + half_size,
                                                   z]

                    # just sanity check if the patch is already in the list
                    if any((random_img_patch == x).all()
                           for x in img_patches[str(size)]):
                        print('Skip patch because already extracted. size:',
                              size)
                        break
                    else:
                        # append the extracted patches to the dictionaries
                        img_patches[str(size)].append(random_img_patch)
                        label_patches[str(size)].append(random_label_patch)
                        current_nr_extracted_patches += 1
                        if current_nr_extracted_patches % 100 == 0:
                            print(current_nr_extracted_patches,
                                  'PATCHES CREATED')

        # -----------------------------------------------------------
        # EXTRACT RANDOM EMPTY PATCHES
        # -----------------------------------------------------------
        # cut off half of the biggest patch on the edges to create the searchable area -> to ensure that there will be
        # enough space for getting the patch
        searchable_mask_area = mask_mat[half_max_size:max_row,
                                        half_max_size:max_col, :]
        # find all brain voxel indices
        brain_inds = np.asarray(np.where(searchable_mask_area == 1))

        # keep extracting patches while the desired number of patches has not been reached yet, this just in case some
        # patches would be skipped, because they were already extracted (we do not want to have same patches in the set
        # more than once)
        while current_nr_extracted_patches < nr_patches * len(patch_sizes):
            # find given number of random indices in the brain area
            random_brain_inds = brain_inds[:,
                                           np.random.choice(brain_inds.
                                                            shape[1],
                                                            nr_empty_patches,
                                                            replace=False)]
            for i in range(nr_empty_patches):
                # stop extracting if the desired number of patches has been reached
                if current_nr_extracted_patches == nr_patches * len(
                        patch_sizes):
                    break

                # get the coordinates of the random brain voxel around which the patch will be extracted
                x = random_brain_inds[0][i] + half_max_size
                y = random_brain_inds[1][i] + half_max_size
                z = random_brain_inds[2][i]

                # extract patches of different quadratic sizes with the random brain voxel in the center of each patch
                for size in patch_sizes:
                    half_size = size // 2
                    random_img_patch = img_mat[x - half_size:x + half_size,
                                               y - half_size:y + half_size, z]
                    random_label_patch = label_mat[x - half_size:x + half_size,
                                                   y - half_size:y + half_size,
                                                   z]

                    # just sanity check if the patch is already in the list
                    if any((random_img_patch == x).all()
                           for x in img_patches[str(size)]):
                        print('Skip patch because already extracted. size:',
                              size)
                        break
                    else:
                        # append the extracted patches to the dictionaries
                        img_patches[str(size)].append(random_img_patch)
                        label_patches[str(size)].append(random_label_patch)
                        current_nr_extracted_patches += 1
                        if current_nr_extracted_patches % 100 == 0:
                            print(current_nr_extracted_patches,
                                  'PATCHES CREATED')

        assert current_nr_extracted_patches == nr_patches * len(
            patch_sizes), "The number of extracted patches is  " + str(
                current_nr_extracted_patches) + " but should be " + str(
                    nr_patches * len(patch_sizes))

        # save extracted patches as numpy arrays
        for size in patch_sizes:
            print('number of extracted image patches:',
                  len(img_patches[str(size)]))
            print('number of extracted label patches:',
                  len(label_patches[str(size)]))
            directory = patch_extraction_dir
            np.save(
                directory + j.split(os.sep)[-1].split('_')[0] + '_' +
                str(size) + '_img', np.asarray(img_patches[str(size)]))
            np.save(
                directory + j.split(os.sep)[-1].split('_')[0] + '_' +
                str(size) + '_label', np.asarray(label_patches[str(size)]))
            print(
                'Image patches saved to',
                directory + j.split(os.sep)[-1].split('_')[0] + '_' +
                str(size) + '_img.npy')
            print(
                'Label patches saved to',
                directory + j.split(os.sep)[-1].split('_')[0] + '_' +
                str(size) + '_label.npy')
        print()

    print('DONE')
Пример #5
0
def main(args):
    test_set_dir = args.test_set_dir
    patch_size = args.patch_size
    model_arch = args.model_arch
    train_metadata_filepath = args.train_metadata_filepath
    model_filepath = args.model_filepath
    prediction_filepath = args.prediction_filepath
    # LOADING MODEL, RESULTS AND WHOLE BRAIN MATRICES
    print(model_filepath)
    model = load_model(model_filepath,
                       custom_objects={
                           'dice_coef_loss': dice_coef_loss,
                           'dice_coef': dice_coef
                       })
    with open(train_metadata_filepath, 'rb') as handle:
        train_metadata = pickle.load(handle)
    print(train_metadata)
    # List filenames of data after the skull stripping process
    unfiltered_filelist = getAllFiles(test_set_dir)
    input_list = [
        item for item in unfiltered_filelist if re.search('_img', item)
    ]
    mask_list = [
        item for item in unfiltered_filelist if re.search('_mask', item)
    ]
    input_list = sorted(input_list)
    mask_list = sorted(mask_list)
    print(input_list)
    print(mask_list)
    # load image, mask and label stacks as matrices
    for i, j in enumerate(input_list):
        print('Loading image...')
        img_mat = load_nifti_mat_from_file(j)
        print('Loading mask...')
        mask_mat = load_nifti_mat_from_file(mask_list[i])
        # prediction
        prob_mat = np.zeros(img_mat.shape, dtype=np.float32)
        x_dim, y_dim, _ = prob_mat.shape
        # get the x, y and z coordinates where there is brain
        x, y, z = np.where(mask_mat)
        print('x shape:', x.shape)
        print('y shape:', y.shape)
        print('z shape:', z.shape)
        # get the z slices with brain
        z_slices = np.unique(z)

        # start cutting out and predicting the patches
        starttime_total = time.time()
        # proceed slice by slice
        for l in z_slices:
            print('Slice:', l)
            starttime_slice = time.time()
            slice_vox_inds = np.where(z == l)
            # find all x and y coordinates with brain in given slice
            x_in_slice = x[slice_vox_inds]
            y_in_slice = y[slice_vox_inds]
            # find min and max x and y coordinates
            slice_x_min = min(x_in_slice)
            slice_x_max = max(x_in_slice)
            slice_y_min = min(y_in_slice)
            slice_y_max = max(y_in_slice)

            # calculate number of predicted patches in x and y direction in given slice
            num_of_x_patches = np.int(
                np.ceil((slice_x_max - slice_x_min) / patch_size))
            num_of_y_patches = np.int(
                np.ceil((slice_y_max - slice_y_min) / patch_size))
            print('num x patches', num_of_x_patches)
            print('num y patches', num_of_y_patches)

            # predict patch by patch in given slice
            for m in range(num_of_x_patches):
                for n in range(num_of_y_patches):
                    # find the starting and ending x and y coordinates of given patch
                    patch_start_x = slice_x_min + patch_size * m
                    patch_end_x = slice_x_min + patch_size * (m + 1)
                    patch_start_y = slice_y_min + patch_size * n
                    patch_end_y = slice_y_min + patch_size * (n + 1)
                    # if the dimensions of the probability matrix are exceeded shift back the last patch
                    if patch_end_x > x_dim:
                        patch_end_x = slice_x_max
                        patch_start_x = slice_x_max - patch_size
                    if patch_end_y > y_dim:
                        patch_end_y = slice_y_max
                        patch_start_y = slice_y_max - patch_size

                    # get the patch with the found coordinates from the image matrix
                    img_patch = img_mat[patch_start_x:patch_end_x,
                                        patch_start_y:patch_end_y, l]

                    # normalize the patch with mean and standard deviation calculated over training set
                    img_patch = img_patch.astype(np.float)
                    img_patch -= train_metadata['mean_train']
                    img_patch /= train_metadata['std_train']

                    # predict the patch with the model and save to probability matrix
                    prob_mat[patch_start_x:patch_end_x,
                             patch_start_y:patch_end_y, l] = np.reshape(
                                 model.predict(np.reshape(
                                     img_patch,
                                     (1, patch_size, patch_size, 1)),
                                               batch_size=1,
                                               verbose=0),
                                 (patch_size, patch_size))

            # how long does the prediction take for one slice
            duration_slice = time.time() - starttime_slice
            print('prediction in slice took:', (duration_slice // 3600) % 60,
                  'hours', (duration_slice // 60) % 60, 'minutes',
                  duration_slice % 60, 'seconds')
        # how long does the prediction take for a patient
        duration_total = time.time() - starttime_total
        print('prediction in total took:', (duration_total // 3600) % 60,
              'hours', (duration_total // 60) % 60, 'minutes',
              duration_total % 60, 'seconds')
        # save file
        print(j.split(os.sep)[-1].split('_')[0])
        if model_arch == 'wnetseg':
            create_and_save_nifti(
                prob_mat, prediction_filepath + 'prediction' + '_' +
                j.split(os.sep)[-1].split('_')[0] + '_wnetseg.nii.gz')
        elif model_arch == 'pnet':
            create_and_save_nifti(
                prob_mat, prediction_filepath + 'prediction' + '_' +
                j.split(os.sep)[-1].split('_')[0] + '_pnet.nii.gz')
        elif model_arch == 'unet':
            create_and_save_nifti(
                prob_mat, prediction_filepath + 'prediction' + '_' +
                j.split(os.sep)[-1].split('_')[0] + '_unet.nii.gz')
Пример #6
0
def main(args):
    train_non_label_dir = args.train_non_label_dir
    patch_size = args.patch_size
    model_filepath = args.model_filepath
    train_metadata_filepath = args.train_metadata_filepath
    grid_label_filepath = args.grid_label_filepath
    # LOADING MODEL, RESULTS AND WHOLE BRAIN MATRICES
    print(model_filepath)
    model = load_model(model_filepath)
    with open(train_metadata_filepath, 'rb') as handle:
        train_metadata = pickle.load(handle)
    print(train_metadata)
    # List filenames of data after the skull stripping process
    unfiltered_filelist = getAllFiles(train_non_label_dir)
    input_list = [
        item for item in unfiltered_filelist if re.search('_img', item)
    ]
    mask_list = [
        item for item in unfiltered_filelist if re.search('_mask', item)
    ]
    input_list = sorted(input_list)
    mask_list = sorted(mask_list)
    print(input_list)
    print(mask_list)
    # load image, mask and label stacks as matrices
    for i, j in enumerate(input_list):
        print('Loading image...')
        img_mat = load_nifti_mat_from_file(j)
        print('Loading mask...')
        mask_mat = load_nifti_mat_from_file(mask_list[i])
        # weak_annotation matrix
        prob_mat = np.zeros(img_mat.shape, dtype=np.float32)
        x_dim, y_dim, _ = prob_mat.shape
        # get the x, y and z coordinates where there is brain
        x, y, z = np.where(mask_mat)
        print('x shape:', x.shape)
        print('y shape:', y.shape)
        print('z shape:', z.shape)
        # get the z slices with brain
        z_slices = np.unique(z)
        # start cutting out and predicting the patches
        # proceed slice by slice
        for l in z_slices:
            print('Slice:', l)
            slice_vox_inds = np.where(z == l)
            # find all x and y coordinates with brain in given slice
            x_in_slice = x[slice_vox_inds]
            y_in_slice = y[slice_vox_inds]
            # find min and max x and y coordinates
            slice_x_min = min(x_in_slice)
            slice_x_max = max(x_in_slice)
            slice_y_min = min(y_in_slice)
            slice_y_max = max(y_in_slice)
            # calculate number of predicted patches in x and y direction in given slice
            num_of_x_patches = np.int(
                np.ceil((slice_x_max - slice_x_min) / patch_size))
            num_of_y_patches = np.int(
                np.ceil((slice_y_max - slice_y_min) / patch_size))
            print('num x patches', num_of_x_patches)
            print('num y patches', num_of_y_patches)
            # predict patch by patch in given slice
            for m in range(num_of_x_patches):
                for n in range(num_of_y_patches):
                    # find the starting and ending x and y coordinates of given patch
                    patch_start_x = slice_x_min + patch_size * m
                    patch_end_x = slice_x_min + patch_size * (m + 1)
                    patch_start_y = slice_y_min + patch_size * n
                    patch_end_y = slice_y_min + patch_size * (n + 1)
                    # if the dimensions of the probability matrix are exceeded shift back the last patch
                    if patch_end_x > x_dim:
                        patch_end_x = slice_x_max
                        patch_start_x = slice_x_max - patch_size
                    if patch_end_y > y_dim:
                        patch_end_y = slice_y_max
                        patch_start_y = slice_y_max - patch_size
                    # get the patch with the found coordinates from the image matrix
                    img_patch = img_mat[patch_start_x:patch_end_x,
                                        patch_start_y:patch_end_y, l]
                    # normalize the patch with mean and standard deviation calculated over training set
                    img_patch = img_patch.astype(np.float)
                    img_patch = img_patch[None, :, :, None]
                    img_patch -= train_metadata['mean_train']
                    img_patch /= train_metadata['std_train']
                    if model.predict(img_patch) >= 0.5:
                        prob_mat[patch_start_x:patch_end_x,
                                 patch_start_y:patch_end_y, l] = 2
                    prob_mat[patch_start_x:patch_end_x, patch_start_y, l] = 1
                    prob_mat[patch_start_x:patch_end_x, patch_end_y, l] = 1
                    prob_mat[patch_start_x, patch_start_y:patch_end_y, l] = 1
                    prob_mat[patch_end_x, patch_start_y:patch_end_y, l] = 1
        # Save weak annotation
        create_and_save_nifti(
            prob_mat, grid_label_filepath + j.split(os.sep)[-1].split('_')[0] +
            '_pnetcls.nii')
        print('done')
    print('DONE')
Пример #7
0
def main(args):
    patch_size = args.patch_size
    clustering = args.clustering
    patch_annotation_dir = os.path.expanduser(args.patch_annotation_dir)
    rough_mask_dir = os.path.expanduser(args.rough_mask_dir)
    if not os.path.exists(rough_mask_dir):
        os.makedirs(rough_mask_dir)
    # List filenames of data after the skull stripping process
    unfiltered_filelist = getAllFiles(patch_annotation_dir)
    input_list = [
        item for item in unfiltered_filelist if re.search('_img', item)
    ]
    mask_list = [
        item for item in unfiltered_filelist if re.search('_mask', item)
    ]
    label_list = [
        item for item in unfiltered_filelist if re.search('_grid', item)
    ]
    input_list = sorted(input_list)
    mask_list = sorted(mask_list)
    label_list = sorted(label_list)
    print(input_list)
    print(mask_list)
    print(label_list)
    # load image, mask and label stacks as matrices
    for i, j in enumerate(input_list):
        print('Loading image...')
        img_mat = load_nifti_mat_from_file(j)
        # Normalization
        mask = img_mat > 0
        img_mat = (img_mat - img_mat[mask].mean()) / img_mat[mask].std()
        print('Loading mask...')
        mask_mat = load_nifti_mat_from_file(mask_list[i])
        print('Loading weak label...')
        label_mat = load_nifti_mat_from_file(label_list[i])
        # extract square
        prob_mat = np.zeros(img_mat.shape, dtype=np.float32)
        x_dim, y_dim, _ = prob_mat.shape
        # get the x, y and z coordinates where there is brain
        x, y, z = np.where(mask_mat)
        z_slices = np.unique(z)
        cnt = 0
        for l in z_slices:
            slice_vox_inds = np.where(z == l)
            # find all x and y coordinates with brain in given slice
            x_in_slice = x[slice_vox_inds]
            y_in_slice = y[slice_vox_inds]
            # find min and max x and y coordinates
            slice_x_min = min(x_in_slice)
            slice_x_max = max(x_in_slice)
            slice_y_min = min(y_in_slice)
            slice_y_max = max(y_in_slice)
            # calculate number of patches in x and y direction in given slice
            num_of_x_patches = np.int(
                np.ceil((slice_x_max - slice_x_min) / patch_size))
            num_of_y_patches = np.int(
                np.ceil((slice_y_max - slice_y_min) / patch_size))
            for m in range(num_of_x_patches):
                for n in range(num_of_y_patches):
                    # find the starting and ending x and y coordinates of given patch
                    patch_start_x = slice_x_min + patch_size * m
                    patch_end_x = slice_x_min + patch_size * (m + 1)
                    patch_start_y = slice_y_min + patch_size * n
                    patch_end_y = slice_y_min + patch_size * (n + 1)
                    if patch_end_x > x_dim:
                        patch_end_x = slice_x_max
                        patch_start_x = slice_x_max - patch_size
                    if patch_end_y > y_dim:
                        patch_end_y = slice_y_max
                        patch_start_y = slice_y_max - patch_size
                    # get the patch with the found coordinates from the image matrix
                    img_patch = img_mat[patch_start_x:patch_end_x,
                                        patch_start_y:patch_end_y, l]
                    label_patch = label_mat[patch_start_x:patch_end_x,
                                            patch_start_y:patch_end_y, l]
                    mask_check = mask_mat[patch_start_x:patch_end_x,
                                          patch_start_y:patch_end_y, l]
                    if 2 in label_patch:
                        image = img_patch
                        pixels = image
                        pixels = pixels.astype('float32')
                        if clustering == 'kmeans':
                            vectorized = pixels.reshape((-1, 1))
                            vectorized = np.float32(vectorized)
                            criteria = (cv2.TERM_CRITERIA_EPS +
                                        cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
                            attempts = 10
                            if 0 in mask_check:
                                K = 4
                            else:
                                K = 2
                            _, label, center = cv2.kmeans(
                                vectorized, K, None, criteria, attempts,
                                cv2.KMEANS_PP_CENTERS)
                            res = center[label.flatten()]
                            result_image = res.reshape((image.shape))
                            result_image[result_image != np.amax(center)] = 0
                            result_image[result_image == np.amax(center)] = 1
                            result_image = result_image.astype('int8')
                            num_labels, labels_im = cv2.connectedComponents(
                                result_image)
                            threshold = 350
                            if (0 in mask_check):
                                threshold = 100
                            if (3 in label_patch):
                                threshold = 350
                            if (num_labels <
                                    5) and (np.count_nonzero(result_image) <
                                            threshold):
                                cnt += 1
                                prob_mat[patch_start_x:patch_end_x,
                                         patch_start_y:patch_end_y,
                                         l] = result_image
                        elif clustering == 'gmm':
                            pixels = image[image != 0]
                            vectorized = pixels.reshape((-1, 1))
                            vectorized = np.float32(vectorized)
                            if 0 in mask_check:
                                n_components = 4
                            else:
                                n_components = 2
                            if (3 in label_patch):
                                n_components = 2
                            gmm_model_tied = GMM(
                                n_components=2,
                                covariance_type='tied').fit(vectorized)
                            center_tied = gmm_model_tied.means_
                            label_tied = gmm_model_tied.predict(
                                vectorized).reshape(-1, 1)
                            res_tied = center_tied[label_tied.flatten()]
                            result_image_tied = res_tied
                            result_image_tied[
                                result_image_tied != np.amax(center_tied)] = 0
                            result_image_tied[result_image_tied == np.amax(
                                center_tied)] = 1
                            b = np.zeros(img_patch.shape)
                            pos = np.where(img_patch != 0)
                            b[pos[0], pos[1]] = result_image_tied.reshape(
                                len(img_patch[img_patch != 0]))
                            b = b.astype('int8')
                            num_labels, labels_im = cv2.connectedComponents(b)
                            threshold = 350
                            if (0 in mask_check):
                                threshold = 100
                            if (3 in label_patch):
                                threshold = 350
                            if (num_labels < 5) and (np.count_nonzero(b) <
                                                     threshold):
                                cnt += 1
                                prob_mat[patch_start_x:patch_end_x,
                                         patch_start_y:patch_end_y, l] = b
        # save prob_mat
        print('the number of vessel patch:', cnt)
        create_and_save_nifti(
            prob_mat, rough_mask_dir + j.split(os.sep)[-1].split('_')[0] +
            '_label_rough.nii.gz')
    print()
    print('DONE')
Пример #8
0
def main(args):
    patch_size = args.patch_size
    skull_stripping_dir = os.path.expanduser(args.skull_stripping_dir)
    patch_vessel_dir = os.path.expanduser(args.patch_vessel_dir)
    if not os.path.exists(patch_vessel_dir):
        os.makedirs(patch_vessel_dir)
    # List filenames of data after the skull stripping process
    unfiltered_filelist = getAllFiles(skull_stripping_dir)
    input_list = [
        item for item in unfiltered_filelist if re.search('_img', item)
    ]
    mask_list = [
        item for item in unfiltered_filelist if re.search('_mask', item)
    ]
    label_list = [
        item for item in unfiltered_filelist if re.search('_grid', item)
    ]
    input_list = sorted(input_list)
    mask_list = sorted(mask_list)
    label_list = sorted(label_list)
    print(input_list)
    print(mask_list)
    print(label_list)
    # load image, mask and label stacks as matrices
    for i, j in enumerate(input_list):
        print('Loading image...')
        img_mat = load_nifti_mat_from_file(j)
        print('Loading mask...')
        mask_mat = load_nifti_mat_from_file(mask_list[i])
        print('Loading weak label...')
        label_mat = load_nifti_mat_from_file(label_list[i])
        vessel_patches = []  # list to save vessel patches
        empty_patches = []  # list to save label patches
        #img_patches_empty = []  # list to save wo-vessel patches
        # extract square
        prob_mat = np.zeros(img_mat.shape, dtype=np.float32)
        x_dim, y_dim, _ = prob_mat.shape
        # get the x, y and z coordinates where there is brain
        x, y, z = np.where(mask_mat)
        z_slices = np.unique(z)
        cnt = 0
        for l in z_slices:
            slice_vox_inds = np.where(z == l)
            # find all x and y coordinates with brain in given slice
            x_in_slice = x[slice_vox_inds]
            y_in_slice = y[slice_vox_inds]
            # find min and max x and y coordinates
            slice_x_min = min(x_in_slice)
            slice_x_max = max(x_in_slice)
            slice_y_min = min(y_in_slice)
            slice_y_max = max(y_in_slice)
            # calculate number of patches in x and y direction in given slice
            num_of_x_patches = np.int(
                np.ceil((slice_x_max - slice_x_min) / patch_size))
            num_of_y_patches = np.int(
                np.ceil((slice_y_max - slice_y_min) / patch_size))
            for m in range(num_of_x_patches):
                for n in range(num_of_y_patches):
                    # find the starting and ending x and y coordinates of given patch
                    patch_start_x = slice_x_min + patch_size * m
                    patch_end_x = slice_x_min + patch_size * (m + 1)
                    patch_start_y = slice_y_min + patch_size * n
                    patch_end_y = slice_y_min + patch_size * (n + 1)
                    if patch_end_x > x_dim:
                        patch_end_x = slice_x_max
                        patch_start_x = slice_x_max - patch_size
                    if patch_end_y > y_dim:
                        patch_end_y = slice_y_max
                        patch_start_y = slice_y_max - patch_size
                    # get the patch with the found coordinates from the image matrix
                    img_patch = img_mat[patch_start_x:patch_end_x,
                                        patch_start_y:patch_end_y, l]
                    label_patch = label_mat[patch_start_x:patch_end_x,
                                            patch_start_y:patch_end_y, l]
                    if 2 in label_patch:
                        cnt += 1
                        vessel_patches.append(img_patch)
                    else:
                        empty_patches.append(img_patch)
        # save extracted patches as numpy arrays
        print('the number of vessel patch:', cnt)
        print('the number of extracted img patches:', len(vessel_patches))
        print('the number of extracted empty patches:', len(empty_patches))
        np.save(
            patch_vessel_dir + j.split(os.sep)[-1].split('_')[0] + '_' +
            str(patch_size) + '_vessel', np.asarray(vessel_patches))
        np.save(
            patch_vessel_dir + j.split(os.sep)[-1].split('_')[0] + '_' +
            str(patch_size) + '_empty', np.asarray(empty_patches))
        print(
            'img patches saved to',
            patch_vessel_dir + j.split(os.sep)[-1].split('_')[0] + '_' +
            str(patch_size) + '_vessel.npy')
        print(
            'label patches saved to',
            patch_vessel_dir + j.split(os.sep)[-1].split('_')[0] + '_' +
            str(patch_size) + '_empty.npy')
    print()
    print('DONE')
def main(args):
    patch_dir = os.path.expanduser(args.patch_dir)
    model_arch = args.model_arch
    train_metadata_filepath = args.train_metadata_filepath
    model_filepath = args.model_filepath
    unfiltered_filelist = getAllFiles(patch_dir)
    vessel_list = [
        item for item in unfiltered_filelist if re.search('_vessel', item)
    ]
    empty_list = [
        item for item in unfiltered_filelist if re.search('_empty', item)
    ]
    vessel_list = sorted(vessel_list)
    empty_list = sorted(empty_list)
    train_X = np.concatenate((np.load(vessel_list[0]), np.load(empty_list[0])),
                             axis=0)
    train_y = np.r_[1 * np.ones(
        (len(np.load(vessel_list[0])), 1)).astype('int'), 0 * np.ones(
            (len(np.load(empty_list[0])), 1)).astype('int')]
    for i, j in enumerate(vessel_list):
        vessel_mat = np.load(j)
        empty_mat = np.load(empty_list[i])
        if i != 0:
            train_X = np.concatenate((train_X, vessel_mat, empty_mat), axis=0)
            train_y = np.r_[train_y, 1 * np.ones(
                (len(vessel_mat), 1)).astype('int'), 0 * np.ones(
                    (len(empty_mat), 1)).astype('int')]
    train_X = train_X[:, :, :, None]
    lb = LabelBinarizer()
    train_y = lb.fit_transform(train_y)
    print('Shape of train_X, train_y: ', train_X.shape, len(train_y))
    # normalize training set
    mean1 = np.mean(train_X)  # mean for data centering
    std1 = np.std(train_X)  # std for data normalization
    train_X -= mean1
    train_X /= std1
    # CREATING MODEL
    patch_size = 32
    if model_arch == 'pnetcls':
        model = get_pnetcls(patch_size)
    elif model_arch == 'vgg':
        model = get_vgg()
    elif model_arch == 'resnet':
        model = get_resnet()
    elif model_arch == 'unetcls':
        model = get_unetcls()
    elif model_arch == 'pnetcls':
        model = get_pnetcls(patch_size)
    # train model
    print('Training model...')
    model.fit(train_X, train_y, validation_split=0.2, epochs=10, batch_size=64)
    # saving model
    print('Saving model to ', model_filepath)
    model.save(model_filepath)
    # saving mean and std
    print('Saving params to ', train_metadata_filepath)
    results = {'mean_train': mean1, 'std_train': std1}
    with open(train_metadata_filepath, 'wb') as handle:
        pickle.dump(results, handle)
    print()
    print('DONE')
Пример #10
0
def main(args):
    label_rough_dir = os.path.expanduser(args.label_rough)
    patch_size = args.patch_size
    model_arch = args.model_arch
    train_metadata_filepath = args.train_metadata_filepath
    model_filepath = args.model_filepath
    unfiltered_filelist = getAllFiles(label_rough_dir)
    input_list = [item for item in unfiltered_filelist if re.search('_img', item)]
    label_list = [item for item in unfiltered_filelist if re.search('_label', item)]
    input_list = sorted(input_list)
    label_list = sorted(label_list)
    train_X = np.load(input_list[0])
    train_y = np.load(label_list[0])
    for i, j in enumerate(input_list):
        img_mat = np.load(j)
        label_mat = np.load(label_list[i])
        if i !=0:
            train_X = np.concatenate((train_X, img_mat), axis=0)
            train_y = np.concatenate((train_y, label_mat), axis=0)
    train_X, train_y = train_X[:, :, :, None], train_y[:, :, :, None]
    # normalize data: mean and std calculated on the train set and applied to the train and val set
    mean1 = np.mean(train_X)  # mean for data centering
    std1 = np.std(train_X)  # std for data normalization
    train_X -= mean1
    train_X /= std1
    print(train_X.shape)
    print(train_y.shape)
    # set hyperparameters
    batch_size = 64
    num_channels = 1
    activation = 'relu'
    final_activation = 'sigmoid'
    optimizer = Adam
    lr = 1e-4
    dropout = 0.1
    num_epochs = 10
    loss = dice_coef_loss
    metrics = [dice_coef, 'accuracy']
    # CREATING MODEL
    if model_arch == 'wnetseg':
        model = get_wnetseg(patch_size, num_channels, activation,
                            final_activation, optimizer, lr, dropout, loss, metrics)
    elif model_arch == 'unet':
        model = get_unet(patch_size, num_channels, activation,
                         final_activation, optimizer, lr, dropout, loss, metrics)
    elif model_arch == 'pnet':
        model = get_pnet(patch_size, optimizer, lr,
                         loss, metrics, num_channels)
    # CREATING DATAGENTORATOR
    # how many times to augment training samples with the ImageDataGenerator per one epoch
    factor_train_samples = 2
    rotation_range = 30  # for ImageDataGenerator
    horizontal_flip = False  # for ImageDataGenerator
    vertical_flip = True  # for ImageDataGenerator
    shear_range = 20  # for ImageDataGenerator
    width_shift_range = 0  # for ImageDataGenerator
    height_shift_range = 0  # for ImageDataGenerator
    data_gen_args = dict(rotation_range=rotation_range,
                         horizontal_flip=horizontal_flip,
                         vertical_flip=vertical_flip,
                         shear_range=shear_range,
                         width_shift_range=width_shift_range,
                         height_shift_range=height_shift_range,
                         fill_mode='constant')
    X_datagen = ImageDataGenerator(**data_gen_args)
    y_datagen = ImageDataGenerator(**data_gen_args)

    # Provide the same seed and keyword arguments to the fit and flow methods
    seed = 1
    X_datagen.fit(train_X, augment=True, seed=seed)
    y_datagen.fit(train_y, augment=True, seed=seed)

    X_generator = X_datagen.flow(
        train_X, batch_size=batch_size, seed=seed, shuffle=True)
    y_generator = y_datagen.flow(
        train_y, batch_size=batch_size, seed=seed, shuffle=True)

    # combine generators into one which yields image and label
    train_generator = zip(X_generator, y_generator)
    # TRAINING MODEL
    start_train = time.time()
    model.fit_generator(train_generator,
                                  steps_per_epoch=factor_train_samples *
                                  len(train_X) // batch_size,
                                  epochs=num_epochs,
                                  verbose=2, shuffle=True)
    duration_train = int(time.time() - start_train)
    print('training took:', (duration_train // 3600) % 60, 'hours', (duration_train // 60) % 60,
          'minutes', duration_train % 60, 'seconds')
    # saving model
    print('Saving model to ', model_filepath)
    model.save(model_filepath)
    # saving mean and std
    print('Saving params to ', train_metadata_filepath)
    results = {'mean_train': mean1, 'std_train': std1}
    with open(train_metadata_filepath, 'wb') as handle:
        pickle.dump(results, handle)
    print()
    print('DONE')