keras-team/keras · error · ValueError
If using `weights="imagenet"` with `include_top=True`, `clas
Error message
If using `weights="imagenet"` with `include_top=True`, `classes` should be 1000. Received classes={classes} What it means
Raised by _extract_patches_3d when strides, after int expansion, is not a length-3 sequence. Strides default to the patch size; if you override them you must give one stride per spatial dim (d, h, w) so the extractor knows the sampling step along each axis.
Source
Thrown at keras/src/applications/convnext.py:409
Returns:
A model instance.
"""
if backend.image_data_format() == "channels_first":
raise ValueError(
"ConvNeXt does not support the `channels_first` image data "
"format. Switch to `channels_last` by editing your local "
"config file at ~/.keras/keras.json"
)
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=True`, '
"`classes` should be 1000. "
f"Received classes={classes}"
)
# 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:View on GitHub (pinned to 7a34a03db6)
Solutions
- Pass strides as an int (same stride on all 3 axes) or a length-3 tuple matching (depth, height, width)
- Omit strides entirely if you want non-overlapping patches (defaults to size)
- Mirror the shape of size when building strides programmatically: strides = tuple(s // 2 for s in size)
Example fix
before: extract_patches_3d(v, size=(4,4,4), strides=(2,2)) -> ValueError; after: extract_patches_3d(v, size=(4,4,4), strides=2)
Defensive patterns
Strategy: validation
Validate before calling
if strides is not None and not isinstance(strides, int):
strides = tuple(strides)
assert len(strides) == 3, f"strides for 3D must have length 3, got {len(strides)}" Type guard
def valid_3d_strides(strides) -> bool:
return strides is None or isinstance(strides, int) or (isinstance(strides, (tuple, list)) and len(strides) == 3) Prevention
- Prefer a single int stride unless per-axis control is required
- Build strides from size with the same length
- Remember strides default to size (non-overlapping) when omitted
When it happens
Trigger: extract_patches_3d(vols, size=(4,4,4), strides=(2,2)) or strides=[2] on 5D inputs; passing a 2D stride tuple from an image pipeline into a volume pipeline; strides computed as (s, s) + something that dropped an axis.
Common situations: Reusing an image-model stride config for video/voxel models; strides derived from size[:-1] or another slicing bug; frameworks that accept 2-tuples elsewhere (conv2d) making the 3-tuple requirement easy to miss.
Related errors
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
- The `weights` argument should be either `None` (random initi
- If using `weights` as `"imagenet"` with `include_top` as tru
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/f1d3c36e2a534769.
Report an issue: GitHub.