{"record":{"id":"acbfffdb92615260","repo":"keras-team/keras","slug":"first-dim-of-coordinates-must-be-the-same-as-the-acbfff","errorCode":null,"errorMessage":"First dim of `coordinates` must be the same as the rank of `inputs`. Received inputs with shape: {inputs.shape} and coordinate leading dim of {coordinates.shape[0]}","messagePattern":"First dim of `coordinates` must be the same as the rank of `inputs`\\. Received inputs with shape: (.+?) and coordinate leading dim of (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":1465,"sourceCode":"class MapCoordinates(Operation):\n    def __init__(self, order, fill_mode=\"constant\", fill_value=0, *, name=None):\n        super().__init__(name=name)\n        self.order = order\n        self.fill_mode = fill_mode\n        self.fill_value = fill_value\n\n    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.","sourceCodeStart":1447,"sourceCodeEnd":1483,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L1447-L1483","documentation":"keras.ops.image.map_coordinates samples inputs at arbitrary coordinates, where coordinates must be a tensor of shape (input_rank, ...) — one coordinate vector per input axis along the leading dim. The output-spec check raises when coordinates.shape[0] != len(inputs.shape), i.e. you supplied too few or too many coordinate components.","triggerScenarios":"map_coordinates(img, coords) where img has rank 3 (H,W,C) but coords.shape[0] is 2 (only y,x) or 4; passing per-pixel (row, col) pairs stacked along the last axis instead of the first.","commonSituations":"Coming from scipy.ndimage.map_coordinates (same leading-dim convention but users wrap coords wrongly); building coordinate grids with meshgrid and stacking on axis=-1; adding/removing a batch or channel axis after writing the coordinate code.","solutions":["Stack coordinate components on axis 0: coords = np.stack([ys, xs], axis=0) for a rank-2 image, plus one component per extra input axis","Ensure coords.shape[0] equals the full rank of inputs (include the channel axis if inputs is rank 3)","If you only care about spatial sampling, sample per channel or drop the channel axis from inputs"],"exampleFix":"# before\ncoords = np.stack([ys, xs], axis=-1)  # (N, 2)\nmap_coordinates(img, coords)          # img rank 3 -> error\n\n# after\ncoords = np.stack([ys_c, xs_c, cs], axis=0)  # (3, N) matching img rank 3\nmap_coordinates(img, coords)","handlingStrategy":"validation","validationCode":"assert coordinates.shape[0] == len(inputs.shape), 'coords leading dim must equal input rank'","typeGuard":"def coords_rank_ok(inputs, coordinates) -> bool:\n    return coordinates.shape[0] == len(inputs.shape)","tryCatchPattern":null,"preventionTips":["Build coords with np.stack([...], axis=0)","Re-check after adding/removing channel or batch axes"],"tags":["keras","image","map-coordinates","shape-mismatch","input-validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}