{"record":{"id":"dd60fc58c9d5bda8","repo":"keras-team/keras","slug":"invalid-coordinates-rank-expected-at-least-rank-2-dd60fc","errorCode":null,"errorMessage":"Invalid coordinates rank: expected at least rank 2. Received input with shape: {coordinates.shape}","messagePattern":"Invalid coordinates rank: expected at least rank 2\\. Received input with shape: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":1472,"sourceCode":"    def call(self, inputs, coordinates):\n        return backend.image.map_coordinates(\n            inputs,\n            coordinates,\n            order=self.order,\n            fill_mode=self.fill_mode,\n            fill_value=self.fill_value,\n        )\n\n    def compute_output_spec(self, inputs, coordinates):\n        if coordinates.shape[0] != len(inputs.shape):\n            raise ValueError(\n                \"First dim of `coordinates` must be the same as the rank of \"\n                \"`inputs`. \"\n                f\"Received inputs with shape: {inputs.shape} and coordinate \"\n                f\"leading dim of {coordinates.shape[0]}\"\n            )\n        if len(coordinates.shape) < 2:\n            raise ValueError(\n                \"Invalid coordinates rank: expected at least rank 2.\"\n                f\" Received input with shape: {coordinates.shape}\"\n            )\n        return KerasTensor(coordinates.shape[1:], dtype=inputs.dtype)\n\n\n@keras_export(\"keras.ops.image.map_coordinates\")\ndef map_coordinates(\n    inputs, coordinates, order, fill_mode=\"constant\", fill_value=0\n):\n    \"\"\"Map the input array to new coordinates by interpolation.\n\n    Note that interpolation near boundaries differs from the scipy function,\n    because we fixed an outstanding bug\n    [scipy/issues/2640](https://github.com/scipy/scipy/issues/2640).\n\n    Args:\n        inputs: The input array.","sourceCodeStart":1454,"sourceCodeEnd":1490,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L1454-L1490","documentation":"map_coordinates requires coordinates of rank >= 2: leading dim indexes input axes, remaining dims are the output batch shape. A rank-1 coordinates tensor (a bare coordinate vector) has no output shape, so compute_output_spec raises.","triggerScenarios":"map_coordinates(img, coords) with coords of shape (2,) or (k,), e.g. passing a single (y, x) point instead of a (2, N) stack of points.","commonSituations":"Sampling one point and forgetting to add a trailing axis; flattening coordinates too aggressively in preprocessing; coordinate tensors produced as 1-D lists.","solutions":["Give coordinates an output-shape axis: coords[..., None] or np.stack([ys, xs], axis=0) so rank is 2+","For a single point use coords of shape (rank, 1)","Check len(coords.shape) >= 2 before calling"],"exampleFix":"# before\nout = map_coordinates(img, np.array([3.0, 5.0]))\n\n# after\nout = map_coordinates(img, np.array([[3.0], [5.0]]))  # shape (2, 1)","handlingStrategy":"validation","validationCode":"assert len(coordinates.shape) >= 2, 'coordinates need rank >= 2'","typeGuard":"def coords_rank2(coords) -> bool:\n    return len(coords.shape) >= 2","tryCatchPattern":null,"preventionTips":["Single point -> shape (rank, 1)","Avoid flattening coordinate arrays"],"tags":["keras","image","map-coordinates","rank-error","input-validation"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}