deepfakes/faceswap · error · FaceswapError

'{self._old_model_file}' is not a valid Faceswap 2 model fil

Error message

'{self._old_model_file}' is not a valid Faceswap 2 model file

What it means

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.

Source

Thrown at plugins/train/model/_base/update.py:65

    def _get_model_config(self) -> dict[str, T.Any]:
        """Obtain a keras 2.x config from a keras 2.x .h5 file.

        As keras 3.x will error out loading the file, we collect it directly from the .h5 file

        Returns
        -------
        A keras 2.x model configuration dictionary

        Raises
        ------
        FaceswapError
            If the file is not a valid Faceswap 2 .h5 model file
        """
        h5file = h5py.File(self._old_model_file, "r")
        s_version = T.cast(str | None, h5file.attrs.get("keras_version"))
        s_config = T.cast(str | None, h5file.attrs.get("model_config"))
        if not s_version or not s_config:
            raise FaceswapError(f"'{self._old_model_file}' is not a valid Faceswap 2 model file")

        version = s_version.split(".")[:2]
        if len(version) != 2 or version[0] != "2":
            raise FaceswapError(f"'{self._old_model_file}' is not a valid Faceswap 2 model file")

        retval = json.loads(s_config)
        logger.debug("Loaded keras 2.x model config: %s", retval)
        return retval

    @classmethod
    def _unwrap_outputs(cls, outputs: list[list[T.Any]]) -> list[list[str | int]]:
        """Unwrap nested output tensors from a config dict to be a single list of output tensor

        Parameters
        ----------
        outputs
            The outputs that exist within the Keras 2 config dict that may be nested

View on GitHub (pinned to f530cb7508)

Solutions

  1. 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))")
  2. Re-download or re-copy the original Faceswap 2 model file if attributes are missing (corruption or wrong source)
  3. Ensure you are porting a Faceswap 2.x model, not a Faceswap 1.x or foreign .h5 file

Example fix

# before: porting an arbitrary h5
python tools.py port -m /models/mystery.h5

# after: verify first
import h5py
with h5py.File('/models/mystery.h5', 'r') as f:
    assert 'keras_version' in f.attrs and 'model_config' in f.attrs, 'not a FS2 model'
Defensive patterns

Strategy: validation

Validate before calling

import h5py
with h5py.File(model_h5, "r") as f:
    attrs = dict(f.attrs)
if "keras_version" not in attrs or "model_config" not in attrs:
    raise SystemExit(f"{model_h5} is not a Keras 2 Faceswap model; cannot port")

Type guard

def is_fs2_model(path: str) -> bool:
    """True if the h5 file carries Keras 2 model metadata."""
    import h5py
    with h5py.File(path, "r") as f:
        return "keras_version" in f.attrs and "model_config" in f.attrs

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of deepfakes/faceswap@f530cb7508 (2026-08-15). Data as JSON: /api/errors/4274623a94e39891. Report an issue: GitHub.