RandomRotation: boxes are rotated about a different center than the image (half-pixel offset)
Hi — I think RandomRotation rotates bounding boxes about a slightly different center than the image, so boxes end up shifted by a fraction of a pixel.
Other Keras box kernels treat box coordinates as continuous edges: RandomFlip maps boxes through 1 - x in relative coordinates, i.e. W - x in absolute terms, and Resizing scales them by x * new_width / width. Both are correct only if a box edge at x means position x in continuous image coordinates.
RandomRotation builds its image matrix around the center cx = 0.5 * (width - 1), cy = 0.5 * (height - 1) (base_image_preprocessing_layer.py), which is the image center in pixel-index coordinates. The box path then applies that same matrix directly to the box endpoints (random_rotation.py → affine_transform in bounding_box.py) with no +0.5 / -0.5 conversion. So boxes are rotated about a point half a pixel away from where the image actually rotates.
The repro maps the box corners through the image's matrix with the half-pixel conversion and compares against transform_bounding_boxes. On a 65×97 image rotated by 17°, every corner is off by the same amount — about +0.124 px in x and −0.168 px in y — i.e. a pure translation, which is what a rotation-center mismatch produces. Its magnitude matches (I − R) · (0.5, 0.5) for a 17° rotation.
Reproduces on TensorFlow, JAX, and torch, on 3.15.0 and on current master (3d2b275, reports 3.16.0).
Repro:
import copy
import keras
import numpy as np
H, W = 65, 97
box = np.array([30, 20, 60, 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.RandomRotation((17/360, 17/360), 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(t["rotation_matrix"])[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]]])
def mapped(shift):
q = (np.linalg.inv(M) @ np.c_[pts-shift, np.ones(4)].T).T[:, :2] + shift
return np.r_[q.min(0), q.max(0)]
aligned = mapped(.5)
print("keras", keras.__version__)
print("reported", reported.tolist())
print("image-aligned", aligned.tolist())
print("difference", (reported-aligned).tolist())Output (TensorFlow; JAX is identical, torch agrees to ~1e-6):
keras 3.16.0
reported [27.27805519104004, 17.015880584716797, 62.399375915527344, 46.82573699951172]
image-aligned [27.15371615553497, 17.183915618825303, 62.2750351043513, 46.99377006767848]
difference [0.12433903550506997, -0.16803503410850595, 0.12434081117604023, -0.1680330681667641]Is the intended behavior that boxes rotate about the same center as the image? If so, one option is to shift box coordinates by -0.5 before applying the matrix and +0.5 after, matching the pixel-index frame the image matrix is built in. Happy to open a PR with that change plus a test.
Source: keras-team/keras