keras-team/keras · error · ValueError
compute_iou() expects boxes2 to be batched, or to be unbatch
Error message
compute_iou() expects boxes2 to be batched, or to be unbatched. Received len(boxes1.shape)={boxes1_rank}, len(boxes2.shape)={boxes2_rank}. Expected either len(boxes2.shape)=2 AND or len(boxes2.shape)=3. What it means
Same rank contract as boxes1, applied to the second argument: compute_iou requires boxes2 to be rank 2 ((M, 4), unbatched) or rank 3 ((batch, M, 4), batched). A boxes2 of any other rank raises this ValueError with both ranks echoed in the message.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/iou.py:112
`image_shape` is used for normalization.
Returns:
iou_lookup_table: a vector containing the pairwise ious of boxes1 and
boxes2.
""" # noqa: E501
boxes1_rank = len(ops.shape(boxes1))
boxes2_rank = len(ops.shape(boxes2))
if boxes1_rank not in [2, 3]:
raise ValueError(
"compute_iou() expects boxes1 to be batched, or to be unbatched. "
f"Received len(boxes1.shape)={boxes1_rank}, "
f"len(boxes2.shape)={boxes2_rank}. Expected either "
"len(boxes1.shape)=2 AND or len(boxes1.shape)=3."
)
if boxes2_rank not in [2, 3]:
raise ValueError(
"compute_iou() expects boxes2 to be batched, or to be unbatched. "
f"Received len(boxes1.shape)={boxes1_rank}, "
f"len(boxes2.shape)={boxes2_rank}. Expected either "
"len(boxes2.shape)=2 AND or len(boxes2.shape)=3."
)
target_format = "yxyx"
if "rel" in bounding_box_format and image_shape is None:
raise ValueError(
"When using relative bounding box formats (e.g. `rel_yxyx`) "
"the `image_shape` argument must be provided."
f"Received `image_shape`: {image_shape}"
)
if image_shape is None:
height, width = None, None
else:
height, width, _ = image_shapeView on GitHub (pinned to 7a34a03db6)
Solutions
- Ensure boxes2 has shape (M, 4) or (batch, M, 4).
- Add a leading batch axis with ops.expand_dims(boxes2, axis=0) when boxes1 is batched.
- Validate len(ops.shape(boxes2)) in [2, 3] before calling in data-pipeline code.
Example fix
# before iou = compute_iou(pred_boxes, gt_flat_list) # after iou = compute_iou(pred_boxes, ops.convert_to_tensor(gt_flat_list).reshape(-1, 4))
Defensive patterns
Strategy: validation
Validate before calling
boxes2 = ops.convert_to_tensor(boxes2)
assert len(ops.shape(boxes2)) in (2, 3), f"boxes2 rank {len(ops.shape(boxes2))} not in (2,3)" Type guard
def boxes_have_valid_rank(t):
return len(ops.shape(ops.convert_to_tensor(t))) in (2, 3)
Prevention
- Reshape loaded ground-truth to (-1, 4) at data-loading time, not at IoU time.
When it happens
Trigger: Passing ground-truth boxes as a flat list of coordinates, a rank-4 array, or a nesting depth that does not match a batched boxes1.
Common situations: Comparing predictions (batched) against labels loaded from a JSON that are flat or singly nested; forgetting to expand dims on the reference boxes.
Related errors
- compute_iou() expects boxes1 to be batched, or to be unbatch
- When using relative bounding box formats (e.g. `rel_yxyx`) t
- Architecture configuration does not match {weights_name} var
- `height` and `width` must be set if `format='xyxy'`.
- `variance` must be length 4, got {variance}
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/f0b83b17dc4ab51d.
Report an issue: GitHub.