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: inputs.shape={inputs.shape}, cropping={self.cropping}

What it means

Same constraint as compute_output_shape, but enforced at call time on the real tensor: Cropping3D.call subtracts each cropping pair from the actual spatial dimensions of the input and raises when a resulting dimension would be <= 0. This catches cases static shape inference could not (unknown dims resolved only at runtime).

Source

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

                )

        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:
                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"inputs.shape={inputs.shape}, cropping={self.cropping}"
                )

        if self.data_format == "channels_first":
            if (
                self.cropping[0][1]
                == self.cropping[1][1]
                == self.cropping[2][1]
                == 0
            ):
                return inputs[
                    :,
                    :,
                    self.cropping[0][0] :,
                    self.cropping[1][0] :,
                    self.cropping[2][0] :,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Print/log inputs.shape in a trace or catch the error to inspect the actual runtime spatial dims vs self.cropping
  2. Lower cropping values per axis so sum(pair) < runtime dim, or pad inputs first (e.g. keras.layers.ZeroPadding3D) when large crops are required
  3. If input shapes vary, enforce a minimum spatial size upstream (crop/resize/pad pipeline) before Cropping3D

Example fix

# before
x = tf.random.uniform((2, 4, 8, 8, 3))
y = Cropping3D(cropping=((3,3),(4,4),(4,4)))(x)  # depth 4 - 6 <= 0

# after
x = ZeroPadding3D(padding=(2,2,2,2,2))(x)
y = Cropping3D(cropping=((3,3),(4,4),(4,4)))(x)
Defensive patterns

Strategy: validation

Validate before calling

def safe_crop3d(shape, layer):
    axes = (2, 3, 4) if layer.data_format == 'channels_first' else (1, 2, 3)
    for ax, pair in zip(axes, layer.cropping):
        d = shape[ax]
        if d is not None and d - sum(pair) <= 0:
            return False
    return True

assert safe_crop3d(tuple(x.shape), layer), 'batch too small for Cropping3D config'

Try / catch

try:
    y = layer(x)
except ValueError as e:
    if 'cropping' in str(e):
        x = ops.pad(x, [[0,0],[1,1],[1,1],[1,1],[0,0]])  # minimal pad fallback
        y = layer(x)
    else:
        raise

Prevention

When it happens

Trigger: Passing a concrete tensor to a Cropping3D layer (or calling the model) where any spatial axis is smaller than or equal to the sum of its crop pair, e.g. axis size 5 with cropping (3,3) on that axis. Common when the static input shape contains None so compute_output_shape skipped the check.

Common situations: Dynamic input shapes (None dims) that only fail at runtime with real data; datasets where later batches have smaller spatial extents than the first; cropping configs tuned on one dataset reused on another with smaller volumes.

Related errors


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