Skip to content

Commit

Permalink
fix shape checking (tensorflow#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
WindQAQ authored and seanpmorgan committed Jun 15, 2019
1 parent 829cd88 commit 320ad67
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
71 changes: 47 additions & 24 deletions tensorflow_addons/image/dense_image_warp.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,49 @@ def interpolate_bilinear(grid, query_points, indexing="ij", name=None):
if len(query_points.shape) != 3:
raise ValueError("Query points must be 3 dimensional.")

if query_points.shape[2] is not None and query_points.shape[2] != 2:
raise ValueError("Query points must be size 2 in dim 2.")

if grid.shape[1] is not None and grid.shape[1] < 2:
raise ValueError("Grid height must be at least 2.")

if grid.shape[2] is not None and grid.shape[2] < 2:
raise ValueError("Grid width must be at least 2.")

grid_shape = tf.shape(grid)
query_shape = tf.shape(query_points)

batch_size, height, width, channels = (grid_shape[0], grid_shape[1],
grid_shape[2], grid_shape[3])

shape = [batch_size, height, width, channels]
num_queries = query_shape[1]

# pylint: disable=bad-continuation
with tf.control_dependencies([
tf.debugging.assert_equal(
query_shape[2],
2,
message="Query points must be size 2 in dim 2.")
]):
num_queries = query_shape[1]
# pylint: enable=bad-continuation

query_type = query_points.dtype
grid_type = grid.dtype

tf.debugging.assert_equal(
query_shape[2], 2, message="Query points must be size 2 in dim 2.")

tf.debugging.assert_greater_equal(
height, 2, message="Grid height must be at least 2."),
tf.debugging.assert_greater_equal(
width, 2, message="Grid width must be at least 2.")

alphas = []
floors = []
ceils = []
index_order = [0, 1] if indexing == "ij" else [1, 0]
unstacked_query_points = tf.unstack(query_points, axis=2)
# pylint: disable=bad-continuation
with tf.control_dependencies([
tf.debugging.assert_greater_equal(
height, 2, message="Grid height must be at least 2."),
tf.debugging.assert_greater_equal(
width, 2, message="Grid width must be at least 2."),
]):
alphas = []
floors = []
ceils = []
index_order = [0, 1] if indexing == "ij" else [1, 0]
unstacked_query_points = tf.unstack(query_points, axis=2)
# pylint: enable=bad-continuation

for dim in index_order:
with tf.name_scope("dim-" + str(dim)):
Expand Down Expand Up @@ -112,16 +130,21 @@ def interpolate_bilinear(grid, query_points, indexing="ij", name=None):
alpha = tf.expand_dims(alpha, 2)
alphas.append(alpha)

tf.debugging.assert_less_equal(
tf.cast(batch_size * height * width, dtype=tf.dtypes.float32),
np.iinfo(np.int32).max / 8.0,
message="The image size or batch size is sufficiently large "
"that the linearized addresses used by tf.gather "
"may exceed the int32 limit.")
flattened_grid = tf.reshape(grid,
[batch_size * height * width, channels])
batch_offsets = tf.reshape(
tf.range(batch_size) * height * width, [batch_size, 1])
# pylint: disable=bad-continuation
with tf.control_dependencies([
tf.debugging.assert_less_equal(
tf.cast(
batch_size * height * width, dtype=tf.dtypes.float32),
np.iinfo(np.int32).max / 8.0,
message="The image size or batch size is sufficiently "
"large that the linearized addresses used by tf.gather "
"may exceed the int32 limit.")
]):
flattened_grid = tf.reshape(
grid, [batch_size * height * width, channels])
batch_offsets = tf.reshape(
tf.range(batch_size) * height * width, [batch_size, 1])
# pylint: enable=bad-continuation

# This wraps tf.gather. We reshape the image data such that the
# batch, y, and x coordinates are pulled into the first dimension.
Expand Down
6 changes: 3 additions & 3 deletions tensorflow_addons/image/dense_image_warp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ def test_gradients_exist(self):
for _ in range(10):
sess.run(opt_func)

# TODO: run in both graph and eager modes
@test_utils.run_in_graph_and_eager_modes
def test_size_exception(self):
"""Make sure it throws an exception for images that are too small."""
shape = [1, 2, 1, 1]
with self.assertRaisesRegexp(tf.errors.InvalidArgumentError,
"Grid width must be at least 2."):
errors = (ValueError, tf.errors.InvalidArgumentError)
with self.assertRaisesRegexp(errors, "Grid width must be at least 2."):
self._check_interpolation_correctness(shape, "float32", "float32")


Expand Down

0 comments on commit 320ad67

Please sign in to comment.