keras-team/keras · error · ValueError
Invalid images rank: expected rank 3 (single image) or rank
Error message
Invalid images rank: expected rank 3 (single image) or rank 4 (batch of images). Received: input_shape={input_shape} What it means
RandomColorDegeneration.compute_output_shape requires the input shape to have rank 3 (single image) or 4 (batch). Called during model building, it rejects shapes of other lengths, e.g. rank 2 or rank 5 input specifications.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py:143
def transform_bounding_boxes(
self, bounding_boxes, transformation, training=True
):
return bounding_boxes
def get_config(self):
config = super().get_config()
config.update(
{
"factor": self.factor,
"value_range": self.value_range,
"seed": self.seed,
}
)
return config
def compute_output_shape(self, input_shape):
if len(input_shape) not in (3, 4):
raise ValueError(
"Invalid images rank: expected rank 3 (single image) "
"or rank 4 (batch of images). "
f"Received: input_shape={input_shape}"
)
channels_axis = -1 if self.data_format == "channels_last" else -3
channels = input_shape[channels_axis]
if channels is not None and channels != 3:
raise ValueError(
"Input images must have 3 channels, but received images with "
f"{channels} channels."
)
return input_shape
if RandomColorDegeneration.__doc__ is not None:
RandomColorDegeneration.__doc__ = RandomColorDegeneration.__doc__.replace(
"{{base_image_preprocessing_color_example}}",
base_image_preprocessing_color_example.replace(View on GitHub (pinned to 7a34a03db6)
Solutions
- Reshape inputs to (H, W, C) / (batch, H, W, C) before this layer
- For video, apply the layer per frame instead of a rank-5 tensor
- Check keras.Input(shape=...) has exactly 3 dims
Example fix
# before x = keras.Input(shape=(1, 224, 224, 3)) # rank 4 unbatched -> 5 with batch # after x = keras.Input(shape=(224, 224, 3))
Defensive patterns
Strategy: validation
Validate before calling
assert len(input_shape) in (3, 4), input_shape
Type guard
def is_img_shape3or4(s):
return len(s) in (3, 4) Prevention
- Check keras.Input(shape=...) has exactly 3 entries before stacking preprocessing layers
When it happens
Trigger: keras.Input(shape=(1, 224, 224, 3)) making a rank-5 batched shape passed through this layer; any input spec whose batched rank is not 3 or 4.
Common situations: Multi-frame/video inputs (T, H, W, C); extra leading axes from data loaders; constructing Input with unnecessary batch-and-time axes.
Related errors
- Expected the input image to be rank 3 or 4. Received inputs.
- Input images must have 3 channels, but received images with
- Found bounding_boxes['boxes'].shape={boxes_shape} and expect
- Found bounding_boxes['boxes'].shape={boxes_shape} and expect
- Expected `bounding_boxes['boxes']` to have rank 2 or 3, with
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/4bcb71a667390c05.
Report an issue: GitHub.