{"record":{"id":"7b3413c409ab797a","repo":"keras-team/keras","slug":"input-images-must-have-3-channels-but-received-im-7b3413","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":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":108,"sourceCode":"\n    def compute_output_spec(self, images):\n        images_shape = list(images.shape)\n        dtype = images.dtype\n        if len(images_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: images.shape={images_shape}\"\n            )\n        if not backend.is_float_dtype(dtype):\n            raise ValueError(\n                \"Invalid images dtype: expected float dtype. \"\n                f\"Received: images.dtype={dtype}\"\n            )\n        channels_axis = -1 if self.data_format == \"channels_last\" else -3\n        channels = images_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 KerasTensor(shape=images_shape, dtype=images.dtype)\n\n\n@keras_export(\"keras.ops.image.rgb_to_hsv\")\ndef rgb_to_hsv(images, data_format=None):\n    \"\"\"Convert RGB images to HSV.\n\n    `images` must be of float dtype, and the output is only well defined if the\n    values in `images` are in `[0, 1]`.\n\n    All HSV values are in `[0, 1]`. A hue of `0` corresponds to pure red, `1/3`\n    is pure green, and `2/3` is pure blue.\n\n    Args:\n        images: Input image or batch of images. Must be 3D or 4D.","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L90-L126","documentation":"rgb_to_hsv requires exactly 3 channels on the channels axis (which it reads per data_format). Any other concrete channel count — most commonly 1 (grayscale) or 4 (RGBA) — raises this ValueError.","triggerScenarios":"Passing grayscale (H,W,1) tensors or RGBA (H,W,4) tensors to rgb_to_hsv; passing channels_first data while leaving data_format at default so the wrong axis is read as channels.","commonSituations":"Applying HSV augmentation to a mixed dataset including grayscale images; alpha-channel PNGs; forgetting data_format='channels_first' for PyTorch-style tensors.","solutions":["Convert grayscale to RGB by broadcasting the channel axis, and drop alpha: images = images[..., :3]","Pass data_format='channels_first' when your tensors are (N,C,H,W)"],"exampleFix":"# before\nhsv = keras.ops.image.rgb_to_hsv(gray)  # gray.shape=(H,W,1)\n\n# after\nrgb = keras.ops.broadcast_to(gray, gray.shape[:-1] + (3,))\nhsv = keras.ops.image.rgb_to_hsv(rgb)","handlingStrategy":"validation","validationCode":"c = images.shape[-1 if data_format == 'channels_last' else -3]\nassert c is None or c == 3","typeGuard":"def is_rgb(images, data_format='channels_last') -> bool:\n    return images.shape[-1 if data_format == 'channels_last' else -3] in (3, None)","tryCatchPattern":null,"preventionTips":["Filter/convert grayscale samples before HSV ops","Pass data_format='channels_first' for (N,C,H,W) tensors"],"tags":["keras","image","channel-validation","rgb-to-hsv"],"backgroundTag":"image-channel-validation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}