{"record":{"id":"4bcb71a667390c05","repo":"keras-team/keras","slug":"invalid-images-rank-expected-rank-3-single-image-4bcb71","errorCode":null,"errorMessage":"Invalid images rank: expected rank 3 (single image) or rank 4 (batch of images). Received: input_shape={input_shape}","messagePattern":"Invalid images rank: expected rank 3 \\(single image\\) or rank 4 \\(batch of images\\)\\. Received: input_shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py","lineNumber":143,"sourceCode":"    def transform_bounding_boxes(\n        self, bounding_boxes, transformation, training=True\n    ):\n        return bounding_boxes\n\n    def get_config(self):\n        config = super().get_config()\n        config.update(\n            {\n                \"factor\": self.factor,\n                \"value_range\": self.value_range,\n                \"seed\": self.seed,\n            }\n        )\n        return config\n\n    def compute_output_shape(self, input_shape):\n        if len(input_shape) not in (3, 4):\n            raise ValueError(\n                \"Invalid images rank: expected rank 3 (single image) \"\n                \"or rank 4 (batch of images). \"\n                f\"Received: input_shape={input_shape}\"\n            )\n        channels_axis = -1 if self.data_format == \"channels_last\" else -3\n        channels = input_shape[channels_axis]\n        if channels is not None and channels != 3:\n            raise ValueError(\n                \"Input images must have 3 channels, but received images with \"\n                f\"{channels} channels.\"\n            )\n        return input_shape\n\n\nif RandomColorDegeneration.__doc__ is not None:\n    RandomColorDegeneration.__doc__ = RandomColorDegeneration.__doc__.replace(\n        \"{{base_image_preprocessing_color_example}}\",\n        base_image_preprocessing_color_example.replace(","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py#L125-L161","documentation":"RandomColorDegeneration.compute_output_shape requires the input shape to have rank 3 (single image) or 4 (batch). Called during model building, it rejects shapes of other lengths, e.g. rank 2 or rank 5 input specifications.","triggerScenarios":"keras.Input(shape=(1, 224, 224, 3)) making a rank-5 batched shape passed through this layer; any input spec whose batched rank is not 3 or 4.","commonSituations":"Multi-frame/video inputs (T, H, W, C); extra leading axes from data loaders; constructing Input with unnecessary batch-and-time axes.","solutions":["Reshape inputs to (H, W, C) / (batch, H, W, C) before this layer","For video, apply the layer per frame instead of a rank-5 tensor","Check keras.Input(shape=...) has exactly 3 dims"],"exampleFix":"# before\nx = keras.Input(shape=(1, 224, 224, 3))  # rank 4 unbatched -> 5 with batch\n# after\nx = keras.Input(shape=(224, 224, 3))","handlingStrategy":"validation","validationCode":"assert len(input_shape) in (3, 4), input_shape","typeGuard":"def is_img_shape3or4(s):\n    return len(s) in (3, 4)","tryCatchPattern":null,"preventionTips":["Check keras.Input(shape=...) has exactly 3 entries before stacking preprocessing layers"],"tags":["keras","color-degeneration","compute-output-shape","tensor-rank"],"backgroundTag":"input-shape-validation-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}