{"record":{"id":"6800754edb650abd","repo":"keras-team/keras","slug":"found-bounding-boxes-boxes-shape-boxes-shape","errorCode":null,"errorMessage":"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} ","messagePattern":"Found bounding_boxes\\['boxes'\\]\\.shape=(.+?) and expected bounding_boxes\\['labels'\\] to have rank 1 or 2, 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":159,"sourceCode":"                \"`bounding_boxes['labels']` are both lists, \"\n                \"they must have the same length. Received: \"\n                f\"len(bounding_boxes['boxes'])={len(boxes)} and \"\n                f\"len(bounding_boxes['labels'])={len(labels)} and \"\n            )\n    elif tf_utils.is_ragged_tensor(boxes):\n        if not tf_utils.is_ragged_tensor(labels):\n            raise ValueError(\n                \"If `bounding_boxes['boxes']` is a Ragged tensor, \"\n                \" `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']` \"","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/validation.py#L141-L177","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nbbs = {'boxes': boxes, 'labels': labels[None, None, :]}\n# after\nbbs = {'boxes': boxes, 'labels': keras.ops.reshape(labels, (-1,))}","handlingStrategy":"validation","validationCode":"if len(bbs['boxes'].shape) == 2:\n    assert len(bbs['labels'].shape) in (1, 2), bbs['labels'].shape","typeGuard":"def labels_rank_ok(boxes, labels):\n    r = len(boxes.shape)\n    lr = len(labels.shape)\n    return (r == 2 and lr in (1, 2)) or (r == 3 and lr in (2, 3))","tryCatchPattern":null,"preventionTips":["Standardize labels as (num_boxes,) unbatched / (batch, num_boxes) batched","Print .shape of both entries before calling densifying ops"],"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"}