keras-team/keras · error · ValueError
When setting `include_top=True` and loading `imagenet`…
Error message
When setting `include_top=True` and loading `imagenet` weights, `input_shape` should be {default_shape}. Received: input_shape={input_shape} What it means
obtain_input_shape in keras/src/applications/imagenet_utils.py: when loading pretrained "imagenet" weights with include_top=True, the classifier weights fix the spatial size, so input_shape must exactly equal the model's default shape (e.g. (224,224,3)). Any other shape — including correct channel count but wrong width/height — is rejected because the dense top cannot be reshaped to it.
Solutions
- Pass input_shape equal to the model's default_size, e.g. (224, 224, 3), or omit input_shape entirely to use the default
- Set include_top=False if you need custom spatial dimensions and will add your own head
- Use random initialization (weights=None) if a non-standard input size is required with a top
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/applications/imagenet_utils.py:352 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/71a897cdd69d1f36.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/applications/imagenet_utils.py:352
default_shape = (input_shape[0], default_size, default_size)
else:
if input_shape[-1] not in {1, 3}:
warnings.warn(
"This model usually expects 1 or 3 input channels. "
"However, it was passed an input_shape "
f"with {input_shape[-1]} input channels.",
stacklevel=2,
)
default_shape = (default_size, default_size, input_shape[-1])
else:
if data_format == "channels_first":
default_shape = (3, default_size, default_size)
else:
default_shape = (default_size, default_size, 3)
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}`"
)View on GitHub (pinned to 7a34a03db6)