keras-team/keras · error · ValueError
Invalid axis' indices: {actual_indices - valid_indices}
Error message
Invalid axis' indices: {actual_indices - valid_indices} What it means
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.
Source
Thrown at keras/src/legacy/preprocessing/image.py:1803
order=1,
):
"""Applies an affine transformation specified by the parameters given.
DEPRECATED.
"""
# Input sanity checks:
# 1. x must 2D image with one or more channels (i.e., a 3D tensor)
# 2. channels must be either first or last dimension
if np.unique([row_axis, col_axis, channel_axis]).size != 3:
raise ValueError(
"'row_axis', 'col_axis', and 'channel_axis' must be distinct"
)
# shall we support negative indices?
valid_indices = set([0, 1, 2])
actual_indices = set([row_axis, col_axis, channel_axis])
if actual_indices != valid_indices:
raise ValueError(
f"Invalid axis' indices: {actual_indices - valid_indices}"
)
if x.ndim != 3:
raise ValueError("Input arrays must be multi-channel 2D images.")
if channel_axis not in [0, 2]:
raise ValueError(
"Channels are allowed and the first and last dimensions."
)
transform_matrix = None
if theta != 0:
theta = np.deg2rad(theta)
rotation_matrix = np.array(
[
[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1],View on GitHub (pinned to 7a34a03db6)
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
Example fix
# before apply_affine_transform(img, tx=2, row_axis=0, col_axis=1, channel_axis=-1) # after apply_affine_transform(img, tx=2, row_axis=0, col_axis=1, channel_axis=2)
Defensive patterns
Strategy: validation
Validate before calling
if any(a not in (0, 1, 2) for a in (row_axis, col_axis, channel_axis)):
raise ValueError('axes must be in 0..2; negative indices unsupported') Type guard
def valid_axes(r, c, ch):
return all(isinstance(a, int) and 0 <= a <= 2 for a in (r, c, ch)) Prevention
- Never pass negative axis indices to legacy image helpers
- Assert x.ndim == 3 before calling
When it happens
Trigger: 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.
Common situations: Porting numpy/scipy-style code that uses negative axis indices; feeding a batched NHWC tensor and giving axis=3 for channels.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- 'row_axis', 'col_axis', and 'channel_axis' must be distinct
- The `factor` argument should be a number (or a list of two n
- Received: {factor_name}={factor}
- Received: {factor_name}={factor}
- Received: value_range={value_range}
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/f5bbabbdbed5da34.
Report an issue: GitHub.