keras-team/keras · error · ValueError

Values in `cropping` argument should be smaller than the cor

Error message

Values in `cropping` argument should be smaller than the corresponding spatial dimension of the input. Received: input_shape={input_shape}, cropping={self.cropping}

What it means

Raised by Cropping3D.compute_output_shape when the sum of the crop amounts for any spatial axis is greater than or equal to that axis's size in the input shape, which would leave a non-positive output dimension. Keras validates this eagerly so an invalid crop fails at shape-inference time instead of producing a corrupt tensor. The check is skipped for unknown (None) dimensions.

Source

Thrown at keras/src/layers/reshaping/cropping3d.py:114

                "((left_dim1_crop, right_dim1_crop),"
                " (left_dim2_crop, right_dim2_crop),"
                " (left_dim3_crop, right_dim2_crop)). "
                f"Received: {cropping}."
            )
        self.input_spec = InputSpec(ndim=5)

    def compute_output_shape(self, input_shape):
        if self.data_format == "channels_first":
            spatial_dims = list(input_shape[2:5])
        else:
            spatial_dims = list(input_shape[1:4])

        for index in range(0, 3):
            if spatial_dims[index] is None:
                continue
            spatial_dims[index] -= sum(self.cropping[index])
            if spatial_dims[index] <= 0:
                raise ValueError(
                    "Values in `cropping` argument should be smaller than the "
                    "corresponding spatial dimension of the input. Received: "
                    f"input_shape={input_shape}, cropping={self.cropping}"
                )

        if self.data_format == "channels_first":
            return (input_shape[0], input_shape[1], *spatial_dims)
        else:
            return (input_shape[0], *spatial_dims, input_shape[4])

    def call(self, inputs):
        if self.data_format == "channels_first":
            spatial_dims = list(inputs.shape[2:5])
        else:
            spatial_dims = list(inputs.shape[1:4])

        for index in range(0, 3):
            if spatial_dims[index] is None:

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Reduce the cropping values so that each spatial dim minus the sum of its crop pair stays >= 1: verify input_shape and self.cropping per axis
  2. Check data_format: for channels_first the channel axis is dim 1 and cropping applies to axes 2-4; a wrong data_format makes cropping hit the wrong dimension
  3. Remember cropping is ((d1_crop),(d2_crop),(d3_crop)) with each entry a (head, tail) pair whose sum must be < the corresponding spatial dim
  4. If you need aggressive shrinking, use a pooling/striding layer instead of large crops

Example fix

# before
layer = keras.layers.Cropping3D(cropping=((2,2),(2,2),(2,2)))
out = layer(tf.random.normal((1, 3, 3, 3, 4)))  # ValueError

# after
layer = keras.layers.Cropping3D(cropping=((1,1),(1,1),(1,1)))
out = layer(tf.random.normal((1, 3, 3, 3, 4)))  # ok: dims become (1,1,1)
Defensive patterns

Strategy: validation

Validate before calling

def check_cropping3d(input_shape, cropping, data_format='channels_last'):
    # spatial axes: channels_last -> (1,2,3); channels_first -> (2,3,4)
    axes = (2, 3, 4) if data_format == 'channels_first' else (1, 2, 3)
    for ax, pair in zip(axes, cropping):
        d = input_shape[ax]
        if d is not None and d - sum(pair) <= 0:
            raise ValueError(f'crop {pair} too large for axis {ax} (dim {d})')
    return True

Type guard

def is_valid_cropping3d(input_shape, cropping, data_format='channels_last'):
    axes = (2, 3, 4) if data_format == 'channels_first' else (1, 2, 3)
    return all(
        input_shape[ax] is None or input_shape[ax] - sum(pair) > 0
        for ax, pair in zip(axes, cropping)
    )

Prevention

When it happens

Trigger: Calling Cropping3D(cropping=((a,b),(c,d),(e,f))) on an input whose spatial dims (depth, height, width for channels_last) satisfy dim - sum(crop_pair) <= 0 for any axis, e.g. Cropping3D(cropping=((2,2),(2,2),(2,2))) on a (1, 3, 3, 3, 4) tensor. Triggered when building the model or during compute_output_shape, even before real data flows.

Common situations: Copy-pasting a Cropping2D/Cropping3D config from a model built for larger inputs (e.g. 224x224 images) onto small inputs; forgetting that cropping pairs are (before, after) per axis, not per-axis totals; applying a cropping layer intended for channels_last tensors to channels_first data so the wrong axis is cropped.

Related errors


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