{"record":{"id":"7640a634070136d9","repo":"keras-team/keras","slug":"compute-iou-expects-boxes1-to-be-batched-or-to","errorCode":null,"errorMessage":"compute_iou() expects boxes1 to be batched, or to be unbatched. Received len(boxes1.shape)={boxes1_rank}, len(boxes2.shape)={boxes2_rank}. Expected either len(boxes1.shape)=2 AND or len(boxes1.shape)=3.","messagePattern":"compute_iou\\(\\) expects boxes1 to be batched, or to be unbatched\\. Received len\\(boxes1\\.shape\\)=(.+?), len\\(boxes2\\.shape\\)=(.+?)\\. Expected either len\\(boxes1\\.shape\\)=2 AND or len\\(boxes1\\.shape\\)=3\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/iou.py","lineNumber":105,"sourceCode":"        use_masking: whether masking will be applied. This will mask all\n            `boxes1` or `boxes2` that have values less than 0 in all its 4\n            dimensions. Default to `False`.\n        mask_val: int to mask those returned IOUs if the masking is True,\n            defaults to -1.\n        image_shape: `Tuple[int]`. The shape of the image (height, width, 3).\n            When using relative bounding box format for `box_format` the\n            `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.\"","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/iou.py#L87-L123","documentation":"compute_iou requires boxes1 to have rank 2 (unbatched, shape (num_boxes, 4)) or rank 3 (batched, shape (batch, num_boxes, 4)). If the first argument has any other rank - rank 1 (a flat vector of 4 numbers), rank 4, or a nested list whose shape ops cannot interpret as 2D/3D - this ValueError is raised before any IoU math.","triggerScenarios":"Passing a single box as [0,0,1,1] (rank 1) instead of [[0,0,1,1]]; passing one-hot-encoded boxes of rank 4; passing a nested list with inconsistent depths.","commonSituations":"Computing IoU for a single predicted box; feeding boxes straight from a model output with an extra leading dimension without squeezing.","solutions":["Reshape single boxes to (1, 4), e.g. ops.expand_dims(box, axis=0) or [box].","Squeeze spurious leading/trailing dims so boxes1 is rank 2 or 3 with last dim 4.","For batched inputs keep shape (batch, N, 4)."],"exampleFix":"# before\niou = compute_iou([0.0, 0.0, 1.0, 1.0], boxes)\n# after\niou = compute_iou([[0.0, 0.0, 1.0, 1.0]], boxes)","handlingStrategy":"validation","validationCode":"boxes1 = ops.convert_to_tensor(boxes1)\nassert len(ops.shape(boxes1)) in (2, 3), f\"boxes1 rank {len(ops.shape(boxes1))} 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":["Normalize single boxes to shape (1, 4) immediately after extraction from model output."],"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"}