Merge pull request #42143 from yongtang:42129-tf.image.crop_and_resize

PiperOrigin-RevId: 337926784
Change-Id: I64c86f53021e0588c72fcc4142b7cb8947c19d4a
This commit is contained in:
TensorFlower Gardener 2020-10-19 14:02:19 -07:00
commit c031923133
2 changed files with 31 additions and 1 deletions

View File

@ -207,7 +207,7 @@ class CropAndResizeOp : public AsyncOpKernel {
namespace functor {
template <typename T>
struct CropAndResize<CPUDevice, T> {
bool operator()(const OpKernelContext* context,
bool operator()(OpKernelContext* context,
typename TTypes<T, 4>::ConstTensor image,
typename TTypes<float, 2>::ConstTensor boxes,
typename TTypes<int32, 1>::ConstTensor box_index,
@ -222,6 +222,17 @@ struct CropAndResize<CPUDevice, T> {
const int crop_width = crops.dimension(2);
const int depth = crops.dimension(3);
// Since `functor::CropAndResize` operates on float, we first validate
// that we don't overflow (since overflow causes undefined behavior which
// could result in segfault in this scenario).
const Eigen::Tensor<bool, 0, Eigen::RowMajor> only_finite_elements =
boxes.isfinite().all();
if (!only_finite_elements()) {
context->SetStatus(errors::InvalidArgument(
"Boxes contains at least one element that is not finite"));
return false;
}
// Sharding across boxes.
auto CropAndResizePerBox = [&](int64 start_box, int64 limit_box) {
for (int b = start_box; b < limit_box; ++b) {

View File

@ -5710,6 +5710,25 @@ class DecodeImageTest(test_util.TensorFlowTestCase, parameterized.TestCase):
self.assertAllEqual(list(image2.shape), [12, 40, 20, 3])
self.assertAllEqual(image2, image3)
def testImageCropAndResize(self):
if test_util.is_gpu_available():
op = 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(op)
else:
message = "Boxes contains at least one element that is not finite"
with self.assertRaisesRegex((errors.InvalidArgumentError, ValueError),
message):
op = 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(op)
@parameterized.named_parameters(
("_jpeg", "JPEG", "jpeg_merge_test1.jpg"),
("_png", "PNG", "lena_rgba.png"),