Esempio n. 1
0
def create_patch_index_list(index_list,
                            image_shape,
                            patch_shape,
                            patch_overlap,
                            patch_start_offset=None):
    """
    Function that returns an array of all the patches - of a given patch shape - in a given image.
    :param index_list:
    :param image_shape:
    :param patch_shape:
    :param patch_overlap:
    :param patch_start_offset:
    :return:
    """
    patch_index = list()
    for index in index_list:
        if patch_start_offset is not None:
            random_start_offset = np.negative(
                get_random_nd_index(patch_start_offset))
            patches = compute_patch_indices(image_shape,
                                            patch_shape,
                                            overlap=patch_overlap,
                                            start=random_start_offset)
        else:
            patches = compute_patch_indices(image_shape,
                                            patch_shape,
                                            overlap=patch_overlap)
        patch_index.extend(itertools.product([index], patches))
    return patch_index
Esempio n. 2
0
def create_patch_index_list(index_list,
                            image_shape,
                            patch_shape,
                            patch_overlap,
                            patch_start_offset=None):
    patch_index = list()
    for index in index_list:
        if patch_start_offset is not None:
            random_start_offset = np.negative(
                get_random_nd_index(patch_start_offset))
            patches = compute_patch_indices(image_shape,
                                            patch_shape,
                                            overlap=patch_overlap,
                                            start=random_start_offset)
        else:
            patches = compute_patch_indices(image_shape,
                                            patch_shape,
                                            overlap=patch_overlap)
        patch_index.extend(itertools.product([index], patches))
    return patch_index
Esempio n. 3
0
 def test_reconstruct_from_patches(self):
     patch_shape = (32, 32, 32)
     patch_overlap = 0
     patch_indices = compute_patch_indices(self.image.shape, patch_shape,
                                           patch_overlap)
     patches = [
         get_patch_from_3d_data(self.image.get_data(), patch_shape, index)
         for index in patch_indices
     ]
     reconstruced_data = reconstruct_from_patches(patches, patch_indices,
                                                  self.image.shape)
     # noinspection PyTypeChecker
     self.assertTrue(np.all(self.image.get_data() == reconstruced_data))
Esempio n. 4
0
    def test_reconstruct_with_overlapping_patches2(self):
        image_shape = (144, 144, 144)
        data = np.arange(0, image_shape[0] * image_shape[1] *
                         image_shape[2]).reshape(image_shape)
        patch_overlap = 16
        patch_shape = (64, 64, 64)
        patch_indices = compute_patch_indices(data.shape, patch_shape,
                                              patch_overlap)
        patches = [
            get_patch_from_3d_data(data, patch_shape, index)
            for index in patch_indices
        ]

        no_overlap_indices = compute_patch_indices(data.shape, patch_shape, 32)
        patch_indices = np.concatenate([patch_indices, no_overlap_indices])
        patches.extend([
            get_patch_from_3d_data(data, patch_shape, index)
            for index in no_overlap_indices
        ])
        reconstruced_data = reconstruct_from_patches(patches, patch_indices,
                                                     data.shape)
        # noinspection PyTypeChecker
        self.assertTrue(np.all(data == reconstruced_data))
Esempio n. 5
0
 def test_reconstruct_with_overlapping_patches(self):
     patch_overlap = 0
     patch_shape = (32, 32, 32)
     patch_indices = compute_patch_indices(self.image.shape, patch_shape,
                                           patch_overlap)
     patches = [
         get_patch_from_3d_data(self.image.get_data(), patch_shape, index)
         for index in patch_indices
     ]
     # extend patches with modified patches that are 2 lower than the original patches
     patches.extend([patch - 2 for patch in patches])
     patch_indices = np.concatenate([patch_indices, patch_indices], axis=0)
     reconstruced_data = reconstruct_from_patches(patches, patch_indices,
                                                  self.image.shape)
     # The reconstructed data should be 1 lower than the original data as 2 was subtracted from half the patches.
     # The resulting reconstruction should be the average.
     # noinspection PyTypeChecker
     self.assertTrue(
         np.all((self.image.get_data() - 1) == reconstruced_data))
Esempio n. 6
0
    def test_reconstruct_with_multiple_channels(self):
        image_shape = (144, 144, 144)
        n_channels = 4
        data = np.arange(
            0, image_shape[0] * image_shape[1] * image_shape[2] *
            n_channels).reshape([n_channels] + list(image_shape))
        patch_overlap = 16
        patch_shape = (64, 64, 64)
        patch_indices = compute_patch_indices(image_shape, patch_shape,
                                              patch_overlap)
        patches = [
            get_patch_from_3d_data(data, patch_shape, index)
            for index in patch_indices
        ]
        self.assertEqual(patches[0].shape, tuple([4] + list(patch_shape)))

        reconstruced_data = reconstruct_from_patches(patches, patch_indices,
                                                     data.shape)
        # noinspection PyTypeChecker
        self.assertTrue(np.all(data == reconstruced_data))
Esempio n. 7
0
def patch_wise_prediction(model,
                          data,
                          overlap=0,
                          batch_size=10,
                          permute=False):
    """
    :param batch_size:
    :param model:
    :param data:
    :param overlap:
    :return:
    """
    patch_shape = tuple([int(dim) for dim in model.input.shape[-3:]])
    predictions = list()
    print("Patch_shape prediction:", patch_shape)
    print("Overlap_ :", overlap)
    indices = compute_patch_indices(data.shape[-3:],
                                    patch_size=patch_shape,
                                    overlap=overlap)
    batch = list()
    i = 0
    while i < len(indices):
        while len(batch) < batch_size and i < len(indices):
            patch = get_patch_from_3d_data(data[0],
                                           patch_shape=patch_shape,
                                           patch_index=indices[i])
            batch.append(patch)
            i += 1
        prediction = predict(model, np.asarray(batch), permute=permute)
        batch = list()
        if isinstance(prediction, list):
            prediction = prediction[-1]
        for predicted_patch in prediction:
            predictions.append(predicted_patch)
    if isinstance(model.output, list):
        output_shape = [int(model.output[-1].shape[1])] + list(data.shape[-3:])
    else:
        output_shape = [int(model.output.shape[1])] + list(data.shape[-3:])
    return reconstruct_from_patches(predictions,
                                    patch_indices=indices,
                                    data_shape=output_shape)