keras-team/keras · error · ValueError

The input must have 3 channels; Received `input_shape=

Error message

The input must have 3 channels; Received `input_shape={input_shape}`

What it means

obtain_input_shape in keras/src/applications/imagenet_utils.py: when the channel dimension is specified, it must be 3 because pretrained ImageNet models consume RGB input. It fires when input_shape declares channels other than 3 (e.g. grayscale 1-channel or 4-channel input) while building a 3-channel model.

Solutions

  1. Convert your images to RGB (3 channels) before feeding the model, e.g. cv2.cvtColor or tf.image.grayscale_to_rgb
  2. Replicate a grayscale channel three times: np.repeat(img, 3, axis=-1)
  3. Build the model with weights=None and your own first conv layer if non-RGB input is required
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/applications/imagenet_utils.py:367 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/b6f4673fef39fb24. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/applications/imagenet_utils.py:367

    if weights == "imagenet" and require_flatten:
        if input_shape is not None:
            if input_shape != default_shape:
                raise ValueError(
                    "When setting `include_top=True` "
                    "and loading `imagenet` weights, "
                    f"`input_shape` should be {default_shape}.  "
                    f"Received: input_shape={input_shape}"
                )
        return default_shape
    if input_shape:
        if data_format == "channels_first":
            if input_shape is not None:
                if len(input_shape) != 3:
                    raise ValueError(
                        "`input_shape` must be a tuple of three integers."
                    )
                if input_shape[0] != 3 and weights == "imagenet":
                    raise ValueError(
                        "The input must have 3 channels; Received "
                        f"`input_shape={input_shape}`"
                    )
                if (
                    input_shape[1] is not None and input_shape[1] < min_size
                ) or (input_shape[2] is not None and input_shape[2] < min_size):
                    raise ValueError(
                        f"Input size must be at least {min_size}"
                        f"x{min_size}; Received: "
                        f"input_shape={input_shape}"
                    )
        else:
            if input_shape is not None:
                if len(input_shape) != 3:
                    raise ValueError(
                        "`input_shape` must be a tuple of three integers."
                    )
                if input_shape[-1] != 3 and weights == "imagenet":

View on GitHub (pinned to 7a34a03db6)