keras-team/keras · error · ValueError
The `weights` argument should be either `None` (random initi
Error message
The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.
What it means
Width-axis counterpart: the input width dim is None and target_width was not specified, so the cropping op cannot determine its output width in the symbolic shape.
Source
Thrown at keras/src/applications/efficientnet.py:275
last convolutional layer, and thus
the output of the model will be a 2D tensor.
- `max` means that global max pooling will
be applied.
classes: optional number of classes to classify images
into, only to be specified if `include_top` is True, and
if no `weights` argument is specified.
classifier_activation: A `str` or callable. The activation function to use
on the "top" layer. Ignored unless `include_top=True`. Set
`classifier_activation=None` to return the logits of the "top" layer.
Returns:
A model instance.
"""
if blocks_args == "default":
blocks_args = DEFAULT_BLOCKS_ARGS
if not (weights in {"imagenet", None} or file_utils.exists(weights)):
raise ValueError(
"The `weights` argument should be either "
"`None` (random initialization), `imagenet` "
"(pre-training on ImageNet), "
"or the path to the weights file to be loaded."
)
if weights == "imagenet" and include_top and classes != 1000:
raise ValueError(
'If using `weights="imagenet"` with `include_top`'
" as true, `classes` should be 1000"
)
# Determine proper input shape
input_shape = imagenet_utils.obtain_input_shape(
input_shape,
default_size=default_size,
min_size=32,
data_format=backend.image_data_format(),View on GitHub (pinned to 7a34a03db6)
Solutions
- Specify target_width on the operation
- Fix the input width in keras.Input
- Handle fully-dynamic crops with raw slicing outside the layer
Example fix
# before inputs = keras.Input(shape=(224, None, 3)) x = Cropping2D(2)(inputs) # after inputs = keras.Input(shape=(224, 224, 3)) x = Cropping2D(2)(inputs)
Defensive patterns
Strategy: validation
Validate before calling
if images.shape[-2] is None and target_width is None:
raise ValueError('fixed input width or target_width required') Type guard
def has_static_width(images) -> bool:
return images.shape[-2] is not None Prevention
- Set explicit target_width for variable-width pipelines
- Use fixed Input shapes in functional models
When it happens
Trigger: Variable-width inputs (Input(shape=(H, None, 3))) with a cropping layer lacking target_width.
Common situations: Variable-resolution models; Keras 3 symbolic tracing where TF1-style code assumed lazy shapes.
Related errors
- weights_path undefined
- If using `weights="imagenet"` as true, `classes` should be 1
- The number of repeats in `EfficientNet` must be > 0. Receive
- The `weights` argument should be either `None` (random initi
- If using `weights="imagenet"` as true, `classes` should be 1
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/39c1a56bea686866.
Report an issue: GitHub.