keras-team/keras · error · ValueError

`input_shape` must be a non-nested tuple or list of rank-1 w

Error message

`input_shape` must be a non-nested tuple or list of rank-1 with size 3 (unbatched) or 4 (batched). 

What it means

CenterCrop.compute_output_shape only accepts a flat rank-1 shape of length 3 (H, W, C) or 4 (batch, H, W, C). Passing a nested structure (list of shapes, as when input is a dict/tuple of inputs) or a shape of length != 3/4 raises this ValueError.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/center_crop.py:251

                return inputs[
                    h_start : h_start + self.height,
                    w_start : w_start + self.width,
                    :,
                ]
        return image_utils.smart_resize(
            inputs,
            [self.height, self.width],
            interpolation=interpolation,
            data_format=self.data_format,
            backend_module=self.backend,
        )

    def compute_output_shape(self, input_shape):
        input_shape = list(input_shape)
        if isinstance(input_shape[0], (list, tuple)) or len(
            input_shape
        ) not in (3, 4):
            raise ValueError(
                "`input_shape` must be a non-nested tuple or list "
                "of rank-1 with size 3 (unbatched) or 4 (batched). "
            )
        if len(input_shape) == 4:
            if self.data_format == "channels_last":
                input_shape[1] = self.height
                input_shape[2] = self.width
            else:
                input_shape[2] = self.height
                input_shape[3] = self.width
        else:
            if self.data_format == "channels_last":
                input_shape[0] = self.height
                input_shape[1] = self.width
            else:
                input_shape[1] = self.height
                input_shape[2] = self.width
        return tuple(input_shape)

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass a single flat shape: layer.compute_output_shape((None, 224, 224, 3))
  2. If the layer receives nested inputs, extract the image shape: input_shape[0] or input_shape['images']
  3. Upgrade keras — newer versions handle nested input specs in compute_output_shape for preprocessing layers

Example fix

# before
out = layer.compute_output_shape([(None, 224, 224, 3)])
# after
out = layer.compute_output_shape((None, 224, 224, 3))
Defensive patterns

Strategy: validation

Validate before calling

def flat_img_shape(s):
    if isinstance(s[0], (list, tuple)):
        s = s[0]
    assert len(s) in (3, 4), f'bad image shape {s}'
    return tuple(s)

Type guard

def is_flat_shape3or4(s):
    return not isinstance(s[0], (list, tuple)) and len(s) in (3, 4)

Try / catch

try:
    out = layer.compute_output_shape(input_shape)
except ValueError:
    out = layer.compute_output_shape(input_shape[0])

Prevention

When it happens

Trigger: Calling layer.compute_output_shape([(None, 224, 224, 3)]) or compute_output_shape((None, 10, 224, 224, 3)); also building a model whose input spec is a nested structure routed to this layer.

Common situations: Multi-input models where Keras passes a list of shapes to each layer's compute_output_shape; manually probing output shapes with a wrapped shape; functional API with dict inputs.

Related errors


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