Skip to content

Commit 6be06cc

Browse files
yongtangmihaimaruseac
authored andcommitted
PR #51732: Fix crash of tf.image.crop_and_resize when input is large number
Imported from GitHub PR #51732 This PR is part of the effort in #46890 where tf.image.crop_and_resize will crash if shape consists of large number. Signed-off-by: Yong Tang <yong.tang.github@outlook.com> Copybara import of the project: -- c8d8705 by Yong Tang <yong.tang.github@outlook.com>: Fix crash of tf.image.crop_and_resize when input is large number This PR is part of the effort in 46890 where tf.image.crop_and_resize will crash if shape consists of large number. Signed-off-by: Yong Tang <yong.tang.github@outlook.com> COPYBARA_INTEGRATE_REVIEW=#51732 from yongtang:46890-tf.image.crop_and_resize c8d8705 PiperOrigin-RevId: 394109830 Change-Id: If049dad0844df9353722029ee95bc76819eda1f4
1 parent dd68305 commit 6be06cc

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

tensorflow/core/kernels/image/crop_and_resize_op.cc

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ class CropAndResizeOp : public AsyncOpKernel {
170170
context, crop_height > 0 && crop_width > 0,
171171
errors::InvalidArgument("crop dimensions must be positive"), done);
172172

173+
TensorShape shape;
174+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(num_boxes), done);
175+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(crop_height), done);
176+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(crop_width), done);
177+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(depth), done);
173178
// Allocate output tensor.
174179
Tensor* output = nullptr;
175-
OP_REQUIRES_OK_ASYNC(
176-
context,
177-
context->allocate_output(
178-
0, TensorShape({num_boxes, crop_height, crop_width, depth}),
179-
&output),
180-
done);
180+
OP_REQUIRES_OK_ASYNC(context, context->allocate_output(0, shape, &output),
181+
done);
181182

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

421+
TensorShape shape;
422+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(batch_size), done);
423+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(image_height), done);
424+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(image_width), done);
425+
OP_REQUIRES_OK_ASYNC(context, shape.AddDimWithStatus(depth), done);
420426
// Allocate output tensor.
421427
Tensor* output = nullptr;
422-
OP_REQUIRES_OK_ASYNC(
423-
context,
424-
context->allocate_output(
425-
0, TensorShape({batch_size, image_height, image_width, depth}),
426-
&output),
427-
done);
428+
OP_REQUIRES_OK_ASYNC(context, context->allocate_output(0, shape, &output),
429+
done);
428430

429431
auto compute_callback = [this, context, output]() {
430432
const Tensor& grads = context->input(0);

tensorflow/python/ops/image_ops_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6036,6 +6036,16 @@ def testImageCropAndResize(self):
60366036
crop_size=[1, 1])
60376037
self.evaluate(op)
60386038

6039+
def testImageCropAndResizeWithInvalidInput(self):
6040+
with self.session():
6041+
with self.assertRaises((errors.InternalError, ValueError)):
6042+
op = image_ops_impl.crop_and_resize_v2(
6043+
image=np.ones((1, 1, 1, 1)),
6044+
boxes=np.ones((11, 4)),
6045+
box_indices=np.ones((11)),
6046+
crop_size=[2065374891, 1145309325])
6047+
self.evaluate(op)
6048+
60396049
@parameterized.named_parameters(
60406050
("_jpeg", "JPEG", "jpeg_merge_test1.jpg"),
60416051
("_png", "PNG", "lena_rgba.png"),

0 commit comments

Comments
 (0)