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
In the cropping output spec, top_cropping (explicit or inferred as height - target_height - bottom_cropping) must be >= 0. Negative top cropping means you asked to crop more than the image height allows (or to grow via negative crop).
Source
Thrown at keras/src/applications/efficientnet.py:283
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(),
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
- Use pad_images to enlarge instead of negative cropping
- Ensure target_height <= height - top_cropping - bottom_cropping
- Clamp crop amounts to the available extent
Example fix
# before out = ops.image.crop_images(img, target_height=(256, 256)) # img is 128 tall # after out = ops.image.pad_images(img, target_height=(256, 256))
Defensive patterns
Strategy: validation
Validate before calling
if height is not None and target_height + (top_cropping or 0) + (bottom_cropping or 0) > height:
raise ValueError('crop window exceeds image height') Prevention
- Use pad_images to enlarge, never negative crops
- Center-crop math: crop = max(0, (size - target)//2)
When it happens
Trigger: Explicit negative top_cropping, or croppings left None with target_height > height.
Common situations: Upsizing attempts via negative crop values; symmetric crop arithmetic going negative on small images; off-by-one target sizes.
Related errors
- The number of repeats in `EfficientNet` must be > 0. Receive
- If using `weights="imagenet"` as true, `classes` should be 1
- 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/f1cab21d36da304f.
Report an issue: GitHub.