{"record":{"id":"f0b83b17dc4ab51d","repo":"keras-team/keras","slug":"compute-iou-expects-boxes2-to-be-batched-or-to","errorCode":null,"errorMessage":"compute_iou() expects boxes2 to be batched, or to be unbatched. Received len(boxes1.shape)={boxes1_rank}, len(boxes2.shape)={boxes2_rank}. Expected either len(boxes2.shape)=2 AND or len(boxes2.shape)=3.","messagePattern":"compute_iou\\(\\) expects boxes2 to be batched, or to be unbatched\\. Received len\\(boxes1\\.shape\\)=(.+?), len\\(boxes2\\.shape\\)=(.+?)\\. Expected either len\\(boxes2\\.shape\\)=2 AND or len\\(boxes2\\.shape\\)=3\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/iou.py","lineNumber":112,"sourceCode":"            `image_shape` is used for normalization.\n\n    Returns:\n        iou_lookup_table: a vector containing the pairwise ious of boxes1 and\n            boxes2.\n    \"\"\"  # noqa: E501\n\n    boxes1_rank = len(ops.shape(boxes1))\n    boxes2_rank = len(ops.shape(boxes2))\n\n    if boxes1_rank not in [2, 3]:\n        raise ValueError(\n            \"compute_iou() expects boxes1 to be batched, or to be unbatched. \"\n            f\"Received len(boxes1.shape)={boxes1_rank}, \"\n            f\"len(boxes2.shape)={boxes2_rank}. Expected either \"\n            \"len(boxes1.shape)=2 AND or len(boxes1.shape)=3.\"\n        )\n    if boxes2_rank not in [2, 3]:\n        raise ValueError(\n            \"compute_iou() expects boxes2 to be batched, or to be unbatched. \"\n            f\"Received len(boxes1.shape)={boxes1_rank}, \"\n            f\"len(boxes2.shape)={boxes2_rank}. Expected either \"\n            \"len(boxes2.shape)=2 AND or len(boxes2.shape)=3.\"\n        )\n\n    target_format = \"yxyx\"\n    if \"rel\" in bounding_box_format and image_shape is None:\n        raise ValueError(\n            \"When using relative bounding box formats (e.g. `rel_yxyx`) \"\n            \"the `image_shape` argument must be provided.\"\n            f\"Received `image_shape`: {image_shape}\"\n        )\n\n    if image_shape is None:\n        height, width = None, None\n    else:\n        height, width, _ = image_shape","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/iou.py#L94-L130","documentation":"Same rank contract as boxes1, applied to the second argument: compute_iou requires boxes2 to be rank 2 ((M, 4), unbatched) or rank 3 ((batch, M, 4), batched). A boxes2 of any other rank raises this ValueError with both ranks echoed in the message.","triggerScenarios":"Passing ground-truth boxes as a flat list of coordinates, a rank-4 array, or a nesting depth that does not match a batched boxes1.","commonSituations":"Comparing predictions (batched) against labels loaded from a JSON that are flat or singly nested; forgetting to expand dims on the reference boxes.","solutions":["Ensure boxes2 has shape (M, 4) or (batch, M, 4).","Add a leading batch axis with ops.expand_dims(boxes2, axis=0) when boxes1 is batched.","Validate len(ops.shape(boxes2)) in [2, 3] before calling in data-pipeline code."],"exampleFix":"# before\niou = compute_iou(pred_boxes, gt_flat_list)\n# after\niou = compute_iou(pred_boxes, ops.convert_to_tensor(gt_flat_list).reshape(-1, 4))","handlingStrategy":"validation","validationCode":"boxes2 = ops.convert_to_tensor(boxes2)\nassert len(ops.shape(boxes2)) in (2, 3), f\"boxes2 rank {len(ops.shape(boxes2))} not in (2,3)\"","typeGuard":"def boxes_have_valid_rank(t):\n    return len(ops.shape(ops.convert_to_tensor(t))) in (2, 3)\n","tryCatchPattern":null,"preventionTips":["Reshape loaded ground-truth to (-1, 4) at data-loading time, not at IoU time."],"tags":["keras","bounding-boxes","iou","rank-error"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}