keras-team/keras · error · ValueError
Model name "{name}" does not match weights variant "{weights
Error message
Model name "{name}" does not match weights variant "{weights_name}". When using imagenet weights, model name must contain the weights variant (e.g., "convnext_{weights_name.split("convnext_")[-1]}"). What it means
compute_output_spec of reconstruct_patches requires the last axis of patches to be exactly prod(size)*channels (the flattened patch content). If the final dim is not divisible by patch_d*patch_h*patch_w, the flattened values cannot be reshaped back into spatial patches, so it fails before any computation.
Source
Thrown at keras/src/applications/convnext.py:543
# Validate weights before requesting them from the API
if weights == "imagenet":
expected_config = MODEL_CONFIGS[weights_name.split("convnext_")[-1]]
if (
depths != expected_config["depths"]
or projection_dims != expected_config["projection_dims"]
):
raise ValueError(
f"Architecture configuration does not match {weights_name} "
f"variant. When using pre-trained weights, the model "
f"architecture must match the pre-trained configuration "
f"exactly. Expected depths: {expected_config['depths']}, "
f"got: {depths}. Expected projection_dims: "
f"{expected_config['projection_dims']}, got: {projection_dims}."
)
if weights_name not in name:
raise ValueError(
f'Model name "{name}" does not match weights variant '
f'"{weights_name}". When using imagenet weights, model name '
f'must contain the weights variant (e.g., "convnext_'
f'{weights_name.split("convnext_")[-1]}").'
)
# Load weights.
if weights == "imagenet":
if include_top:
file_suffix = ".h5"
file_hash = WEIGHTS_HASHES[weights_name][0]
else:
file_suffix = "_notop.h5"
file_hash = WEIGHTS_HASHES[weights_name][1]
file_name = name + file_suffix
weights_path = file_utils.get_file(
file_name,
BASE_WEIGHTS_PATH + file_name,View on GitHub (pinned to 7a34a03db6)
Solutions
- Ensure patches.shape[-1] == prod(size) * channels; if a linear layer changed the dim, project back to prod(size)*channels first
- Pass exactly the same size (and data_format) used for extract_patches
- If reconstructing a projected embedding, invert the projection or skip reconstruction
Example fix
before: reconstruct_patches(x, size=(8,8)) with x.shape[-1] == 768 and 3 channels; after: x2 = ops.matmul(x, W_inv); reconstruct_patches(x2, size=(8,8)) where x2.shape[-1] == 192
Defensive patterns
Strategy: validation
Validate before calling
import math prod_size = size if isinstance(size, int) else math.prod(size) assert patches.shape[-1] % prod_size == 0, (patches.shape[-1], prod_size)
Type guard
def flat_dim_matches(flat: int, size, channels: int) -> bool:
prod_size = size if isinstance(size, int) else math.prod(size)
return flat % (prod_size * channels) == 0 and flat > 0 Prevention
- Pass the identical size and data_format used at extraction
- Compute expected flat dim as prod(size)*channels and assert before reconstruct
- Never reconstruct from post-projection embeddings without inverting the projection
When it happens
Trigger: Passing a projection/output dim of a ViT layer (e.g. 768) where the patch flatten dim is 8*8*3=192; using size=(8,8) when patches were extracted with (4,4); channels_first patches fed without specifying data_format, so C ends up on the wrong axis.
Common situations: Vision-transformer pipelines that append a dense projection after patch extraction and reconstruct from the projected tensor; patch size changed between extract and reconstruct; forgetting that the flat dim includes channels for channels_last data.
Related errors
- Architecture configuration does not match {weights_name} var
- DenseNet does not support the `channels_first` image data fo
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/123a0aa4bd842130.
Report an issue: GitHub.