keras-team/keras · error · ValueError

RandomCrop requires the input to have a fully defined height

Error message

RandomCrop requires the input to have a fully defined height and width. Received: images.shape={input_shape}

What it means

RandomCrop.get_random_transformation needs concrete input height and width to compute crop offsets. If the height or width entry of the input shape is None (dynamic dimension), it cannot pick a random crop window and raises this ValueError.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/random_crop.py:97

        self.supports_jit = False
        self._convert_input_args = False
        self._allow_non_tensor_positional_args = True

    def get_random_transformation(self, data, training=True, seed=None):
        if seed is None:
            seed = self._get_seed_generator(self.backend._backend)

        if isinstance(data, dict):
            input_shape = self.backend.shape(data["images"])
        else:
            input_shape = self.backend.shape(data)

        input_height, input_width = (
            input_shape[self.height_axis],
            input_shape[self.width_axis],
        )
        if input_height is None or input_width is None:
            raise ValueError(
                "RandomCrop requires the input to have a fully defined "
                f"height and width. Received: images.shape={input_shape}"
            )

        if training and input_height > self.height and input_width > self.width:
            h_start = self.backend.cast(
                self.backend.random.uniform(
                    (),
                    0,
                    maxval=float(input_height - self.height + 1),
                    seed=seed,
                ),
                "int32",
            )
            w_start = self.backend.cast(
                self.backend.random.uniform(
                    (),
                    0,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Fix the spatial dims: keras.Input(shape=(256, 256, 3)) so height/width are static
  2. Resize images to a fixed size before RandomCrop (e.g. Resizing(256, 256) first)
  3. If variable size is required, set_shape on the tensor or run in eager mode where concrete shapes are available

Example fix

# before
inputs = keras.Input(shape=(None, None, 3))
x = RandomCrop(224, 224)(inputs)
# after
inputs = keras.Input(shape=(256, 256, 3))
x = RandomCrop(224, 224)(inputs)
Defensive patterns

Strategy: validation

Validate before calling

if images.shape[1] is None or images.shape[2] is None:
    images = tf.image.resize(images, (256, 256))  # pin spatial dims
assert images.shape[1] is not None and images.shape[2] is not None

Type guard

def has_static_spatial_dims(x):
    s = x.shape
    return len(s) >= 3 and s[-3] is not None and s[-2] is not None

Prevention

When it happens

Trigger: Calling RandomCrop(height, width) on a tensor whose spatial dims are undefined — e.g. within a tf.function graph with dynamic shape, or a Keras Input(shape=(None, None, 3)).

Common situations: Models built with variable image sizes; RaggedTensor/ragged batches; graph-mode execution where shape is only known at runtime; datasets with mixed resolutions.

Related errors


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