{"record":{"id":"700c0f224ac34c50","repo":"keras-team/keras","slug":"expected-the-input-image-to-be-rank-3-or-4-receiv-700c0f","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_contrast.py","lineNumber":80,"sourceCode":"        self.value_range = value_range\n        self.seed = seed\n        self.generator = SeedGenerator(seed)\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            factor_shape = (1, 1, 1)\n        elif rank == 4:\n            # Keep only the batch dim. This will ensure to have same adjustment\n            # with in one image, but different across the images.\n            factor_shape = [images_shape[0], 1, 1, 1]\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 not training:\n            return {\"contrast_factor\": self.backend.numpy.zeros(factor_shape)}\n\n        if seed is None:\n            seed = self._get_seed_generator(self.backend._backend)\n\n        factor = self.backend.random.uniform(\n            shape=factor_shape,\n            minval=1.0 - self.factor[0],\n            maxval=1.0 + self.factor[1],\n            seed=seed,\n            dtype=self.compute_dtype,\n        )\n        return {\"contrast_factor\": factor}","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/random_contrast.py#L62-L98","documentation":"RandomContrast.get_random_transformation builds a contrast factor shaped for rank-3 (single image) or rank-4 (batched) inputs. Any other rank raises this ValueError before the factor is computed.","triggerScenarios":"Feeding a rank-2 grayscale (H, W) array, or a rank-5 tensor with an extra axis, to RandomContrast.","commonSituations":"Grayscale images loaded without a channel dim; accidentally double-batched tensors; datasets yielding (H, W) for 'L'-mode images.","solutions":["Ensure shape (H, W, C) or (batch, H, W, C)","Add channel axis: images[..., None]","Squeeze extra batch dims: np.squeeze(images, axis=0)"],"exampleFix":"# before\nimages = gray  # (H, W)\nout = layer(images)\n# after\nimages = gray[..., None]  # (H, W, 1)\nout = layer(images)","handlingStrategy":"validation","validationCode":"if len(images.shape) == 2:\n    images = images[..., None]\nassert len(images.shape) in (3, 4)","typeGuard":"def is_rank3or4(x):\n    return len(getattr(x, 'shape', ())) in (3, 4)","tryCatchPattern":null,"preventionTips":["Ensure dataset.map output always has a channels axis"],"tags":["keras","random-contrast","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"}