{"record":{"id":"bdcd9efb89e788a3","repo":"keras-team/keras","slug":"values-in-cropping-argument-should-be-smaller-th-bdcd9e","errorCode":null,"errorMessage":"Values in `cropping` argument should be smaller than the corresponding spatial dimension of the input. Received: inputs.shape={inputs.shape}, cropping={self.cropping}","messagePattern":"Values in `cropping` argument should be smaller than the corresponding spatial dimension of the input\\. Received: inputs\\.shape=(.+?), cropping=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/reshaping/cropping3d.py","lineNumber":136,"sourceCode":"                )\n\n        if self.data_format == \"channels_first\":\n            return (input_shape[0], input_shape[1], *spatial_dims)\n        else:\n            return (input_shape[0], *spatial_dims, input_shape[4])\n\n    def call(self, inputs):\n        if self.data_format == \"channels_first\":\n            spatial_dims = list(inputs.shape[2:5])\n        else:\n            spatial_dims = list(inputs.shape[1:4])\n\n        for index in range(0, 3):\n            if spatial_dims[index] is None:\n                continue\n            spatial_dims[index] -= sum(self.cropping[index])\n            if spatial_dims[index] <= 0:\n                raise ValueError(\n                    \"Values in `cropping` argument should be smaller than the \"\n                    \"corresponding spatial dimension of the input. Received: \"\n                    f\"inputs.shape={inputs.shape}, cropping={self.cropping}\"\n                )\n\n        if self.data_format == \"channels_first\":\n            if (\n                self.cropping[0][1]\n                == self.cropping[1][1]\n                == self.cropping[2][1]\n                == 0\n            ):\n                return inputs[\n                    :,\n                    :,\n                    self.cropping[0][0] :,\n                    self.cropping[1][0] :,\n                    self.cropping[2][0] :,","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/reshaping/cropping3d.py#L118-L154","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Print/log inputs.shape in a trace or catch the error to inspect the actual runtime spatial dims vs self.cropping","Lower cropping values per axis so sum(pair) < runtime dim, or pad inputs first (e.g. keras.layers.ZeroPadding3D) when large crops are required","If input shapes vary, enforce a minimum spatial size upstream (crop/resize/pad pipeline) before Cropping3D"],"exampleFix":"# before\nx = tf.random.uniform((2, 4, 8, 8, 3))\ny = Cropping3D(cropping=((3,3),(4,4),(4,4)))(x)  # depth 4 - 6 <= 0\n\n# after\nx = ZeroPadding3D(padding=(2,2,2,2,2))(x)\ny = Cropping3D(cropping=((3,3),(4,4),(4,4)))(x)","handlingStrategy":"validation","validationCode":"def safe_crop3d(shape, layer):\n    axes = (2, 3, 4) if layer.data_format == 'channels_first' else (1, 2, 3)\n    for ax, pair in zip(axes, layer.cropping):\n        d = shape[ax]\n        if d is not None and d - sum(pair) <= 0:\n            return False\n    return True\n\nassert safe_crop3d(tuple(x.shape), layer), 'batch too small for Cropping3D config'","typeGuard":null,"tryCatchPattern":"try:\n    y = layer(x)\nexcept ValueError as e:\n    if 'cropping' in str(e):\n        x = ops.pad(x, [[0,0],[1,1],[1,1],[1,1],[0,0]])  # minimal pad fallback\n        y = layer(x)\n    else:\n        raise","preventionTips":["Enforce minimum spatial size in the input pipeline (tf.data map asserting dims)","Log input shapes on the first batches of a new dataset before attaching crop layers","Prefer fixed-size inputs when using aggressive cropping"],"tags":["keras","cropping3d","runtime-shape-check","reshaping-layer"],"backgroundTag":"invalid-shape-argument","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}