Add more error information about the current shape that isn't correct.

PiperOrigin-RevId: 314962826
Change-Id: Ie45124beb74238bc683f45f4438df21247b299ca
This commit is contained in:
Vijay Vasudevan 2020-06-05 11:23:31 -07:00 committed by TensorFlower Gardener
parent 1c144fdb67
commit 86c745a112
2 changed files with 29 additions and 15 deletions

View File

@ -218,7 +218,8 @@ def _CheckAtLeast3DImage(image, require_static=True):
else: else:
image_shape = image.get_shape().with_rank_at_least(3) image_shape = image.get_shape().with_rank_at_least(3)
except ValueError: except ValueError:
raise ValueError("'image' must be at least three-dimensional.") raise ValueError("'image' (shape %s) must be at least three-dimensional." %
image.shape)
if require_static and not image_shape.is_fully_defined(): if require_static and not image_shape.is_fully_defined():
raise ValueError('\'image\' must be fully defined.') raise ValueError('\'image\' must be fully defined.')
if any(x == 0 for x in image_shape[-3:]): if any(x == 0 for x in image_shape[-3:]):
@ -283,7 +284,8 @@ def _CheckGrayscaleImage(image, require_static=True):
else: else:
image_shape = image.get_shape().with_rank_at_least(2) image_shape = image.get_shape().with_rank_at_least(2)
except ValueError: except ValueError:
raise ValueError('A grayscale image must be at least two-dimensional.') raise ValueError('A grayscale image (shape %s) must be at least '
'two-dimensional.' % image.shape)
if require_static and not image_shape.is_fully_defined(): if require_static and not image_shape.is_fully_defined():
raise ValueError('\'image\' must be fully defined.') raise ValueError('\'image\' must be fully defined.')
if image_shape.is_fully_defined(): if image_shape.is_fully_defined():
@ -450,7 +452,8 @@ def _random_flip(image, flip_index, seed, scope_name):
flipped_input = array_ops.reverse(image, [flip_index + 1]) flipped_input = array_ops.reverse(image, [flip_index + 1])
return flips * flipped_input + (1 - flips) * image return flips * flipped_input + (1 - flips) * image
else: else:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s) must have either 3 or 4 dimensions.' % shape)
@tf_export('image.flip_left_right') @tf_export('image.flip_left_right')
@ -551,7 +554,8 @@ def _flip(image, flip_index, scope_name):
elif shape.ndims == 4: elif shape.ndims == 4:
return array_ops.reverse(image, [flip_index + 1]) return array_ops.reverse(image, [flip_index + 1])
else: else:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s)must have either 3 or 4 dimensions.' % shape)
@tf_export('image.rot90') @tf_export('image.rot90')
@ -600,7 +604,8 @@ def rot90(image, k=1, name=None):
elif shape.ndims == 4: elif shape.ndims == 4:
return _rot90_4D(image, k, scope) return _rot90_4D(image, k, scope)
else: else:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s) must have either 3 or 4 dimensions.' % shape)
def _rot90_3D(image, k, name_scope): def _rot90_3D(image, k, name_scope):
@ -722,7 +727,8 @@ def transpose(image, name=None):
elif shape.ndims == 4: elif shape.ndims == 4:
return array_ops.transpose(image, [0, 2, 1, 3], name=name) return array_ops.transpose(image, [0, 2, 1, 3], name=name)
else: else:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s) must have either 3 or 4 dimensions.' % shape)
@tf_export('image.central_crop') @tf_export('image.central_crop')
@ -929,7 +935,9 @@ def pad_to_bounding_box(image, offset_height, offset_width, target_height,
image = array_ops.expand_dims(image, 0) image = array_ops.expand_dims(image, 0)
image.set_shape([None] * 4) image.set_shape([None] * 4)
elif image_shape.ndims != 4: elif image_shape.ndims != 4:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s) must have either 3 or 4 dimensions.' %
image_shape)
assert_ops = _CheckAtLeast3DImage(image, require_static=False) assert_ops = _CheckAtLeast3DImage(image, require_static=False)
batch, height, width, depth = _ImageDimensions(image, rank=4) batch, height, width, depth = _ImageDimensions(image, rank=4)
@ -1013,7 +1021,9 @@ def crop_to_bounding_box(image, offset_height, offset_width, target_height,
image = array_ops.expand_dims(image, 0) image = array_ops.expand_dims(image, 0)
image.set_shape([None] * 4) image.set_shape([None] * 4)
elif image_shape.ndims != 4: elif image_shape.ndims != 4:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s) must have either 3 or 4 dimensions.' %
image_shape)
assert_ops = _CheckAtLeast3DImage(image, require_static=False) assert_ops = _CheckAtLeast3DImage(image, require_static=False)
@ -1093,7 +1103,9 @@ def resize_image_with_crop_or_pad(image, target_height, target_width):
image = array_ops.expand_dims(image, 0) image = array_ops.expand_dims(image, 0)
image.set_shape([None] * 4) image.set_shape([None] * 4)
elif image_shape.ndims != 4: elif image_shape.ndims != 4:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s) must have either 3 or 4 dimensions.' %
image_shape)
assert_ops = _CheckAtLeast3DImage(image, require_static=False) assert_ops = _CheckAtLeast3DImage(image, require_static=False)
assert_ops += _assert(target_width > 0, ValueError, assert_ops += _assert(target_width > 0, ValueError,
@ -1549,7 +1561,9 @@ def _resize_image_with_pad_common(image, target_height, target_width,
image = array_ops.expand_dims(image, 0) image = array_ops.expand_dims(image, 0)
image.set_shape([None] * 4) image.set_shape([None] * 4)
elif image_shape.ndims != 4: elif image_shape.ndims != 4:
raise ValueError('\'image\' must have either 3 or 4 dimensions.') raise ValueError(
'\'image\' (shape %s) must have either 3 or 4 dimensions.' %
image_shape)
assert_ops = _CheckAtLeast3DImage(image, require_static=False) assert_ops = _CheckAtLeast3DImage(image, require_static=False)
assert_ops += _assert(target_width > 0, ValueError, assert_ops += _assert(target_width > 0, ValueError,

View File

@ -239,7 +239,7 @@ class GrayscaleToRGBTest(test_util.TensorFlowTestCase):
x_tf = constant_op.constant(x_np, shape=x_np.shape) x_tf = constant_op.constant(x_np, shape=x_np.shape)
# this is the error message we expect the function to raise # this is the error message we expect the function to raise
err_msg = "A grayscale image must be at least two-dimensional" err_msg = "must be at least two-dimensional"
with self.assertRaisesRegexp(ValueError, err_msg): with self.assertRaisesRegexp(ValueError, err_msg):
image_ops.grayscale_to_rgb(x_tf) image_ops.grayscale_to_rgb(x_tf)
@ -1682,7 +1682,7 @@ class CropToBoundingBoxTest(test_util.TensorFlowTestCase):
for x_shape in ([3, 5], [1, 3, 5, 1, 1]): for x_shape in ([3, 5], [1, 3, 5, 1, 1]):
self._assertRaises(x, x_shape, offset_height, offset_width, target_height, self._assertRaises(x, x_shape, offset_height, offset_width, target_height,
target_width, target_width,
"'image' must have either 3 or 4 dimensions.") "must have either 3 or 4 dimensions.")
@test_util.run_deprecated_v1 @test_util.run_deprecated_v1
def testZeroLengthInput(self): def testZeroLengthInput(self):
@ -2022,7 +2022,7 @@ class PadToBoundingBoxTest(test_util.TensorFlowTestCase):
for x_shape in ([3, 5], [1, 3, 5, 1, 1]): for x_shape in ([3, 5], [1, 3, 5, 1, 1]):
self._assertRaises(x, x_shape, offset_height, offset_width, target_height, self._assertRaises(x, x_shape, offset_height, offset_width, target_height,
target_width, target_width,
"'image' must have either 3 or 4 dimensions.") "must have either 3 or 4 dimensions.")
@test_util.run_deprecated_v1 @test_util.run_deprecated_v1
def testZeroLengthInput(self): def testZeroLengthInput(self):
@ -3734,11 +3734,11 @@ class ResizeImageWithCropOrPadTest(test_util.TensorFlowTestCase):
for x_shape in ([3, 5],): for x_shape in ([3, 5],):
self._assertRaises(x, x_shape, target_height, target_width, self._assertRaises(x, x_shape, target_height, target_width,
"'image' must have either 3 or 4 dimensions.") "must have either 3 or 4 dimensions.")
for x_shape in ([1, 3, 5, 1, 1],): for x_shape in ([1, 3, 5, 1, 1],):
self._assertRaises(x, x_shape, target_height, target_width, self._assertRaises(x, x_shape, target_height, target_width,
"'image' must have either 3 or 4 dimensions.") "must have either 3 or 4 dimensions.")
@test_util.run_deprecated_v1 @test_util.run_deprecated_v1
def testZeroLengthInput(self): def testZeroLengthInput(self):