Skip to content

Commit

Permalink
Merge pull request #52841 from tensorflow/mm-cp-pr-2-on-r2.6
Browse files Browse the repository at this point in the history
PR #51732: Fix crash of tf.image.crop_and_resize when input is large …
  • Loading branch information
mihaimaruseac committed Oct 28, 2021
2 parents 5ac6f83 + 6be06cc commit 8b0691c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
26 changes: 14 additions & 12 deletions tensorflow/core/kernels/image/crop_and_resize_op.cc
Expand Up @@ -170,14 +170,15 @@ class CropAndResizeOp : public AsyncOpKernel {
context, crop_height > 0 && crop_width > 0,
errors::InvalidArgument("crop dimensions must be positive"), done);

TensorShape shape;
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(num_boxes), done);
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(crop_height), done);
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(crop_width), done);
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(depth), done);
// Allocate output tensor.
Tensor* output = nullptr;
OP_REQUIRES_OK_ASYNC(
context,
context->allocate_output(
0, TensorShape({num_boxes, crop_height, crop_width, depth}),
&output),
done);
OP_REQUIRES_OK_ASYNC(context, context->allocate_output(0, shape, &output),
done);

auto compute_callback = [this, context, output]() {
const Tensor& image = context->input(0);
Expand Down Expand Up @@ -417,14 +418,15 @@ class CropAndResizeGradImageOp : public AsyncOpKernel {
done);
}

TensorShape shape;
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(batch_size), done);
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(image_height), done);
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(image_width), done);
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(depth), done);
// Allocate output tensor.
Tensor* output = nullptr;
OP_REQUIRES_OK_ASYNC(
context,
context->allocate_output(
0, TensorShape({batch_size, image_height, image_width, depth}),
&output),
done);
OP_REQUIRES_OK_ASYNC(context, context->allocate_output(0, shape, &output),
done);

auto compute_callback = [this, context, output]() {
const Tensor& grads = context->input(0);
Expand Down
10 changes: 10 additions & 0 deletions tensorflow/python/ops/image_ops_test.py
Expand Up @@ -6051,6 +6051,16 @@ def testImageCropAndResize(self):
crop_size=[1, 1])
self.evaluate(op)

def testImageCropAndResizeWithInvalidInput(self):
with self.session():
with self.assertRaises((errors.InternalError, ValueError)):
op = image_ops_impl.crop_and_resize_v2(
image=np.ones((1, 1, 1, 1)),
boxes=np.ones((11, 4)),
box_indices=np.ones((11)),
crop_size=[2065374891, 1145309325])
self.evaluate(op)

@parameterized.named_parameters(
("_jpeg", "JPEG", "jpeg_merge_test1.jpg"),
("_png", "PNG", "lena_rgba.png"),
Expand Down

0 comments on commit 8b0691c

Please sign in to comment.