keras-team/keras · error · ValueError
input_tensor.shape[2] must equal input_shape[1]; Received…
Error message
input_tensor.shape[2] must equal input_shape[1]; Received `input_tensor.shape={input_tensor.shape}`, `input_shape={input_shape}` What it means
MobileNetV2 builder in keras/src/applications/mobilenet_v2.py: when both input_shape and input_tensor are supplied (channels_last layout), input_tensor.shape[2] (the spatial width) must equal input_shape[1]. The guard fires when the declared input shape and the actual shape of the provided input tensor disagree, which would silently build an inconsistent graph.
Solutions
- Align input_shape with input_tensor.shape, e.g. input_shape=(224, 224, 3) for a (None, 224, 224, 3) tensor
- Pass only one of input_shape or input_tensor and let the builder infer the rest
- Recreate the input tensor via keras.Input(shape=input_shape) to guarantee consistency
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/applications/mobilenet_v2.py:155 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/1b690b2e07af0057.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/applications/mobilenet_v2.py:155
except ValueError:
raise ValueError(
f"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(
"input_shape[1] must equal shape(input_tensor)[1] "
"when `image_data_format` is `channels_first`; "
"Received `input_tensor.shape="
f"{input_tensor.shape}`"
f", `input_shape={input_shape}`"
)
else:
if input_tensor.shape[2] != input_shape[1]:
raise ValueError(
"input_tensor.shape[2] must equal input_shape[1]; "
"Received `input_tensor.shape="
f"{input_tensor.shape}`, "
f"`input_shape={input_shape}`"
)
else:
raise ValueError(
"input_tensor is not a Keras tensor; "
f"Received `input_tensor={input_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:
raise ValueError(
"input_tensor must be a valid Keras tensor type; "View on GitHub (pinned to 7a34a03db6)