{"record":{"id":"4274623a94e39891","repo":"deepfakes/faceswap","slug":"self-old-model-file-is-not-a-valid-faceswap-2","errorCode":null,"errorMessage":"'{self._old_model_file}' is not a valid Faceswap 2 model file","messagePattern":"'(.+?)' is not a valid Faceswap 2 model file","errorType":"exception","errorClass":"FaceswapError","httpStatus":null,"severity":"error","filePath":"plugins/train/model/_base/update.py","lineNumber":65,"sourceCode":"    def _get_model_config(self) -> dict[str, T.Any]:\n        \"\"\"Obtain a keras 2.x config from a keras 2.x .h5 file.\n\n        As keras 3.x will error out loading the file, we collect it directly from the .h5 file\n\n        Returns\n        -------\n        A keras 2.x model configuration dictionary\n\n        Raises\n        ------\n        FaceswapError\n            If the file is not a valid Faceswap 2 .h5 model file\n        \"\"\"\n        h5file = h5py.File(self._old_model_file, \"r\")\n        s_version = T.cast(str | None, h5file.attrs.get(\"keras_version\"))\n        s_config = T.cast(str | None, h5file.attrs.get(\"model_config\"))\n        if not s_version or not s_config:\n            raise FaceswapError(f\"'{self._old_model_file}' is not a valid Faceswap 2 model file\")\n\n        version = s_version.split(\".\")[:2]\n        if len(version) != 2 or version[0] != \"2\":\n            raise FaceswapError(f\"'{self._old_model_file}' is not a valid Faceswap 2 model file\")\n\n        retval = json.loads(s_config)\n        logger.debug(\"Loaded keras 2.x model config: %s\", retval)\n        return retval\n\n    @classmethod\n    def _unwrap_outputs(cls, outputs: list[list[T.Any]]) -> list[list[str | int]]:\n        \"\"\"Unwrap nested output tensors from a config dict to be a single list of output tensor\n\n        Parameters\n        ----------\n        outputs\n            The outputs that exist within the Keras 2 config dict that may be nested\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/deepfakes/faceswap/blob/f530cb7508ae670f6474f8a7d9c4df94705cf96b/plugins/train/model/_base/update.py#L47-L83","documentation":"FaceswapError raised during Keras 2 -> Faceswap 3 model porting (UpdateModelBase.get_legacy_config) when the .h5 file lacks the 'keras_version' or 'model_config' HDF5 root attributes. Those attributes are what identify a genuine Keras 2.x-saved Faceswap 2 model; without them the file cannot be interpreted as a portable model. The file is either not a Keras model at all or was saved by an incompatible tool/version.","triggerScenarios":"Running the porting/update path with -m containing a file whose HDF5 root attrs miss keras_version or model_config: e.g. pointing at a raw weights file, a corrupted download, or a file written by a different framework.","commonSituations":"User tries to port a partially downloaded model, a file renamed to .h5, or a Faceswap 1.x / third-party file; or the h5 was re-saved by a tool that strips root attributes.","solutions":["Verify the file with h5py that h5.attrs contains 'keras_version' and 'model_config' (python -c \"import h5py; print(dict(h5py.File(f,'r').attrs))\")","Re-download or re-copy the original Faceswap 2 model file if attributes are missing (corruption or wrong source)","Ensure you are porting a Faceswap 2.x model, not a Faceswap 1.x or foreign .h5 file"],"exampleFix":"# before: porting an arbitrary h5\npython tools.py port -m /models/mystery.h5\n\n# after: verify first\nimport h5py\nwith h5py.File('/models/mystery.h5', 'r') as f:\n    assert 'keras_version' in f.attrs and 'model_config' in f.attrs, 'not a FS2 model'","handlingStrategy":"validation","validationCode":"import h5py\nwith h5py.File(model_h5, \"r\") as f:\n    attrs = dict(f.attrs)\nif \"keras_version\" not in attrs or \"model_config\" not in attrs:\n    raise SystemExit(f\"{model_h5} is not a Keras 2 Faceswap model; cannot port\")","typeGuard":"def is_fs2_model(path: str) -> bool:\n    \"\"\"True if the h5 file carries Keras 2 model metadata.\"\"\"\n    import h5py\n    with h5py.File(path, \"r\") as f:\n        return \"keras_version\" in f.attrs and \"model_config\" in f.attrs","tryCatchPattern":null,"preventionTips":["Keep Faceswap 2 model folders intact (h5 + state json) when archiving for later porting","Validate archive integrity (checksums) after downloading models"],"tags":["faceswap","hdf5","keras","migration","model-porting"],"backgroundTag":null,"analyzedSha":"f530cb7508ae670f6474f8a7d9c4df94705cf96b","analyzedAt":"2026-08-15T02:59:26.626Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}