From 2b84e03933c0176f89e2c35fa1a344a7fa5aaaff Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 9 Jan 2020 22:18:32 +0000 Subject: [PATCH] Fix bug in "xy" mode for bilinear interpolation (#845) * Fix bug in "xy" mode for bilinear interpolation * fixed error in interp implementation, and added test for non-square images. * removed erroneous non-square test, and modified small_grid tests to be non-square. --- tensorflow_addons/image/dense_image_warp.py | 4 ++-- .../image/dense_image_warp_test.py | 24 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tensorflow_addons/image/dense_image_warp.py b/tensorflow_addons/image/dense_image_warp.py index 9b17c4a45f..4166e6c6bc 100644 --- a/tensorflow_addons/image/dense_image_warp.py +++ b/tensorflow_addons/image/dense_image_warp.py @@ -116,11 +116,11 @@ def interpolate_bilinear(grid, query_points, indexing="ij", name=None): index_order = [0, 1] if indexing == "ij" else [1, 0] unstacked_query_points = tf.unstack(query_points, axis=2, num=2) - for dim in index_order: + for i, dim in enumerate(index_order): with tf.name_scope("dim-" + str(dim)): queries = unstacked_query_points[dim] - size_in_indexing_dimension = grid_shape[dim + 1] + size_in_indexing_dimension = grid_shape[i + 1] # max_floor is size_in_indexing_dimension - 2 so that max_floor + 1 # is still a valid index into the grid. diff --git a/tensorflow_addons/image/dense_image_warp_test.py b/tensorflow_addons/image/dense_image_warp_test.py index 52a454712a..84ed8b41f7 100644 --- a/tensorflow_addons/image/dense_image_warp_test.py +++ b/tensorflow_addons/image/dense_image_warp_test.py @@ -30,22 +30,28 @@ @test_utils.run_all_in_graph_and_eager_modes class InterpolateBilinearTest(tf.test.TestCase): def test_interpolate_small_grid_ij(self): - grid = tf.constant([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]], - shape=[1, 3, 3, 1]) - query_points = tf.constant([[0., 0.], [1., 0.], [2., 0.5], [1.5, 1.5]], - shape=[1, 4, 2]) - expected_results = np.reshape(np.array([0., 3., 6.5, 6.]), [1, 4, 1]) + grid = tf.constant( + [[0., 1., 2.], [3., 4., 5.], [6., 7., 8.], [9., 10., 11.]], + shape=[1, 4, 3, 1]) + query_points = tf.constant( + [[0., 0.], [1., 0.], [2., 0.5], [1.5, 1.5], [3., 2.]], + shape=[1, 5, 2]) + expected_results = np.reshape( + np.array([0., 3., 6.5, 6., 11.]), [1, 5, 1]) interp = interpolate_bilinear(grid, query_points) self.assertAllClose(expected_results, interp) def test_interpolate_small_grid_xy(self): - grid = tf.constant([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]], - shape=[1, 3, 3, 1]) + grid = tf.constant( + [[0., 1., 2.], [3., 4., 5.], [6., 7., 8.], [9., 10., 11.]], + shape=[1, 4, 3, 1]) query_points = tf.constant( - [[0., 0.], [0., 1.], [0.5, 2.0], [1.5, 1.5]], shape=[1, 4, 2]) - expected_results = np.reshape(np.array([0., 3., 6.5, 6.]), [1, 4, 1]) + [[0., 0.], [0., 1.], [0.5, 2.0], [1.5, 1.5], [2., 3.]], + shape=[1, 5, 2]) + expected_results = np.reshape( + np.array([0., 3., 6.5, 6., 11.]), [1, 5, 1]) interp = interpolate_bilinear(grid, query_points, indexing="xy")