Repository navigation
Description
Issue type
Bug
Have you reproduced the bug with TensorFlow Nightly?
Yes
Source
binary
TensorFlow version
2.21.0, tf-nightly 2.23.0.dev20261028
Custom code
Yes
OS platform and distribution
macOS 15.7.2 (arm64)
Python version
3.11.7
Current behavior?
tf.image.resize_with_pad often resizes the image one pixel short of the target and pads the missing row or column with zeros. This happens even when the image already has the target's aspect ratio and no padding is needed. For example, a 512x512 image resized to 224x224 comes back as a 223x223 image with a one-pixel black border on the bottom and right. Because resize_with_pad is commonly used to prepare model inputs, this silently feeds models black borders that shouldn't be there.
The cause is float32 rounding in _resize_image_with_pad_common (tensorflow/python/ops/image_ops_impl.py):
ratio = max_(f_width / f_target_width, f_height / f_target_height)
resized_height_float = f_height / ratio
resized_width = math_ops.cast(math_ops.floor(resized_width_float), dtype=dtypes.int32)When the scaled size is a whole number (for example 512 / (512 / 224) = 224), the float32 result is often just below it (223.99998), and floor drops a pixel. The padding is computed with floor((target - resized_height_float) / 2) from the same float, so it can also come out one pixel off, which shifts the image off center.
This isn't a rare edge case. Over 200,000 random (height, width, target_height, target_width) combinations from 1 to 2000, the float32 result differs from the exact rational result in about 5% of them. Among common sizes (480x640, 720x1280, 1080x1920, 512x512, ...) and common targets (224, 256, 299, 300, 384, 416, 512, 600, ...), it hits 35 of 360 pairs.
Expected: compute the same quantities exactly. With integers, resized = min(target, size * other_target // other_size) and padding = floor((target - scaled size) / 2) give exact results. For 512x512 -> 224x224 that means no padding, and for 480x640 -> 300x300 it means a 225x300 image with 37 rows of padding on top and 38 on the bottom.
Standalone code to reproduce the issue
import numpy as np
import tensorflow as tf
def padding(out):
"""Number of all-zero rows/columns on each side of a 2-D image."""
rows = out.max(axis=1) == 0
cols = out.max(axis=0) == 0
def leading(m):
return int(np.argmin(m)) if not m.all() else len(m)
return (leading(rows), leading(rows[::-1]), leading(cols), leading(cols[::-1]))
# An all-ones image: every zero in the output is padding.
for (h, w), (th, tw) in [((512, 512), (224, 224)),
((480, 640), (300, 300)),
((720, 1280), (600, 600))]:
out = tf.image.resize_with_pad(tf.ones([h, w, 1]), th, tw).numpy()[..., 0]
top, bottom, left, right = padding(out)
print(f"{h}x{w} -> {th}x{tw}: padding top={top} bottom={bottom} "
f"left={left} right={right}")Relevant log output
2.23.0-dev20261028 v1.12.1-148391-g87a97a3655f
512x512 -> 224x224: padding top=0 bottom=1 left=0 right=1
480x640 -> 300x300: padding top=37 bottom=39 left=0 right=1
720x1280 -> 600x600: padding top=131 bottom=132 left=0 right=1Expected:
512x512 -> 224x224: padding top=0 bottom=0 left=0 right=0
480x640 -> 300x300: padding top=37 bottom=38 left=0 right=0
720x1280 -> 600x600: padding top=131 bottom=132 left=0 right=0I have a fix ready and will open a PR that links here.
Activity
Venkat6871 commentedon Nov 2, 2026
Hi @bodapatisaikrishna ,
Thank you for reporting this issue and raising the PR to address it. A fix has been proposed in PR, and the issue will be resolved once it is merged.
We truly appreciate your initiative and effort in contributing this fix.
google-ml-butler commentedon Nov 3, 2026
Are you satisfied with the resolution of your issue?
Yes
No