コード例 #1
0
def find_downsized_info(training_data_files, input_shape):
    foreground = get_complete_foreground(training_data_files)
    crop_slices = crop_img(foreground, return_slices=True, copy=True)
    cropped = crop_img_to(foreground, crop_slices, copy=True)
    final_image = resize(cropped,
                         new_shape=input_shape,
                         interpolation="nearest")
    return crop_slices, final_image.affine, final_image.header
コード例 #2
0
 def test_images_align(self):
     data = np.arange(1, 9).reshape((2, 2, 2))
     affine = np.diag(np.ones(4) * 2)
     affine[3, 3] = 1
     image_nib = nib.Nifti1Image(data, affine=affine)
     new_image_nib = resize(image_nib, (4, 4, 4), interpolation="nearest")
     self.assertTrue(np.all(new_image_nib.get_data()[0] == np.asarray([[1, 1, 2, 2],
                                                                       [1, 1, 2, 2],
                                                                       [3, 3, 4, 4],
                                                                       [3, 3, 4, 4]])))
     self.assertTrue(np.all(new_image_nib.affine == np.asarray([[1., 0., 0., -0.5],
                                                                [0., 1., 0., -0.5],
                                                                [0., 0., 1., -0.5],
                                                                [0., 0., 0., 1.]])))
コード例 #3
0
 def _resize_image_test(self, image, target_shape):
     original_image_shape = image.shape
     new_image = resize(image, target_shape)
     self.assertEqual(new_image.shape, target_shape)
     new_image = resize(new_image, original_image_shape, interpolation="linear")
     self.assertEqual(new_image.shape, original_image_shape)
コード例 #4
0
def add_data(x_list,
             y_list,
             data_file,
             index,
             truth_index,
             truth_size=1,
             augment=None,
             patch_shape=None,
             skip_blank=True,
             truth_downsample=None,
             truth_crop=True,
             prev_truth_index=None,
             prev_truth_size=None,
             pred_index=None,
             pred_size=None,
             drop_easy_patches=False):
    """
    Adds data from the data file to the given lists of feature and target data
    :param prev_truth_index:
    :param truth_downsample:
    :param skip_blank: Data will not be added if the truth vector is all zeros (default is True).
    :param patch_shape: Shape of the patch to add to the data lists. If None, the whole image will be added.
    :param x_list: list of data to which data from the data_file will be appended.
    :param y_list: list of data to which the target data from the data_file will be appended.
    :param data_file: hdf5 data file.
    :param index: index of the data file from which to extract the data.
    :param augment: if not None, data will be augmented according to the augmentation parameters
    :return:
    """

    data, truth, pred = get_data_from_file(data_file,
                                           index,
                                           patch_shape=None,
                                           add_pred=pred_index)

    patch_corner = [
        np.random.randint(low=low, high=high) for low, high in
        zip((0, 0, 0), truth.shape -
            np.array(patch_shape))  # - np.array(patch_shape) // 2)
    ]
    if augment is not None:
        data_range = [(start, start + size)
                      for start, size in zip(patch_corner, patch_shape)]

        truth_range = data_range[:2] + [
            (patch_corner[2] + truth_index,
             patch_corner[2] + truth_index + truth_size)
        ]

        if prev_truth_index is not None:
            prev_truth_range = data_range[:2] + [
                (patch_corner[2] + prev_truth_index,
                 patch_corner[2] + prev_truth_index + prev_truth_size)
            ]
        else:
            prev_truth_range = None

        if pred_size is not None:
            pred_range = data_range[:2] + [
                (patch_corner[2] + pred_index,
                 patch_corner[2] + pred_index + pred_size)
            ]
        else:
            pred_range = None

        data, truth, prev_truth, real_pred, _ = augment_data(
            data,
            truth,
            data_min=data_file.stats.min[index],
            data_max=data_file.stats.max[index],
            pred=pred,
            scale_deviation=augment.get('scale', None),
            iso_scale_deviation=augment.get('iso_scale', None),
            rotate_deviation=augment.get('rotate', None),
            translate_deviation=augment.get('translate', None),
            flip=augment.get('flip', None),
            contrast_deviation=augment.get('contrast', None),
            piecewise_affine=augment.get('piecewise_affine', None),
            elastic_transform=augment.get('elastic_transform', None),
            intensity_multiplication_range=augment.get(
                'intensity_multiplication', None),
            poisson_noise=augment.get("poisson_noise", None),
            gaussian_filter=augment.get("gaussian_filter", None),
            coarse_dropout=augment.get("coarse_dropout", None),
            data_range=data_range,
            truth_range=truth_range,
            prev_truth_range=prev_truth_range,
            pred_range=pred_range,
            transpose_prob=augment.get("transpose_prob", 0))
    else:
        data, truth, prev_truth, real_pred = \
            extract_patch(data, patch_corner, patch_shape, truth,
                          truth_index=truth_index, truth_size=truth_size,
                          prev_truth_index=prev_truth_index, prev_truth_size=prev_truth_size,
                          pred=pred, pred_index=pred_index, pred_size=pred_size)

    if real_pred is not None:
        data = np.concatenate([data, real_pred], axis=-1)

    if prev_truth is not None:
        data = np.concatenate([data, prev_truth], axis=-1)

    if drop_easy_patches:
        truth_mean = np.mean(truth[16:-16, 16:-16, :])
        if 1 - np.abs(truth_mean - 0.5) < np.random.random():
            #print("Dropped - easy patch!!!")
            return

    if truth_downsample is not None and truth_downsample > 1:
        truth_shape = patch_shape[:-1] + (1, )
        new_shape = np.array(truth_shape)
        new_shape[:-1] = new_shape[:-1] // truth_downsample
        if truth_crop:
            truth = get_patch_from_3d_data(
                truth, new_shape,
                list(np.subtract(truth_shape[:2], new_shape[:2]) // 2) + [1])
        else:
            truth = resize(get_image(truth), new_shape=new_shape).get_data()

    if not skip_blank or np.any(truth != 0):
        x_list.append(data)
        y_list.append(truth)
    else:
        pass