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 segmentation fault in tf.image.crop_and_resize when boxes is inf or nan #42143

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next
Fix segmentation fault in tf.image.crop_and_resize when boxes is inf …
…or nan

This fix tries to address the issue raised in 42129 where segmentation fault
happened in tf.image.crop_and_resize when boxes is inf or nan.

This fix adds the check to make sure boxes is not inf or nan (isfinite)

This fix fixes 42129.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
  • Loading branch information
yongtang committed Aug 25, 2020
commit 3ade2efec2e90c6237de32a19680caaa3ebc2845
13 changes: 13 additions & 0 deletions tensorflow/core/kernels/image/crop_and_resize_op.cc
Expand Up @@ -71,6 +71,18 @@ static inline Status ParseAndCheckBoxSizes(const Tensor& boxes,
if (boxes.dim_size(1) != 4) {
return errors::InvalidArgument("boxes must have 4 columns");
}
for (int64 i = 0; i < *num_boxes; i++) {
for (int64 j = 0; j < 4; j++) {
if (!isfinite(boxes.tensor<float, 2>()(i, j))) {
return errors::InvalidArgument(
"boxes values must be finite, received boxes[", i, "]: ",
boxes.tensor<float, 2>()(i, 0), ", ",
boxes.tensor<float, 2>()(i, 1), ", ",
boxes.tensor<float, 2>()(i, 2), ", ",
boxes.tensor<float, 2>()(i, 3));
}
}
}
// The shape of 'box_index' is [num_boxes].
if (box_index.dims() != 1) {
return errors::InvalidArgument("box_index must be 1-D",
Expand Down Expand Up @@ -256,6 +268,7 @@ struct CropAndResize<CPUDevice, T> {
continue;
}
if (method_name == "bilinear") {

mihaimaruseac marked this conversation as resolved.
Show resolved Hide resolved
const int top_y_index = floorf(in_y);
const int bottom_y_index = ceilf(in_y);
const float y_lerp = in_y - top_y_index;
Expand Down
12 changes: 12 additions & 0 deletions tensorflow/python/ops/image_ops_test.py
Expand Up @@ -5663,6 +5663,18 @@ def testExpandAnimations(self):
self.assertAllEqual(list(image2.shape), [12, 40, 20, 3])
self.assertAllEqual(image2, image3)

def testImageCropAndResize(self):
# Test case for GitHub issue 42129
message = "boxes values must be finite"
with self.assertRaisesRegex(
(errors.InvalidArgumentError, ValueError), message):
v = image_ops_impl.crop_and_resize_v2(
image=array_ops.zeros((2, 1, 1, 1)),
boxes=[[1.0e+40, 0, 0, 0]],
box_indices=[1],
crop_size=[1, 1])
self.evaluate(v)


if __name__ == "__main__":
googletest.main()