{"record":{"id":"2382e09e8f009b76","repo":"WZMIAOMIAO/deep-learning-for-image-processing","slug":"no-match-key","errorCode":null,"errorMessage":"no match key '{}'","messagePattern":"no match key '(.+?)'","errorType":"validation","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"pytorch_classification/Test9_efficientNet/trans_weights_to_pytorch.py","lineNumber":93,"sourceCode":"            torch_name = \"features.top.1.weight\"\n            weights_dict[torch_name] = data\n        elif \"top_bn/beta:0\" == name:\n            torch_name = \"features.top.1.bias\"\n            weights_dict[torch_name] = data\n        elif \"top_bn/moving_mean:0\" == name:\n            torch_name = \"features.top.1.running_mean\"\n            weights_dict[torch_name] = data\n        elif \"top_bn/moving_variance:0\" == name:\n            torch_name = \"features.top.1.running_var\"\n            weights_dict[torch_name] = data\n        elif \"predictions/kernel:0\" == name:\n            torch_name = \"classifier.1.weight\"\n            weights_dict[torch_name] = np.transpose(data, (1, 0)).astype(np.float32)\n        elif \"predictions/bias:0\" == name:\n            torch_name = \"classifier.1.bias\"\n            weights_dict[torch_name] = data\n        else:\n            raise KeyError(\"no match key '{}'\".format(name))\n\n    for k, v in weights_dict.items():\n        weights_dict[k] = torch.as_tensor(v)\n\n    torch.save(weights_dict, save_path)\n    print(\"Conversion complete.\")\n\n\nif __name__ == '__main__':\n    main()\n","sourceCodeStart":75,"sourceCodeEnd":104,"githubUrl":"https://github.com/WZMIAOMIAO/deep-learning-for-image-processing/blob/1ec3fe6f374fc9969973a61f819de25658595afa/pytorch_classification/Test9_efficientNet/trans_weights_to_pytorch.py#L75-L104","documentation":"trans_weights_to_pytorch.py maps each TensorFlow/official EfficientNet weight tensor name to the matching PyTorch key. If an incoming name (e.g. a TF variable like 'conv2d/kernel:0' variant or a new official checkpoint layout) has no if/elif branch, the script raises KeyError('no match key ...') instead of silently dropping tensors.","triggerScenarios":"Converting a TF checkpoint whose layer names don't match the expected official EfficientNet naming scheme, e.g. different prefix, numbered conv names (conv2d_5), or added head variables like 'predictions/proliferation' not handled above.","commonSituations":"Using a different EfficientNet release (B1-B7 vs B0, or EfficientNetV2) whose TF names differ; Keras auto-renamed duplicate layers with _N suffixes; checking checkpoints from tensorflow hub models with altered naming.","solutions":["Add an elif branch mapping the reported name to the correct PyTorch key (with proper transpose like (1, 0) or (3, 2, 1, 0) for conv kernels)","Print/inspect all unmatched names first and extend the mapping table for the variant you are converting","Use a checkpoint from the same model version the script was written for (official EfficientNet B0 naming)"],"exampleFix":"// before\nelse:\n    raise KeyError(\"no match key '{}'\".format(name))\n// after\nelif name.startswith(\"conv2d_\") and name.endswith(\"kernel:0\"):\n    torch_name = \"features.{}.weight\".format(...)  # map per matched index\n    weights_dict[torch_name] = np.transpose(data, (3, 2, 0, 1)).astype(np.float32)\nelse:\n    raise KeyError(\"no match key '{}'\".format(name))","handlingStrategy":"try-catch","validationCode":"# pre-check all TF names against the mapping before converting\nfor name in tf_weights:\n    if not any(pattern matches name for pattern in known_patterns):\n        print('unmapped key:', name)","typeGuard":"def is_known_tf_key(name: str) -> bool:\n    return name.endswith((':0',)) and any(p in name for p in KNOWN_PATTERNS)","tryCatchPattern":"try:\n    main()\nexcept KeyError as e:\n    print('Add an elif branch for this TF variable name:', e)\n    sys.exit(1)","preventionTips":["Dump all TF variable names first and diff against handled patterns before conversion","Match model version exactly (EfficientNet B0 official naming) with the script","Print unmatched keys instead of hard-failing during exploratory conversions"],"tags":["pytorch","tensorflow","weight-conversion","keyerror"],"backgroundTag":"weight-conversion-key-mismatch","analyzedSha":"1ec3fe6f374fc9969973a61f819de25658595afa","analyzedAt":"2026-08-30T09:19:11.901Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}