CorentinJ/Real-Time-Voice-Cloning · error · Exception

Model was not loaded. Call load_model() before inference.

Error message

Model was not loaded. Call load_model() before inference.

What it means

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.

Source

Thrown at encoder/inference.py:51

    _model.load_state_dict(checkpoint["model_state"])
    _model.eval()
    print("Loaded encoder \"%s\" trained to step %d" % (weights_fpath.name, checkpoint["step"]))


def is_loaded():
    return _model is not None


def embed_frames_batch(frames_batch):
    """
    Computes embeddings for a batch of mel spectrogram.

    :param frames_batch: a batch mel of spectrogram as a numpy array of float32 of shape
    (batch_size, n_frames, n_channels)
    :return: the embeddings as a numpy array of float32 of shape (batch_size, model_embedding_size)
    """
    if _model is None:
        raise Exception("Model was not loaded. Call load_model() before inference.")

    frames = torch.from_numpy(frames_batch).to(_device)
    embed = _model.forward(frames).detach().cpu().numpy()
    return embed


def compute_partial_slices(n_samples, partial_utterance_n_frames=partials_n_frames,
                           min_pad_coverage=0.75, overlap=0.5):
    """
    Computes where to split an utterance waveform and its corresponding mel spectrogram to obtain
    partial utterances of <partial_utterance_n_frames> each. Both the waveform and the mel
    spectrogram slices are returned, so as to make each partial utterance waveform correspond to
    its spectrogram. This function assumes that the mel spectrogram parameters used are those
    defined in params_data.py.

    The returned ranges may be indexing further than the length of the waveform. It is
    recommended that you pad the waveform with zeros up to wave_slices[-1].stop.

View on GitHub (pinned to 890f3a0318)

Solutions

  1. Call encoder.inference.load_model(Path("encoder/saved_models/<name>/encoder.pt")) once at startup, before any embed_* call.
  2. If it still fires, check that load_model ran successfully and did not raise (missing checkpoint path, CUDA OOM) — fix the load error itself.
  3. 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.

Example fix

# before
from encoder import inference
embeds = inference.embed_frames_batch(frames)  # _model is None -> raises

# after
from encoder import inference
from pathlib import Path
inference.load_model(Path("encoder/saved_models/pretrained.pt"))
embeds = inference.embed_frames_batch(frames)
Defensive patterns

Strategy: validation

Validate before calling

from encoder import inference
from pathlib import Path

def ensure_encoder_loaded(model_fpath: Path):
    if not inference.is_loaded():
        inference.load_model(model_fpath)

Try / catch

try:
    embed = inference.embed_frames_batch(frames)
except Exception as e:
    if "load_model" in str(e):
        inference.load_model(Path("encoder/saved_models/pretrained/encoder.pt"))
        embed = inference.embed_frames_batch(frames)
    else:
        raise

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of CorentinJ/Real-Time-Voice-Cloning@890f3a0318 (2026-08-15). Data as JSON: /api/errors/9471d35aa6eeeaee. Report an issue: GitHub.