def test_untile_image(): shapes = [(3, 8, 16, 2), (1, 64, 64, 1), (1, 41, 58, 1), (1, 93, 61, 1)] rand_rel_diff_thresh = 2e-2 model_input_shapes = [(16, 20), (32, 32), (41, 51), (64, 64), (100, 90)] stride_ratios = [0.33, 0.5, 0.51, 0.66, 0.75, 1] dtypes = ['int32', 'float32', 'uint16', 'float16'] prod = product(shapes, model_input_shapes, stride_ratios, dtypes) # Test that randomly generated arrays are unchanged within a moderate tolerance for shape, input_shape, stride_ratio, dtype in prod: big_image = (np.random.random(shape) * 100).astype(dtype) tiles, tiles_info = utils.tile_image(big_image, model_input_shape=input_shape, stride_ratio=stride_ratio) untiled_image = utils.untile_image(tiles, tiles_info) assert untiled_image.dtype == dtype assert untiled_image.shape == shape np.testing.assert_allclose(big_image, untiled_image, rand_rel_diff_thresh) # Test that constant arrays are unchanged by tile/untile for shape, input_shape, stride_ratio, dtype in prod: for x in [0, 1, np.random.randint(2, 99)]: big_image = np.empty(shape).astype(dtype).fill(x) tiles, tiles_info = utils.tile_image(big_image, model_input_shape=input_shape, stride_ratio=stride_ratio) untiled_image = utils.untile_image(tiles, tiles_info) assert untiled_image.dtype == dtype assert untiled_image.shape == shape np.testing.assert_equal(big_image, untiled_image) # test that a stride_fraction of 0 raises an error with pytest.raises(ValueError): big_image_test = np.zeros((4, 4)).astype('int32') tiles, tiles_info = utils.tile_image(big_image_test, model_input_shape=(2, 2), stride_ratio=0) untiled_image = utils.untile_image(tiles, tiles_info)
def _process(im, tiles_info): out = untile_image(im, tiles_info, model_input_shape=self.model_image_shape) return out