{"record":{"id":"9f23f506da0a44a8","repo":"keras-team/keras","slug":"expected-bounding-boxes-boxes-to-have-rank-2","errorCode":null,"errorMessage":"Expected `bounding_boxes['boxes']` to have rank 2 or 3, with shape (num_boxes, 4) or (batch_size, num_boxes, 4). Received: bounding_boxes['boxes'].shape={boxes_shape}","messagePattern":"Expected `bounding_boxes\\['boxes'\\]` to have rank 2 or 3, with shape \\(num_boxes, 4\\) or \\(batch_size, num_boxes, 4\\)\\. Received: bounding_boxes\\['boxes'\\]\\.shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py","lineNumber":176,"sourceCode":"            if len(labels_shape) not in {1, 2}:\n                raise ValueError(\n                    \"Found \"\n                    f\"bounding_boxes['boxes'].shape={boxes_shape} \"\n                    \"and expected bounding_boxes['labels'] to have \"\n                    \"rank 1 or 2, but received: \"\n                    f\"bounding_boxes['labels'].shape={labels_shape} \"\n                )\n        elif len(boxes_shape) == 3:\n            if len(labels_shape) not in {2, 3}:\n                raise ValueError(\n                    \"Found \"\n                    f\"bounding_boxes['boxes'].shape={boxes_shape} \"\n                    \"and expected bounding_boxes['labels'] to have \"\n                    \"rank 2 or 3, but received: \"\n                    f\"bounding_boxes['labels'].shape={labels_shape} \"\n                )\n        else:\n            raise ValueError(\n                \"Expected `bounding_boxes['boxes']` \"\n                \"to have rank 2 or 3, with shape \"\n                \"(num_boxes, 4) or (batch_size, num_boxes, 4). \"\n                \"Received: \"\n                f\"bounding_boxes['boxes'].shape={boxes_shape}\"\n            )\n","sourceCodeStart":158,"sourceCodeEnd":183,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py#L158-L183","documentation":"validate_bounding_boxes only accepts dense 'boxes' of rank 2 ((num_boxes, 4), unbatched) or rank 3 ((batch, num_boxes, 4), batched). Rank-1, rank-4, or higher boxes tensors cannot be interpreted as a set of xyxy boxes and are rejected.","triggerScenarios":"densify_bounding_boxes with boxes of rank 1 (flat 8-vector), rank 4 (extra leading dims, e.g. (1, batch, boxes, 4)).","commonSituations":"Residual batch dim of 1 left after expand_dims; wrapping once more in np.array([x]) making rank 4; feeding keypoint arrays of the wrong rank.","solutions":["Squeeze stray leading dims: boxes = keras.ops.squeeze(boxes, axis=0) until rank is 2 or 3","For a single image ensure shape (num_boxes, 4); for a batch (batch, num_boxes, 4)","Print boxes.shape right before the call to spot the extra axis"],"exampleFix":"# before\nboxes = np.array([all_boxes])  # shape (1, batch, boxes, 4) -> rank 4\n# after\nboxes = np.array(all_boxes)    # shape (batch, boxes, 4)","handlingStrategy":"validation","validationCode":"assert len(bbs['boxes'].shape) in (2, 3), bbs['boxes'].shape","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Squeeze singleton leading dims before packing boxes","Verify last dim is 4 (xyxy) when constructing the tensor"],"tags":["keras","bounding-boxes","tensor-rank","input-validation"],"backgroundTag":"input-shape-validation-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}