keras-team/keras · error · ValueError
Found bounding_boxes['boxes'].shape={boxes_shape} and expect
Error message
Found bounding_boxes['boxes'].shape={boxes_shape} and expected bounding_boxes['labels'] to have rank 1 or 2, but received: bounding_boxes['labels'].shape={labels_shape} What it means
For dense tensors, when boxes has rank 2 (single image, shape (num_boxes, 4)), labels must have rank 1 (num_boxes,) or rank 2 (1, num_boxes). Any other rank (e.g. a scalar or rank-3 labels tensor) breaks the box-to-label correspondence.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py:159
"`bounding_boxes['labels']` are both lists, "
"they must have the same length. Received: "
f"len(bounding_boxes['boxes'])={len(boxes)} and "
f"len(bounding_boxes['labels'])={len(labels)} and "
)
elif tf_utils.is_ragged_tensor(boxes):
if not tf_utils.is_ragged_tensor(labels):
raise ValueError(
"If `bounding_boxes['boxes']` is a Ragged tensor, "
" `bounding_boxes['labels']` must also be a "
"Ragged tensor. "
f"Received: bounding_boxes['labels']={labels}"
)
else:
boxes_shape = current_backend.shape(boxes)
labels_shape = current_backend.shape(labels)
if len(boxes_shape) == 2: # (boxes, 4)
if len(labels_shape) not in {1, 2}:
raise ValueError(
"Found "
f"bounding_boxes['boxes'].shape={boxes_shape} "
"and expected bounding_boxes['labels'] to have "
"rank 1 or 2, but received: "
f"bounding_boxes['labels'].shape={labels_shape} "
)
elif len(boxes_shape) == 3:
if len(labels_shape) not in {2, 3}:
raise ValueError(
"Found "
f"bounding_boxes['boxes'].shape={boxes_shape} "
"and expected bounding_boxes['labels'] to have "
"rank 2 or 3, but received: "
f"bounding_boxes['labels'].shape={labels_shape} "
)
else:
raise ValueError(
"Expected `bounding_boxes['boxes']` "View on GitHub (pinned to 7a34a03db6)
Solutions
- Reshape labels to (num_boxes,) — e.g. labels.reshape(-1) or keras.ops.reshape(labels, (-1,))
- If one-hot, keep rank 2 as (num_boxes, num_classes)
- Check boxes actually is rank 2; a stray leading dim on boxes also shifts the expected labels rank
Example fix
# before
bbs = {'boxes': boxes, 'labels': labels[None, None, :]}
# after
bbs = {'boxes': boxes, 'labels': keras.ops.reshape(labels, (-1,))} Defensive patterns
Strategy: validation
Validate before calling
if len(bbs['boxes'].shape) == 2:
assert len(bbs['labels'].shape) in (1, 2), bbs['labels'].shape Type guard
def labels_rank_ok(boxes, labels):
r = len(boxes.shape)
lr = len(labels.shape)
return (r == 2 and lr in (1, 2)) or (r == 3 and lr in (2, 3)) Prevention
- Standardize labels as (num_boxes,) unbatched / (batch, num_boxes) batched
- Print .shape of both entries before calling densifying ops
When it happens
Trigger: densify_bounding_boxes with boxes shape (num_boxes, 4) and labels of rank 0, 3+, e.g. labels shape (1, 1, num_boxes) or a scalar class id.
Common situations: Squeezing/reshaping labels incorrectly during preprocessing; carrying extra leading dims (e.g. (1, batch, boxes)) from a previous stage; using one-hot labels with extra dims.
Related errors
- Expected `bounding_boxes['boxes']` to have rank 2 or 3, with
- If `bounding_boxes['boxes']` is a list, then `bounding_boxes
- Found bounding_boxes['boxes'].shape={boxes_shape} and expect
- Expected as input a list/tuple of 2 tensors. Received input_
- `height` and `width` must be set if `format='xyxy'`.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/6800754edb650abd.
Report an issue: GitHub.