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 resize image bug #37115

Merged
merged 5 commits into from
Mar 5, 2020
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
14 changes: 6 additions & 8 deletions tensorflow/python/ops/image_ops_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,20 +1201,17 @@ def _resize_images_common(images, resizer_fn, size, preserve_aspect_ratio, name,
if not size.get_shape().is_compatible_with([2]):
raise ValueError('\'size\' must be a 1-D Tensor of 2 elements: '
'new_height, new_width')
size_const_as_shape = tensor_util.constant_value_as_shape(size)
new_height_const = size_const_as_shape.dims[0].value
new_width_const = size_const_as_shape.dims[1].value

if preserve_aspect_ratio:
# Get the current shapes of the image, even if dynamic.
_, current_height, current_width, _ = _ImageDimensions(images, rank=4)

# do the computation to find the right scale and height/width.
scale_factor_height = (
math_ops.cast(new_height_const, dtypes.float32) /
math_ops.cast(size[0], dtypes.float32) /
math_ops.cast(current_height, dtypes.float32))
scale_factor_width = (
math_ops.cast(new_width_const, dtypes.float32) /
math_ops.cast(size[1], dtypes.float32) /
math_ops.cast(current_width, dtypes.float32))
scale_factor = math_ops.minimum(scale_factor_height, scale_factor_width)
scaled_height_const = math_ops.cast(
Expand All @@ -1230,9 +1227,10 @@ def _resize_images_common(images, resizer_fn, size, preserve_aspect_ratio, name,
size = ops.convert_to_tensor([scaled_height_const, scaled_width_const],
dtypes.int32,
name='size')
size_const_as_shape = tensor_util.constant_value_as_shape(size)
new_height_const = size_const_as_shape.dims[0].value
new_width_const = size_const_as_shape.dims[1].value

size_const_as_shape = tensor_util.constant_value_as_shape(size)
new_height_const = size_const_as_shape.dims[0].value
new_width_const = size_const_as_shape.dims[1].value

# If we can determine that the height and width will be unmodified by this
# transformation, we avoid performing the resize.
Expand Down
15 changes: 10 additions & 5 deletions tensorflow/python/ops/image_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,8 @@ def _ResizeImageCall(self, x, max_h, max_w, preserve_aspect_ratio,
feed_dict = {}

y = image_ops.resize_images(
x_tensor, target_max, preserve_aspect_ratio=preserve_aspect_ratio)
x_tensor, ops.convert_to_tensor(target_max),
preserve_aspect_ratio=preserve_aspect_ratio)

with self.cached_session(use_gpu=True):
return y.eval(feed_dict=feed_dict)
Expand Down Expand Up @@ -2753,11 +2754,15 @@ def _assertResizeCheckShape(self,

@test_util.run_deprecated_v1
def testPreserveAspectRatioMultipleImages(self):
x_shape = [10, 100, 100, 10]
x_shape = [10, 100, 80, 10]
x = np.random.uniform(size=x_shape)

self._assertResizeCheckShape(
x, x_shape, [250, 250], [10, 250, 250, 10], preserve_aspect_ratio=False)
for preserve_aspect_ratio in [True, False]:
with self.subTest(preserve_aspect_ratio=preserve_aspect_ratio):
expect_shape = [10, 250, 200, 10] if preserve_aspect_ratio \
else [10, 250, 250, 10]
self._assertResizeCheckShape(
x, x_shape, [250, 250], expect_shape,
preserve_aspect_ratio=preserve_aspect_ratio)

@test_util.run_deprecated_v1
def testPreserveAspectRatioNoOp(self):
Expand Down