#23772·keras

Resizing with crop_to_aspect_ratio / pad_to_aspect_ratio: boxes use real-valued crop/pad geometry while the image uses integer-rounded geometry

Author: MaverickTopGCreated Sep 26, 2026Updated Sep 26, 2026
Labelslayers

Hi — I think Resizing with crop_to_aspect_ratio=True or pad_to_aspect_ratio=True computes the crop/pad geometry differently for images and for bounding boxes, so boxes end up slightly misaligned with the image content.

On the image side, each backend converts the intermediate crop/pad dimensions and offsets to integers before resizing (keras/src/backend/{tensorflow,jax,torch}/ops/image.py, e.g. crop_height = int(...), crop_box_wstart = int(...)). On the box side, resizing.py derives the scale and offsets from the ideal real-valued aspect ratio instead of reusing the raster's rounded geometry (the pad branch and the crop branch of the box transform). Whenever the ideal crop or pad size isn't an integer, the two paths scale by slightly different factors.

The repro below computes where a box should land given the image path's actual integer crop/pad, and compares it with what transform_bounding_boxes returns. For a 65×97 image resized to 53×79 with cropping, the returned box is off by up to 0.58 px; for a 31×47 image resized to 19×29 with padding, up to 0.15 px. The error is along the axis being cropped or padded, and the other axis matches to float precision.

Reproduces identically on TensorFlow, JAX, and torch, on 3.15.0 and on current master (3d2b275, reports 3.16.0).

Repro:

python
import copy
import keras
import numpy as np

def run(kind, h, w, oh, ow):
    box = np.array([.2*w, .2*h, .8*w, .8*h], np.float32)
    data = {"images": np.zeros((1, h, w, 1), np.float32),
            "bounding_boxes": {"boxes": box[None, None], "labels": np.array([[1]])}}
    layer = keras.layers.Resizing(oh, ow, crop_to_aspect_ratio=kind == "crop",
                                  pad_to_aspect_ratio=kind == "pad", bounding_box_format="xyxy")
    t = layer.get_random_transformation(data)
    out = layer.transform_bounding_boxes(copy.deepcopy(data["bounding_boxes"]), t)
    reported = keras.ops.convert_to_numpy(out["boxes"])[0, 0]
    if kind == "crop":
        ch = max(min(h, int(w*oh/ow)), 1); cw = max(min(w, int(h*ow/oh)), 1)
        top, left = int((h-ch)/2), int((w-cw)/2)
        aligned = (box-np.array([left, top, left, top]))*np.array([ow/cw, oh/ch]*2)
    else:
        ph, pw = max(h, int(w*oh/ow)), max(w, int(h*ow/oh))
        top, left = int((ph-h)/2), int((pw-w)/2)
        ah, aw = h+2*top, w+2*left
        aligned = (box+np.array([left, top, left, top]))*np.array([ow/aw, oh/ah]*2)
    print(kind, "reported", reported.tolist())
    print(kind, "image-aligned", aligned.tolist(), "difference", (reported-aligned).tolist())

print("keras", keras.__version__)
run("crop", 65, 97, 53, 79)
run("pad", 31, 47, 19, 29)

Output (identical on TensorFlow, JAX, and torch):

keras 3.16.0
crop reported [15.818461418151855, 10.600000381469727, 63.27384567260742, 42.400001525878906]
crop image-aligned [15.964583019415537, 10.6, 63.85833207766215, 42.4] difference [-0.14612160126368146, 3.8146972691777137e-07, -0.5844864050547258, 1.5258789076710855e-06]
pad reported [5.761290073394775, 3.799999952316284, 23.0451602935791, 15.199999809265137]
pad image-aligned [5.7999997646250625, 3.7999998830979873, 23.19999905850025, 15.19999953239195] difference [-0.03870969123028711, 6.921829687200898e-08, -0.15483876492114845, 2.768731874880359e-07]

Is the intended behavior that boxes follow the raster's actual (integer-rounded) crop/pad geometry? If so, one option is to compute the box transform from the same rounded intermediate dimensions and offsets the image path uses. Happy to open a PR with that change plus a test.