keras-team/keras · error · ValueError
Expected the input image to be rank 3 or 4. Received inputs.
Error message
Expected the input image to be rank 3 or 4. Received inputs.shape={images_shape} What it means
RandomColorDegeneration.get_random_transformation branches on input rank: 3 = single image, 4 = batch. Any other rank (2, 5, ...) raises this ValueError before computing the degeneration factor.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py:84
raise ValueError(
self._VALUE_RANGE_VALIDATION_ERROR
+ f"Received: value_range={value_range}"
)
self.value_range = sorted(value_range)
def get_random_transformation(self, data, training=True, seed=None):
if isinstance(data, dict):
images = data["images"]
else:
images = data
images_shape = self.backend.shape(images)
rank = len(images_shape)
if rank == 3:
batch_size = 1
elif rank == 4:
batch_size = images_shape[0]
else:
raise ValueError(
"Expected the input image to be rank 3 or 4. Received: "
f"inputs.shape={images_shape}"
)
if seed is None:
seed = self._get_seed_generator(self.backend._backend)
factor = self.backend.random.uniform(
(batch_size, 1, 1, 1),
minval=self.factor[0],
maxval=self.factor[1],
seed=seed,
)
factor = factor
return {"factor": factor}
def transform_images(self, images, transformation=None, training=True):
if training:View on GitHub (pinned to 7a34a03db6)
Solutions
- Ensure inputs are (H, W, 3) or (batch, H, W, 3)
- Add the channel axis: images[..., None]
- Drop extra dims with np.squeeze then verify len(shape)
Example fix
# before images = gray_array # (H, W) out = layer(images) # after images = gray_array[..., None] # (H, W, 1) out = layer(images)
Defensive patterns
Strategy: validation
Validate before calling
if len(images.shape) == 2:
images = images[..., None] Type guard
def is_rank3or4(x):
return len(getattr(x, 'shape', ())) in (3, 4) Prevention
- Normalize dataset output shape to (H, W, C) in the load function
When it happens
Trigger: Feeding a (H, W) grayscale array without a channel axis, or a rank-5 tensor with a duplicated batch dim.
Common situations: Grayscale pipelines after convert('L'); over-batched tensors from previous dataset .batch() plus manual expand_dims.
Related errors
- Expected the input image to be rank 3 or 4. Received inputs.
- Invalid images rank: expected rank 3 (single image) or rank
- Expected the input image to be rank 3 or 4. Received inputs.
- {self._VALUE_RANGE_VALIDATION_ERROR}Received: value_range={v
- The `value_range` argument should be a list of two numbers.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/7c5b0071acaecadb.
Report an issue: GitHub.