{"record":{"id":"f5bbabbdbed5da34","repo":"keras-team/keras","slug":"invalid-axis-indices-actual-indices-valid-ind","errorCode":null,"errorMessage":"Invalid axis' indices: {actual_indices - valid_indices}","messagePattern":"Invalid axis' indices: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/image.py","lineNumber":1803,"sourceCode":"    order=1,\n):\n    \"\"\"Applies an affine transformation specified by the parameters given.\n\n    DEPRECATED.\n    \"\"\"\n    # Input sanity checks:\n    # 1. x must 2D image with one or more channels (i.e., a 3D tensor)\n    # 2. channels must be either first or last dimension\n    if np.unique([row_axis, col_axis, channel_axis]).size != 3:\n        raise ValueError(\n            \"'row_axis', 'col_axis', and 'channel_axis' must be distinct\"\n        )\n\n    # shall we support negative indices?\n    valid_indices = set([0, 1, 2])\n    actual_indices = set([row_axis, col_axis, channel_axis])\n    if actual_indices != valid_indices:\n        raise ValueError(\n            f\"Invalid axis' indices: {actual_indices - valid_indices}\"\n        )\n\n    if x.ndim != 3:\n        raise ValueError(\"Input arrays must be multi-channel 2D images.\")\n    if channel_axis not in [0, 2]:\n        raise ValueError(\n            \"Channels are allowed and the first and last dimensions.\"\n        )\n\n    transform_matrix = None\n    if theta != 0:\n        theta = np.deg2rad(theta)\n        rotation_matrix = np.array(\n            [\n                [np.cos(theta), -np.sin(theta), 0],\n                [np.sin(theta), np.cos(theta), 0],\n                [0, 0, 1],","sourceCodeStart":1785,"sourceCodeEnd":1821,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/image.py#L1785-L1821","documentation":"apply_affine_transform only accepts axis indices in {0,1,2} because it operates on 3D (multi-channel 2D) arrays. After checking the axes are distinct it verifies the index set equals {0,1,2} and raises this error listing the offending indices. Negative indices such as -1 are explicitly unsupported.","triggerScenarios":"Passing row_axis=-1 or channel_axis=3 to apply_affine_transform or the random_* wrappers; passing axes meant for a 4D batch tensor (axis=3) while feeding a single image.","commonSituations":"Porting numpy/scipy-style code that uses negative axis indices; feeding a batched NHWC tensor and giving axis=3 for channels.","solutions":["Replace negative indices: use 2 instead of -1 for channels-last","Ensure input x is a single 3D image (H, W, C); loop over the batch dimension yourself","Confirm all three axes form a permutation of 0, 1, 2"],"exampleFix":"# before\napply_affine_transform(img, tx=2, row_axis=0, col_axis=1, channel_axis=-1)\n# after\napply_affine_transform(img, tx=2, row_axis=0, col_axis=1, channel_axis=2)","handlingStrategy":"validation","validationCode":"if any(a not in (0, 1, 2) for a in (row_axis, col_axis, channel_axis)):\n    raise ValueError('axes must be in 0..2; negative indices unsupported')","typeGuard":"def valid_axes(r, c, ch):\n    return all(isinstance(a, int) and 0 <= a <= 2 for a in (r, c, ch))","tryCatchPattern":null,"preventionTips":["Never pass negative axis indices to legacy image helpers","Assert x.ndim == 3 before calling"],"tags":["keras","preprocessing","image","validation","negative-index"],"backgroundTag":"invalid-argument-value","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}