{"record":{"id":"bb5a0d6cbefd77b7","repo":"keras-team/keras","slug":"architecture-configuration-does-not-match-weights","errorCode":null,"errorMessage":"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}.","messagePattern":"Architecture configuration does not match (.+?) variant\\. When using pre-trained weights, the model architecture must match the pre-trained configuration exactly\\. Expected depths: (.+?), got: (.+?)\\. Expected projection_dims: (.+?), got: (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/convnext.py","lineNumber":533,"sourceCode":"        )(x)\n\n    else:\n        if pooling == \"avg\":\n            x = layers.GlobalAveragePooling2D()(x)\n        elif pooling == \"max\":\n            x = layers.GlobalMaxPooling2D()(x)\n        x = layers.LayerNormalization(epsilon=1e-6)(x)\n\n    model = Functional(inputs=inputs, outputs=x, name=name)\n\n    # Validate weights before requesting them from the API\n    if weights == \"imagenet\":\n        expected_config = MODEL_CONFIGS[weights_name.split(\"convnext_\")[-1]]\n        if (\n            depths != expected_config[\"depths\"]\n            or projection_dims != expected_config[\"projection_dims\"]\n        ):\n            raise ValueError(\n                f\"Architecture configuration does not match {weights_name} \"\n                f\"variant. When using pre-trained weights, the model \"\n                f\"architecture must match the pre-trained configuration \"\n                f\"exactly. Expected depths: {expected_config['depths']}, \"\n                f\"got: {depths}. Expected projection_dims: \"\n                f\"{expected_config['projection_dims']}, got: {projection_dims}.\"\n            )\n\n        if weights_name not in name:\n            raise ValueError(\n                f'Model name \"{name}\" does not match weights variant '\n                f'\"{weights_name}\". When using imagenet weights, model name '\n                f'must contain the weights variant (e.g., \"convnext_'\n                f'{weights_name.split(\"convnext_\")[-1]}\").'\n            )\n\n    # Load weights.\n    if weights == \"imagenet\":","sourceCodeStart":515,"sourceCodeEnd":551,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/convnext.py#L515-L551","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"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))","handlingStrategy":"validation","validationCode":"want = 5 if is_3d else 4\nassert patches.ndim in (want - 1, want), f\"patches rank {patches.ndim} not in {(want-1, want)}\"","typeGuard":"def has_reconstructable_rank(patches, is_3d: bool) -> bool:\n    batched, unbatched = (5, 4) if is_3d else (4, 3)\n    return patches.ndim in (batched, unbatched)","tryCatchPattern":"try:\n    recon = keras.ops.image.reconstruct_patches(patches, size=size)\nexcept ValueError as e:\n    raise ValueError(f\"expected grid-shaped patches, got shape {patches.shape}\") from e","preventionTips":["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"],"tags":["keras","image-ops","rank-error","shape-mismatch"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}