{"record":{"id":"7453005bfb88f04d","repo":"keras-team/keras","slug":"variance-must-be-length-4-got-variance","errorCode":null,"errorMessage":"`variance` must be length 4, got {variance}","messagePattern":"`variance` must be length 4, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/converters.py","lineNumber":305,"sourceCode":"            are divided by the variance. Defaults to None.\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    Returns:\n        Encoded box deltas. The return type matches the `encode_format`.\n\n    Raises:\n        ValueError: If `variance` is not None and its length is not 4.\n        ValueError: If `encoding_format` is not `\"center_xywh\"` or\n            `\"center_yxhw\"`.\n\n    \"\"\"\n    if variance is not None:\n        variance = ops.convert_to_tensor(variance, \"float32\")\n        var_len = variance.shape[-1]\n\n        if var_len != 4:\n            raise ValueError(f\"`variance` must be length 4, got {variance}\")\n\n    if encoding_format not in [\"center_xywh\", \"center_yxhw\"]:\n        raise ValueError(\n            \"`encoding_format` should be one of 'center_xywh' or \"\n            f\"'center_yxhw', got {encoding_format}\"\n        )\n\n    if image_shape is None:\n        height, width = None, None\n    else:\n        height, width, _ = image_shape\n\n    encoded_anchors = convert_format(\n        anchors,\n        source=anchor_format,\n        target=encoding_format,\n        height=height,\n        width=width,","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/bounding_boxes/converters.py#L287-L323","documentation":"encode_box_to_deltas converts anchor boxes plus offsets into encoded deltas, optionally normalizing by a per-coordinate variance. The variance vector must have exactly 4 elements (x, y, w, h components); after conversion to a float32 tensor, a last-dimension size other than 4 raises this ValueError.","triggerScenarios":"Passing variance=[0.1, 0.1, 0.2] (3 elements), a scalar variance, or an (N, 5) variance array to encode_box_to_deltas.","commonSituations":"Copying variance values from an SSD/RetinaNet config that lists variances for a different box parameterization; building variance from a loop with an off-by-one length.","solutions":["Supply exactly four variance values, e.g. variance=[0.1, 0.1, 0.2, 0.2].","Or omit variance (pass None) if you do not need variance normalization."],"exampleFix":"# before\ndeltas = encode_box_to_deltas(boxes, anchors, variance=[0.1, 0.1, 0.2])\n# after\ndeltas = encode_box_to_deltas(boxes, anchors, variance=[0.1, 0.1, 0.2, 0.2])","handlingStrategy":"validation","validationCode":"if variance is not None:\n    assert len(variance) == 4, f\"variance must have 4 elements, got {len(variance)}\"","typeGuard":"def is_valid_variance(v):\n    return v is None or (hasattr(v, \"__len__\") and len(v) == 4)\n","tryCatchPattern":null,"preventionTips":["Define VARIANCE = [0.1, 0.1, 0.2, 0.2] once and import it on both encode and decode sides."],"tags":["keras","bounding-boxes","encoding","variance"],"backgroundTag":"invalid-argument-shape","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}