Example #1
0
    def test_merge(self):

        # Test padding
        tiler = Tiler(data_shape=self.data.shape, tile_shape=(12, ))
        merger = Merger(tiler)
        for t_id, t in tiler(self.data):
            merger.add(t_id, t)

        np.testing.assert_equal(merger.merge(unpad=True), self.data)
        np.testing.assert_equal(
            merger.merge(unpad=False),
            np.hstack((self.data, [0, 0, 0, 0, 0, 0, 0, 0])))

        # Test argmax
        merger = Merger(tiler, logits=3)
        for t_id, t in tiler(self.data):
            merger.add(t_id, np.vstack((t, t / 2, t / 3)))

        np.testing.assert_equal(merger.merge(unpad=True, argmax=True),
                                np.zeros((100, )))
        np.testing.assert_equal(
            merger.merge(unpad=True, argmax=False),
            np.vstack((self.data, self.data / 2, self.data / 3)))

        np.testing.assert_equal(merger.merge(unpad=False, argmax=True),
                                np.zeros((108, )))
        np.testing.assert_equal(
            merger.merge(unpad=False, argmax=False),
            np.vstack((np.hstack((self.data, [0, 0, 0, 0, 0, 0, 0, 0])),
                       np.hstack((self.data, [0, 0, 0, 0, 0, 0, 0, 0])) / 2,
                       np.hstack((self.data, [0, 0, 0, 0, 0, 0, 0, 0])) / 3)))
Example #2
0
    def test_generate_window(self):
        tiler = Tiler(data_shape=self.data.shape, tile_shape=(10, ))

        with self.assertRaises(ValueError):
            Merger(tiler=tiler, window='unsupported_window')

        with self.assertRaises(ValueError):
            Merger(tiler=tiler, window=np.zeros((10, 10)))

        with self.assertRaises(ValueError):
            Merger(tiler=tiler, window=10)

        window = np.zeros((10, ))
        window[1:10] = 1
        merger = Merger(tiler=tiler, window=window)
        for t_id, t in tiler(self.data):
            merger.add(t_id, t)
        np.testing.assert_equal(merger.merge(),
                                [i if i % 10 else 0 for i in range(100)])
Example #3
0
        patch[-32:, :, :] = 0
        patch[:, :32, :] = 0
        patch[:, -32:, :] = 0
        return patch

    # Another example can be to just modify the whole patch
    # Using PIL, we adjust the color balance
    enhancer = ImageEnhance.Color(Image.fromarray(patch))
    return np.array(enhancer.enhance(5.0))


# Iterate through all the tile and apply the processing function
# as well as add them back to the merger
for tile_id, tile in tiler(padded_image):
    processed_tile = process(tile)
    merger.add(tile_id, processed_tile)

# Merger.merge() returns unpadded from tiler image, but we still need to unpad line#21
final_image = merger.merge().astype(np.uint8)
final_unpadded_image = final_image[32:-32, 32:-32, :]

# Show the final merged image, weights and number of times each pixel was seen in tiles
fig, ax = plt.subplots(3, 2, sharex=True, sharey=True)
ax[0, 0].set_title('Original image')
ax[0, 0].imshow(image)
ax[0, 1].set_title('Final unpadded image')
ax[0, 1].imshow(final_unpadded_image)

ax[1, 0].set_title('Padded image')
ax[1, 0].imshow(padded_image)
ax[1, 1].set_title('Merged image')
Example #4
0
from PIL import Image
from tiler import Tiler, Merger

# Loading image
# Photo by Christian Holzinger on Unsplash: https://unsplash.com/photos/CUY_YHhCFl4
image = np.array(Image.open('example_image.jpg'))  # 1280x1920x3

# Setup Tiler and Merger
tiler = Tiler(data_shape=image.shape,
              tile_shape=(200, 200, 3),
              channel_dimension=2)
merger = Merger(tiler)

# Example 1: process all tiles one by one, i.e. batch_size=0
for tile_i, tile in tiler(image, batch_size=0):
    merger.add(tile_i, tile)
result_bs0 = merger.merge().astype(np.uint8)

# Example 2: process all tiles in batches of 1, i.e. batch_size=1
merger.reset()
for batch_i, batch in tiler(image, batch_size=1):
    merger.add_batch(batch_i, 1, batch)
result_bs1 = merger.merge().astype(np.uint8)

# Example 3: process all tiles in batches of 10, i.e. batch_size=10
merger.reset()
for batch_i, batch in tiler(image, batch_size=10):
    merger.add_batch(batch_i, 10, batch)
result_bs10 = merger.merge().astype(np.uint8)

# Example 4: process all tiles in batches of 10, but drop the batch that has <batch_size tiles, drop_last=True
Example #5
0
    def test_add(self):
        tiler = Tiler(data_shape=self.data.shape, tile_shape=(10, ))
        tiler2 = Tiler(data_shape=self.data.shape,
                       tile_shape=(12, ),
                       mode='irregular')
        tiler3 = Tiler(data_shape=(3, ) + self.data.shape,
                       tile_shape=(
                           3,
                           10,
                       ),
                       channel_dimension=0)

        merger = Merger(tiler)
        merger_logits = Merger(tiler, logits=3)
        merger_irregular = Merger(tiler2)
        merger_channel_dim = Merger(tiler3)

        tile = tiler.get_tile(self.data, 0)
        tile_logits = np.vstack((tile, tile, tile))
        tile_irregular = tiler2.get_tile(self.data, len(tiler2) - 1)

        # Wrong tile id cases
        with self.assertRaises(IndexError):
            merger.add(-1, np.ones((10, )))
        with self.assertRaises(IndexError):
            merger.add(len(tiler), np.ones((10, )))

        # Usual mergers expect tile_shape == data_shape
        with self.assertRaises(ValueError):
            merger.add(0, np.ones((
                3,
                10,
            )))
        merger.add(0, tile)
        np.testing.assert_equal(merger.merge()[:10], tile)

        # Logits merger expects an extra dimension in front for logits
        with self.assertRaises(ValueError):
            merger_logits.add(0, np.ones((10, )))
        merger_logits.add(0, tile_logits)
        np.testing.assert_equal(merger_logits.merge()[:, :10], tile_logits)
        np.testing.assert_equal(
            merger_logits.merge(argmax=True)[:10], np.zeros((10, )))

        # Irregular merger expects all(data_shape <= tile_shape)
        with self.assertRaises(ValueError):
            merger_irregular.add(0, np.ones((13, )))
        merger_irregular.add(len(tiler2) - 1, tile_irregular)
        np.testing.assert_equal(
            merger_irregular.merge()[-len(tile_irregular):], tile_irregular)

        # Channel dimension merger
        with self.assertRaises(ValueError):
            merger_channel_dim.add(0, np.ones((10, )))
        merger_channel_dim.add(0, tile_logits)
        np.testing.assert_equal(merger_channel_dim.merge()[:, :10],
                                tile_logits)

        # gotta get that 100% coverage
        # this should just print a warning
        # let's suppress it to avoid confusion
        with open(os.devnull, "w") as null:
            with redirect_stderr(null):
                merger.set_window('boxcar')