{"record":{"id":"0034b2d19a10a179","repo":"keras-team/keras","slug":"input-images-must-have-3-channels-but-received-im","errorCode":null,"errorMessage":"Input images must have 3 channels, but received images with {channels} channels.","messagePattern":"Input images must have 3 channels, but received images with (.+?) channels\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py","lineNumber":151,"sourceCode":"            {\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(\n            \"{LayerName}\", \"RandomColorDegeneration\"\n        ),\n    )\n","sourceCodeStart":133,"sourceCodeEnd":165,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/image_preprocessing/random_color_degeneration.py#L133-L165","documentation":"RandomColorDegeneration converts images to grayscale internally, so compute_output_shape enforces exactly 3 channels (RGB). A channel count that is known and != 3 (1, 4, or more) fails model building with this ValueError.","triggerScenarios":"keras.Input(shape=(224, 224, 1)) or (224, 224, 4) fed through RandomColorDegeneration and the model built/summarized.","commonSituations":"Grayscale datasets; RGBA images with alpha channel; medical imaging with multi-channel inputs.","solutions":["Convert inputs to RGB before the layer: tf.image.grayscale_to_rgb or np.repeat(x, 3, axis=-1) for 1-channel","Drop the alpha channel for RGBA: x[..., :3]","Apply color degeneration only on 3-channel branches"],"exampleFix":"# before\nx = keras.Input(shape=(224, 224, 1))\nx = RandomColorDegeneration(0.5)(x)\n# after\nx = keras.Input(shape=(224, 224, 3))\n# or pre-convert: images = tf.image.grayscale_to_rgb(images)","handlingStrategy":"validation","validationCode":"c = images.shape[-1]\nif c is not None and c != 3:\n    images = np.repeat(images, 3 // c, axis=-1) if c == 1 else images[..., :3]","typeGuard":"def is_rgb(x):\n    c = x.shape[-1]\n    return c is None or c == 3","tryCatchPattern":null,"preventionTips":["Convert grayscale to RGB and drop alpha in the input pipeline before augmentation layers"],"tags":["keras","color-degeneration","channels","compute-output-shape"],"backgroundTag":"input-shape-validation-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}