{"record":{"id":"82dc21c32e0bc781","repo":"keras-team/keras","slug":"randomcrop-requires-the-input-to-have-a-fully-defi","errorCode":null,"errorMessage":"RandomCrop requires the input to have a fully defined height and width. Received: images.shape={input_shape}","messagePattern":"RandomCrop requires the input to have a fully defined height and width\\. Received: images\\.shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/random_crop.py","lineNumber":97,"sourceCode":"        self.supports_jit = False\n        self._convert_input_args = False\n        self._allow_non_tensor_positional_args = True\n\n    def get_random_transformation(self, data, training=True, seed=None):\n        if seed is None:\n            seed = self._get_seed_generator(self.backend._backend)\n\n        if isinstance(data, dict):\n            input_shape = self.backend.shape(data[\"images\"])\n        else:\n            input_shape = self.backend.shape(data)\n\n        input_height, input_width = (\n            input_shape[self.height_axis],\n            input_shape[self.width_axis],\n        )\n        if input_height is None or input_width is None:\n            raise ValueError(\n                \"RandomCrop requires the input to have a fully defined \"\n                f\"height and width. Received: images.shape={input_shape}\"\n            )\n\n        if training and input_height > self.height and input_width > self.width:\n            h_start = self.backend.cast(\n                self.backend.random.uniform(\n                    (),\n                    0,\n                    maxval=float(input_height - self.height + 1),\n                    seed=seed,\n                ),\n                \"int32\",\n            )\n            w_start = self.backend.cast(\n                self.backend.random.uniform(\n                    (),\n                    0,","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/random_crop.py#L79-L115","documentation":"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.","triggerScenarios":"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)).","commonSituations":"Models built with variable image sizes; RaggedTensor/ragged batches; graph-mode execution where shape is only known at runtime; datasets with mixed resolutions.","solutions":["Fix the spatial dims: keras.Input(shape=(256, 256, 3)) so height/width are static","Resize images to a fixed size before RandomCrop (e.g. Resizing(256, 256) first)","If variable size is required, set_shape on the tensor or run in eager mode where concrete shapes are available"],"exampleFix":"# before\ninputs = keras.Input(shape=(None, None, 3))\nx = RandomCrop(224, 224)(inputs)\n# after\ninputs = keras.Input(shape=(256, 256, 3))\nx = RandomCrop(224, 224)(inputs)","handlingStrategy":"validation","validationCode":"if images.shape[1] is None or images.shape[2] is None:\n    images = tf.image.resize(images, (256, 256))  # pin spatial dims\nassert images.shape[1] is not None and images.shape[2] is not None","typeGuard":"def has_static_spatial_dims(x):\n    s = x.shape\n    return len(s) >= 3 and s[-3] is not None and s[-2] is not None","tryCatchPattern":null,"preventionTips":["Fix input spatial dims in keras.Input or add a Resizing layer before RandomCrop","Avoid RandomCrop on ragged or variable-size batches"],"tags":["keras","random-crop","dynamic-shape","image-preprocessing"],"backgroundTag":"dynamic-shape-not-allowed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}