{"record":{"id":"9471d35aa6eeeaee","repo":"CorentinJ/Real-Time-Voice-Cloning","slug":"model-was-not-loaded-call-load-model-before-inf","errorCode":null,"errorMessage":"Model was not loaded. Call load_model() before inference.","messagePattern":"Model was not loaded\\. Call load_model\\(\\) before inference\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"encoder/inference.py","lineNumber":51,"sourceCode":"    _model.load_state_dict(checkpoint[\"model_state\"])\n    _model.eval()\n    print(\"Loaded encoder \\\"%s\\\" trained to step %d\" % (weights_fpath.name, checkpoint[\"step\"]))\n\n\ndef is_loaded():\n    return _model is not None\n\n\ndef embed_frames_batch(frames_batch):\n    \"\"\"\n    Computes embeddings for a batch of mel spectrogram.\n\n    :param frames_batch: a batch mel of spectrogram as a numpy array of float32 of shape\n    (batch_size, n_frames, n_channels)\n    :return: the embeddings as a numpy array of float32 of shape (batch_size, model_embedding_size)\n    \"\"\"\n    if _model is None:\n        raise Exception(\"Model was not loaded. Call load_model() before inference.\")\n\n    frames = torch.from_numpy(frames_batch).to(_device)\n    embed = _model.forward(frames).detach().cpu().numpy()\n    return embed\n\n\ndef compute_partial_slices(n_samples, partial_utterance_n_frames=partials_n_frames,\n                           min_pad_coverage=0.75, overlap=0.5):\n    \"\"\"\n    Computes where to split an utterance waveform and its corresponding mel spectrogram to obtain\n    partial utterances of <partial_utterance_n_frames> each. Both the waveform and the mel\n    spectrogram slices are returned, so as to make each partial utterance waveform correspond to\n    its spectrogram. This function assumes that the mel spectrogram parameters used are those\n    defined in params_data.py.\n\n    The returned ranges may be indexing further than the length of the waveform. It is\n    recommended that you pad the waveform with zeros up to wave_slices[-1].stop.\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/CorentinJ/Real-Time-Voice-Cloning/blob/890f3a03187195b9829db2079b75c2ba2ab0405c/encoder/inference.py#L33-L69","documentation":"Raised by embed_frames_batch() (encoder/inference.py) when the module-level singleton _model is None. The encoder inference API is stateful: load_model(hp, model_fpath) must be called once to populate _model and _device before any embedding call. The guard is a plain None check, not a file check — a failed or skipped load_model leaves _model as None and this fires on the first batch.","triggerScenarios":"Calling embed_frames_batch() (directly, or via embed_utterance()/speaker_similarity flows in demo_cli.py, toolbox, or synthesizer_preprocess_embeds.py) without a prior successful load_model(). Also happens when load_model raised earlier in the process and the caller swallowed the exception and continued.","commonSituations":"New demo code that forgets the load step; scripts that conditionally load the model only if the checkpoint file exists; a load_model call inside a try/except that silently passes; reusing the module in a notebook after an exception during load.","solutions":["Call encoder.inference.load_model(Path(\"encoder/saved_models/<name>/encoder.pt\")) once at startup, before any embed_* call.","If it still fires, check that load_model ran successfully and did not raise (missing checkpoint path, CUDA OOM) — fix the load error itself.","In long-lived apps, call is_loaded() (the provided _model is not None check) at startup and fail fast with your own message naming the model path."],"exampleFix":"# before\nfrom encoder import inference\nembeds = inference.embed_frames_batch(frames)  # _model is None -> raises\n\n# after\nfrom encoder import inference\nfrom pathlib import Path\ninference.load_model(Path(\"encoder/saved_models/pretrained.pt\"))\nembeds = inference.embed_frames_batch(frames)","handlingStrategy":"validation","validationCode":"from encoder import inference\nfrom pathlib import Path\n\ndef ensure_encoder_loaded(model_fpath: Path):\n    if not inference.is_loaded():\n        inference.load_model(model_fpath)","typeGuard":null,"tryCatchPattern":"try:\n    embed = inference.embed_frames_batch(frames)\nexcept Exception as e:\n    if \"load_model\" in str(e):\n        inference.load_model(Path(\"encoder/saved_models/pretrained/encoder.pt\"))\n        embed = inference.embed_frames_batch(frames)\n    else:\n        raise","preventionTips":["Wrap application startup: load the encoder once, then check is_loaded() before serving requests.","Do not swallow exceptions from load_model — a silent failure here resurfaces as this inference error.","In notebooks/scripts, keep a single init cell that loads all three models and assert is_loaded() for each."],"tags":["encoder","inference","model-loading","state"],"backgroundTag":null,"analyzedSha":"890f3a03187195b9829db2079b75c2ba2ab0405c","analyzedAt":"2026-08-15T02:15:13.202Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}