RandomShear: boxes are sheared in normalized coordinates while the image is sheared in pixel coordinates
Hi — I think RandomShear transforms images and bounding boxes in different coordinate frames, so boxes end up misaligned with the image content on non-square images.
The image path applies the shear matrix [[1, sx], [sy, 1]] directly in pixel coordinates (random_shear.py, _get_shear_matrix / affine_transform). The box path first normalizes x by width and y by height (rel_xyxy), then applies the shear arithmetic in normalized space (_shear_bboxes). After denormalizing, the horizontal cross-term is effectively scaled by W/H and the vertical one by H/W, so for any non-square image the boxes get a different shear than the pixels.
The repro below takes the box corners, maps them through the same matrix the image uses (inverse, since affine_transform maps output to input; the ±0.5 converts to pixel-center coordinates to match the raster), and compares the resulting box with what transform_bounding_boxes returns. On a 65×97 image with shear magnitude (0.08, 0.06), the returned box is off by up to 1.38 px (TensorFlow, torch) and 1.82 px (JAX, which sampled the opposite shear sign from the same seed).
Reproduces on 3.15.0 and on current master (3d2b275, reports 3.16.0), on all three backends.
Repro:
import copy
import keras
import numpy as np
H, W = 65, 97
box = np.array([30, 21, 62, 42], 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.RandomShear((.08, .08), (.06, .06), seed=1, bounding_box_format="xyxy")
t = layer.get_random_transformation(data, training=True, seed=1)
out = layer.transform_bounding_boxes(copy.deepcopy(data["bounding_boxes"]), t, training=True)
reported = keras.ops.convert_to_numpy(out["boxes"])[0, 0]
raw = keras.ops.convert_to_numpy(layer._get_shear_matrix(t["shear_factor"]))[0]
M = np.array([[raw[0], raw[1], raw[2]], [raw[3], raw[4], raw[5]], [0, 0, 1.]])
pts = np.array([[box[0], box[1]], [box[2], box[1]], [box[2], box[3]], [box[0], box[3]]])
q = (np.linalg.inv(M) @ np.c_[pts-.5, np.ones(4)].T).T[:, :2] + .5
aligned = np.r_[q.min(0), q.max(0)]
print("keras", keras.__version__, "shear", keras.ops.convert_to_numpy(t["shear_factor"])[0].tolist())
print("reported", reported.tolist())
print("image-aligned", aligned.tolist())
print("difference", (reported-aligned).tolist())Output (TensorFlow; torch is identical):
keras 3.16.0 shear [-0.07999999821186066, -0.05999999865889549]
reported [32.507076263427734, 22.30698585510254, 67.01415252685547, 44.69438552856445]
image-aligned [31.79019288246483, 22.87741153098447, 65.6326365673395, 45.9079581066907]
difference [0.7168833809629049, -0.5704256758819319, 1.3815159595159656, -1.2135725781262465]Output (JAX):
keras 3.16.0 shear [0.07999999821186066, 0.05999999865889549]
reported [24.9858455657959, 18.608016967773438, 59.492919921875, 40.99541473388672]
image-aligned [26.806270165356786, 17.391077249651822, 60.64871385023145, 40.42162382535805]
difference [-1.8204245995608872, 1.2169397181216155, -1.1557939283564522, 0.5737909085286717]Is the intended behavior that boxes follow the image's pixel-space shear? If so, one option is to shear boxes in absolute pixel coordinates with the same matrix as the image, then convert back to the requested format. Happy to open a PR with that change plus a test.
Source: keras-team/keras