{"record":{"id":"123a0aa4bd842130","repo":"keras-team/keras","slug":"model-name-name-does-not-match-weights-variant","errorCode":null,"errorMessage":"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]}\").","messagePattern":"Model name \"(.+?)\" does not match weights variant \"(.+?)\"\\. When using imagenet weights, model name must contain the weights variant \\(e\\.g\\., \"convnext_(.+?)\"\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/convnext.py","lineNumber":543,"sourceCode":"\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\":\n        if include_top:\n            file_suffix = \".h5\"\n            file_hash = WEIGHTS_HASHES[weights_name][0]\n        else:\n            file_suffix = \"_notop.h5\"\n            file_hash = WEIGHTS_HASHES[weights_name][1]\n        file_name = name + file_suffix\n        weights_path = file_utils.get_file(\n            file_name,\n            BASE_WEIGHTS_PATH + file_name,","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/convnext.py#L525-L561","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"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","handlingStrategy":"validation","validationCode":"import math\nprod_size = size if isinstance(size, int) else math.prod(size)\nassert patches.shape[-1] % prod_size == 0, (patches.shape[-1], prod_size)","typeGuard":"def flat_dim_matches(flat: int, size, channels: int) -> bool:\n    prod_size = size if isinstance(size, int) else math.prod(size)\n    return flat % (prod_size * channels) == 0 and flat > 0","tryCatchPattern":null,"preventionTips":["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"],"tags":["keras","image-ops","shape-mismatch","patch-embedding"],"backgroundTag":"tensor-dim-not-divisible","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}