keras-team/keras · error · ValueError

input_shape[1] must equal input_tensor.shape[2]. Received…

Error message

input_shape[1] must equal input_tensor.shape[2].  Received input_shape={input_shape}, input_tensor.shape={input_tensor.shape}

What it means

MobileNetV3 builder in keras/src/applications/mobilenet_v3.py: under channels_last, when both input_shape and input_tensor are supplied, input_tensor.shape[2] must equal input_shape[1]. The guard fires when the declared shape and the tensor's actual width disagree, which would yield an inconsistent model graph.

Solutions

  1. Set input_shape to match input_tensor.shape, e.g. (224, 224, 3) for a (None, 224, 224, 3) tensor
  2. Provide only input_shape or only input_tensor and let the builder infer the other
  3. Recreate the tensor via keras.Input(shape=input_shape)
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/applications/mobilenet_v3.py:216 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/65fc052c470530a7. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/applications/mobilenet_v3.py:216

                    "input_tensor: ",
                    input_tensor,
                    "is not type input_tensor.  "
                    f"Received type(input_tensor)={type(input_tensor)}",
                )
        if is_input_t_tensor:
            if backend.image_data_format() == "channels_first":
                if input_tensor.shape[1] != input_shape[1]:
                    raise ValueError(
                        "When backend.image_data_format()=channels_first, "
                        "input_shape[1] must equal "
                        "input_tensor.shape[1].  Received "
                        f"input_shape={input_shape}, "
                        "input_tensor.shape="
                        f"{input_tensor.shape}"
                    )
            else:
                if input_tensor.shape[2] != input_shape[1]:
                    raise ValueError(
                        "input_shape[1] must equal "
                        "input_tensor.shape[2].  Received "
                        f"input_shape={input_shape}, "
                        "input_tensor.shape="
                        f"{input_tensor.shape}"
                    )
        else:
            raise ValueError(
                "input_tensor specified: ",
                input_tensor,
                "is not a keras tensor",
            )

    # If input_shape is None, infer shape from input_tensor
    if input_shape is None and input_tensor is not None:
        try:
            backend.is_keras_tensor(input_tensor)
        except ValueError:

View on GitHub (pinned to 7a34a03db6)