{"record":{"id":"ead7be0f490ed650","repo":"deepfakes/faceswap","slug":"trainer-name-could-not-be-read-from-state-file","errorCode":null,"errorMessage":"Trainer name could not be read from state file.","messagePattern":"Trainer name could not be read from state file\\.","errorType":"exception","errorClass":"FaceswapError","httpStatus":null,"severity":"error","filePath":"scripts/convert.py","lineNumber":874,"sourceCode":"        model_dir\n            The folder that contains the trained Faceswap model\n\n        Returns\n        -------\n        The name of the Faceswap model being used.\n        \"\"\"\n        state_files = [fname for fname in os.listdir(str(model_dir))\n                       if fname.endswith(\"_state.json\")]\n        if len(state_files) != 1:\n            raise FaceswapError(\"There should be 1 state file in your model folder. \"\n                                f\"{len(state_files)} were found.\")\n        state_file = os.path.join(str(model_dir), state_files[0])\n\n        state = self._serializer.load(state_file)\n        trainer = state.get(\"name\", None)\n\n        if not trainer:\n            raise FaceswapError(\"Trainer name could not be read from state file.\")\n        logger.debug(\"Trainer from state file: '%s'\", trainer)\n        return trainer\n\n    def launch(self, load_queue: EventQueue) -> None:\n        \"\"\"Launch the prediction process in a background thread.\n\n        Starts the prediction thread and returns the thread.\n\n        Parameters\n        ----------\n        load_queue\n            The queue that contains images and detected faces for feeding the model\n        \"\"\"\n        self._in_queue = load_queue\n        self._thread = MultiThread(self._predict_faces, thread_count=1)\n        self._thread.start()\n\n    def _predict_faces(self) -> None:","sourceCodeStart":856,"sourceCodeEnd":892,"githubUrl":"https://github.com/deepfakes/faceswap/blob/f530cb7508ae670f6474f8a7d9c4df94705cf96b/scripts/convert.py#L856-L892","documentation":"Raised when the single *_state.json file was loaded successfully but does not contain a 'name' key (or it maps to an empty/None value). The 'name' entry records the trainer plugin that created the model; without it convert cannot select the model class to load the weights.","triggerScenarios":"Calling convert with a model folder whose *_state.json parses as JSON but `state.get(\"name\", None)` returns falsy — either the key is absent, null, or an empty string. Typical with hand-edited state files, state files from a much older/other Faceswap fork, or a corrupted-but-valid-JSON file.","commonSituations":"Migrating a model from an old Faceswap version or a different fork whose state schema lacked 'name'; manually editing the state JSON and removing/renaming the key; a truncated write during training where serialization still produced valid JSON.","solutions":["Inspect the state file: `python -c \"import json;print(json.load(open('model/_state.json')))` and check whether a 'name' key exists.","If missing, add the correct trainer name (e.g. \"original\", \"dfl-sae\", \"phaze-a\") as the 'name' value, matching the model weights in the folder.","If you do not know the trainer, check the model weights filename prefix or training logs, then restore/fix the state file accordingly.","As a last resort restore the state file from a backup of the training run."],"exampleFix":"# before (state file contents)\n{\"timestamp\": 1690000000, \"iterations\": 100000}   # no \"name\" key -> convert fails\n\n# after\n{\"name\": \"original\", \"timestamp\": 1690000000, \"iterations\": 100000}","handlingStrategy":"validation","validationCode":"import json\n\ndef state_has_trainer(state_file: str) -> bool:\n    with open(state_file, encoding=\"utf-8\") as fh:\n        state = json.load(fh)\n    return bool(state.get(\"name\"))","typeGuard":null,"tryCatchPattern":"from lib.exceptions import FaceswapError\ntry:\n    trainer = get_trainer(model_dir)\nexcept FaceswapError as err:\n    if \"Trainer name\" in str(err):\n        # inspect and repair the state file\n        ...","preventionTips":["Never hand-edit *_state.json without keeping a backup copy.","When copying models between machines, copy the whole folder verbatim (rsync) rather than cherry-picking weight files.","After any manual state-file edit, verify json round-trips and 'name' is present."],"tags":["faceswap","convert","model-state","json","corruption"],"backgroundTag":null,"analyzedSha":"f530cb7508ae670f6474f8a7d9c4df94705cf96b","analyzedAt":"2026-08-15T02:59:26.626Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}