{"record":{"id":"7c5b0071acaecadb","repo":"keras-team/keras","slug":"expected-the-input-image-to-be-rank-3-or-4-receiv-7c5b00","errorCode":null,"errorMessage":"Expected the input image to be rank 3 or 4. Received inputs.shape={images_shape}","messagePattern":"Expected the input image to be rank 3 or 4\\. Received inputs\\.shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py","lineNumber":84,"sourceCode":"            raise ValueError(\n                self._VALUE_RANGE_VALIDATION_ERROR\n                + f\"Received: value_range={value_range}\"\n            )\n        self.value_range = sorted(value_range)\n\n    def get_random_transformation(self, data, training=True, seed=None):\n        if isinstance(data, dict):\n            images = data[\"images\"]\n        else:\n            images = data\n        images_shape = self.backend.shape(images)\n        rank = len(images_shape)\n        if rank == 3:\n            batch_size = 1\n        elif rank == 4:\n            batch_size = images_shape[0]\n        else:\n            raise ValueError(\n                \"Expected the input image to be rank 3 or 4. Received: \"\n                f\"inputs.shape={images_shape}\"\n            )\n\n        if seed is None:\n            seed = self._get_seed_generator(self.backend._backend)\n\n        factor = self.backend.random.uniform(\n            (batch_size, 1, 1, 1),\n            minval=self.factor[0],\n            maxval=self.factor[1],\n            seed=seed,\n        )\n        factor = factor\n        return {\"factor\": factor}\n\n    def transform_images(self, images, transformation=None, training=True):\n        if training:","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py#L66-L102","documentation":"RandomColorDegeneration.get_random_transformation branches on input rank: 3 = single image, 4 = batch. Any other rank (2, 5, ...) raises this ValueError before computing the degeneration factor.","triggerScenarios":"Feeding a (H, W) grayscale array without a channel axis, or a rank-5 tensor with a duplicated batch dim.","commonSituations":"Grayscale pipelines after convert('L'); over-batched tensors from previous dataset .batch() plus manual expand_dims.","solutions":["Ensure inputs are (H, W, 3) or (batch, H, W, 3)","Add the channel axis: images[..., None]","Drop extra dims with np.squeeze then verify len(shape)"],"exampleFix":"# before\nimages = gray_array  # (H, W)\nout = layer(images)\n# after\nimages = gray_array[..., None]  # (H, W, 1)\nout = layer(images)","handlingStrategy":"validation","validationCode":"if len(images.shape) == 2:\n    images = images[..., None]","typeGuard":"def is_rank3or4(x):\n    return len(getattr(x, 'shape', ())) in (3, 4)","tryCatchPattern":null,"preventionTips":["Normalize dataset output shape to (H, W, C) in the load function"],"tags":["keras","color-degeneration","tensor-rank","image-preprocessing"],"backgroundTag":"input-shape-validation-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}