def test_batch_add(self): tiler = Tiler(data_shape=self.data.shape, tile_shape=(10, )) merger = Merger(tiler) batch1 = [x for _, x in tiler(self.data, False, batch_size=1)] np.testing.assert_equal(len(batch1), 10) np.testing.assert_equal(batch1[0].shape, ( 1, 10, )) for i, b in enumerate(batch1): merger.add_batch(i, 1, b) np.testing.assert_equal(merger.merge(), self.data) merger.reset() batch10 = [x for _, x in tiler(self.data, False, batch_size=10)] for i, b in enumerate(batch10): merger.add_batch(i, 10, b) np.testing.assert_equal(merger.merge(), self.data) merger.reset() batch8 = [x for _, x in tiler(self.data, False, batch_size=8)] np.testing.assert_equal(len(batch8), 2) np.testing.assert_equal(batch8[0].shape, ( 8, 10, )) np.testing.assert_equal(batch8[1].shape, ( 2, 10, )) for i, b in enumerate(batch8): merger.add_batch(i, 8, b) np.testing.assert_equal(merger.merge(), self.data) merger.reset() batch8_drop = [ x for _, x in tiler(self.data, False, batch_size=8, drop_last=True) ] np.testing.assert_equal(len(batch8_drop), 1) np.testing.assert_equal(batch8_drop[0].shape, ( 8, 10, )) for i, b in enumerate(batch8_drop): merger.add_batch(i, 8, b) np.testing.assert_equal(merger.merge()[:80], self.data[:80]) np.testing.assert_equal(merger.merge()[80:], np.zeros((20, ))) with self.assertRaises(IndexError): merger.add_batch(-1, 10, batch10[0]) with self.assertRaises(IndexError): merger.add_batch(10, 10, batch10[9])
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)))
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)])
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') ax[1, 1].imshow(final_image) ax[2, 0].set_title('Weights sum')
# Let's define a function that will be applied to each tile # For this example, let's black out the sides that should be "cropped" by window function # as a way to confirm that only the middle parts are being merged def process(patch: np.ndarray) -> np.ndarray: patch[:14, :, :] = 0 patch[-14:, :, :] = 0 patch[:, :14, :] = 0 patch[:, -14:, :] = 0 patch[:, :, :14] = 0 patch[:, :, -14:] = 0 return patch # Iterate through all the tiles and apply the processing function and merge everything back for tile_id, tile in tiler(padded_volume, progress_bar=True): processed_tile = process(tile) merger.add(tile_id, processed_tile) final_volume = merger.merge() final_unpadded_volume = final_volume[14:-14, 14:-14, 14:-14] # Show all the with napari.gui_qt(): v = napari.Viewer() v.add_image(volume, name='Original volume') v.add_image(padded_volume, name='Padded volume') v.add_image(final_volume, name='Final volume') v.add_image(final_unpadded_volume, name='Final unpadded volume') v.add_image(merger.weights_sum, name='Merger weights sum') v.add_image(merger.data_visits, name='Merger data visits')
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 merger.reset()
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')