keras-team/keras · error · ValueError
Architecture configuration does not match {weights_name} var
Error message
Architecture configuration does not match {weights_name} variant. When using pre-trained weights, the model architecture must match the pre-trained configuration exactly. Expected depths: {expected_config['depths']}, got: {depths}. Expected projection_dims: {expected_config['projection_dims']}, got: {projection_dims}. What it means
compute_output_spec of reconstruct_patches rejects a patches tensor whose rank matches neither the unbatched nor the batched layout for the chosen 2D/3D mode. Reconstruction must know whether the flat patch grid is (rows, cols[, planes], patch_flat) or has a leading batch dim; any other rank makes grid inference impossible.
Source
Thrown at keras/src/applications/convnext.py:533
)(x)
else:
if pooling == "avg":
x = layers.GlobalAveragePooling2D()(x)
elif pooling == "max":
x = layers.GlobalMaxPooling2D()(x)
x = layers.LayerNormalization(epsilon=1e-6)(x)
model = Functional(inputs=inputs, outputs=x, name=name)
# 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":View on GitHub (pinned to 7a34a03db6)
Solutions
- Reshape patches to (grid_h, grid_w, patch_flat) or (batch, grid_h, grid_w, patch_flat) for 2D (add grid_d for 3D) before reconstruct_patches
- Regenerate patches with extract_patches and pass them through unmodified
- Check patches.ndim and the is_3d flag agree before the call
Example fix
before: recon = reconstruct_patches(tokens, size=(8,8)) where tokens.shape == (n, 192) -> ValueError; after: recon = reconstruct_patches(tokens.reshape(1, gh, gw, 192), size=(8,8))
Defensive patterns
Strategy: validation
Validate before calling
want = 5 if is_3d else 4
assert patches.ndim in (want - 1, want), f"patches rank {patches.ndim} not in {(want-1, want)}" Type guard
def has_reconstructable_rank(patches, is_3d: bool) -> bool:
batched, unbatched = (5, 4) if is_3d else (4, 3)
return patches.ndim in (batched, unbatched) Try / catch
try:
recon = keras.ops.image.reconstruct_patches(patches, size=size)
except ValueError as e:
raise ValueError(f"expected grid-shaped patches, got shape {patches.shape}") from e Prevention
- Keep the grid axes produced by extract_patches; only reshape the last axis
- For ViT tokens, reshape (batch, tokens, dim) back to (batch, gh, gw, dim) first
- Log patches.shape right before reconstruction when debugging pipelines
When it happens
Trigger: Passing a rank-3 tensor to a 2D reconstruction (grid dims lost, e.g. patches reshaped to (n_patches, flat) beforehand); passing a rank-5 tensor to 2D reconstruct; reconstructing 3D patches while the op was configured 2D (is_3d mismatch); extra/missing axes from squeeze/reshape in a preprocessing pipeline.
Common situations: Feeding patches straight from a dataloader that flattens them further; vision-transformer code that reshapes patch tokens to (batch, tokens, dim) and forgets to restore the 2D grid; switching a pipeline between 2D and 3D without regenerating the patches.
Related errors
- Model name "{name}" does not match weights variant "{weights
- 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/bb5a0d6cbefd77b7.
Report an issue: GitHub.