{"record":{"id":"dfe35a77a1198626","repo":"babysor/MockingBird","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":"models/encoder/inference.py","lineNumber":60,"sourceCode":"    if device is None:\n        _device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    _device = device\n    _model.to(device)\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, rate=None):\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":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/babysor/MockingBird/blob/28dc5e14f12d7c754612af2fde8e78a4b03f8616/models/encoder/inference.py#L42-L78","documentation":"Raised by embed_frames_batch when the module-level _model is None, i.e. inference was attempted before encoder.load_model() initialized the model/device. The encoder uses lazy global state, so any embedding call (embed_utterance, toolbox, VC pipelines) fails until load_model is invoked once.","triggerScenarios":"Calling encoder.embed_frames_batch/embed_utterance (directly or via toolbox/VC code) before encoder.load_model(weights_fpath) in the same process; or after a failed/silent load in a subprocess.","commonSituations":"Reordering startup code so inference runs before model loading; refactoring the toolbox into a service where load_model was skipped; load_model raising earlier and being swallowed so _model stays None.","solutions":["Call encoder.load_model(path_to_encoder.pt) once at startup before any embed_* call","Ensure the weights path is valid and load_model didn't raise (check logs) before inferring","Add an explicit init check/guard in your wrapper that fails fast with a clear message"],"exampleFix":"# before\nembeds = encoder.embed_utterance(wav)  # _model is None\n# after\nencoder.load_model(Path('encoder/saved_models/encoder.pt'))\nembeds = encoder.embed_utterance(wav)","handlingStrategy":"validation","validationCode":"import encoder\n\nif not encoder.is_loaded():\n    encoder.load_model(Path('encoder/saved_models/encoder.pt'))\nembeds = encoder.embed_utterance(wav)","typeGuard":"def ensure_encoder_ready() -> bool:\n    import encoder\n    return getattr(encoder, '_model', None) is not None","tryCatchPattern":"try:\n    encoder.embed_frames_batch(frames)\nexcept Exception as e:\n    if 'load_model' in str(e):\n        encoder.load_model(weights_path)  # lazy init then retry once\n        return encoder.embed_frames_batch(frames)\n    raise","preventionTips":["Call load_model once at process startup, before serving requests","Check the load_model return/exception path; a swallowed failure leaves _model None","Wrap encoder inference in a component that owns initialization state"],"tags":["python","encoder","lazy-initialization","inference","state"],"backgroundTag":"model-not-initialized","analyzedSha":"28dc5e14f12d7c754612af2fde8e78a4b03f8616","analyzedAt":"2026-08-27T02:26:53.589Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}