keras-team/keras · error · ValueError

Invalid coordinates rank: expected at least rank 2. Received

Error message

Invalid coordinates rank: expected at least rank 2. Received input with shape: {coordinates.shape}

What it means

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.

Source

Thrown at keras/src/ops/image.py:1472

    def call(self, inputs, coordinates):
        return backend.image.map_coordinates(
            inputs,
            coordinates,
            order=self.order,
            fill_mode=self.fill_mode,
            fill_value=self.fill_value,
        )

    def compute_output_spec(self, inputs, coordinates):
        if coordinates.shape[0] != len(inputs.shape):
            raise ValueError(
                "First dim of `coordinates` must be the same as the rank of "
                "`inputs`. "
                f"Received inputs with shape: {inputs.shape} and coordinate "
                f"leading dim of {coordinates.shape[0]}"
            )
        if len(coordinates.shape) < 2:
            raise ValueError(
                "Invalid coordinates rank: expected at least rank 2."
                f" Received input with shape: {coordinates.shape}"
            )
        return KerasTensor(coordinates.shape[1:], dtype=inputs.dtype)


@keras_export("keras.ops.image.map_coordinates")
def map_coordinates(
    inputs, coordinates, order, fill_mode="constant", fill_value=0
):
    """Map the input array to new coordinates by interpolation.

    Note that interpolation near boundaries differs from the scipy function,
    because we fixed an outstanding bug
    [scipy/issues/2640](https://github.com/scipy/scipy/issues/2640).

    Args:
        inputs: The input array.

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Give coordinates an output-shape axis: coords[..., None] or np.stack([ys, xs], axis=0) so rank is 2+
  2. For a single point use coords of shape (rank, 1)
  3. Check len(coords.shape) >= 2 before calling

Example fix

# before
out = map_coordinates(img, np.array([3.0, 5.0]))

# after
out = map_coordinates(img, np.array([[3.0], [5.0]]))  # shape (2, 1)
Defensive patterns

Strategy: validation

Validate before calling

assert len(coordinates.shape) >= 2, 'coordinates need rank >= 2'

Type guard

def coords_rank2(coords) -> bool:
    return len(coords.shape) >= 2

Prevention

When it happens

Trigger: 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.

Common situations: Sampling one point and forgetting to add a trailing axis; flattening coordinates too aggressively in preprocessing; coordinate tensors produced as 1-D lists.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/dd60fc58c9d5bda8. Report an issue: GitHub.