{"record":{"id":"4e979d18e464b1f8","repo":"keras-team/keras","slug":"found-bounding-boxes-boxes-shape-boxes-shape-4e979d","errorCode":null,"errorMessage":"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} ","messagePattern":"Found bounding_boxes\\['boxes'\\]\\.shape=(.+?) and expected bounding_boxes\\['labels'\\] to have rank 2 or 3, but received: bounding_boxes\\['labels'\\]\\.shape=(.+?) ","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py","lineNumber":168,"sourceCode":"                \" `bounding_boxes['labels']` must also be a \"\n                \"Ragged tensor. \"\n                f\"Received: bounding_boxes['labels']={labels}\"\n            )\n    else:\n        boxes_shape = current_backend.shape(boxes)\n        labels_shape = current_backend.shape(labels)\n        if len(boxes_shape) == 2:  # (boxes, 4)\n            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":150,"sourceCodeEnd":183,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py#L150-L183","documentation":"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.","triggerScenarios":"densify_bounding_boxes with boxes shape (8, 10, 4) but labels of rank 1 or rank 4, e.g. flat labels of shape (80,).","commonSituations":"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.","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]"],"exampleFix":"# before\nbbs = {'boxes': boxes, 'labels': labels.reshape(-1)}\n# after\nbbs = {'boxes': boxes, 'labels': labels.reshape(boxes.shape[0], boxes.shape[1])}","handlingStrategy":"validation","validationCode":"if len(bbs['boxes'].shape) == 3:\n    assert len(bbs['labels'].shape) in (2, 3), bbs['labels'].shape","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When batching, reshape labels alongside boxes","Keep one-hot labels as (batch, num_boxes, num_classes) rank 3"],"tags":["keras","bounding-boxes","tensor-rank","batching"],"backgroundTag":"input-shape-validation-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}