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 2 or 3, but received: bounding_boxes['labels'].shape={labels_shape} What it means
For dense tensors, when boxes has rank 3 (batched, shape (batch, num_boxes, 4)), labels must have rank 2 (batch, num_boxes) or rank 3 (batch, num_boxes, num_classes). Any other rank breaks the per-batch, per-box label alignment.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py:168
" `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']` "
"to have rank 2 or 3, with shape "
"(num_boxes, 4) or (batch_size, num_boxes, 4). "
"Received: "
f"bounding_boxes['boxes'].shape={boxes_shape}"
)
View on GitHub (pinned to 7a34a03db6)
Solutions
- Reshape labels to (batch, num_boxes): labels.reshape(boxes.shape[0], boxes.shape[1])
- For one-hot labels use (batch, num_boxes, num_classes)
- Verify batch dim of labels equals boxes.shape[0]
Example fix
# before
bbs = {'boxes': boxes, 'labels': labels.reshape(-1)}
# after
bbs = {'boxes': boxes, 'labels': labels.reshape(boxes.shape[0], boxes.shape[1])} Defensive patterns
Strategy: validation
Validate before calling
if len(bbs['boxes'].shape) == 3:
assert len(bbs['labels'].shape) in (2, 3), bbs['labels'].shape Prevention
- When batching, reshape labels alongside boxes
- Keep one-hot labels as (batch, num_boxes, num_classes) rank 3
When it happens
Trigger: densify_bounding_boxes with boxes shape (8, 10, 4) but labels of rank 1 or rank 4, e.g. flat labels of shape (80,).
Common situations: Forgetting to batch labels when moving from single-image to batched inference; flattening labels with reshape(-1); keeping a trailing singleton dim making rank 4.
Related errors
- Found bounding_boxes['boxes'].shape={boxes_shape} and expect
- Expected `bounding_boxes['boxes']` to have rank 2 or 3, with
- `height` and `width` must be set if `format='xyxy'`.
- `variance` must be length 4, got {variance}
- `encoding_format` should be one of 'center_xywh' or 'center_
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/4e979d18e464b1f8.
Report an issue: GitHub.