keras-team/keras · error · ValueError
`height` and `width` must be set if `format='xyxy'`.
Error message
`height` and `width` must be set if `format='xyxy'`.
What it means
clip_to_image_size clips bounding boxes to the image boundaries. Only 'xyxy' and 'rel_xyxy' formats are supported (others raise NotImplementedError). In absolute 'xyxy' coordinates the function needs the pixel height and width to clip against, so passing height=None or width=None with that format raises this ValueError.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/bounding_box.py:116
if source.startswith("rel_") and target.startswith("rel_"):
source = source.replace("rel_", "", 1)
target = target.replace("rel_", "", 1)
to_xyxy_converter = to_xyxy_converters[source]
from_xyxy_converter = from_xyxy_converters[target]
in_xyxy_boxes = to_xyxy_converter(boxes, height, width)
return from_xyxy_converter(in_xyxy_boxes, height, width)
def clip_to_image_size(
self,
bounding_boxes,
height=None,
width=None,
bounding_box_format="xyxy",
):
if bounding_box_format not in ("xyxy", "rel_xyxy"):
raise NotImplementedError
if bounding_box_format == "xyxy" and (height is None or width is None):
raise ValueError(
"`height` and `width` must be set if `format='xyxy'`."
)
ops = self.backend
boxes = bounding_boxes["boxes"]
labels = bounding_boxes.get("labels", None)
if width is not None:
width = ops.cast(width, boxes.dtype)
if height is not None:
height = ops.cast(height, boxes.dtype)
if bounding_box_format == "xyxy":
x1, y1, x2, y2 = ops.numpy.split(boxes, 4, axis=-1)
x1 = ops.numpy.clip(x1, 0, width)
y1 = ops.numpy.clip(y1, 0, height)
x2 = ops.numpy.clip(x2, 0, width)
y2 = ops.numpy.clip(y2, 0, height)
boxes = ops.numpy.concatenate([x1, y1, x2, y2], axis=-1)View on GitHub (pinned to 7a34a03db6)
Solutions
- Pass integer height and width matching the image the boxes refer to, e.g. clip_to_image_size(bb, height=416, width=416, bounding_box_format="xyxy").
- If sizes are unknown, use format='rel_xyxy' so no height/width is needed.
Example fix
# before clipped = clip_to_image_size(boxes, bounding_box_format="xyxy") # after clipped = clip_to_image_size(boxes, bounding_box_format="xyxy", height=img.shape[1], width=img.shape[2])
Defensive patterns
Strategy: validation
Validate before calling
if bounding_box_format == "xyxy":
assert height is not None and width is not None, "xyxy requires height and width" Prevention
- Thread image height/width through your preprocessing pipeline alongside the boxes.
When it happens
Trigger: Calling clip_to_image_size(boxes, bounding_box_format="xyxy") without height/width, or with only one of them set.
Common situations: Pipeline refactor from relative to absolute coordinates where the image-size arguments were dropped; calling the utility on tensors whose spatial dims are unknown so the caller skipped sizes.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- When using relative bounding box formats (e.g. `rel_yxyx`) t
- `variance` must be length 4, got {variance}
- `encoding_format` should be one of 'center_xywh' or 'center_
- `encoded_format` should be 'center_xywh' or 'center_yxhw', b
- compute_iou() expects boxes1 to be batched, or to be unbatch
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/e95d75ebc4eb19e7.
Report an issue: GitHub.