keras-team/keras · error · ValueError
If using `weights="imagenet"` as true, `classes` should be 1
Error message
If using `weights="imagenet"` as true, `classes` should be 1000
What it means
Width-axis cropping check: left_cropping (explicit or inferred as width - target_width - right_cropping) must be >= 0; negative means the horizontal crop configuration is impossible.
Source
Thrown at keras/src/applications/efficientnet_v2.py:905
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,
data_format=backend.image_data_format(),
require_flatten=include_top,
weights=weights,
)
if input_tensor is None:
img_input = layers.Input(shape=input_shape)
else:
if not backend.is_keras_tensor(input_tensor):View on GitHub (pinned to 7a34a03db6)
Solutions
- Ensure target_width + left + right cropping <= width
- Use pad_images to enlarge
- Double-check which axis is width under your data_format
Example fix
# before out = ops.image.crop_images(img, target_width=(64, 64)) # img width 32 # after out = ops.image.pad_images(img, target_width=(64, 64))
Defensive patterns
Strategy: validation
Validate before calling
left = left_cropping or 0
right = right_cropping or 0
if width is not None and left + right + target_width > width:
raise ValueError('horizontal crop exceeds width') Prevention
- Clamp center-crop amounts to >= 0
- Pad first when the image is smaller than the crop window
When it happens
Trigger: Negative left_cropping argument, or None with right_cropping + target_width > width.
Common situations: Center-crop math going negative on narrow images; aspect-ratio-preserving pipelines; swapped width/height.
Related errors
- If using `weights="imagenet"` as true, `classes` should be 1
- The number of repeats in `EfficientNet` must be > 0. Receive
- The number of repeats in `EfficientNetV2` must be > 0. Recei
- weights_path undefined
- The `weights` argument should be either `None` (random initi
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/bd5d31cd16c99f5e.
Report an issue: GitHub.