Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash of tf.image.extract_glimpse with negative input #51658

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions tensorflow/core/kernels/image/attention_ops.cc
Expand Up @@ -87,9 +87,10 @@ class ExtractGlimpseOp : public OpKernel {

const int64_t output_height = window_size.tensor<int, 1>()(0);
const int64_t output_width = window_size.tensor<int, 1>()(1);

TensorShape output_shape = input_shape;
output_shape.set_dim(1, output_height);
output_shape.set_dim(2, output_width);
OP_REQUIRES_OK(context, output_shape.SetDimWithStatus(1, output_height));
OP_REQUIRES_OK(context, output_shape.SetDimWithStatus(2, output_width));

const Tensor& offsets = context->input(2);
OP_REQUIRES(context, offsets.shape().dims() == 2,
Expand Down
10 changes: 10 additions & 0 deletions tensorflow/python/kernel_tests/attention_ops_test.py
Expand Up @@ -22,6 +22,7 @@

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import errors
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_image_ops
from tensorflow.python.ops import image_ops
Expand Down Expand Up @@ -301,6 +302,15 @@ def testGlimpseNonNormalizedNonCentered(self):
np.asarray([[5, 6, 7], [10, 11, 12], [15, 16, 17]]),
self.evaluate(result2)[0, :, :, 0])

def testGlimpseNegativeInput(self):
img = np.arange(9).reshape([1,3,3,1])
with self.test_session():
with self.assertRaises((errors.InternalError, ValueError)):
result = image_ops.extract_glimpse_v2(
img, size=[1023, -63], offsets=[1023, 63],
centered=False, normalized=False)
self.evaluate(result)


if __name__ == '__main__':
test.main()