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.Received: weights={weights} What it means
The cropping operation's target_height must be >= 0; a negative final cropped height is rejected during output-shape computation.
Source
Thrown at keras/src/applications/efficientnet_v2.py:896
only to be specified if `include_top` is `True`, and if no `weights`
argument is specified.
classifier_activation: A string 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.
include_preprocessing: Boolean, whether to include the preprocessing
layer (`Rescaling`) at the bottom of the network.
Defaults to `True`.
Returns:
A model instance.
"""
if blocks_args == "default":
blocks_args = DEFAULT_BLOCKS_ARGS[name]
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."
f"Received: weights={weights}"
)
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,View on GitHub (pinned to 7a34a03db6)
Solutions
- Validate target_height >= 0 before calling
- Fix the arithmetic that produced the negative value
- Skip cropping for degenerate sizes
Example fix
# before
out = ops.image.crop_images(img, target_height=(h - 2 * c, w - 2 * c)) # h < 2c
# after
if h - 2 * c < 0:
raise ValueError('image too small to crop')
out = ops.image.crop_images(img, target_height=(h - 2 * c, w - 2 * c)) Defensive patterns
Strategy: validation
Validate before calling
target_height = int(target_height)
if target_height < 0:
raise ValueError('target_height must be >= 0') Prevention
- Guard computed targets (size - margins) against underflow
- Reject degenerate small inputs early
When it happens
Trigger: Passing a negative target_height, often from a subtraction like target = size - 2*crop that underflows.
Common situations: Computed crop targets on small images; config typos; -1 sentinel dynamic dims leaking into the call.
Related errors
- Expected mode to be one of `caffe`, `tf` or `torch`. Receive
- weights_path undefined
- The `weights` argument should be either `None` (random initi
- If using `weights="imagenet"` as true, `classes` should be 1
- The number of repeats in `EfficientNet` must be > 0. Receive
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/3c6612d55a982892.
Report an issue: GitHub.