keras-team/keras · error · ValueError

Invalid transform rank: expected rank 1 (single transform)…

Error message

Invalid transform rank: expected rank 1 (single transform) or rank 2 (batch of transforms). Received input with shape: transform.shape={transform.shape}

What it means

Rank guard in `affine_transform` (NumPy backend): the `transform` argument must be a single transformation matrix (rank 1, the flattened 3x3-in-8-params form) or a batch of them (rank 2). After converting `transform` to a tensor, the length check on its shape rejects other ranks (a scalar, an unflattened 2D matrix passed as rank-0-style input, or rank >= 3), reporting the actual shape so the malformed argument is easy to spot.

Solutions

  1. Flatten per-image 3x3 matrices to 1D parameter vectors and stack them into a (N, 8/9) rank-2 batch
  2. Pass a single 1D transform vector for one image
  3. Reshape unexpected-rank transforms with keras.ops.reshape to rank 1 or 2 before calling affine_transform
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/numpy/ops/image.py:581 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at keras/src/backend/numpy/ops/image.py:581

            f"interpolation={interpolation}"
        )
    if fill_mode not in AFFINE_TRANSFORM_FILL_MODES:
        raise ValueError(
            "Invalid value for argument `fill_mode`. Expected of one "
            f"{AFFINE_TRANSFORM_FILL_MODES}. Received: fill_mode={fill_mode}"
        )

    images = convert_to_tensor(images)
    transform = convert_to_tensor(transform)

    if len(images.shape) not in (3, 4):
        raise ValueError(
            "Invalid images rank: expected rank 3 (single image) "
            "or rank 4 (batch of images). Received input with shape: "
            f"images.shape={images.shape}"
        )
    if len(transform.shape) not in (1, 2):
        raise ValueError(
            "Invalid transform rank: expected rank 1 (single transform) "
            "or rank 2 (batch of transforms). Received input with shape: "
            f"transform.shape={transform.shape}"
        )

    # `scipy.ndimage.map_coordinates` lacks support for float16 and bfloat16.
    input_dtype = backend.standardize_dtype(images.dtype)
    compute_dtype = backend.result_type(input_dtype, "float32")
    images = images.astype(compute_dtype)
    transform = transform.astype(compute_dtype)

    # unbatched case
    need_squeeze = False
    if len(images.shape) == 3:
        images = np.expand_dims(images, axis=0)
        need_squeeze = True
    if len(transform.shape) == 1:
        transform = np.expand_dims(transform, axis=0)

View on GitHub (pinned to 7a34a03db6)